Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/openai/_utils/_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ def setup_logging() -> None:
class SensitiveHeadersFilter(logging.Filter):
@override
def filter(self, record: logging.LogRecord) -> bool:
# Case 1: headers passed as a dict in record.args (structured logging)
if is_dict(record.args) and "headers" in record.args and is_dict(record.args["headers"]):
headers = record.args["headers"] = {**record.args["headers"]}
for header in headers:
if str(header).lower() in SENSITIVE_HEADERS:
headers[header] = "<redacted>"

# Case 2: headers already interpolated into the log message string
# (e.g. httpx debug output: "headers={'authorization': 'Bearer sk-...'}")
import re
msg = record.getMessage()
for header in SENSITIVE_HEADERS:
# Match header: 'value' or header: "value" in the formatted message
pattern = rf"(?i)({re.escape(header)}['"]?\s*:\s*['"]?)([^'"\s,}}]+)"
redacted = re.sub(pattern, r"\1<redacted>", msg)
Comment on lines +50 to +51
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Redact full sensitive value, including spaces

The new regex only replaces characters up to the first whitespace ([^'"\s,}}]+), so an Authorization value like Bearer sk-... becomes <redacted> sk-... and still leaks the token. This is the common HTTP auth format, so debug logs can continue exposing credentials even after this fix.

Useful? React with 👍 / 👎.

if redacted != msg:
record.msg = redacted
record.args = ()
break
Comment on lines +52 to +55
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Redact every sensitive header occurrence in a message

The loop stops at the first matched header (break), so if a single log line contains multiple sensitive headers (e.g., both authorization and api-key), only one is scrubbed and the other remains visible. This creates partial redaction and leaves secrets in logs for multi-header requests.

Useful? React with 👍 / 👎.


return True