Skip to content

Commit 839a279

Browse files
Potential fix for code scanning alert no. 17: Clear-text logging of sensitive information
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 31151a5 commit 839a279

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

teaagent/cli/_handlers/_doctor.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@
1515
redact_wizard_payload,
1616
)
1717

18+
_REDACTED = '***REDACTED***'
19+
_SENSITIVE_KEY_MARKERS = (
20+
'token',
21+
'password',
22+
'passwd',
23+
'secret',
24+
'api_key',
25+
'apikey',
26+
'authorization',
27+
'auth',
28+
)
29+
30+
31+
def _is_sensitive_key(key: str) -> bool:
32+
lowered = key.lower()
33+
return any(marker in lowered for marker in _SENSITIVE_KEY_MARKERS)
34+
35+
36+
def _redact_sensitive_fields(value: Any) -> Any:
37+
if isinstance(value, dict):
38+
redacted: dict[Any, Any] = {}
39+
for k, v in value.items():
40+
key_str = str(k)
41+
if _is_sensitive_key(key_str):
42+
redacted[k] = _REDACTED
43+
else:
44+
redacted[k] = _redact_sensitive_fields(v)
45+
return redacted
46+
if isinstance(value, list):
47+
return [_redact_sensitive_fields(item) for item in value]
48+
return value
49+
1850

1951
def doctor_graphqlite(args: argparse.Namespace) -> int:
2052
ok, message = args._check_graphqlite(args.database) # type: ignore[attr-defined]
@@ -604,4 +636,5 @@ def doctor_migration_command(args: argparse.Namespace) -> int:
604636
def print_json(value: Any) -> None:
605637
if isinstance(value, dict) and value.get('mode') in {'wizard', 'setup'}:
606638
value = redact_wizard_payload(value)
607-
print(json.dumps(value, ensure_ascii=False, sort_keys=True))
639+
safe_value = _redact_sensitive_fields(value)
640+
print(json.dumps(safe_value, ensure_ascii=False, sort_keys=True))

0 commit comments

Comments
 (0)