Skip to content

fix(agent-server): redact LLM & condenser secrets in download-trajectory - #4217

Merged
enyst merged 4 commits into
OpenHands:mainfrom
smolpaws:fix/mask-secrets-in-trajectory-download
Jul 24, 2026
Merged

enyst merged 4 commits into
OpenHands:mainfrom
smolpaws:fix/mask-secrets-in-trajectory-download

Conversation

@smolpaws

@smolpaws smolpaws commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

Trajectory downloads must not leak LLM API keys — asked the agent to fix this and keep the redaction logic out of the router files.


AGENT:

Why

GET /api/file/download-trajectory/{id} zips the raw conversation directory with no masking of its own, so a downloaded trajectory carries LLM API-key material:

  • plaintext keys when the server runs without OH_SECRET_KEY, or
  • recoverable Fernet ciphertext when a cipher is configured.

Both the agent LLM key and the condenser LLM key leak, since the whole agent config is serialized into base_state.json / meta.json. A trajectory archive is meant to be shared for debugging, so secrets must never leave the server in it.

Summary

  • New security utility module openhands/agent_server/_secret_redaction.py (mirroring the existing _secrets_exposure.py pattern) that redacts secret-bearing fields at the export boundary: redact_secrets_in_obj walks parsed JSON and masks any LLM_SECRET_FIELDS (api_key, aws_access_key_id, aws_secret_access_key, aws_session_token) plus the settings-level llm_api_key alias, for both plaintext and Fernet (gAAAA…) values. Any Fernet ciphertext is redacted regardless of field name, so cipher-encrypted custom secrets in secret_registry.secret_sources (stored under value) are covered too.
  • redacted_file_bytes applies this to *.json and *.jsonl (per-event log) files; anything that isn't JSON is passed through untouched, so non-secret content stays byte-identical.
  • _create_zip_from_directory in file_router.py streams redacted bytes for changed files and copies the rest verbatim, so the guarantee holds regardless of how the conversation was persisted.

Issue Number

Fixes #4216

How to Test

  1. Start the agent server without OH_SECRET_KEY and run a conversation with a real api_key configured (optionally with a condenser LLM).
  2. curl -OJ http://localhost:8000/api/file/download-trajectory/<conversation_id> and unzip the archive.
  3. grep -r "sk-" <unzipped dir> — no API-key material (plaintext or gAAAA… ciphertext) appears; api_key fields read ********** while model names and all other content are preserved.
  4. Automated: uv run pytest tests/agent_server/test_file_router.py -q → 78 passed. The new test_download_trajectory_redacts_llm_and_condenser_secrets seeds meta.json (plaintext main key + encrypted condenser blob), base_state.json, and a full_state event, and asserts no secret material survives into the zip. Verified it fails without the fix and passes with it. ruff + pyright clean.

Video/Screenshots

N/A (backend security fix; see test evidence above)

Type

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

Notes

Redaction happens only at the download boundary; on-disk persistence is unchanged, so resuming conversations on the server keeps working with the original (optionally encrypted) secrets.

enyst and others added 2 commits July 24, 2026 17:11
The /download-trajectory endpoint zips the raw conversation directory
(base_state.json, meta.json, events) with no masking of its own. A
downloaded trajectory therefore carried API-key material: plaintext keys
when the server ran without OH_SECRET_KEY, or recoverable Fernet
ciphertext when a cipher was configured. Both the agent LLM key and the
condenser LLM key were affected, since the whole agent config is
serialized.

Redact secret-bearing fields (LLM_SECRET_FIELDS + the llm_api_key
settings alias) at the export boundary while building the zip, for both
plaintext and encrypted values, so a shared trajectory never leaks
credentials regardless of how the conversation was persisted. Non-secret
content is left byte-identical.

Co-authored-by: smolpaws <engel@enyst.org>
@enyst

enyst commented Jul 24, 2026

Copy link
Copy Markdown
Member

