diff --git a/backend/docs/CONFIGURATION.md b/backend/docs/CONFIGURATION.md index 89245d7682..752ea985c9 100644 --- a/backend/docs/CONFIGURATION.md +++ b/backend/docs/CONFIGURATION.md @@ -1998,8 +1998,15 @@ performed local side effects; the agent must retry it explicitly. Non-interactiv runs auto-deny without opening a card or waiting for input. The sidecar rejects hostnames that policy does not allow before DNS resolution; allowed hostnames that resolve to loopback, private, link-local, multicast, IPv6 ULA/site-local, -or cloud metadata destinations are rejected and can never be approved. Raw -TCP/UDP, Git-over-SSH, and other non-HTTP protocols remain unavailable in +or cloud metadata destinations are rejected and can never be approved. This +screening also unwraps IPv6 spellings that carry an IPv4 destination — +IPv4-mapped, 6to4, NAT64 (`64:ff9b::/96`), IPv4-translated (`::ffff:0:0:0/96`), +and the deprecated IPv4-compatible form — and judges the embedded IPv4 +address, while NAT64 spellings of public IPv4 targets stay reachable. +The RFC 8215 local-use translation prefix (`64:ff9b:1::/48`) is rejected +explicitly. Operator-defined NAT64 prefixes outside `64:ff9b::/96` are not +recognized. +Raw TCP/UDP, Git-over-SSH, and other non-HTTP protocols remain unavailable in restricted modes. Restricted modes currently require the local Docker backend and Docker Engine diff --git a/backend/packages/harness/deerflow/community/aio_sandbox/network_proxy.py b/backend/packages/harness/deerflow/community/aio_sandbox/network_proxy.py index 4c2972a806..99612773d4 100644 --- a/backend/packages/harness/deerflow/community/aio_sandbox/network_proxy.py +++ b/backend/packages/harness/deerflow/community/aio_sandbox/network_proxy.py @@ -89,11 +89,44 @@ def domain_matches(host: str, rule: str) -> bool: return host == rule +def _embedded_ipv4(address: ipaddress.IPv6Address) -> ipaddress.IPv4Address | None: + """Return the IPv4 address an IPv6 spelling carries, if any. + + Mirrors the embedded-IPv4 unwrapping in + ``deerflow.community.url_safety._is_public_url_host``, plus the + IPv4-translated prefix that url_safety rejects via is_reserved. The proxy + module is copied into a sidecar as a single file, so it cannot import + that module (which pulls pydantic). + """ + embedded = address.ipv4_mapped or address.sixtofour + if embedded is None and address in ipaddress.ip_network("64:ff9b::/96"): + embedded = ipaddress.IPv4Address(int(address) & 0xFFFFFFFF) + if embedded is None and address in ipaddress.ip_network("::ffff:0:0:0/96"): + embedded = ipaddress.IPv4Address(int(address) & 0xFFFFFFFF) + if embedded is None and 1 < int(address) < 1 << 32: + embedded = ipaddress.IPv4Address(int(address) & 0xFFFFFFFF) + return embedded + + def address_is_public(address: str, *, allow_synthetic_dns: bool = False) -> bool: try: parsed = ipaddress.ip_address(address.split("%", 1)[0]) except ValueError: return False + if isinstance(parsed, ipaddress.IPv6Address): + # The RFC 8215 local-use translation prefix must never egress. CPython + # classifies it as non-global only from 3.12.4 (gh-113171), and the + # sidecar image can pin an older interpreter, so reject it explicitly + # instead of relying on the interpreter's classification. + if parsed in ipaddress.ip_network("64:ff9b:1::/48"): + return False + embedded = _embedded_ipv4(parsed) + # NAT64 (64:ff9b::/96) and IPv4-compatible (::a.b.c.d) spellings report + # is_global, so the flags below cannot catch a loopback, private or + # metadata address carried inside them. Screen the IPv4 the spelling + # actually reaches. + if embedded is not None and not address_is_public(str(embedded), allow_synthetic_dns=allow_synthetic_dns): + return False if parsed.is_global and not parsed.is_multicast and not getattr(parsed, "is_site_local", False): return True # Docker Desktop's DNS inhibition layer maps public names into the diff --git a/backend/tests/test_aio_sandbox_network_proxy.py b/backend/tests/test_aio_sandbox_network_proxy.py index f87299ee9c..cdb87cfa00 100644 --- a/backend/tests/test_aio_sandbox_network_proxy.py +++ b/backend/tests/test_aio_sandbox_network_proxy.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import ipaddress import json import socket import ssl @@ -38,6 +39,56 @@ def test_address_is_public_rejects_host_private_link_local_and_metadata() -> Non assert network_proxy.address_is_public("198.18.1.5", allow_synthetic_dns=True) +def test_address_is_public_rejects_non_public_ipv4_inside_ipv6_spellings() -> None: + # NAT64 (64:ff9b::/96) and IPv4-compatible (::a.b.c.d) spellings report + # is_global even when the embedded IPv4 is loopback, private or metadata. + for address in ( + "64:ff9b::a9fe:a9fe", # 169.254.169.254 + "64:ff9b::a9fe:a9fe%eth0", # the same spelling with a zone id + "64:ff9b::7f00:1", # 127.0.0.1 + "64:ff9b::a00:1", # 10.0.0.1 + "64:ff9b::c0a8:102", # 192.168.1.2 + "64:ff9b::6464:64c8", # 100.100.100.200, CGNAT cloud metadata + "::169.254.169.254", + "::127.0.0.1", + "::ffff:169.254.169.254", + "::ffff:0:a9fe:a9fe", # IPv4-translated (SIIT) metadata spelling + "2002:a9fe:a9fe::1", + "::2", # IPv4-compatible this-network block, not a unicast target + # RFC 8215 local-use translation prefix: rejected explicitly, without + # relying on the interpreter classification that changed in 3.12.4. + "64:ff9b:1::1", + "64:ff9b:1::a9fe:a9fe", + "64:ff9b:1:42::a9fe:a9fe", + "64:ff9b:1::808:808", # even a public IPv4 spelling must not egress + ): + assert not network_proxy.address_is_public(address) + # 6to4 carrying a public IPv4 target follows the interpreter's outer + # classification: CPython marks 2002::/16 private from 3.12.4 (gh-113171), + # so it is rejected there and allowed on earlier 3.12 patch releases. + sixtofour_public = "2002:808:808::1" + assert network_proxy.address_is_public(sixtofour_public) == ipaddress.ip_address(sixtofour_public).is_global + # A NAT64 spelling of a public IPv4 target stays allowed; the prefix + # reports is_reserved, so a blanket reserved check would wrongly reject it. + assert network_proxy.address_is_public("64:ff9b::808:808") # 8.8.8.8 + assert network_proxy.address_is_public("64:ff9b::101:101") # 1.1.1.1 + assert network_proxy.address_is_public("::ffff:8.8.8.8") + assert network_proxy.address_is_public("::ffff:0:808:808") + assert network_proxy.address_is_public("2606:4700:4700::1111") + # The Docker Desktop synthetic-DNS exception passes through the embedded + # check: 64:ff9b::c612:105 carries 198.18.1.5. + assert not network_proxy.address_is_public("64:ff9b::c612:105") + assert network_proxy.address_is_public("64:ff9b::c612:105", allow_synthetic_dns=True) + + +def test_local_use_nat64_prefix_rejected_independent_of_interpreter_flags(monkeypatch) -> None: + # Simulate a pre-3.12.4 interpreter (gh-113171) that reports the RFC 8215 + # local-use range as global; the explicit rejection must still hold. + monkeypatch.setattr(ipaddress.IPv6Address, "is_global", property(lambda self: True)) + for address in ("64:ff9b:1::1", "64:ff9b:1::a9fe:a9fe", "64:ff9b:1::808:808"): + assert not network_proxy.address_is_public(address) + + def test_policy_denial_and_temporary_or_sandbox_grants(tmp_path, monkeypatch) -> None: monkeypatch.setattr(network_proxy, "POLICY_DB", tmp_path / "policy.sqlite3") monkeypatch.setenv("DEERFLOW_NETWORK_MODE", "allowlist") @@ -141,6 +192,19 @@ async def test_resolve_public_fails_closed_when_dns_contains_private_answer(monk assert await network_proxy.resolve_public("example.com", 443) is None +@pytest.mark.anyio +async def test_resolve_public_fails_closed_on_nat64_metadata_answer(monkeypatch) -> None: + loop = asyncio.get_running_loop() + + async def fake_getaddrinfo(*_args, **_kwargs): + return [ + (socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("64:ff9b::a9fe:a9fe", 443, 0, 0)), + ] + + monkeypatch.setattr(loop, "getaddrinfo", fake_getaddrinfo) + assert await network_proxy.resolve_public("example.com", 443) is None + + @pytest.mark.anyio async def test_resolve_public_returns_every_validated_answer_and_open_retries(monkeypatch) -> None: loop = asyncio.get_running_loop()