Skip to content

The agent that grows with you

fix(mcp): mask every secret-shaped argument and show arguments on the text approval

teknium1a8144c63abb5
diff --git a/gateway/run.py b/gateway/run.py
index fbed1eae797..69a3d8d0b6d 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -595,7 +595,7 @@ def _format_exec_approval_fallback(
steps a surface without buttons needs."""
from gateway.platforms.base_exec_approval import (
approval_timeout_seconds, ea_header_text, ea_reason_label_text, format_approval_deadline_line)
- cmd_preview = command[:200] + "..." if len(command) > 200 else command
+ cmd_preview = BasePlatformAdapter._truncate_preview(command, BasePlatformAdapter._EA_CMD_BUDGET) # as on a card
heading = (t("gateway.exec_approval.smart_deny_heading") if smart_denied
else f"⚠️ **{ea_header_text()}**")
diff --git a/tests/tools/test_mcp_tool_costs.py b/tests/tools/test_mcp_tool_costs.py
index 6ae2202a4ed..68090d06835 100644
--- a/tests/tools/test_mcp_tool_costs.py
+++ b/tests/tools/test_mcp_tool_costs.py
@@ -113,21 +113,23 @@ def test_full_trust_asks_only_past_the_limit_and_unattended_refuses_without_disp
assert "used about 140 credits" in result["error"] and session.call_tool.await_count == dispatched
-def test_over_limit_notice_leads_the_untrusted_prompt_and_the_arguments_follow(session):
- """The gateway's plain-text fallback keeps only the first 200 characters of the prompt, so the
- spend-limit notice and cost must sit ahead of the call's arguments."""
+def test_over_limit_untrusted_prompt_shows_notice_then_arguments_on_the_text_fallback(session):
+ """A surface without approval buttons gets the gateway's plain-text prompt: it carries the spend-limit
+ notice and cost first, then the call's arguments."""
+ from gateway.run import _format_exec_approval_fallback
_register(dict(COSTED, spend_limit=30, trust="untrusted"))
asked = []
token = set_current_session_key("chat-a")
try:
with patch("tools.approval_prompt.request_elicitation_consent",
- side_effect=lambda msg, desc, **kw: asked.append(msg) or "decline"):
+ side_effect=lambda msg, desc, **kw: asked.append((msg, desc)) or "decline"):
handlers._make_tool_handler("srv", "paid_render", 30.0)({"scene": "castle", "notes": "y" * 3_000})
finally:
reset_current_session_key(token)
- head = asked[0][:200]
- assert "goes past the chat's spend limit" in head and "Costs about 40 credits." in head
- assert '"scene": "castle"' in asked[0][200:]
+ text = _format_exec_approval_fallback(*asked[0], "/", allow_permanent=False, allow_session=False)
+ notice, cost, arg = (text.find(s) for s in ("goes past the chat's spend limit", "Costs about 40 credits.",
+ '"scene": "castle"'))
+ assert -1 < notice < arg and -1 < cost < arg
assert session.call_tool.await_count == 0
diff --git a/tests/tools/test_mcp_trust_gating.py b/tests/tools/test_mcp_trust_gating.py
index ef724b824a9..5aaed6a6db5 100644
--- a/tests/tools/test_mcp_trust_gating.py
+++ b/tests/tools/test_mcp_trust_gating.py
@@ -117,7 +117,10 @@ class TestTrustGateAtCallTime:
"job": "release",
"api_key": "sk-supersecretcredential123456",
"db_password": "hunter2-correct-horse-battery",
- "headers": {"Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.c2lnbmF0dXJl"},
+ "headers": {"Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.c2lnbmF0dXJl",
+ "x-api-key": "opaqueheadervalue77"},
+ "password": 987654321,
+ "credentials": ["opaque-credential-value"],
"target": "prod\u202e",
"notes": "x" * 5_000,
}
@@ -131,7 +134,8 @@ class TestTrustGateAtCallTime:
message = consent.call_args.args[0]
assert '"job": "release"' in message
for secret in ("sk-supersecretcredential123456", "hunter2-correct-horse-battery",
- "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.c2lnbmF0dXJl", "\u202e"):
+ "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.c2lnbmF0dXJl", "opaqueheadervalue77", "987654321",
+ "opaque-credential-value", "\u202e"):
assert secret not in message
assert "truncated" in message
assert len(message) < 2_000
diff --git a/tools/mcp_tool_handlers.py b/tools/mcp_tool_handlers.py
index 91c2da6b4ad..b1a4ad7c65f 100644
--- a/tools/mcp_tool_handlers.py
+++ b/tools/mcp_tool_handlers.py
@@ -79,12 +79,17 @@ _TRUST_GATE_ARGUMENTS_MAX_CHARS = 1_200
_TRUST_GATE_ARGUMENTS_TRUNCATION_MARKER = "\n... [arguments truncated]"
+def _is_secret_argument_name(key: Any) -> bool:
+ from agent.redact import SECRET_HEADER_NAME_LIST, is_secret_field_name
+ return is_secret_field_name(key) or (isinstance(key, str) and key.casefold() in SECRET_HEADER_NAME_LIST)
+
+
def _mask_secret_fields(value: Any) -> Any:
- """Mask string values under credential-named fields (``db_password``, ``client_secret``): the text
- redactor below only catches the field names and token shapes it knows."""
- from agent.redact import is_secret_field_name
+ """Mask every value under a credential-named field (``db_password``, ``client_secret``) or API-key
+ header name (``x-api-key``), whatever its type: the text redactor below only catches the
+ ``key: value`` and token shapes it knows."""
if isinstance(value, dict):
- return {k: "***" if isinstance(v, str) and v and is_secret_field_name(k) else _mask_secret_fields(v)
+ return {k: "***" if v not in (None, "") and _is_secret_argument_name(k) else _mask_secret_fields(v)
for k, v in value.items()}
if isinstance(value, list):
return [_mask_secret_fields(v) for v in value]
@@ -161,9 +166,9 @@ def _ask_for_call(server_name: str, tool_name: str, charge, booked: Optional[lis
if gated:
head = f"MCP tool '{tool_name}' on UNTRUSTED server '{server_name}' wants to run."
tail = "This tool is write-capable (no readOnlyHint=true annotation) and may modify external state."
- # The limit notice leads: the messaging gateway's text fallback keeps only the first ~200 characters.
+ # The limit notice leads, and the arguments trail: a prompt cut to a platform's cap keeps the notice.
message = " ".join([head, *(["This call goes past the chat's spend limit."] if over else []), *extra, tail])
- if args: # after the notice and cost lines, which must stay in the text fallback's first ~200 characters
+ if args:
message += f"\n\nArguments:\n{_format_trust_gate_arguments(args)}"
# Over the limit the answer covers this call only: a remembered trust grant must not
# pre-approve the next over-limit call.