The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(a2a): refuse browser-originated POSTs in no-token mode
John Paul Soliva881eb6c252d6
diff --git a/plugins/platforms/a2a/README.md b/plugins/platforms/a2a/README.mdindex b7c9258d614..ab6b1b535b3 100644--- a/plugins/platforms/a2a/README.md+++ b/plugins/platforms/a2a/README.md@@ -57,6 +57,8 @@ via `tasks/get`.- **No token ⇒ localhost only.** The server binds `127.0.0.1` and refuses towiden unless you configure a token *and* set `A2A_HOST`.+ Without a token it also refuses browser-originated POSTs (`Origin`,+ cross-site `Sec-Fetch-Site`, non-JSON `Content-Type`).- **Per-peer tokens**: `A2A_PEER_TOKENS="alice:tok1,bob:tok2"` gives eachremote agent its own credential; that authenticated name (never anythingin the request body) drives rate limiting, trust, and audit.diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.pyindex 205da76bee3..cf560d59013 100644--- a/plugins/platforms/a2a/adapter.py+++ b/plugins/platforms/a2a/adapter.py@@ -218,6 +218,8 @@ class A2ARequestHandler(BaseHTTPRequestHandler):identity = adapter._security_context.authenticate(self.headers.get("Authorization"), self._client_ip())if identity is None:return self._error(401, None, protocol.ERR_UNAUTHORIZED, "unauthorized")+ if adapter._security_context.localhost_only() and (refusal := security.browser_post_refusal(self.headers)):+ return self._error(403, None, protocol.ERR_UNAUTHORIZED, refusal)try:length = int(self.headers.get("Content-Length", 0))if length > _MAX_BODY:diff --git a/plugins/platforms/a2a/security.py b/plugins/platforms/a2a/security.pyindex a415e86aecf..a8e07b7aeb9 100644--- a/plugins/platforms/a2a/security.py+++ b/plugins/platforms/a2a/security.py@@ -127,6 +127,24 @@ class A2ASecurityContext:return hmac.new(self.push_secret.encode("utf-8"), body, hashlib.sha256).hexdigest()+def browser_post_refusal(headers) -> Optional[str]:+ """Why a no-token POST must be refused as browser-originated, or None.++ No-token mode trusts anything that reaches 127.0.0.1, and a web page open in the operator's+ browser reaches it too. Refuse what only a browser sends: an ``Origin`` header, a+ ``Sec-Fetch-Site`` other than ``none``/``same-origin``, or a body type a page can POST without a+ CORS preflight (A2A is JSON, so anything but a JSON media type). Local agents and SDKs send JSON+ and none of these headers."""+ if headers.get("Origin") is not None:+ return "browser-originated request refused (no A2A token configured)"+ if (headers.get("Sec-Fetch-Site") or "none").strip().lower() not in {"none", "same-origin"}:+ return "cross-site request refused (no A2A token configured)"+ media_type = (headers.get("Content-Type") or "").split(";", 1)[0].strip().lower()+ if media_type != "application/json" and not media_type.endswith("+json"):+ return "Content-Type must be application/json"+ return None++def localhost_only() -> bool:"""Fresh-context convenience for callers outside the adapter."""return A2ASecurityContext.capture().localhost_only()diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.pyindex 2a38f4d6b5d..96af420947d 100644--- a/tests/plugins/test_a2a_plugin.py+++ b/tests/plugins/test_a2a_plugin.py@@ -973,6 +973,39 @@ def test_ipv6_loopback_bind_serves_and_advertises_a_bracketed_url(monkeypatch, sassert adapter._base_url(None) == base + "/"[email protected]("headers,dispatched", [+ ({}, True), # local agent / SDK+ ({"Origin": "https://site.example"}, False), # any browser Origin+ ({"Sec-Fetch-Site": "cross-site"}, False),+ ({"Content-Type": "text/plain"}, False), # CORS-simple body type+])+def test_no_token_listener_refuses_browser_originated_posts(monkeypatch, headers, dispatched):+ """Without a token the listener trusts the loopback socket, which a web page in the operator's+ browser can also reach: requests carrying browser signals are refused and never dispatched."""+ monkeypatch.delenv("A2A_BEARER_TOKEN", raising=False)+ monkeypatch.delenv("A2A_PEER_TOKENS", raising=False)+ seen: list = []+ adapter, base = _make_live_adapter(monkeypatch, reply_fn=lambda event: seen.append(event) or "ok")++ def post():+ try:+ return 200, _post_json(base + "/", _send_body("hi"), headers)+ except urllib.error.HTTPError as exc:+ return exc.code, json.loads(exc.read().decode())++ async def run():+ assert await adapter.connect() is True+ try:+ return await asyncio.to_thread(post)+ finally:+ await adapter.disconnect()++ status, _ = asyncio.run(run())+ assert (status == 200, bool(seen)) == (dispatched, dispatched), status+ if not dispatched:+ assert status == 403++@pytest.mark.integrationclass TestInboundRoundTrip:def test_live_server_card_and_message_send(self, monkeypatch):diff --git a/website/docs/user-guide/messaging/a2a.md b/website/docs/user-guide/messaging/a2a.mdindex 8a31dc2ef11..53d727d7544 100644--- a/website/docs/user-guide/messaging/a2a.md+++ b/website/docs/user-guide/messaging/a2a.md@@ -110,7 +110,7 @@ to bypass the ownership check.Secure by default; every widening step is explicit:-- **No token ⇒ localhost only.** The server binds `127.0.0.1`. Remote exposure requires a bearer token **and** an explicit `A2A_HOST`.+- **No token ⇒ localhost only.** The server binds `127.0.0.1`. Remote exposure requires a bearer token **and** an explicit `A2A_HOST`. Without a token it also refuses browser-originated POSTs (an `Origin` header, a cross-site `Sec-Fetch-Site`, or a non-JSON `Content-Type`), so a web page open on the same machine cannot send it tasks. Local agents and SDKs that POST JSON are unaffected.- **Per-peer tokens** — `A2A_PEER_TOKENS="alice:tok1,bob:tok2"` gives each peer its own credential; the authenticated name drives rate limiting, trust, and audit.- **Prompt-injection filtering** — inbound text is filtered and framed as untrusted peer input. Remote peers cannot invoke operator slash commands.- **Outbound redaction** — credential-shaped strings (API keys, JWTs, tokens) are scrubbed from replies.