@OpenHands I prefer if such functions are not in the _router.py files. Please fix. A security utility/service or something. Think before you do. Push to PR (it's from a fork, set your remote properly). Then take a step and verify the code /codereview . Post your feedback on the PR with gh api and event approved or what you see fit.

@openhands-ai

openhands-ai Bot commented Jul 24, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

enyst and others added 2 commits July 24, 2026 15:30
…router

Extract the redaction helpers into a dedicated security utility module
(_secret_redaction.py) so router files stay focused on HTTP handling,
mirroring the existing _secrets_exposure.py precedent.

Co-authored-by: openhands <openhands@all-hands.dev>
…y export

Custom secrets in secret_registry.secret_sources are persisted under a
'value' field that is not in LLM_SECRET_FIELDS; treat any Fernet token
as recoverable secret material regardless of the field name.

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

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code Review

🟢 Good taste — simple, boring fix at the single right choke point.

Reviewed at head 5462312 (after the refactor moving redaction helpers out of file_router.py into _secret_redaction.py, per review feedback).

Three questions

  1. Real problem? Yes — #4216 is a genuine data-exposure bug: download-trajectory shipped plaintext API keys (no OH_SECRET_KEY) or recoverable Fernet ciphertext (with one). A trajectory archive exists precisely to be shared, so this is the worst place to leak credentials.
  2. Simpler way? Redacting at the export boundary is the minimal design: persistence is untouched, no schema/mode changes, server-side resume keeps working with the original (optionally encrypted) secrets. The recursive dict/list walk over parsed JSON is the obvious data structure — no special cases per file type beyond json/jsonl.
  3. What breaks? Nothing user-visible. Files that contain no secrets stay byte-identical in the archive; only secret-bearing JSON is re-serialized. Downloaded trajectories no longer carry usable keys — that is the point, not a regression.

What I verified

  • _create_zip_from_directory is only called by the trajectory endpoint (grep confirms one call site), so the redaction does not silently alter the workspace /archive or file-download paths.
  • The Fernet-anywhere rule closes a real gap: cipher-encrypted custom secrets in secret_registry.secret_sources are stored under value — a field name not in LLM_SECRET_FIELDS — and would have leaked as recoverable ciphertext. Now any gAAAAA… token is masked regardless of field name. (Plaintext custom secrets were never persisted; serialize_secret redacts them at save time without a cipher.)
  • The test exercises the real HTTP endpoint end-to-end (TestClient + real files on disk, no mocks of the unit under test), asserts secret material is absent from the raw archive bytes, and asserts non-secret content (model names) is preserved. It fails without the fix.
  • tests/agent_server/test_file_router.py: 78 passed. ruff + pyright clean on the touched files.

Minor notes (non-blocking)

  • The gAAAAA prefix heuristic could theoretically redact a legitimate non-secret string that happens to start with the Fernet prefix inside an event payload. In a shareable debug archive, over-redaction is the correct failure mode — fine as is.
  • The .jsonl branch in redacted_file_bytes is currently unexercised (the event store writes discrete event-*.json files). Cheap future-proofing; acceptable.

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW
    Export-path only, fails safe (over-redacts rather than leaks), on-disk persistence and conversation resume untouched, behavior proven by an end-to-end test against the real endpoint.

VERDICT:
✅ Worth merging — closes a real secret-leak with a contained boundary fix.

KEY INSIGHT:
Redacting once at the export boundary makes the no-leak guarantee independent of how (or whether) secrets were encrypted at persist time — the only design that holds for old and new conversations alike.


This review was generated by an AI agent (OpenHands) on behalf of @enyst.

enyst commented Jul 24, 2026

Copy link
Copy Markdown
Member

@enyst Done — summary of changes:

  • Refactor (55372a0): moved the redaction helpers (redact_secrets_in_obj, redacted_file_bytes, TRAJECTORY_SECRET_FIELDS) out of file_router.py into a dedicated security utility module openhands/agent_server/_secret_redaction.py, mirroring the existing _secrets_exposure.py pattern. The router now only keeps the zip-building logic.
  • Gap found & fixed during review (5462312): cipher-encrypted custom secrets in secret_registry.secret_sources are persisted under a value field that is not in LLM_SECRET_FIELDS, so their ciphertext would still have leaked. Any Fernet (gAAAAA…) token is now redacted regardless of field name; test extended to cover it.
  • PR description rewritten to follow the repo template (HUMAN/AGENT sections, Why/Summary/How to Test), validated locally with .github/scripts/check_pr_description.py — this fixes the failing PR Description Check CI job.
  • Pushed to the fork branch (smolpaws/software-agent-sdk). tests/agent_server/test_file_router.py: 78 passed; ruff + pyright clean.
  • Code review posted as an approving review above.

This comment was created by an AI agent (OpenHands) on behalf of @enyst.

@openhands-ai

This comment was marked as duplicate.

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.

download-trajectory leaks LLM & condenser API keys (no masking at export boundary)

2 participants