Skip to content

[Bug]: Command and agent hooks cannot access the conversation's secrets (hook env has no secrets injected) #6966

Open

Description

@all-hands-bot

In plain words

A hook cannot use the conversation's secrets. If a hook command references $MY_API_KEY (a secret registered on the conversation), the variable resolves to empty at hook runtime — even though the same secret works fine when the agent runs it via execute_command. The hook should have access to the secret of the conversation it runs in.

What I verified (openhands-sdk 1.58.2, matching main)

The hook subprocess environment is built in HookExecutor.execute() (openhands-sdk/openhands/sdk/hooks/executor.py, ~L481):

hook_env = sanitized_env()
hook_env["OPENHANDS_PROJECT_DIR"] = self.working_dir
hook_env["OPENHANDS_SESSION_ID"] = event.session_id or ""
hook_env["OPENHANDS_EVENT_TYPE"] = event.event_type
if event.tool_name:
    hook_env["OPENHANDS_TOOL_NAME"] = event.tool_name
  • sanitized_env() deliberately strips the ambient environment, so nothing secret-bearing leaks in — but the conversation's SecretRegistry is never consulted. There are zero references to secrets anywhere in openhands/sdk/hooks/ (grep for secret comes up empty).
  • HookManager (openhands-sdk/openhands/sdk/hooks/manager.py) also has no secret-registry reference, and neither create_hook_callback() nor HookEventProcessor thread one through.
  • Agent hooks spawn a LocalConversation sub-conversation in _execute_agent_hook() without forwarding secrets either, so a hook sub-agent running bash can't see them either.
  • The irony: SecretRegistry.get_all_secrets_as_env_vars() (openhands-sdk/openhands/sdk/conversation/secret_registry.py) already exists and its docstring says it is meant "for opaque consumers (e.g. an ACP CLI subprocess) that cannot be name-scanned per command and must receive their credentials upfront". Command hooks are exactly such a consumer — the hook command string is never name-scanned — but the hook path never calls it. By contrast, the agent's own bash commands do get secrets via get_secrets_as_env_vars() name-scanning, which is why the agent works and hooks don't.

Steps to reproduce

  1. Create a conversation and register a secret, e.g. FOO=bar (via the conversation secrets API / secret store).

  2. Configure a hook, e.g. on PreToolUse:

    { "pre_tool_use": [{ "command": "echo \"FOO=${FOO:-UNSET}\"" }] }
  3. Trigger any tool call. The HookExecutionEvent stdout shows FOO=UNSET.

  4. Compare with the agent running execute_command echo $FOO → bar (masked as <secret-hidden> in output).

Expected

The hook should have access to the secret of the conversation. A hook doing curl -H "Authorization: Bearer $MY_TOKEN" ... should work.

Suggested approach

  • Pass the conversation's SecretRegistry (or a name→value resolver callable) into create_hook_callback() → HookManager → HookExecutor (the wiring already passes llm, llm_getter, visualizer, etc. the same way).
  • Inject the full registry via get_all_secrets_as_env_vars() into hook_env for command hooks (hooks are opaque consumers — their commands aren't name-scanned), and/or offer per-hook opt-out to keep least privilege.
  • Injection should go through get_secret_value()/track_exported_values() so values land in _exported_values and hook stdout/stderr persisted in HookExecutionEvent stay masked.
  • Agent-hook sub-conversations should receive the parent conversation's secrets.

Notes

  • Scope: command hooks (subprocess env) and agent hooks (sub-conversation secrets). Prompt hooks don't need it (they call the LLM directly), but injected additionalContext flows are unaffected.
  • Security consideration: injecting the whole registry into every hook widens credential exposure to arbitrary hook commands; masking already covers persisted hook output, and an explicit per-hook allowlist (opt-in) could be the conservative default.

OpenHands AI triage

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

Triage

Verified on main: the hook subprocess environment in HookExecutor.execute() (openhands-sdk/openhands/sdk/hooks/executor.py) is built from sanitized_env() and never consults the conversation's SecretRegistry; there are zero references to secrets under openhands/sdk/hooks/. Both create_hook_callback() call sites in LocalConversation (initial plugin load and hook reconfiguration) thread llm_getter, visualizer, and conversation_stats but no secret resolver, and the agent-hook sub-conversation created in _execute_agent_hook() receives no secrets either. SecretRegistry.get_all_secrets_as_env_vars() already exists for opaque consumers and routes values through track_exported_values(), so the mechanism the issue points to is real and unused. The reporter's repro, code citations, and masking analysis check out; this is a genuine gap, not a usage question.

Bounded scope: command hooks receive the conversation's registered secrets in the hook subprocess environment, and agent hooks forward the parent conversation's secrets to their sub-conversation, using the existing SecretRegistry resolution and tracking machinery. Nothing in the issue or repository records a user report, support evidence, or production failure beyond the reporter's own verification, so this stays at priority:low.

Non-goals: per-hook secret allowlists or opt-in scoping (the get_all_secrets_as_env_vars() docstring defers least-privilege scoping to #1039 task 6); name-scanning hook command strings for selective injection; changing prompt hooks (they call the LLM directly and need no secrets); new REST/WebSocket API surface; changes to how the agent's own bash commands receive secrets; and any relaxation of existing secret masking in persisted events and logs.

Acceptance Criteria

  • With a registered conversation secret FOO and a command hook whose command prints the value of $FOO, a triggered tool call produces a HookExecutionEvent whose stdout resolves the secret value instead of empty/unset, while the raw value is masked as <secret-hidden> in the persisted event.
  • Command hook subprocess environments continue to expose the existing non-secret context (OPENHANDS_PROJECT_DIR, OPENHANDS_SESSION_ID, OPENHANDS_EVENT_TYPE, OPENHANDS_TOOL_NAME, PLUGIN_ROOT) with unchanged semantics.
  • An agent hook whose sub-agent runs a bash command referencing a registered secret sees that secret resolve in the sub-conversation, with the parent conversation's secrets available.
  • Hook command execution with zero registered secrets behaves identically to the pre-change behavior (same environment keys, same results).
  • A secret whose lookup fails or resolves to an empty value is omitted from the hook environment without failing or blocking hook execution.
  • Secret values injected for hooks are registered in the same exported-values tracking used by SecretRegistry, so hook stdout/stderr persisted in HookExecutionEvent and in logs never contain plaintext secret values, matching the masking behavior of agent bash output.
  • All existing hook tests pass unchanged except where they asserted the absence of secrets, and a test covers that a command hook referencing a registered secret receives it, including its masked persisted output.

Activity

  1. added
    bug
    hooks
    ready-for-dev
    and removed
    bug
    hooks
    on Nov 18, 2026
  2. added a commit that references this issue on Nov 19, 2026
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

    bughooksready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions