Skip to content

fix(secrets): opt base64 payloads out of secret masking - #5226

Closed
jpshackelford wants to merge 1 commit into
mainfrom
fix/5224-masker-corrupts-base64
Closed

jpshackelford wants to merge 1 commit into
mainfrom
fix/5224-masker-corrupts-base64

Conversation

@jpshackelford

@jpshackelford jpshackelford commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Fixes issue #5224 — the secret masker corrupts base64 screenshot payloads and borks the conversation.


AGENT:

Why

Fixes #5224.

SecretRegistry.mask_secrets_in_model walked every string field on every observation and ran a plain substring .replace(secret_value, "<secret-hidden>") against it. When a field held a large opaque payload — BrowserObservation.screenshot_data, or a data:...;base64,... URL inside ImageContent.image_urls — any registered secret whose raw characters happened to appear as a substring of the base64 (a near-certainty for any 8+ char token in a 200 KB+ screenshot) got the literal string <secret-hidden> spliced into the middle of the payload.

The corrupted observation was persisted into the event store, so from that point on the LLM request built from history was rejected by the provider on every attempt (Anthropic image.source.base64: invalid base64 data), permanently wedging the conversation.

Regression was introduced by #4788, which wired mask_secrets_in_model into the shared tool chokepoint. Present in every release from v1.45.0 through current main.

Summary

  • Introduce SkipSecretMasking in openhands.sdk.utils.masking (re-exported from openhands.sdk.utils). Tag a Pydantic field with Annotated[T, SkipSecretMasking()] and _mask_model skips it entirely; ordinary fields are still walked and masked as before. The class docstring is the design note explaining when and why to reach for the marker.
  • Tag BrowserObservation.screenshot_data (raw base64 blob) and ImageContent.image_urls (the data:...;base64,... URLs rendered from it) so both round-trip byte-for-byte through the masker. Remote URLs never legitimately embed a registered secret either, so opting the whole list out is safe and keeps the rule simple.
  • The marker lives in openhands.sdk.utils so both the masker (sdk.conversation.secret_registry) and the models it walks (sdk.llm.message, which the conversation package imports transitively) can reference it without an import cycle.

Issue Number

Fixes #5224.

How to Test

Repro from the issue, run against this branch:

uv sync
uv run python - <<'PY'
from openhands.sdk.conversation.secret_registry import SecretRegistry
from openhands.tools.browser_use.definition import BrowserObservation

reg = SecretRegistry()
reg.update_secrets({"MY_KEY": "XYZ789"})
reg.get_secret_value("MY_KEY")

payload = "iVBORw0KGgoAAA" + "XYZ789" + "SUhEUgAAB4A=="
obs = BrowserObservation(screenshot_data=payload)
masked = reg.mask_secrets_in_model(obs)
assert masked.screenshot_data == payload, masked.screenshot_data
print("OK — screenshot round-tripped unchanged")
PY

Before this change the assert fails with iVBORw0KGgoAAA<secret-hidden>SUhEUgAAB4A==. On this branch it prints OK.

Regression tests added:

  • tests/sdk/conversation/test_secret_registry_model_masking.py::test_skip_secret_masking_leaves_annotated_string_field_alone — generic marker coverage on a synthetic model.
  • tests/sdk/conversation/test_secret_registry_model_masking.py::test_skip_secret_masking_covers_list_fields — same for list[str] fields (matches the ImageContent.image_urls shape).
  • tests/tools/browser_use/test_browser_observation.py::test_secret_masker_leaves_screenshot_and_image_urls_alone — end-to-end scenario from the issue: register a secret whose characters collide with base64, run a real BrowserObservation (plus the ImageContent it renders) through the masker, and assert the payload is byte-identical while the surrounding prose is still masked.

Full local run (fix branch):

uv run pytest tests/sdk/conversation/ tests/sdk/llm/ tests/tools/browser_use/ tests/sdk/tool/ -q
# ================ 2181 passed, 21 warnings in 202.68s (0:03:22) =================

uv run pytest tests/sdk/agent/ -q
# =============== 915 passed, 6 deselected, 38 warnings in 22.31s ================

uv run ruff format ...   # 1 file reformatted, 7 files left unchanged
uv run ruff check  ...   # All checks passed!

Video/Screenshots

N/A — pure logic change in the secret masker plus field-level tags. Behavior is captured by the assertions in the tests above.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

Companion issue #5225 tracks the recovery-path gap: even with this fix, a single provider-rejected tool_result still wedges the whole conversation because LLMBadRequestError has no recovery path in Agent.step. Out of scope for this PR.


This pull request was created by an AI agent (OpenHands) on behalf of @jpshackelford.

The recursive masker in SecretRegistry.mask_secrets_in_model ran a
substring .replace across every string field on every observation. A
registered secret whose characters happened to appear inside a large
opaque blob — screenshots are hundreds of KB of base64 and collide with
any 8+ char token by chance — got <secret-hidden> spliced mid-payload.
The corrupted observation was persisted, so every subsequent turn
rebuilt the same poisoned tool_result and the provider rejected the
whole conversation as invalid base64.

