Skip to content

fix(sdk): mask registered secrets at the tool chokepoint - #4715

Closed
alanhuangyoo wants to merge 1 commit into
OpenHands:mainfrom
alanhuangyoo:fix/mask-secrets-at-tool-chokepoint
Closed

alanhuangyoo wants to merge 1 commit into
OpenHands:mainfrom
alanhuangyoo:fix/mask-secrets-at-tool-chokepoint

Conversation

@alanhuangyoo

@alanhuangyoo alanhuangyoo commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

Ran the new masking tests plus the existing tool suite; checked a registered secret is masked in the observation content and that an unregistered string is left alone.


AGENT:

Why

Sweeping the tools package for masking call sites returns one file:

$ grep -rln "mask_secrets_in_output\|secret_registry" openhands-tools/openhands/tools/
openhands-tools/openhands/tools/terminal/impl.py

Twelve other tool packages hand their output to the model unmasked, so whether a registered secret is masked depends on which tool the model happened to pick.

ToolDefinition.__call__ is the one place every tool with an executor returns through, and it already receives conversation. Masking there covers the whole tool surface instead of the tools that remembered — the property #4677 asks for, so tool #14 is covered by default rather than by remembering.

Summary

  • Mask registered secrets in ToolDefinition.__call__, against Observation.content — the base field every tool's text lands in, so the masking is not tool-specific. acall delegates to __call__, so the async path is covered.
  • terminal/impl.py keeps its own masking: TerminalExecutor is also called directly, without going through ToolDefinition, and test_terminal_executor_with_conversation_secrets covers exactly that path. Masking twice is idempotent.
  • Add tests/sdk/tool/test_secret_masking_chokepoint.py, including a structural test that fails when a class starts overriding ToolDefinition.__call__ without arranging its own masking.

Issue Number

Closes #4677

How to Test

End-to-end against a real Conversation with a registered secret, no mocks:

cat > /tmp/repro.py <<'EOF'
import os, tempfile
from pydantic import SecretStr
from openhands.sdk.agent import Agent
from openhands.sdk.conversation import Conversation
from openhands.sdk.llm import LLM
from openhands.tools.file_editor import FileEditorTool

SECRET = "sk-super-secret-value-123456"
tmp = tempfile.mkdtemp()
env = os.path.join(tmp, ".env")
open(env, "w").write(f"API_KEY={SECRET}\n")

llm = LLM(model="gpt-4o-mini", api_key=SecretStr("k"), usage_id="t")
conv = Conversation(agent=Agent(llm=llm, tools=[]), workspace=tmp,
                    persistence_dir=tmp, secrets={"API_KEY": SECRET})
tool = FileEditorTool.create(conv_state=conv.state)[0]
obs = tool(tool.action_type(command="view", path=env), conv)
text = " ".join(c.text for c in obs.content if hasattr(c, "text"))
print("raw secret present:", SECRET in text)
print("mask marker present:", "<secret-hidden>" in text)
EOF
uv run python /tmp/repro.py

On main:

raw secret present: True
mask marker present: False

On this branch:

raw secret present: False
mask marker present: True

Same harness across the tools named in the acceptance criteria, and a call with conversation=None to confirm behaviour is unchanged when there is no registry:

file_editor    raw=False  masked_marker=True
grep(content)  raw=False
apply_patch    raw=False
no conversation -> raw retained: True

Note on the issue text: grep and glob are listed there next to file_editor, but both return only file paths, never matched line content —

Found 1 file(s) containing pattern 'token' in '/tmp/...'
/tmp/.../cfg.txt

so they are covered by construction rather than because they were leaking. file_editor is the reproducible leak.

Suites:

uv run pytest tests/sdk/tool/ tests/cross/test_agent_secrets_integration.py \
              tests/tools/terminal/test_secrets_masking.py -q
# 221 passed

uv run ruff check && uv run ruff format --check
# clean

The new test_file_editor_does_not_hand_the_model_a_raw_secret fails on main with

assert 'sk-test-sec...abcdef123456' not in "Here's the ...\n     2\t\n"

Video/Screenshots

Not applicable — no GUI surface; the console transcripts above are the evidence.

Design Doc

Not added; the change is one function and one call site.

Sweeping openhands-tools for masking call sites returns exactly one file,
terminal/impl.py. The other twelve tool packages hand their output to the
model unmasked, so whether a secret is masked depends on which tool the
model happened to pick:

    cat .env            via terminal      ->  MASKED
    view .env           via file_editor   ->  RAW

ToolDefinition.__call__ is the one place every tool with an executor
returns through, and it already receives the conversation. Mask there,
against Observation.content, which is the base surface every tool's text
lands in. A tool added later is covered without touching it.

MCPToolDefinition overrides __call__ and masks on its own path; the test
pins that it is the only class allowed to, so a tool that leaves the
chokepoint has to be named deliberately.

terminal/impl.py keeps its own masking: TerminalExecutor is also called
directly, without going through ToolDefinition, and its own test covers
that path. Masking twice is idempotent.

Closes OpenHands#4677
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🚦 CI is currently failing on this PR's latest commit.

Please fix the failing checks before OpenHands reviews it - this is re-checked automatically once you push a new commit. (A maintainer can also request @all-hands-bot as a reviewer to have it reviewed regardless of CI status.)

This is an automated check - no AI was used to generate this comment.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

👋 This PR needs a couple of things fixed before OpenHands can review it:

  • the PR description's HUMAN: section needs at least 20 characters describing what you tested, not just the template placeholder

Push an update once this is addressed and this check re-runs automatically.

This is an automated check - no AI was used to generate this comment.

@alanhuangyoo

Copy link
Copy Markdown
Contributor Author

Superseded by #4788, which landed on 1 Sep and fixes this at the same place.

Closing rather than rebasing, because the merged version is the better of the two and the difference is not cosmetic. Both hook ToolDefinition.__call__, but this PR walks observation.content and masks the text blocks, while #4788 added SecretRegistry.mask_secrets_in_model and walks the whole model. Its own docstring names the case mine misses:

Masking one known text field is not enough for tool output: Observation.to_llm_content is overridable, and several tools build what the model sees out of their own fields rather than content.

That is right, and it means an observation that renders from its own fields would still have leaked under this PR. Nothing here is worth carrying forward on top of it.

Thanks @VascoSch92.

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.

[Security] Secret masking covers 1 tool out of 13 — the model reads raw secrets through file_editor, grep, glob, apply_patch and browser_use

2 participants