Skip to content

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

Open

Description

@zsicheng0214

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.

Activity

  1. added
    priority:mediumFor bugs, a serious source of annoyance, but not blocking a large number of users.
    ready-for-devIssue meets development readiness criteria
    on Nov 19, 2026
  2. all-hands-bot commented on Nov 19, 2026

    @all-hands-bot
    Collaborator

    New report bundles three defects in SecretRegistry/secret delegation. Two are genuinely new (update_secrets() not invalidating _exported_values/_failed_lookups so the refreshed value is never resolved for masking; and _exported_values holding only one value per key so a re-export drops the previous value from masking). The third (sub-agent secret inheritance) is a stale re-report: #6724 covered the TaskManager paths and was closed as fixed by #6757, and the DelegateExecutor gap is the subject of open PR #7000. The rotation-masking contract question for the Python registry is already the explicit subject of #5708, whose triage decided Python's last-resolved-value behavior was intended and narrowed that issue to the TypeScript mirror. No single issue captures the update_secrets() masking bug, so nothing here should be auto-closed, but the author should be pointed at #5708 (rotation-masking policy decision) and #6724/#7000 (sub-agent secret propagation) before opening competing fixes.

    Found 2 possible duplicate issues:

    1. #5708 — [Bug]: TypeScript masking re-resolves already exported secret getters
    2. #6724 — [Bug]: Task sub-agents do not inherit the parent conversation’s secret registry

    These may not be exact duplicates, but the scope appears to overlap enough that keeping discussion in one place may be more useful.

    This comment was created by an AI assistant (OpenHands) on behalf of the repository maintainer.

  3. enyst commented on Nov 20, 2026

    @enyst
    Member

    I'm an AI agent helping Engel Nyst (@enyst) with project work.

    The daily feature-map pass reproduced this on main@722e5d40 on both surfaces:

    • SDK: a real Conversation with a scripted terminal agent. Program: .agents/skills/verify-sdk/programs/S13/secret_rotation.py. The first run is masked. After update_secrets({"DEPLOY_TOKEN": "dep-v2-s13"}):
      • new_value_leaks is ["dep-v2-s13"].
      • After both names are exported, previous_values_leaks is ["dep-v1-s13","oauth-v1-s13"].
      • All three values reach the persisted events/*.json.
      • [Transcript](https://github.com/enyst/openhands-evidence/blob/3723e9c6bf25dd053c3b3ace90adc57885a1cb2f/software-agent-sdk/map-maintenance/2026-11-20/edited/sdk/final-S13/S13-security-confirmation-04.log).
    • Agent server: a stub LLM, with a conversation created with secret QA_ROT.
      • Before rotation, output is OLD=<secret-hidden>.
      • After POST /api/conversations/{id}/secrets with a new value, printing the new value shows NEW=qa-rotnew-sekret.
      • After the new value is exported, the old one shows: OLD2=qa-rotold-sekret.
      • [Observations](https://github.com/enyst/openhands-evidence/blob/3723e9c6bf25dd053c3b3ace90adc57885a1cb2f/software-agent-sdk/map-maintenance/2026-11-20/repro/7002-server/obs.json).

    The feature maps now mark it as a known failure (SDK S13.secret-registry, agent-server Gotcha on F08.secrets-override) and point at #7003.


    Generated by Claude Code

  4. zsicheng0214 commented on Nov 21, 2026

    @zsicheng0214
    Author

    Thanks @enyst for reproducing this across both surfaces and adding S13/secret_rotation.py + F08.secrets-override in #7031!

    We cherry-picked #7003 directly onto #7031 (04abbc70) and ran S13/secret_rotation.py end-to-end — all four leak checks (before_rotation_leaks, new_value_leaks, previous_values_leaks, and persisted_leaks) drop to [] (passed: true), alongside the sub-agent delegation masking sync tests (TaskManager + DelegateExecutor). Happy to rebase #7003 once #7031 lands if helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumFor bugs, a serious source of annoyance, but not blocking a large number of users.ready-for-devIssue meets development readiness criteria

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions