-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: redact sensitive headers in debug logs (fixes #1196) #2977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -476,18 +476,37 @@ def _prepare_url(self, url: str) -> URL: | |
| def _make_sse_decoder(self) -> SSEDecoder | SSEBytesDecoder: | ||
| return SSEDecoder() | ||
|
|
||
| def _redact_sensitive_headers(self, headers): | ||
| """Redact sensitive headers (API keys, auth tokens) for safe logging.""" | ||
| if not headers or not isinstance(headers, dict): | ||
| return headers | ||
|
|
||
| redacted = {} | ||
| sensitive_keys = { | ||
| "authorization", "api-key", "x-api-key", | ||
| "x-openai-api-key", "openai-api-key" | ||
| } | ||
|
|
||
| for key, value in headers.items(): | ||
| key_lower = key.lower() | ||
| if any(sensitive in key_lower for sensitive in sensitive_keys): | ||
| redacted[key] = "[REDACTED]" | ||
| else: | ||
| redacted[key] = value | ||
|
|
||
| return redacted | ||
|
|
||
| def _build_request( | ||
| self, | ||
| options: FinalRequestOptions, | ||
| *, | ||
| retries_taken: int = 0, | ||
| ) -> httpx.Request: | ||
| if log.isEnabledFor(logging.DEBUG): | ||
| log.debug( | ||
| "Request options: %s", | ||
| model_dump( | ||
| options, | ||
| exclude_unset=True, | ||
| safe_options = options.model_dump() | ||
| if "headers" in safe_options: | ||
| safe_options["headers"] = self._redact_sensitive_headers(safe_options["headers"]) | ||
| log.debug("Request options: %s", safe_options) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This line terminates Useful? React with 👍 / 👎. |
||
| # Pydantic v1 can't dump every type we support in content, so we exclude it for now. | ||
| exclude={ | ||
| "content", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
options.model_dump()directly here drops the compatibility layer used elsewhere in the repo; under Pydantic v1,FinalRequestOptions(apydantic.BaseModel) does not guarantee amodel_dumpmethod, so enabling DEBUG logging can raiseAttributeErrorbefore the request is built. This should keep using the existing compatibility helper to avoid version-specific runtime failures.Useful? React with 👍 / 👎.