Skip to content

The agent that grows with you

fix(browser): cap redaction walks so nested page data cannot crash

Mining440c9b61dfd1
diff --git a/agent/redact.py b/agent/redact.py
index a5c32a8ec7e..482d6f6f8f1 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -17,6 +17,12 @@ from agent.file_safety import _BLOCKED_PROJECT_ENV_BASENAMES as _ENV_FILE_BASENA
logger = logging.getLogger(__name__)
+# Browser and CDP results can be a page-controlled JSON tree. The walkers stop
+# at this depth and replace the remainder, so a nested payload cannot raise
+# RecursionError on the way to the model.
+REDACT_WALK_MAX_DEPTH = 100
+REDACT_WALK_DEPTH_MARKER = "[redacted: nesting limit]"
+
# ---------------------------------------------------------------------------
# Vault-value redaction registry (profile-scoped, bounded)
# ---------------------------------------------------------------------------
diff --git a/tests/tools/test_browser_redact_depth.py b/tests/tools/test_browser_redact_depth.py
new file mode 100644
index 00000000000..2e77b9612c9
--- /dev/null
+++ b/tests/tools/test_browser_redact_depth.py
@@ -0,0 +1,45 @@
+"""Page-controlled JSON must not crash browser output redaction."""
+
+import json
+
+from agent.redact import REDACT_WALK_DEPTH_MARKER, REDACT_WALK_MAX_DEPTH
+from tools.browser_cdp_tool import _redact_cdp_output
+from tools.browser_tool_snapshot import _redact_browser_output
+
+SECRET = "sk-" + "a" * 30
+
+
+def _nest(value, levels: int):
+ for _ in range(levels):
+ value = [value]
+ return value
+
+
+def test_shallow_secret_is_still_redacted():
+ redacted = _redact_browser_output({"note": SECRET})
+ blob = json.dumps(redacted)
+ assert SECRET not in blob
+ assert REDACT_WALK_DEPTH_MARKER not in blob
+
+
+def test_browser_output_replaces_a_pathological_tree():
+ redacted = _redact_browser_output(_nest(SECRET, REDACT_WALK_MAX_DEPTH + 20))
+ blob = json.dumps(redacted)
+ assert SECRET not in blob
+ assert REDACT_WALK_DEPTH_MARKER in blob
+
+
+def test_cdp_output_replaces_a_pathological_tree():
+ redacted = _redact_cdp_output(_nest({"body": SECRET}, REDACT_WALK_MAX_DEPTH + 20))
+ blob = json.dumps(redacted)
+ assert SECRET not in blob
+ assert REDACT_WALK_DEPTH_MARKER in blob
+
+
+def test_cdp_binary_leaf_stays_byte_identical():
+ payload = "gAAAA" + "A" * 40
+ redacted = _redact_cdp_output(
+ {"data": payload},
+ always_paths=(("data",),),
+ )
+ assert redacted["data"] == payload
diff --git a/tools/browser_cdp_tool.py b/tools/browser_cdp_tool.py
index 46a8b58a478..1ac5db764c2 100644
--- a/tools/browser_cdp_tool.py
+++ b/tools/browser_cdp_tool.py
@@ -45,7 +45,13 @@ _CDP_FLAGGED_BINARY_PATHS: dict[str, tuple] = {
}
-def _redact_cdp_output(value: Any, *, always_paths: tuple = (), flagged_paths: tuple = ()) -> Any:
+def _redact_cdp_output(
+ value: Any,
+ *,
+ always_paths: tuple = (),
+ flagged_paths: tuple = (),
+ _depth: int = 0,
+) -> Any:
"""Redact browser-originated CDP result text; opaque bytes stay byte-identical.
Exemptions come ONLY from the calling method's spec as exact result paths. Path
@@ -53,13 +59,24 @@ def _redact_cdp_output(value: Any, *, always_paths: tuple = (), flagged_paths: t
solely as a sibling on the trusted carrier object — never as ambient trust a
``Runtime.evaluate`` by-value object could spoof.
+ ``_depth`` stops a page-controlled tree from raising RecursionError. The
+ unread remainder is replaced, not returned raw.
+
See #94138, #94142.
"""
- from agent.redact import redact_sensitive_text
+ from agent.redact import (
+ REDACT_WALK_DEPTH_MARKER,
+ REDACT_WALK_MAX_DEPTH,
+ redact_sensitive_text,
+ )
+ if _depth >= REDACT_WALK_MAX_DEPTH:
+ return REDACT_WALK_DEPTH_MARKER
if isinstance(value, str):
return redact_sensitive_text(value, force=True)
if isinstance(value, (list, tuple)):
- return type(value)(_redact_cdp_output(item) for item in value)
+ return type(value)(
+ _redact_cdp_output(item, _depth=_depth + 1) for item in value
+ )
if not isinstance(value, dict):
return value
base64_flagged = value.get("base64Encoded") is True
@@ -72,7 +89,11 @@ def _redact_cdp_output(value: Any, *, always_paths: tuple = (), flagged_paths: t
opaque = leaf(always_paths, key) or (leaf(flagged_paths, key) and base64_flagged)
out_key = redact_sensitive_text(key, force=True) if isinstance(key, str) else key # by-value objects can carry a secret as a KEY
redacted[out_key] = item if isinstance(item, str) and opaque else _redact_cdp_output(
- item, always_paths=descend(always_paths, key), flagged_paths=descend(flagged_paths, key))
+ item,
+ always_paths=descend(always_paths, key),
+ flagged_paths=descend(flagged_paths, key),
+ _depth=_depth + 1,
+ )
return redacted
diff --git a/tools/browser_tool_snapshot.py b/tools/browser_tool_snapshot.py
index d6b89641426..0b95d77969d 100644
--- a/tools/browser_tool_snapshot.py
+++ b/tools/browser_tool_snapshot.py
@@ -108,18 +108,31 @@ def _truncate_snapshot(snapshot_text: str, max_chars: Optional[int] = None) -> s
return '\n'.join(result)
-def _redact_browser_output(value: Any) -> Any:
+def _redact_browser_output(value: Any, *, _depth: int = 0) -> Any:
"""Force-redact secrets in browser-originated data (snapshots, console, eval
results can carry page-rendered keys/cookies/tokens). Tool output is a model
- boundary, so this applies even when global log redaction is disabled."""
- from agent.redact import redact_sensitive_text
+ boundary, so this applies even when global log redaction is disabled.
+ ``_depth`` stops a page-controlled tree from raising RecursionError. The
+ unread remainder is replaced, not returned raw.
+ """
+ from agent.redact import (
+ REDACT_WALK_DEPTH_MARKER,
+ REDACT_WALK_MAX_DEPTH,
+ redact_sensitive_text,
+ )
+
+ if _depth >= REDACT_WALK_MAX_DEPTH:
+ return REDACT_WALK_DEPTH_MARKER
if isinstance(value, str):
return redact_sensitive_text(value, force=True)
if isinstance(value, list):
- return [_redact_browser_output(item) for item in value]
+ return [_redact_browser_output(item, _depth=_depth + 1) for item in value]
if isinstance(value, tuple):
- return tuple(_redact_browser_output(item) for item in value)
+ return tuple(_redact_browser_output(item, _depth=_depth + 1) for item in value)
if isinstance(value, dict):
- return {_redact_browser_output(key): _redact_browser_output(item) for key, item in value.items()}
+ return {
+ _redact_browser_output(key, _depth=_depth + 1): _redact_browser_output(item, _depth=_depth + 1)
+ for key, item in value.items()
+ }
return value