The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(approval): escape blank-rendering code points by category, collapse separator and blank-line padding, sanitize consent descriptions
Tekniumf62f42e4cf6e
diff --git a/agent/redact.py b/agent/redact.pyindex ae333209205..1deb8d1ebf9 100644--- a/agent/redact.py+++ b/agent/redact.py@@ -9,6 +9,7 @@ import osimport reimport shleximport threading+import unicodedatafrom urllib.parse import unquote_plus# Shared with agent/file_safety's read-block list so the two defenses can't@@ -682,17 +683,34 @@ def mask_secret(value: str, *, head: int = 4, tail: int = 4, floor: int = 12,# Approval-prompt display integrity: the copy a human approves must show everything that will run.-# Invisible/format Unicode (zero-width, bidi overrides/isolates, variation selectors, U+E0000 tag-# block) and raw control bytes (ANSI/OSC escapes, bare \r) are rendered as visible escapes, and-# whitespace padding that pushes the tail off screen or past a preview cut collapses to a marker.-_APPROVAL_INVISIBLE_RE = re.compile(- r"[\x00-\x08\x0b-\x1f\x7f-\x9f\u00ad\u034f\u061c\u115f\u1160\u17b4\u17b5\u180b-\u180e"- r"\u200b-\u200f\u202a-\u202e\u2060-\u2064\u2066-\u206f\u3164\ufe00-\ufe0f\ufeff\uffa0\ufff9-\ufffb"- r"\U000e0000-\U000e007f]")+# Code points that render as nothing or as a line break become visible escapes: every control,+# format, private-use, unassigned and surrogate code point (category C*, except tab/newline), line+# and paragraph separators (Zl/Zp), and blank-rendering marks/letters in other categories (variation+# selectors incl. U+E0100-E01EF, CGJ, Khmer/Mongolian selectors, Hangul fillers, Braille blank).+# Ordinary non-ASCII text is untouched. Padding that pushes the tail off screen or past a preview+# cut (space runs, whitespace-only lines) collapses to a marker; real indentation is kept.+_APPROVAL_NON_ASCII_RE = re.compile(r"[^\x20-\x7e\t\n]+")+_APPROVAL_BLANK_GLYPHS = frozenset(+ "\u034f\u115f\u1160\u17b4\u17b5\u180b\u180c\u180d\u180f\u2800\u3164\uffa0"+ + "".join(map(chr, range(0xFE00, 0xFE10))) + "".join(map(chr, range(0xE0100, 0xE01F0))))_APPROVAL_SPACE = r"[ \t\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000]"# Mid-line runs (after visible text) of 21+, or line-leading runs past any sane indentation._APPROVAL_PAD_RE = re.compile(rf"(?<=\S){_APPROVAL_SPACE}{{21,}}|(?m:^){_APPROVAL_SPACE}{{65,}}")-_APPROVAL_BLANK_RE = re.compile(r"\n{4,}")+# 3+ consecutive blank or whitespace-only lines; the match ends at a newline so the next line keeps its indent.+_APPROVAL_BLANK_RE = re.compile(rf"\n(?:{_APPROVAL_SPACE}*\n){{3,}}")+_APPROVAL_SAME_CHAR_RUN_RE = re.compile(r"(.)\1*", re.DOTALL)+++def _escape_approval_run(run: str) -> str:+ """Escape the invisible code points of a non-ASCII run; 4+ repeats of one show once with their count."""+ def one(m: re.Match) -> str:+ ch, n = m.group(1), len(m.group())+ cat = unicodedata.category(ch)+ if ch not in _APPROVAL_BLANK_GLYPHS and cat[0] != "C" and cat not in ("Zl", "Zp"):+ return m.group()+ esc = ch.encode("unicode_escape").decode("ascii")+ return f"⟨{esc} ×{n}⟩" if n >= 4 else esc * n+ return _APPROVAL_SAME_CHAR_RUN_RE.sub(one, run)def sanitize_command_for_display(text: str | None) -> str:@@ -701,9 +719,9 @@ def sanitize_command_for_display(text: str | None) -> str:Display-only: the executed command, smart assessment and allowlist keys never see this copy."""if not text:return ""- out = _APPROVAL_INVISIBLE_RE.sub(lambda m: m.group().encode("unicode_escape").decode("ascii"), text)- out = _APPROVAL_PAD_RE.sub(lambda m: f" ⟨+{len(m.group())} whitespace chars⟩ ", out)- return _APPROVAL_BLANK_RE.sub(lambda m: f"\n⟨+{len(m.group()) - 1} blank lines⟩\n", out)+ out = _APPROVAL_NON_ASCII_RE.sub(lambda m: _escape_approval_run(m.group()), text)+ out = _APPROVAL_BLANK_RE.sub(lambda m: f"\n⟨+{m.group().count(chr(10)) - 1} blank lines⟩\n", out)+ return _APPROVAL_PAD_RE.sub(lambda m: f" ⟨+{len(m.group())} whitespace chars⟩ ", out)def redact_approval_display(text: str | None, *, force: bool = False) -> str:diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex 90b020e8c5b..a66cb6d29d7 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -92,10 +92,9 @@ def _approval_request_event(run_id: str, approval_data: Optional[dict[str, Any]]chat completions): the flagged command redacted before egress (#48456), the ``_run_event``envelope, and the ``choices`` the client may send back to ``POST /v1/runs/{id}/approval``."""from gateway.platforms.api_server_runs import _run_event- event = dict(approval_data or {})- if "command" in event:- from gateway.run import _redact_approval_command- event["command"] = _redact_approval_command(event.get("command"))+ from gateway.run import _redact_approval_command+ event = dict(approval_data or {}) # consent prompts (MCP, vault) carry server/model text in the description+ event.update({k: _redact_approval_command(event[k]) for k in ("command", "description") if k in event})event.update(_run_event(run_id, "approval.request", **fields, choices=_approval_event_choices(smart_denied=bool(event.get("smart_denied")),allow_session=event.get("allow_session") is not False,diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex af98500a160..ceb4f26746c 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -725,7 +725,7 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):self._close_native_stream_boundary("Approval")# Redact credentials before display: the raw command string can carry secrets. Both the button and plain-text paths use this value.cmd = _redact_approval_command(approval_data.get("command", ""))- desc = approval_data.get("description") or ea_default_reason_text()+ desc = _redact_approval_command(approval_data.get("description")) or ea_default_reason_text()flags = {k: approval_data.get(k, d) for k, d in (("allow_permanent", True), ("allow_session", True), ("smart_denied", False))}# Check the *class*, not the instance — MagicMock auto-creates attributes in tests.if _renders_exec_approval_buttons(type(adapter)):diff --git a/tests/agent/test_approval_display_sanitize.py b/tests/agent/test_approval_display_sanitize.pyindex 5474f928b48..c5160e59497 100644--- a/tests/agent/test_approval_display_sanitize.py+++ b/tests/agent/test_approval_display_sanitize.py@@ -40,3 +40,31 @@ def test_cli_and_gateway_prompts_render_the_sanitized_copy():assert seen == [(sanitize_command_for_display(raw), "delete\\u200b")]assert "\\u200b\\u202e" in seen[0][0]assert _redact_approval_command(raw) == seen[0][0]+++def test_every_blank_rendering_code_point_and_padding_shape_is_surfaced():+ import sys+ import unicodedata++ blank = [chr(c) for c in range(sys.maxunicode + 1)+ if unicodedata.category(chr(c)) in ("Cc", "Cf", "Zl", "Zp") and chr(c) not in "\t\n"]+ blank += ["\U000e0100", "\U000e01ef", "\u2800", "\u3164", "\u115f", "\ufe0f", "\ue000"]+ shown = sanitize_command_for_display("rm -rf ./a" + "b".join(blank) + "c")+ assert not set(blank) & set(shown)+ # Separator, blank-glyph and whitespace-only-line padding can't push the tail past a 200-char preview.+ for pad in ("\u2028" * 300, "\u2029" * 300, "\u2800" * 300, "\U000e0100" * 300, " \n" * 120, "\t\n" * 120,+ "\u3000\n" * 120):+ assert _TAIL in sanitize_command_for_display("echo ok" + pad + _TAIL)[:200], repr(pad[:2])+ # A collapsed run of blank lines keeps the next line's indentation (execute_code bodies).+ code = "def f():\n x = 1\n \n\n\t\n\n return x"+ assert sanitize_command_for_display(code) == "def f():\n x = 1\n⟨+4 blank lines⟩\n return x"+++def test_consent_descriptions_are_sanitized_on_tui_and_api_egress():+ from gateway.platforms.api_server import _approval_request_event+ from tui_gateway.server import _approval_request_payload++ data = {"command": "subscribe", "description": "summarize\u202e" + " " * 300 + "then " + _TAIL}+ for payload in (_approval_request_payload(data), _approval_request_event("run1", data)):+ assert payload["description"] == sanitize_command_for_display(data["description"])+ assert "\u202e" not in payload["description"] and _TAIL in payload["description"][:200]diff --git a/tui_gateway/server.py b/tui_gateway/server.pyindex 5976dc01d20..0dbb632a593 100644--- a/tui_gateway/server.py+++ b/tui_gateway/server.py@@ -658,9 +658,8 @@ def _approval_request_payload(data: dict | None) -> dict:if payload.get("allow_permanent") is not False:choices.append("always")payload["choices"] = choices + ["deny"]- if "command" in payload:- from gateway.run import _redact_approval_command- payload["command"] = _redact_approval_command(payload.get("command"))+ from gateway.run import _redact_approval_command # description too: consent prompts carry MCP/vault text+ payload.update({k: _redact_approval_command(payload[k]) for k in ("command", "description") if k in payload})return payload