Skip to content

The agent that grows with you

fix(a2a): scope multiplexed peer authorization

fangliquanflq8167cfa4b6d3
diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.py
index b546edc8415..7a9b6b11428 100644
--- a/plugins/platforms/a2a/adapter.py
+++ b/plugins/platforms/a2a/adapter.py
@@ -249,7 +249,7 @@ class A2ARequestHandler(BaseHTTPRequestHandler):
}
# Do not leak profile/tenant topology on remote unauthenticated GETs.
# Agent Cards are intentionally public; health topology is not.
- if security.localhost_only() or security.authenticate(
+ if self.adapter._security_context.localhost_only() or self.adapter._security_context.authenticate(
self.headers.get("Authorization"),
self.client_address[0] if self.client_address else "",
) is not None:
@@ -268,7 +268,9 @@ class A2ARequestHandler(BaseHTTPRequestHandler):
# Identity comes from the presented credential (or the socket in
# localhost-only mode) — never from the request body.
- identity = security.authenticate(self.headers.get("Authorization"), client_ip)
+ identity = adapter._security_context.authenticate(
+ self.headers.get("Authorization"), client_ip
+ )
if identity is None:
self._json(401, protocol.jsonrpc_error(None, protocol.ERR_UNAUTHORIZED, "unauthorized"))
return
@@ -314,7 +316,7 @@ class A2ARequestHandler(BaseHTTPRequestHandler):
self._json(429, protocol.jsonrpc_error(req_id, protocol.ERR_RATE_LIMITED, "rate limit exceeded"))
return
- if not security.is_trusted_peer(identity):
+ if not adapter._security_context.is_trusted_peer(identity):
self._json(403, protocol.jsonrpc_error(
req_id, protocol.ERR_UNTRUSTED_PEER, f"peer '{identity}' not trusted"))
return
@@ -372,9 +374,10 @@ class A2AAdapter(BasePlatformAdapter):
# shape but is left unscoped here — see the "Scope note" in this
# fix's PR description: open PR #98937 is actively rewriting this
# field's None-vs-empty-list semantics.)
+ self._security_context = security.A2ASecurityContext.capture()
_port_env = None if _profile_scoped() else os.getenv("A2A_PORT")
self.port = int(_port_env or extra.get("port", _DEFAULT_PORT))
- self.host = security.resolve_bind_host()
+ self.host = self._security_context.resolve_bind_host()
self.agent_name = _default_agent_name()
self._advertised_toolsets = [
t.strip() for t in (
@@ -470,7 +473,11 @@ class A2AAdapter(BasePlatformAdapter):
self._mark_connected()
- exposure = "localhost-only" if security.localhost_only() else "REMOTE (bearer auth)"
+ exposure = (
+ "localhost-only"
+ if self._security_context.localhost_only()
+ else "REMOTE (bearer auth)"
+ )
logger.info(
"A2A: serving Agent Card + JSON-RPC on http://%s:%s (%s) as %r; %d routed agent(s)",
self.host, self.port, exposure, self.agent_name, len(self._agents),
@@ -649,7 +656,7 @@ class A2AAdapter(BasePlatformAdapter):
skills=self._advertised_skills(agent),
streaming=bool(agent.get("local", True)),
push_notifications=True,
- auth_required=not security.localhost_only(),
+ auth_required=not self._security_context.localhost_only(),
tenant=str(agent.get("tenant") or ""),
)
@@ -1228,7 +1235,10 @@ class A2AAdapter(BasePlatformAdapter):
if not callback_url:
return
- if not security.is_safe_callback_url(callback_url):
+ if not security.is_safe_callback_url(
+ callback_url,
+ localhost_mode=self._security_context.localhost_only(),
+ ):
logger.warning("A2A: push notification for task %s blocked — unsafe callback URL: %s",
task_id, callback_url)
protocol.metrics.push_failed += 1
@@ -1237,7 +1247,7 @@ class A2AAdapter(BasePlatformAdapter):
# Push payload uses the StreamResponse format (same as streaming).
payload = protocol.status_update(task_id, context_id, state, (reply or "")[:2000])
- signature = security.sign_push_payload(payload)
+ signature = self._security_context.sign_push_payload(payload)
headers = {"Content-Type": "application/json"}
if signature:
headers["X-A2A-Signature"] = signature
diff --git a/plugins/platforms/a2a/security.py b/plugins/platforms/a2a/security.py
index 753c202a548..350031b9dfd 100644
--- a/plugins/platforms/a2a/security.py
+++ b/plugins/platforms/a2a/security.py
@@ -31,29 +31,44 @@ import logging
import os
import re
import time
+from dataclasses import dataclass
from pathlib import Path
from typing import Optional
logger = logging.getLogger(__name__)
-# --------------------------------------------------------------------------
-# Bearer auth + peer identity
-# --------------------------------------------------------------------------
+def _profile_scoped() -> bool:
+ """True when running inside a multiplexed secondary profile's scope.
-def get_bearer_token() -> str:
- """Return the configured shared inbound bearer token (empty if none)."""
- return os.getenv("A2A_BEARER_TOKEN", "").strip()
+ Same discriminator as the Buzz/SimpleX/Raft adapters (#98738): secret
+ scope installed + multiplex active. The DEFAULT profile under
+ multiplexing (and every single-profile process) runs unscoped and keeps
+ its legacy ``os.environ`` precedence.
+ """
+ try:
+ from agent.secret_scope import current_secret_scope, is_multiplex_active
+ return bool(is_multiplex_active() and current_secret_scope() is not None)
+ except Exception:
+ return False
-def get_peer_tokens() -> dict[str, str]:
- """Parse A2A_PEER_TOKENS ("alice:tok1,bob:tok2") into {token: peer_name}.
- Per-peer tokens give each remote agent its own credential, so the identity
- used for rate limiting, trust, and audit is authenticated — not whatever
- the request body claims.
+def _startup_env(name: str) -> str:
+ """Read one A2A setting from the active profile's scope, else the env.
+
+ Inside a secondary profile's scope the scope is authoritative: a miss
+ yields "" and never falls through to ``os.environ`` (which holds the
+ default profile's tokens in a multiplexer).
"""
- raw = os.getenv("A2A_PEER_TOKENS", "").strip()
+ if _profile_scoped():
+ from agent.secret_scope import get_secret
+
+ return (get_secret(name) or "").strip()
+ return os.getenv(name, "").strip()
+
+
+def _parse_peer_tokens(raw: str) -> dict[str, str]:
out: dict[str, str] = {}
for pair in raw.split(","):
pair = pair.strip()
@@ -66,6 +81,115 @@ def get_peer_tokens() -> dict[str, str]:
return out
+def _configured_trusted_peers() -> frozenset[str]:
+ raw = _startup_env("A2A_TRUSTED_PEERS")
+ if raw:
+ return frozenset(p.strip() for p in raw.split(",") if p.strip())
+ try:
+ from hermes_cli.config import load_config
+
+ cfg = load_config() or {}
+ peers = (cfg.get("a2a") or {}).get("trusted_peers", [])
+ if isinstance(peers, list):
+ return frozenset(str(peer).strip() for peer in peers if str(peer).strip())
+ except Exception:
+ pass
+ return frozenset()
+
+
+@dataclass(frozen=True)
+class A2ASecurityContext:
+ """Immutable, profile-scoped security settings captured at adapter startup.
+
+ ``ThreadingHTTPServer`` handles requests on fresh threads that do not inherit
+ the gateway's profile ContextVars. Keeping the resolved settings on the
+ adapter prevents those threads from falling back to another profile's
+ process-global environment.
+ """
+
+ bearer_token: str
+ peer_tokens: tuple[tuple[str, str], ...]
+ trusted_peers: frozenset[str]
+ allow_all_users: bool
+ requested_host: str
+ push_secret: str
+
+ @classmethod
+ def capture(cls) -> "A2ASecurityContext":
+ bearer_token = _startup_env("A2A_BEARER_TOKEN")
+ return cls(
+ bearer_token=bearer_token,
+ peer_tokens=tuple(_parse_peer_tokens(_startup_env("A2A_PEER_TOKENS")).items()),
+ trusted_peers=_configured_trusted_peers(),
+ allow_all_users=_startup_env("A2A_ALLOW_ALL_USERS").lower()
+ in {"1", "true", "yes"},
+ requested_host=_startup_env("A2A_HOST") or "127.0.0.1",
+ push_secret=_startup_env("A2A_PUSH_SECRET") or bearer_token,
+ )
+
+ def localhost_only(self) -> bool:
+ return not (self.bearer_token or self.peer_tokens)
+
+ def resolve_bind_host(self) -> str:
+ loopback = {"127.0.0.1", "localhost", "::1"}
+ if self.requested_host in loopback:
+ return self.requested_host
+ if self.localhost_only():
+ logger.warning(
+ "A2A: A2A_HOST=%s ignored — no A2A_BEARER_TOKEN or "
+ "A2A_PEER_TOKENS set; binding to 127.0.0.1. Configure a token "
+ "to expose A2A remotely.",
+ self.requested_host,
+ )
+ return "127.0.0.1"
+ return self.requested_host
+
+ def authenticate(self, auth_header: Optional[str], client_ip: str = "") -> Optional[str]:
+ if self.localhost_only():
+ return f"ip:{client_ip or 'local'}"
+ presented = _parse_bearer(auth_header)
+ if presented is None:
+ return None
+ for token, name in self.peer_tokens:
+ if hmac.compare_digest(presented, token):
+ return name
+ if self.bearer_token and hmac.compare_digest(presented, self.bearer_token):
+ return f"ip:{client_ip or 'unknown'}"
+ return None
+
+ def is_trusted_peer(self, identity: str) -> bool:
+ if self.allow_all_users or self.localhost_only() or not self.trusted_peers:
+ return True
+ return identity in self.trusted_peers
+
+ def sign_push_payload(self, payload: dict) -> str:
+ if not self.push_secret:
+ return ""
+ body = json.dumps(payload, sort_keys=True, ensure_ascii=False).encode("utf-8")
+ return hmac.new(
+ self.push_secret.encode("utf-8"), body, hashlib.sha256
+ ).hexdigest()
+
+
+# --------------------------------------------------------------------------
+# Bearer auth + peer identity
+# --------------------------------------------------------------------------
+
+def get_bearer_token() -> str:
+ """Return the configured shared inbound bearer token (empty if none)."""
+ return _startup_env("A2A_BEARER_TOKEN")
+
+
+def get_peer_tokens() -> dict[str, str]:
+ """Parse A2A_PEER_TOKENS ("alice:tok1,bob:tok2") into {token: peer_name}.
+
+ Per-peer tokens give each remote agent its own credential, so the identity
+ used for rate limiting, trust, and audit is authenticated — not whatever
+ the request body claims.
+ """
+ return _parse_peer_tokens(_startup_env("A2A_PEER_TOKENS"))
+
+
def _parse_bearer(auth_header: Optional[str]) -> Optional[str]:
if not auth_header:
return None
@@ -85,24 +209,12 @@ def authenticate(auth_header: Optional[str], client_ip: str = "") -> Optional[st
Comparisons are constant-time (hmac.compare_digest).
"""
- peer_tokens = get_peer_tokens()
- shared = get_bearer_token()
- if not peer_tokens and not shared:
- return f"ip:{client_ip or 'local'}"
- presented = _parse_bearer(auth_header)
- if presented is None:
- return None
- for token, name in peer_tokens.items():
- if hmac.compare_digest(presented, token):
- return name
- if shared and hmac.compare_digest(presented, shared):
- return f"ip:{client_ip or 'unknown'}"
- return None
+ return A2ASecurityContext.capture().authenticate(auth_header, client_ip)
def localhost_only() -> bool:
"""True when we must refuse non-loopback binds (no token of any kind set)."""
- return not (get_bearer_token() or get_peer_tokens())
+ return A2ASecurityContext.capture().localhost_only()
def resolve_bind_host() -> str:
@@ -112,18 +224,7 @@ def resolve_bind_host() -> str:
per-peer) AND explicitly asked for a wider host. A token alone does not
widen the bind — opting into remote exposure must be deliberate.
"""
- requested = os.getenv("A2A_HOST", "").strip() or "127.0.0.1"
- loopback = {"127.0.0.1", "localhost", "::1"}
- if requested in loopback:
- return requested
- if localhost_only():
- logger.warning(
- "A2A: A2A_HOST=%s ignored — no A2A_BEARER_TOKEN or A2A_PEER_TOKENS "
- "set; binding to 127.0.0.1. Configure a token to expose A2A remotely.",
- requested,
- )
- return "127.0.0.1"
- return requested
+ return A2ASecurityContext.capture().resolve_bind_host()
# --------------------------------------------------------------------------
@@ -138,18 +239,7 @@ def get_trusted_peers() -> set[str]:
names from ``authenticate()`` — peer-token names, or ``ip:<addr>`` for
shared-token callers.
"""
- env_peers = os.getenv("A2A_TRUSTED_PEERS", "").strip()
- if env_peers:
- return {p.strip() for p in env_peers.split(",") if p.strip()}
- try:
- from hermes_cli.config import load_config
- cfg = load_config() or {}
- peers_list = (cfg.get("a2a") or {}).get("trusted_peers", [])
- if isinstance(peers_list, list):
- return {str(p).strip() for p in peers_list if p}
- except Exception:
- pass
- return set()
+ return set(_configured_trusted_peers())
def is_trusted_peer(identity: str) -> bool:
@@ -160,14 +250,7 @@ def is_trusted_peer(identity: str) -> bool:
otherwise any *authenticated* identity is allowed (authentication is the
primary gate — the allow-list is an optional restriction on top).
"""
- if os.getenv("A2A_ALLOW_ALL_USERS", "").strip().lower() in ("1", "true", "yes"):
- return True
- if localhost_only():
- return True
- trusted = get_trusted_peers()
- if not trusted:
- return True
- return identity in trusted
+ return A2ASecurityContext.capture().is_trusted_peer(identity)
# --------------------------------------------------------------------------
@@ -259,10 +342,7 @@ def get_push_secret() -> str:
Falls back to the bearer token if no dedicated push secret is set.
If neither is configured, push notifications are unsigned (localhost-only mode).
"""
- secret = os.getenv("A2A_PUSH_SECRET", "").strip()
- if secret:
- return secret
- return get_bearer_token()
+ return A2ASecurityContext.capture().push_secret
def sign_push_payload(payload: dict) -> str:
@@ -304,12 +384,14 @@ _BLOCKED_PREFIXES = (
)
-def is_safe_callback_url(url: str) -> bool:
+def is_safe_callback_url(url: str, *, localhost_mode: Optional[bool] = None) -> bool:
"""Check if a push notification callback URL is safe from SSRF.
Blocks internal/private/loopback/metadata addresses.
Only allows http:// and https:// schemes.
"""
+ if localhost_mode is None:
+ localhost_mode = localhost_only()
if not url or not isinstance(url, str):
return False
try:
@@ -324,16 +406,16 @@ def is_safe_callback_url(url: str) -> bool:
hostname_lower = hostname.lower()
if hostname_lower == "localhost":
# Loopback callbacks only make sense for local testing.
- return localhost_only()
+ return localhost_mode
for prefix in _BLOCKED_PREFIXES:
if hostname_lower.startswith(prefix.lower()):
- if localhost_only() and prefix in ("127.", "::1"):
+ if localhost_mode and prefix in ("127.", "::1"):
return True
return False
try:
ip = ipaddress.ip_address(hostname)
if ip.is_loopback or ip.is_link_local or ip.is_private or ip.is_reserved:
- if localhost_only() and ip.is_loopback:
+ if localhost_mode and ip.is_loopback:
return True
return False
except ValueError:
diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.py
index 7e4f933ebd7..e5d26688842 100644
--- a/tests/plugins/test_a2a_plugin.py
+++ b/tests/plugins/test_a2a_plugin.py
@@ -910,7 +910,9 @@ def _make_live_adapter(monkeypatch, reply_fn=None):
port = _free_port()
monkeypatch.setenv("A2A_PORT", str(port))
- adapter = A2AAdapter(PlatformConfig(enabled=True))
+ # A scoped secondary profile ignores the process env (#100382); pass the
+ # port through config.extra so both construction paths bind the same port.
+ adapter = A2AAdapter(PlatformConfig(enabled=True, extra={"port": port}))
async def fake_handle_message(event):
if reply_fn is None:
@@ -1223,6 +1225,54 @@ class TestInboundRoundTrip:
asyncio.run(run())
+ def test_multiplex_adapter_keeps_profile_scoped_peer_tokens(self, monkeypatch):
+ """A secondary listener must not authenticate with the default profile's tokens."""
+ from agent.secret_scope import (
+ reset_secret_scope,
+ set_multiplex_active,
+ set_secret_scope,
+ )
+
+ monkeypatch.setenv("A2A_PEER_TOKENS", "default:default-token")
+ monkeypatch.delenv("A2A_BEARER_TOKEN", raising=False)
+ monkeypatch.setenv("A2A_HOST", "127.0.0.1")
+
+ set_multiplex_active(True)
+ scope_token = set_secret_scope(
+ {"A2A_PEER_TOKENS": "secondary:secondary-token"}
+ )
+ try:
+ adapter, base = _make_live_adapter(monkeypatch)
+ finally:
+ reset_secret_scope(scope_token)
+
+ async def run():
+ try:
+ assert await adapter.connect() is True
+ response = await asyncio.to_thread(
+ _post_json,
+ base + "/",
+ _send_body("profile-scoped auth"),
+ {"Authorization": "Bearer secondary-token"},
+ )
+ assert response["result"]["status"]["state"] == "TASK_STATE_COMPLETED"
+
+ with pytest.raises(urllib.error.HTTPError) as exc_info:
+ await asyncio.to_thread(
+ _post_json,
+ base + "/",
+ _send_body("wrong profile"),
+ {"Authorization": "Bearer default-token"},
+ )
+ assert exc_info.value.code == 401
+ finally:
+ await adapter.disconnect()
+
+ try:
+ asyncio.run(run())
+ finally:
+ set_multiplex_active(False)
+
# --------------------------------------------------------------------------
# Push notifications end-to-end (inline config in message/send)