Repository navigation
fix(llm): keep provider credentials out of completion logs - #5657
Conversation
The telemetry kwargs snapshot shared its header dicts with the live LiteLLM call, so a provider handler that adds Authorization to extra_headers in place leaked the key into completion logs (local files and the agent server's LLMCompletionLogEvent). Telemetry.on_request now copies extra_headers/headers through sanitize_dict before the call, so in-place provider mutations never reach the log and caller-supplied credential headers are redacted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Co-authored-by: openhands <openhands@all-hands.dev>
|
@enyst, I reviewed #5657 and found a few adjacent cases in the shared error-text redaction path. Lowercase and JSON-style header keys, mixed quote styles, and escaped quotes can bypass the current matching. I have a small two-file patch with focused tests. Would you like me to send it as a commit for you to review or cherry-pick? |
redact_text_secrets matched sensitive dict entries only with matching single or double quotes and Authorization only as 'Authorization'. Provider errors echo headers in other shapes: lowercase names (httpx), JSON double quotes, hyphenated names such as api-key and x-api-key, mixed quote styles, and JSON embedded with escaped quotes. One pattern now covers all of them. Reported by @Fieldnote-Echo on #5657. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
|
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance. @Fieldnote-Echo, thank you for the careful look. I checked each case against a19a6db replaces the separate dict-entry, If your patch covers anything beyond these cases, please do send it. I'd be glad to compare. Generated by Claude Code |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope check: this is a bug fix in the SDK's LLM telemetry/redaction path, which this repository owns (AGENTS.md and the review guide). It is in scope, and the linked issue #5688 is ready-for-dev with security-related / priority:medium. CI on the current head is green (sdk-tests, agent-server-tests, integration-test, OpenAPI, coverage, etc.), and the two focused suites pass locally: tests/sdk/utils/test_redact.py (50 passed) and tests/sdk/llm/test_llm_log_completions_integration.py (7 passed).
Two material findings, both in the credential-redaction path this PR is meant to harden.
-
openhands-sdk/openhands/sdk/utils/redact.py:284- the new_SENSITIVE_ENTRY_REmatches the value with.*?, which does not cross newlines (the pattern is not compiled withre.DOTALL). The patterns it replaced used[^']*/[^"]*, which do cross newlines, so a sensitive entry whose value spans multiple lines is now left verbatim where the base revision redacted it. Demonstrated on this head:redact_text_secrets("{'private_key': '<PEM key with newlines>'}")returns the key unchanged, while the base revision returns<redacted>. This is a regression, not just a gap: it weakens everyredact_text_secretscaller (telemetry errormessage/repr/traceback,acp_agent,utils/command.py,profiles_router). Fix: addre.DOTALL, or use a value body that tolerates\n. -
openhands-sdk/openhands/sdk/llm/utils/telemetry.py:487-_redact_request_credentialsonly redacts a logged option whenisinstance(value, str). A caller-suppliedapi_keypassed aspydantic.SecretStr(the SDK's own credential type;LLM.api_keyisSecretStr) is not astr, so it is copied verbatim, and_safe_jsonserializes it to its internal field. Demonstrated:llm.completion(msgs, api_key=SecretStr("CALLERSECRET123456"))writes{"api_key": {"_secret_value": "CALLERSECRET123456"}}into the completion log. The PR states credential-like options in logged kwargs are redacted; that does not hold for the SDK's canonical secret wrapper. Fix: redact secret-keyed values regardless of type (handleSecretStrexplicitly).
The header-copying change itself is correct: sanitize_dict deep-copies the header dicts, so provider in-place mutation of the live extra_headers cannot reach the log, and the provider request is unchanged.
🔄 CHANGES REQUESTED
Match multiline credential entries in shared error-text redaction and mask SecretStr credential options before completion logs serialize their values. Keep provider requests and numeric token limits unchanged. Cover quote styles, multiline provider errors, and wrapped credentials on both successful and failed completions. Co-authored-by: openhands <openhands@all-hands.dev>
|
@enyst Posting up a detailed review shortly. Keeping it inline because I don't think I have permissions to push commits to your branch. |
Thank you! Cool, up to you, I think you could also make a PR to this PR from your fork |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Fresh review of head 31d81aec. The earlier all-hands-bot review was on 95ff8676, and smolpaws and Fieldnote-Echo have commented since, so this is a new pass rather than a duplicate of the prior one.
Scope: in-repo. The change hardens the SDK's own LLM telemetry/redaction path (openhands-sdk/openhands/sdk/llm/utils/telemetry.py and openhands-sdk/openhands/sdk/utils/redact.py) and extends the existing shared helpers instead of adding a parallel redaction implementation, which matches the repository guidance. No product or architecture decision is required.
What I verified on this head:
- The reported leak is closed.
Telemetry.on_requestnow stores a redacted copy of the request context (_redact_request_credentials), copyingkwargsand passingextra_headers/headersthroughsanitize_dictbefore the provider call. Both the success path (log_llm_call) and the failure path (on_error) log fromself._req_ctx, so anAuthorizationheader a provider writes in place during the call can no longer reach the completion log. The agent-serverLLMCompletionLogEventis built from the same serializedlog_data(event_service._setup_llm_log_streaming), so the event-store path is covered by the same change. - Credential options passed as
strorSecretStrare redacted viais_secret_key+sanitize_dict, while the live request keeps its original values (asserted by the new integration tests). - Provider error text is sanitized:
str(_err),repr(_err), and the formatted traceback all go throughredact_text_secrets. - The new
_SENSITIVE_ENTRY_REhandles multiline values (re.DOTALL), both quote styles, escaped quotes, and hyphenated header names. I exercised it directly against multiline secrets, mixed/escaped quotes, JSON embedded with escaped quotes, and large inputs: the credential is redacted and adjacent public text is preserved. It did not exhibit catastrophic backtracking on the pathological inputs I tried (unterminated entries up to ~80k characters stay linear). The two findings from the previous review (missingDOTALL,SecretStrskipped) are addressed on this head, and their threads are resolved.
Tests and CI:
- Focused run (
tests/sdk/utils/test_redact.pyandtests/sdk/llm/test_llm_log_completions_integration.py): 65 passed. - Broader run (
tests/sdk/llmandtests/sdk/utils): 1504 passed, 2 failed. The two failures (test_get_credentials_dir_default,test_get_credentials_dir_xdgintests/sdk/llm/auth/test_credentials.py) are artifacts of this sandbox'sHOMElayout and are unrelated to the changed files. - Head
31d81aecchecks: all completed checks pass (Run tests,Pre-commit checks,Agent Server, the REST/TypeScript audits, and the binary builds). OneReview Thread Gaterun failed transiently before the resolved threads were recorded and passed on re-run; there are currently 0 unresolved threads.
No material correctness, security, or compatibility findings on this head.
✅ APPROVED
Co-authored-by: openhands <openhands@all-hands.dev>
HUMAN:
This PR fixes a bug found during a full agentic run of agent-server.
AGENT:
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Why
With
log_completions=True, the completion-log snapshot of an LLM call's kwargs shares its header dicts with the live LiteLLM call. Some LiteLLM provider handlers (DeepSeek, for one) add anAuthorizationheader toextra_headersin place, so the provider key can end up in completion logs: the locallog_completions_folderfiles and, on the agent server, theLLMCompletionLogEvents kept in the event store. Credential headers supplied by the caller are also logged as is. This hardens the log path against both. It was found while building the Agent Server feature map in #5621 (F33.completion-logs-redacted).Summary
Telemetry.on_requestcopies the loggedextra_headersandheadersbefore the provider call and passes them through the existingsanitize_dicthelper (openhands/sdk/utils/redact.py), so in-place changes during the call can't reach the log and credential-like headers are logged as<redacted>.log_llm_call) and failed ones (on_error) both log from this context, and the server's log events are built from the same serialized data, so one change covers every place logs are written.SecretStroptions in logged kwargs are redacted. Provider error messages, representations and tracebacks are sanitized before logging, including multiline credential values.Issue Number
Related to #5688.
How to Test
Tests cover a provider adding
Authorizationduring successful and failed calls, caller-supplied credential headers, credential options passed asstrorSecretStron success and failure, and multiline secrets in provider errors. They assert that logs contain no credential while the provider request keeps its original values.Local validation: all 65 focused tests and all pre-commit hooks pass. A wider regression run passed 613 tests and failed
TestACPSessionIdPersistence.test_roundtrip_via_conversation_state_persistence; the same failure reproduces on the unchanged95ff8676head.Type
Notes
SecretStroptions are redacted in logged kwargs; non-secret options such asmax_completion_tokenskeep their values.🤖 Generated with Claude Code
https://claude.ai/code/session_01W9qxyUBKhzmbLrTK2wi7D7
Generated by Claude Code
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:f57fc3a-pythonRun
All tags pushed for this build
About Multi-Architecture Support
f57fc3a-python) is a multi-arch manifest supporting both amd64 and arm64f57fc3a-python-amd64) are also available if neededGenerated by Claude Code