Skip to content

Commit a1328df

Browse files
jwfingclaude
andcommitted
Sanitize sensitive fields in debug logs
Redact password, token, otp, code, api_key and other credentials from request body before writing to debug logs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7141bea commit a1328df

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

insforge/_base_client.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@
1313

1414
logger = logging.getLogger("insforge")
1515

16+
_SENSITIVE_KEYS = frozenset({
17+
"password",
18+
"new_password",
19+
"newPassword",
20+
"token",
21+
"otp",
22+
"code",
23+
"access_token",
24+
"accessToken",
25+
"refresh_token",
26+
"refreshToken",
27+
"api_key",
28+
"apiKey",
29+
})
30+
31+
_REDACTED = "***"
32+
33+
34+
def _sanitize_body(body: Any) -> Any:
35+
"""Return a copy of *body* with sensitive values replaced by ``'***'``."""
36+
if body is None:
37+
return None
38+
if isinstance(body, dict):
39+
return {
40+
k: _REDACTED if k in _SENSITIVE_KEYS else _sanitize_body(v)
41+
for k, v in body.items()
42+
}
43+
if isinstance(body, list):
44+
return [_sanitize_body(item) for item in body]
45+
return body
46+
1647

1748
def build_headers(
1849
api_key: str,
@@ -131,7 +162,7 @@ async def _request(
131162
exception_cls: type[InsforgeHTTPError] = InsforgeHTTPError,
132163
) -> httpx.Response:
133164
url = self._build_url(path)
134-
logger.debug(">>> %s %s params=%s body=%s", method, url, params, json)
165+
logger.debug(">>> %s %s params=%s body=%s", method, url, params, _sanitize_body(json))
135166

136167
response = await self.http_client.request(
137168
method,

0 commit comments

Comments
 (0)