Skip to content

fix(llm): keep provider credentials out of completion logs - #5657

Merged
enyst merged 7 commits into
mainfrom
claude/harden-completion-log-headers
Oct 12, 2026
Merged

enyst merged 7 commits into
mainfrom
claude/harden-completion-log-headers

Conversation

@enyst

@enyst enyst commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

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 an Authorization header to extra_headers in place, so the provider key can end up in completion logs: the local log_completions_folder files and, on the agent server, the LLMCompletionLogEvents 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_request copies the logged extra_headers and headers before the provider call and passes them through the existing sanitize_dict helper (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>.
  • Successful calls (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.
  • Credential-like string and SecretStr options in logged kwargs are redacted. Provider error messages, representations and tracebacks are sanitized before logging, including multiline credential values.
  • Headers that are not credentials keep their values for debugging. The request sent to the provider is unchanged.

Issue Number

Related to #5688.

How to Test

uv run pytest tests/sdk/utils/test_redact.py \
  tests/sdk/llm/test_llm_log_completions_integration.py

Tests cover a provider adding Authorization during successful and failed calls, caller-supplied credential headers, credential options passed as str or SecretStr on 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 unchanged 95ff8676 head.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Credential-like string and SecretStr options are redacted in logged kwargs; non-secret options such as max_completion_tokens keep their values.
  • Logs written before this change are not rewritten.

🤖 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:f57fc3a-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-f57fc3a-python \
  ghcr.io/openhands/agent-server:f57fc3a-python

All tags pushed for this build

ghcr.io/openhands/agent-server:f57fc3a-golang-amd64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-golang-amd64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-golang-amd64
ghcr.io/openhands/agent-server:f57fc3a-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:f57fc3a-golang-arm64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-golang-arm64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-golang-arm64
ghcr.io/openhands/agent-server:f57fc3a-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:f57fc3a-java-amd64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-java-amd64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-java-amd64
ghcr.io/openhands/agent-server:f57fc3a-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:f57fc3a-java-arm64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-java-arm64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-java-arm64
ghcr.io/openhands/agent-server:f57fc3a-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-amd64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-amd64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-arm64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-arm64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-minimal-amd64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-minimal-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-minimal-arm64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-minimal-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-slim-amd64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-slim-amd64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-slim-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:f57fc3a-python-slim-arm64
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-slim-arm64
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-slim-arm64
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:f57fc3a-golang
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-golang
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-golang
ghcr.io/openhands/agent-server:f57fc3a-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:f57fc3a-java
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-java
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-java
ghcr.io/openhands/agent-server:f57fc3a-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:f57fc3a-python-minimal
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-minimal
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-minimal
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-minimal
ghcr.io/openhands/agent-server:f57fc3a-python-slim
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python-slim
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python-slim
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime-slim
ghcr.io/openhands/agent-server:f57fc3a-python
ghcr.io/openhands/agent-server:f57fc3ad1d522bb65c37abf217af051cae8188ef-python
ghcr.io/openhands/agent-server:claude-harden-completion-log-headers-python
ghcr.io/openhands/agent-server:f57fc3a-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., f57fc3a-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., f57fc3a-python-amd64) are also available if needed

Generated by Claude Code

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
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/llm/utils
   telemetry.py2301793%130, 252, 279, 285–286, 318–320, 343–344, 355–356, 437, 439, 453, 459, 464
openhands-sdk/openhands/sdk/utils
   redact.py881484%87, 230–231, 254–260, 276–279
TOTAL472931307572% 

Co-authored-by: openhands <openhands@all-hands.dev>
@Fieldnote-Echo

Copy link
Copy Markdown
Member

@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
@enyst

enyst commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

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 redact_text_secrets on this branch, and all of them got through: lowercase authorization (which is how httpx spells header names), JSON-style "Authorization": "...", hyphenated names such as api-key and x-api-key, mixed quote styles, and JSON embedded with escaped quotes.

a19a6db replaces the separate dict-entry, Authorization and X-Session-API-Key patterns with one case-insensitive pattern. It accepts either quote style for the key and the value (also backslash-escaped), and hyphens in the name. Each of your cases is a parametrized test in tests/sdk/utils/test_redact.py, with a control showing that ordinary headers keep their values. The new tests fail without the change, and the existing redaction, LLM and ACP suites still pass.

If your patch covers anything beyond these cases, please do send it. I'd be glad to compare.


Generated by Claude Code

@enyst
enyst marked this pull request as ready for review October 11, 2026 19:24
@enyst
enyst requested a review from all-hands-bot October 11, 2026 19:24

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

  1. openhands-sdk/openhands/sdk/utils/redact.py:284 - the new _SENSITIVE_ENTRY_RE matches the value with .*?, which does not cross newlines (the pattern is not compiled with re.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 every redact_text_secrets caller (telemetry error message/repr/traceback, acp_agent, utils/command.py, profiles_router). Fix: add re.DOTALL, or use a value body that tolerates \n.

  2. openhands-sdk/openhands/sdk/llm/utils/telemetry.py:487 - _redact_request_credentials only redacts a logged option when isinstance(value, str). A caller-supplied api_key passed as pydantic.SecretStr (the SDK's own credential type; LLM.api_key is SecretStr) is not a str, so it is copied verbatim, and _safe_json serializes 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 (handle SecretStr explicitly).

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>
@Fieldnote-Echo

Copy link
Copy Markdown
Member

@enyst Posting up a detailed review shortly. Keeping it inline because I don't think I have permissions to push commits to your branch.

@enyst

enyst commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

@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 all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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_request now stores a redacted copy of the request context (_redact_request_credentials), copying kwargs and passing extra_headers/headers through sanitize_dict before the provider call. Both the success path (log_llm_call) and the failure path (on_error) log from self._req_ctx, so an Authorization header a provider writes in place during the call can no longer reach the completion log. The agent-server LLMCompletionLogEvent is built from the same serialized log_data (event_service._setup_llm_log_streaming), so the event-store path is covered by the same change.
  • Credential options passed as str or SecretStr are redacted via is_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 through redact_text_secrets.
  • The new _SENSITIVE_ENTRY_RE handles 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 (missing DOTALL, SecretStr skipped) are addressed on this head, and their threads are resolved.

Tests and CI:

  • Focused run (tests/sdk/utils/test_redact.py and tests/sdk/llm/test_llm_log_completions_integration.py): 65 passed.
  • Broader run (tests/sdk/llm and tests/sdk/utils): 1504 passed, 2 failed. The two failures (test_get_credentials_dir_default, test_get_credentials_dir_xdg in tests/sdk/llm/auth/test_credentials.py) are artifacts of this sandbox's HOME layout and are unrelated to the changed files.
  • Head 31d81aec checks: all completed checks pass (Run tests, Pre-commit checks, Agent Server, the REST/TypeScript audits, and the binary builds). One Review Thread Gate run 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

@enyst
enyst enabled auto-merge (squash) October 12, 2026 05:19
@enyst
enyst merged commit e3a2682 into main Oct 12, 2026
44 checks passed
@enyst
enyst deleted the claude/harden-completion-log-headers branch October 12, 2026 05:23
@github-actions github-actions Bot mentioned this pull request Oct 15, 2026
1 of 7 tasks
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.

5 participants