Skip to content

The agent that grows with you

fix(a2a): one IPv6 server class; dual-stack peers keep their IPv4 identity

Tekniumd2b3446b1eac
diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.py
index 0d295f9cfdb..205da76bee3 100644
--- a/plugins/platforms/a2a/adapter.py
+++ b/plugins/platforms/a2a/adapter.py
@@ -122,8 +122,16 @@ def _profile_home(profile: str) -> Optional[str]:
class _ThreadingHTTPServerV6(ThreadingHTTPServer):
+ """IPv6 listener. ``V6ONLY=0`` makes the ``::`` wildcard dual-stack (IPv4 peers arrive as
+ ``::ffff:a.b.c.d``), so one socket serves IPv6-only private networks and IPv4 alike."""
+
address_family = socket.AF_INET6
+ def server_bind(self):
+ with contextlib.suppress(OSError, AttributeError): # no IPV6_V6ONLY: the bind stays v6-only
+ self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
+ super().server_bind()
+
def _http_server_class(host: str) -> type:
"""``ThreadingHTTPServer`` is AF_INET only, so an IPv6 bind (``::1``, ``::``) needs the v6 family."""
@@ -142,32 +150,6 @@ def _safe_context_slug(value: str, max_len: int = 96) -> str:
return (slug or "ctx")[:max_len]
-class _DualStackThreadingHTTPServer(ThreadingHTTPServer):
- """Dual-stack (IPv4 + IPv6) ThreadingHTTPServer for Windows.
-
- The stdlib ``ThreadingHTTPServer`` is AF_INET-pinned (``address_family =
- AF_INET``), so it cannot bind an IPv6 host like ``::`` at all:
- ``getaddrinfo("::", port, AF_INET)`` fails with gaierror 11001. When
- ``A2A_HOST=::`` we want one socket that answers on both stacks
- (``AF_INET6`` + ``IPV6_V6ONLY=0``), matching the DM (aiohttp) dual-stack
- bind on 8642. On non-Windows platforms the same V6ONLY=0 socket gives the
- same dual-stack behavior.
- """
-
- address_family = socket.AF_INET6
-
- def server_bind(self):
- if self.address_family == socket.AF_INET6:
- with contextlib.suppress(
- OSError,
- AttributeError, # platform without IPV6_V6ONLY
- ):
- self.socket.setsockopt(
- socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0
- )
- super().server_bind()
-
-
class A2ARequestHandler(BaseHTTPRequestHandler):
"""HTTP handler for the A2A JSON-RPC surface; all state lives on ``self.server.adapter``."""
@@ -190,7 +172,9 @@ class A2ARequestHandler(BaseHTTPRequestHandler):
self._json(http_code, _err(req_id, code, message))
def _client_ip(self) -> str:
- return self.client_address[0] if self.client_address else ""
+ """The peer's address; an IPv4 peer of a dual-stack ``::`` bind keeps its plain IPv4 identity."""
+ ip = self.client_address[0] if self.client_address else ""
+ return ip.removeprefix("::ffff:") if ip.startswith("::ffff:") and "." in ip else ip
def _request_public_url(self) -> str:
"""A2A_PUBLIC_URL > X-Forwarded-Host / Host (scheme from X-Forwarded-Proto) > "" (bind host).
diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.py
index 8a964532210..c3983750aa3 100644
--- a/tests/plugins/test_a2a_plugin.py
+++ b/tests/plugins/test_a2a_plugin.py
@@ -126,58 +126,30 @@ class TestBindSafety:
assert security.A2ASecurityContext.capture().resolve_bind_host() == "localhost"
- def test_connect_dual_stack_bind_when_host_is_v6_wildcard(self, monkeypatch):
- """A2A_HOST=:: must answer on BOTH stacks, not just loopback IPv4.
-
- Regression: the upstream ``ThreadingHTTPServer`` is AF_INET-pinned,
- so a ``::`` bind fails at ``getaddrinfo("::", port, AF_INET)`` with
- gaierror 11001 (Windows) and the A2A platform stays down. ``::`` must
- select the
- AF_INET6 + ``IPV6_V6ONLY=0`` dual-stack server; other hosts keep the
- standard AF_INET family.
- """
- # Loopback-only: no external services. Skip on hosts with no IPv6
- # (cannot bind '::' at all) rather than falsely fail them.
- v6 = socket.socket(socket.AF_INET6)
+ def test_v6_wildcard_serves_both_stacks_and_keeps_ipv4_identity(self, monkeypatch):
+ """``A2A_HOST=::`` is one dual-stack socket: IPv6 and IPv4 peers both reach it, and an IPv4
+ peer keeps its plain ``ip:a.b.c.d`` identity (not ``ip:::ffff:a.b.c.d``), so an allow-list
+ written in IPv4 still admits it."""
try:
- v6.bind(("::", 0))
+ with socket.socket(socket.AF_INET6) as probe:
+ probe.bind(("::", 0))
except OSError:
pytest.skip("host cannot bind IPv6 '::'")
- finally:
- v6.close()
- import contextlib
-
- from plugins.platforms.a2a.adapter import (
- _DualStackThreadingHTTPServer,
- ThreadingHTTPServer,
- )
-
- # Family selection: :: -> dual-stack subclass; IPv4 host -> standard family.
- assert _DualStackThreadingHTTPServer.address_family == socket.AF_INET6
- assert ThreadingHTTPServer.address_family == socket.AF_INET
-
monkeypatch.setenv("A2A_BEARER_TOKEN", "topsecret")
+ monkeypatch.setenv("A2A_TRUSTED_PEERS", "ip:127.0.0.1")
monkeypatch.setenv("A2A_HOST", "::")
adapter, _base = _make_live_adapter(monkeypatch)
+ auth = {"Authorization": "Bearer topsecret"}
+ tasks_list = {"jsonrpc": "2.0", "id": "1", "method": "tasks/list", "params": {}}
async def run():
assert await adapter.connect() is True
try:
- # The live server socket is the dual-stack one...
- assert isinstance(adapter._httpd, _DualStackThreadingHTTPServer)
- assert adapter._httpd.address_family == socket.AF_INET6
- with contextlib.suppress(
- OSError,
- AttributeError, # platform without IPV6_V6ONLY
- ):
- assert adapter._httpd.socket.getsockopt(
- socket.IPPROTO_IPV6, socket.IPV6_V6ONLY
- ) == 0
- # ...so IPv6 loopback reaches the same socket via the bound port.
- _, port = adapter._httpd.server_address[:2]
- base = f"http://[::1]:{port}"
- card = await asyncio.to_thread(_get_json, base + "/.well-known/agent-card.json")
- assert "capabilities" in card
+ for host in ("[::1]", "127.0.0.1"):
+ card = await asyncio.to_thread(_get_json, f"http://{host}:{adapter.port}/.well-known/agent-card.json")
+ assert "capabilities" in card
+ resp = await asyncio.to_thread(_post_json, f"http://127.0.0.1:{adapter.port}/", tasks_list, auth)
+ assert "error" not in resp, resp
finally:
await adapter.disconnect()