Add SkipSecretMasking, a marker for Annotated[T, SkipSecretMasking()]
that _mask_model skips. Tag BrowserObservation.screenshot_data and
ImageContent.image_urls with it so both the raw blob and the data URL
rendered from it round-trip byte-for-byte through the masker. The
tag lives in openhands.sdk.utils so both the masker and the models
it walks can reference it without a cycle.

Fixes #5224.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/conversation
   secret_registry.py172597%109, 365, 367, 370, 379
openhands-sdk/openhands/sdk/llm
   message.py267499%410, 423–424, 432
openhands-tools/openhands/tools/browser_use
   definition.py1997264%58–61, 80–82, 84–87, 90–92, 94–96, 98–100, 104, 107–108, 111–113, 124–125, 128–129, 132–133, 138–139, 141, 188, 238, 285, 329, 376, 420, 458, 496, 539, 581, 622, 670, 720, 766, 808–810, 824–826, 828–830, 832, 834, 838–840, 848–850, 853, 858, 861–862, 878–879
TOTAL438351252271% 

@jpshackelford
jpshackelford marked this pull request as ready for review September 22, 2026 21:28

@jpshackelford jpshackelford left a comment •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Roasted Output

Taste: good.

Small, focused fix that matches the issue's preferred remedy: an
Annotated[T, SkipSecretMasking()] marker that _mask_model checks before
recursing. The marker lives in openhands.sdk.utils to avoid the cycle
between secret_registry and llm.message, and both known base64 carriers
(BrowserObservation.screenshot_data and ImageContent.image_urls) are
tagged, which also transitively covers MCPToolObservation.from_call_tool_result
since it emits its data-URL through ImageContent. file_editor and
vision_inspect also produce their images through ImageContent, so the
opt-out reaches every current base64 sink without per-tool duplication.

The regression tests exercise both the generic marker (str and list[str]
shapes) and the end-to-end BrowserObservation → to_llm_content →
ImageContent path from the issue reproducer, and untagged sibling fields
are still asserted to be masked. Every current-head CI check is green.

Acceptance criteria

  • Masking a BrowserObservation leaves screenshot_data byte-identical
    when a registered secret's characters occur inside the base64
    (test_secret_masker_leaves_screenshot_and_image_urls_alone).
  • A data:image/*;base64,* URL inside ImageContent.image_urls is not
    substring-replaced under the same condition (same test, plus
    test_skip_secret_masking_covers_list_fields).
  • The MCP image path is covered transitively — MCPToolObservation.from_call_tool_result
    constructs ImageContent(image_urls=[...]), and the tag on that field opts
    the whole list out. No dedicated MCP test, but the mechanism reaches it.
  • BrowserObservation.to_llm_content yields an ImageContent whose data
    URL still ends in the original payload after masking (asserted in the same
    end-to-end test).
  • Text fields are still masked — the sibling label/text fields in
    both new tests assert the marker is still substituted where it should be.
  • No new required fields, serializer changes, or schema-version bump.
    Annotated metadata is invisible to the wire format; previously persisted
    events still load unchanged.
  • Tool.__call__ still routes every observation through
    mask_secrets_in_model; new tools inherit masking by default and only
    opt out per-field.

Observation (non-blocking)

Opting the entire image_urls list out of masking is the right call for the
base64 case, and the file comment says as much. It does mean that if a
caller ever puts a secret's characters into a plain remote URL (e.g., a
signed https://…?token=SECRET link), that string will no longer be masked
in that field. In practice masking such a URL would corrupt it anyway, and
this is called out in the design note, so it's a reasonable trade — flagging
only so a future reader doesn't rediscover it as a surprise.

[RISK ASSESSMENT]

Risk: LOW. Behavior change is confined to two field annotations plus a
23-line marker module; the semantic change is subtractive (some fields are
no longer scanned) and every current caller path is covered by the new
tests. No compatibility or serialization surface is touched.

Verdict: APPROVE.

Architectural insight. The regression was possible because a global,
unconditional pass mutated every string reachable from an observation.
Making the opt-out declarative on the field (rather than a per-tool
post-processing step) keeps a single chokepoint — Tool.__call__ still
routes through mask_secrets_in_model for every tool — while letting each
model own the "this field is an opaque payload" contract in its own
declaration. Future tools that carry binaries pick the safe default (still
masked) but have a one-line opt-out available at the site where the field
is defined.


This review was generated by an AI agent (OpenHands) on behalf of @jpshackelford.

Improve this review? If feedback seems incorrect or irrelevant, update the
repository's .agents/skills/custom-codereview-guide.md (with the /codereview
trigger), then re-request review. The reviewer reads the guide from the PR head.

Resolve with AI? Install the
iterate skill
and run /iterate.

Was this review helpful? React with 👍 or 👎.

@jpshackelford

Copy link
Copy Markdown
Member Author

Closing in favor of #5227.

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.

[Bug]: Secret masking corrupts base64 payloads (e.g. browser screenshots), permanently wedging the conversation

2 participants