Security: SecretRegistry leaks rotated credentials in cleartext after update_secrets() or LookupSecret re-export (and drops masked history across sub-agents)

Aperta
#7,002 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@zsicheng0214 ci sta già lavorando.

Dal 19/11/2026.

  • #7003 di @zsicheng0214 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend, security

Direzione di ricerca

Start with openhands-sdk/openhands/sdk/conversation/secret_registry.py, especially update_secrets, value resolution, and masking; then read openhands-tools/openhands/tools/task/manager.py and openhands-tools/openhands/tools/delegate/impl.py for sub-agent propagation. Run the existing tests under tests/sdk/conversation/, tests/tools/task/, and tests/tools/delegate/; done means rotated values remain masked and sub-agents inherit and sync masking values. A linked pull request (#7003) is already open.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

priority:medium ready-for-dev
Is there an existing issue for the same bug?
  • I have searched existing issues and this is not a duplicate.
Bug Description

SecretRegistry (openhands-sdk/openhands/sdk/conversation/secret_registry.py) leaks credentials in cleartext into persisted events/*.json, LLM context, and WebSocket event streams whenever a secret is rotated mid-conversation — such as refreshing an OAuth / GitHub App installation token via POST /api/conversations/{id}/secrets (EventService.update_secrets -> LocalConversation.update_secrets), updating a key via conversation.update_secrets(...), or re-exporting a rotating LookupSecret / SecretSource.

Three compounding defects cause plaintext credential exposure:

  1. update_secrets() leaves the newly updated active secret completely unmasked in cleartext:
    SecretRegistry.update_secrets (secret_registry.py:196-208) overwrites self.secret_sources[name] without invalidating self._exported_values[name] or self._failed_lookups[name].
    When mask_secrets_in_output (secret_registry.py:309-337) runs resolve_uncached_values() (secret_registry.py:290-308):

    for key in list(self.secret_sources):
        if key in self._exported_values:
            continue
    

    Because name is already in self._exported_values from its initial value (or in self._failed_lookups if an earlier lookup failed within FAILED_LOOKUP_RETRY_SECONDS), resolve_uncached_values() skips name and never resolves the new secret value. Any tool output (file_editor, grep, glob, MCP tools, git remote -v / cat .env, MessageEvent, SecurityAnalysisEvent) containing the currently active refreshed secret writes it in cleartext until a terminal command explicitly names $SECRET_NAME.

  2. Exporting a rotated secret (update_secrets or LookupSecret) overwrites _exported_values[key], leaking the previous credential in cleartext:
    self._exported_values (secret_registry.py:187, 191-194) is a dict[str, str] storing only a single string per key:

    def track_exported_values(self, values: Mapping[str, str]) -> None:
        with self._exported_values_lock:
            self._exported_values.update({k: v for k, v in values.items() if v})
    

    As soon as a terminal command references $SECRET_NAME (calling get_secrets_as_env_vars or get_secret_value), self._exported_values[key] overwrites the previous secret value with the new one rather than retaining prior resolved values for masking. When the prior token still exists in .git/config, workspace logs, or build artifacts (or remains valid during overlap windows), mask_secrets_in_output and compile_stream_mask no longer mask it — directly violating mask_secrets_in_output's own documented invariant (secret_registry.py:314-315: "so a rotated secret masks its previous value").

  3. Sub-agent delegation drops historical masked values (and DelegateExecutor omits secrets altogether):

    • TaskManager._get_conversation and TaskManager._resume_task (openhands-tools/openhands/tools/task/manager.py:224, 324) pass secrets=parent.state.secret_registry.secret_sources, starting each sub-agent with a fresh SecretRegistry that lacks the parent's previously resolved/rotated secret values, and never merge values resolved inside the sub-agent (e.g., from a dynamic/one-time LookupSecret) back into the parent's masking set.
    • DelegateExecutor._spawn_agents (openhands-tools/openhands/tools/delegate/impl.py:227-248) does not pass secrets to LocalConversation at all, leaving delegated sub-agents with an empty SecretRegistry that leaks 100% of parent secrets in cleartext.

Discovered during secret-confinement and credential-rotation auditing with TraceTrial-Bench (arXiv:2610.32285, mechanism nodes G4 — secret masking & representation confinement, C2 — credential rotation & redaction, and L — sub-agent delegation lifecycle).

Expected Behavior
  1. Calling update_secrets({name: new_value}) preserves any previously resolved value for name in the registry's masking history, clears any stale _failed_lookups[name] backoff entry, and ensures resolve_uncached_values() / mask_secrets_in_output() resolves and masks new_value immediately — even before any terminal command names $SECRET_NAME.
  2. Re-exporting or looking up a rotated secret (update_secrets or a dynamic LookupSecret / SecretSource) keeps both historical resolved values and the latest value masked (<secret-hidden>) in mask_secrets_in_output, mask_secrets_in_model, and compile_stream_mask.
  3. Sub-agents spawned or resumed via TaskManager and DelegateExecutor inherit the parent's secret_sources and known masked values, and values resolved during sub-agent execution are synced back to the parent's SecretRegistry for masking.
Actual Behavior

On current main, both the newly updated active secret and previously exported rotated secrets leak in cleartext:

uv sync --frozen --dev
uv run python - <<'PY'
from openhands.sdk.conversation.secret_registry import SecretRegistry
from openhands.sdk.secret import SecretSource


class RotatingTokenSource(SecretSource):
    calls: int = 0

    def get_value(self) -> str:
        self.calls += 1
        return f"dyn_token_v{self.calls}_secret"


reg = SecretRegistry()
reg.update_secrets(
    {"DEPLOY_TOKEN": "static_tok_v1_secret", "OAUTH_TOKEN": RotatingTokenSource()}
)

# Initial masking caches v1 of both secrets
assert "<secret-hidden>" in reg.mask_secrets_in_output(
    "init static_tok_v1_secret dyn_token_v1_secret"
)

# 1. Mid-session token refresh via update_secrets() (e.g. POST /api/conversations/{id}/secrets)
reg.update_secrets({"DEPLOY_TOKEN": "static_tok_v2_secret"})
masked_new = reg.mask_secrets_in_output("config contains static_tok_v2_secret")
assert "static_tok_v2_secret" not in masked_new, (
    f"Active refreshed secret leaked in cleartext: {masked_new!r}"
)

# 2. Next terminal command exports $DEPLOY_TOKEN and $OAUTH_TOKEN
reg.get_secrets_as_env_vars("deploy --token $DEPLOY_TOKEN --oauth $OAUTH_TOKEN")
masked_prev = reg.mask_secrets_in_output(
    "git remote has static_tok_v1_secret and log has dyn_token_v1_secret"
)
assert "static_tok_v1_secret" not in masked_prev
assert "dyn_token_v1_secret" not in masked_prev
PY

Running the snippet above on main fails immediately with:
AssertionError: Active refreshed secret leaked in cleartext: 'config contains static_tok_v2_secret'
(and after get_secrets_as_env_vars, both static_tok_v1_secret and dyn_token_v1_secret also leak unmasked).

Acceptance Criteria
  • After SecretRegistry.update_secrets({name: new_source_or_value}), mask_secrets_in_output and mask_secrets_in_model mask both the newly updated value (without requiring a terminal command to reference name first) and any previously resolved value(s) for name, and stale _failed_lookups[name] backoff state is cleared for updated keys.
  • When a LookupSecret or custom SecretSource returns a rotated value on get_secrets_as_env_vars / get_secret_value, previously resolved values remain masked alongside the newly exported value in both mask_secrets_in_output and compile_stream_mask.
  • TaskManager (on task creation and resumption) and DelegateExecutor._spawn_agents propagate parent secret_sources and known masked secret values to sub-agent conversations, and sync sub-agent-resolved masked values back into the parent's SecretRegistry.
  • Unit and integration tests under tests/sdk/conversation/, tests/tools/task/, and tests/tools/delegate/ cover update_secrets rotation masking, LookupSecret rotation masking, failed-lookup recovery on update_secrets, and parent/sub-agent secret masking propagation without cleartext leaks in persisted events.

OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Verified against main in this repository: SecretRegistry.update_secrets() (openhands-sdk/openhands/sdk/conversation/secret_registry.py) overwrites secret_sources without invalidating _exported_values or _failed_lookups, so resolve_uncached_values() skips the updated key and the new value is never resolved for masking until a command names it; track_exported_values() stores a single string per key, so a re-export of a rotated LookupSecret/SecretSource replaces the previous value in the masking set and the prior credential leaks; and DelegateExecutor._spawn_agents (openhands-tools/openhands/tools/delegate/impl.py) builds sub-agent conv_kwargs without secrets, while TaskManager._get_conversation/_resume_task (openhands-tools/openhands/tools/task/manager.py) pass parent secret_sources only (no resolved masking values). The mask_secrets_in_output docstring already documents the invariant that a rotated secret masks its previous value, so the requested behavior is the documented contract, not a new design. The author reproduced the active-secret leak by running the real registry class on main.

Bounded scope: make SecretRegistry masking history rotation-safe, propagate parent secrets and resolved masking values to sub-agent conversations (both TaskManager and DelegateExecutor paths), and sync sub-agent-resolved values back for masking only.

Non-goals: no new redaction or secret-sentinel mechanism — masking must continue through SecretRegistry's existing masking per repository AGENTS.md ("Never introduce a parallel redaction or secret-sentinel implementation"); no change to terminal env-var injection or to get_all_secrets_as_env_vars scoping (least-privilege scoping remains deferred to #1039); no TypeScript client masking changes (#5708, #6377); no hook-environment secret injection (#6966); no change to persisted-settings or event schemas beyond masking behavior; child-registered secret sources remain child-local as pinned by tests/tools/task/test_secret_inheritance.py.

Acceptance Criteria
  • After SecretRegistry.update_secrets({name: new_value}), mask_secrets_in_output and mask_secrets_in_model mask new_value immediately, without any prior export or terminal command referencing name, and also mask every previously resolved value for name.
  • After a secret lookup fails, update_secrets for that name allows immediate resolution and masking of the new value without waiting out the failed-lookup backoff window.
  • When a LookupSecret or custom SecretSource returns a rotated value on a later get_secrets_as_env_vars/get_secret_value, both the previously exported value and the newly exported value are masked in mask_secrets_in_output and in compile_stream_mask output.
  • Sub-agent conversations created by TaskManager._get_conversation and _resume_task start with the parent's known resolved/masked secret values, and rotated or previously exported parent secrets appear masked (not cleartext) in sub-agent outputs.
  • DelegateExecutor._spawn_agents passes the parent's secret sources and known masked values to spawned sub-agent conversations, and parent secrets appear masked (not cleartext) in delegate sub-agent outputs.
  • A secret value resolved during sub-agent execution is masked in the parent conversation's subsequent outputs after the delegated task completes or resumes.
  • Child-registered secret sources remain child-local: the behavior pinned by tests/tools/task/test_secret_inheritance.py (child-only sources not registered on the parent) is preserved, with sub-agent value sync affecting masking only.
  • Regression tests under tests/sdk/conversation/, tests/tools/task/, and tests/tools/delegate/ cover update-secrets rotation masking, LookupSecret rotation masking, failed-lookup recovery on update, and parent/sub-agent propagation, and fail on current main before the fix.
  • Persisted events/*.json for parent and sub-agent conversations contain no cleartext occurrence of a rotated secret value in the covered scenarios.
Lingua principale
Python
Stelle
1.4k
Fork
797
Merge medio
1g 17h
PR unite (30g)
354

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenHands/software-agent-sdk

Tutte le issue di OpenHands/software-agent-sdk

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.