Skip to content

fix(security): redact sensitive headers from debug logs#2918

Open
giulio-leone wants to merge 3 commits intoopenai:mainfrom
giulio-leone:fix/issue-1196-redact-debug-headers
Open

fix(security): redact sensitive headers from debug logs#2918
giulio-leone wants to merge 3 commits intoopenai:mainfrom
giulio-leone:fix/issue-1196-redact-debug-headers

Conversation

@giulio-leone
Copy link

Summary

Fixes #1196

Response headers were logged unfiltered at debug level in both sync and async HTTP clients (_base_client.py), potentially exposing authorization and api-key header values in log output.

Changes

  • src/openai/_base_client.py: Filter response.headers through a dict comprehension that redacts values for headers in SENSITIVE_HEADERS before logging. Applied to both SyncAPIClient and AsyncAPIClient.
  • src/openai/_utils/__init__.py: Export the existing SENSITIVE_HEADERS set from _logs.py so it can be reused in _base_client.py.
  • tests/test_utils/test_logging.py: Added test verifying response header redaction logic.

Approach

Reuses the existing SENSITIVE_HEADERS set ({"api-key", "authorization"}) from _utils/_logs.py, which is already used by SensitiveHeadersFilter for request options. The filter only needed to cover request args in dict format; this PR extends coverage to response headers logged directly.

Before

DEBUG HTTP Response: POST https://api.openai.com/v1/chat/completions "200 OK" Headers({"authorization": "Bearer sk-...", ...})

After

DEBUG HTTP Response: POST https://api.openai.com/v1/chat/completions "200 OK" {"authorization": "<redacted>", ...}

At debug log level, response headers were logged unfiltered, potentially
exposing API keys and authorization tokens. Now filters sensitive headers
(authorization, api-key) from debug log output using the existing
SENSITIVE_HEADERS set, consistent with SensitiveHeadersFilter.

Fixes openai#1196
Copilot AI review requested due to automatic review settings March 2, 2026 14:36
@giulio-leone giulio-leone requested a review from a team as a code owner March 2, 2026 14:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses #1196 by preventing sensitive response headers (e.g., authorization, api-key) from being emitted in debug logs in the sync and async HTTP clients.

Changes:

  • Redacts sensitive values when logging httpx.Response.headers in both SyncAPIClient and AsyncAPIClient.
  • Re-exports SENSITIVE_HEADERS from openai._utils to reuse the existing sensitive-header allowlist.
  • Adds a test intended to validate the response-header redaction logic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/openai/_base_client.py Redacts sensitive response headers before emitting debug logs for HTTP responses.
src/openai/_utils/__init__.py Exposes SENSITIVE_HEADERS for reuse outside _utils/_logs.py.
tests/test_utils/test_logging.py Adds a test for header redaction behavior using SENSITIVE_HEADERS.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

giulio-leone and others added 2 commits March 2, 2026 16:19
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ase_client

Adds a test using respx to verify that sensitive response headers
are actually redacted in log output when making real client calls,
not just testing the filter utility in isolation.

Refs: openai#2918
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

At debug log level API requests to OpenAI get logged with all headers, including an API key, in plaintext

2 participants