Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/openai/_utils/_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
httpx_logger: logging.Logger = logging.getLogger("httpx")


SENSITIVE_HEADERS = {"api-key", "authorization", "x-amz-security-token"}
SENSITIVE_HEADERS = {
"api-key",
"authorization",
"cookie",
"proxy-authorization",
"set-cookie",
"x-amz-security-token",
"x-api-key",
}


def _basic_config() -> None:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_utils/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ def test_headers_without_sensitive_info(logger_with_filter: logging.Logger, capl
)


def test_proxy_credential_headers_redacted(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
with caplog.at_level(logging.DEBUG):
logger_with_filter.debug(
"Request options: %s",
{
"method": "post",
"url": "chat/completions",
"headers": {
"x-api-key": "gateway-secret",
"Proxy-Authorization": "Basic dXNlcjpwYXNz",
"Cookie": "session=abc123",
"Set-Cookie": "token=xyz",
},
},
)

log_record = cast(Dict[str, Any], caplog.records[0].args)
assert log_record["headers"]["x-api-key"] == "<redacted>"
assert log_record["headers"]["Proxy-Authorization"] == "<redacted>"
assert log_record["headers"]["Cookie"] == "<redacted>"
assert log_record["headers"]["Set-Cookie"] == "<redacted>"


def test_standard_debug_msg(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None:
with caplog.at_level(logging.DEBUG):
logger_with_filter.debug("Sending HTTP Request: %s %s", "POST", "chat/completions")
Expand Down