Repository navigation
fix(agent-server): redact LLM & condenser secrets in download-trajectory - #4217
Conversation
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>
|
@OpenHands I prefer if such functions are not in the |
|
I'm on it! enyst can track my progress at all-hands.dev |
…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
left a comment
There was a problem hiding this comment.
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
- Real problem? Yes — #4216 is a genuine data-exposure bug:
download-trajectoryshipped plaintext API keys (noOH_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. - 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.
- 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_directoryis only called by the trajectory endpoint (grepconfirms one call site), so the redaction does not silently alter the workspace/archiveor file-download paths.- The Fernet-anywhere rule closes a real gap: cipher-encrypted custom secrets in
secret_registry.secret_sourcesare stored undervalue— a field name not inLLM_SECRET_FIELDS— and would have leaked as recoverable ciphertext. Now anygAAAAA…token is masked regardless of field name. (Plaintext custom secrets were never persisted;serialize_secretredacts 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
gAAAAAprefix 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
.jsonlbranch inredacted_file_bytesis currently unexercised (the event store writes discreteevent-*.jsonfiles). 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 Done — summary of changes:
This comment was created by an AI agent (OpenHands) on behalf of @enyst. |
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:OH_SECRET_KEY, orBoth 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
openhands/agent_server/_secret_redaction.py(mirroring the existing_secrets_exposure.pypattern) that redacts secret-bearing fields at the export boundary:redact_secrets_in_objwalks parsed JSON and masks anyLLM_SECRET_FIELDS(api_key,aws_access_key_id,aws_secret_access_key,aws_session_token) plus the settings-levelllm_api_keyalias, for both plaintext and Fernet (gAAAA…) values. Any Fernet ciphertext is redacted regardless of field name, so cipher-encrypted custom secrets insecret_registry.secret_sources(stored undervalue) are covered too.redacted_file_bytesapplies this to*.jsonand*.jsonl(per-event log) files; anything that isn't JSON is passed through untouched, so non-secret content stays byte-identical._create_zip_from_directoryinfile_router.pystreams 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
OH_SECRET_KEYand run a conversation with a realapi_keyconfigured (optionally with a condenser LLM).curl -OJ http://localhost:8000/api/file/download-trajectory/<conversation_id>and unzip the archive.grep -r "sk-" <unzipped dir>— no API-key material (plaintext orgAAAA…ciphertext) appears;api_keyfields read**********while model names and all other content are preserved.uv run pytest tests/agent_server/test_file_router.py -q→ 78 passed. The newtest_download_trajectory_redacts_llm_and_condenser_secretsseedsmeta.json(plaintext main key + encrypted condenser blob),base_state.json, and afull_stateevent, 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
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.