Skip to content

fix(security): egress guards refuse cloud metadata in every numeric, NAT64 and provider form, classified by ipaddress and one maintained library #897

Closed

Description

@dan-petty

Problem

Lines cited at dcafadf.
At 132ecc5, ai/mcp/server.py:2895-2896 is at :2918-2919, and config/constants.py:2296-2299 and :2505 are at :2297-2300 and :2539; no other cited line moved.

Every "today" result below was measured offline at dcafadf, in a network namespace with no network (Python 3.14.7, glibc 2.43, httpx2 2.13.0, pydantic-ai 2.35.0).

The egress guards answer IP questions with string prefixes, literal host sets and hand canonicalisation. Their metadata check misses spellings and provider addresses that the client still dials. The NAT64 gap (found in #860's review) was first filed here. The Azure WireServer gap was filed as #989. This rewrite covers both, along with the rest of the same construct family.

Confirmed holes

  • Numeric metadata spellings pass the metadata check. validate_url(u, allow_private=True) allows http://2852039166/, http://0xa9fea9fe/, http://0xa9.0xfe.0xa9.0xfe/ and http://169。254。169。254/. glibc dials the first three as 169.254.169.254, and httpx2 reads the fourth as 169.254.169.254. The broker's own hook (http/broker.py:53-64, via validate_service_url) runs this check whenever private networks are allowed.
  • NAT64 and IPv4-compatible wrappers count as public. is_non_public_ip (core/validation.py:108-117) is not addr.is_global. Python 3.14 reports 64:ff9b::/96 (RFC 6052) and ::/96 (RFC 4291) as global, so validate_url_egress(u, purpose="web_fetch", allow_private=False) allows http://[64:ff9b::a9fe:a9fe]/, http://[64:ff9b::7f00:1]/ and http://[::127.0.0.1]/. The web_fetch per-hop veto (_validate_fetch_hop, ai/common_tools.py:340-354) therefore sends them. On a NAT64/DNS64 network, including one with a host CLAT, the first reaches the metadata service.
    • The stdlib already unwraps IPv4-mapped addresses (ip_address("::ffff:169.254.169.254").is_link_local is True).
    • It already treats 64:ff9b:1::/48, 6to4 and Teredo as non-global.
    • Under allow_private=True, however, http://[64:ff9b::a9fe:a9fe]/, http://[64:ff9b:1::a9fe:a9fe]/ and http://[2002:a9fe:a9fe::1]/ all pass the metadata check.
  • Azure WireServer (fix(security): ssrf guards admit azure's wireserver metadata address 168.63.129.16 #989). 168.63.129.16 is a public address that every Azure VM can reach, and it serves instance metadata and the guest agent. No list here contains it:
    • web_fetch's check allows it as public;
    • validate_configured_service_url allows it with or without allow_private;
    • a sandbox public_whitelist entry of 168.63.129.16 or 168.63.0.0/16 is emitted as an ipBlock;
    • a name whose DNS answer is 168.63.129.16 passes validate_url_egress and validate_url under both allow_private values. This item checks that answer where the guards already resolve the name, which is under allow_private=False. Under allow_private=True neither guard resolves the name, and the answer is fix(security): every HTTP client dials only addresses its egress level allows, checked at the connect and on every redirect #898's connect-time check.
  • validate_url_egress has no metadata check under allow_private=True. The whole check is the if not allow_private: block (validation.py:208-226). It allows http://169.254.169.254/, http://[fd00:ec2::254]/ and http://metadata.google.internal/. Callers: k8s/service_http.py:63, k8s/logql.py:352, ai/pool_load.py:45.
  • Missing provider entries: fd00:ec2::23 (AWS EKS Pod Identity), fd20:ce::254 (GCP, IPv6-only instances) and metadata.goog. GCP documents metadata.goog next to metadata.google.internal (docs.cloud.google.com/compute/docs/metadata/querying-metadata). All three pass validate_url(allow_private=True) today.

The hand-rolled constructs behind these holes

  • Prefix tests: startswith("169.254.") at validation.py:80, :86, :93 and :103. A fifth copy, telemetry/waterfall.py:114, goes with fix(security): every HTTP client dials only addresses its egress level allows, checked at the connect and on every redirect #898.
  • String sets:
    • _LOOPBACK_AND_LOCAL_HOSTS (validation.py:42-44), used at :157 and :216;
    • CONST_CLOUD_METADATA_HOSTS and CONST_CLOUD_METADATA_IPS (config/constants.py:2296-2299);
    • CONST_LOCAL_HOSTNAMES (constants.py:2505), read at ai/spend/pricing.py:91 and commands/k8s/networking.py:267, :296, :423 and :440-441;
    • loopback literals at networking.py:328, ai/mcp/server.py:2895-2896 and sandbox/metrics.py:398 (loopback hosts there skip the egress check at :410-411).
  • Hand canonicalisation: .strip().lower().strip("[]").rstrip(".") at validation.py:83, :154 and :330.
  • CIDRs built as text. cloudflare/client.py:164-174 builds f"{clean}/32" (:170). It keeps 2001:DB8::1 as typed and accepts a zone id (fe80::1%eth0/128).
  • Exception text as control flow: sandbox/models.py:182 and :225.
  • A wrong host-gateway mapping. sandbox/models.py:213-214 maps host.docker.internal (and localhost) to 127.0.0.1/32, and :129-132 accepts both names in the local whitelist.
    • The host gateway is not the pod's loopback.
    • A 127.0.0.1/32 ipBlock does nothing, because NetworkPolicy never polices pod loopback.
    • Resolving the name on the workstation is no fix either: in a rootless Podman container, /etc/hosts maps it to 169.254.1.2, a link-local address the metadata guard refuses.
  • RFC tables: security/reference_extractor.py:74-102, checked at :134-156 and :230-260.

Pitfalls the replacement must avoid (measured)

  • is_link_local alone is looser for networks. ip_network("169.254.0.0/15").is_link_local is False. Today only the string prefix refuses that network as a sandbox public_whitelist entry. A bare is_link_local swap would emit an ipBlock that covers the metadata service.
  • pydantic does not report the host httpx2 dials. For http://example.com\@169.254.169.254/, pydantic AnyHttpUrl.host is example.com, while httpx2.URL(...).host is 169.254.169.254, the address the request goes to. urllib agrees with httpx2, so today's guard refuses this URL; a pydantic-based guard would admit it.
  • is_global does not imply not is_reserved or not is_multicast. 64:ff9b::a00:1 (the NAT64 form of an RFC 1918 address) and 4000::1 are global and reserved, and 224.0.0.1 is global and multicast. is_public_ip gates Shodan InternetDB lookups (security/vulnerability_lookup.py:315, ai/tools/builtin_tools.py:460). Dropping those terms would send wrapped internal addresses to a third party.

Dropped from this issue's earlier proposal: the hand-written embedded_ipv4 helper and the constants-held CONST_IPV4_EMBEDDING_IPV6_PREFIXES set. pydantic-ai is already pinned, and its maintained classifier decodes the NAT64 (both prefixes), IPv4-compatible, 6to4, Teredo and ISATAP forms, so a local copy would be one more of the constructs this item removes. The earlier body cited AGENTS.md:21-24 as permission for that set; #1107 rewrites those lines.

Replacement: library per construct

  • Host text comes from the parser of the client that sends:
    • for URLs, httpx2.URL(url).host; httpx2 2.13.0 is a direct dependency (pyproject.toml:19);
    • for bare host strings (the MCP bind host, sandbox whitelist entries), the order of parsing is fixed:
      • first ipaddress.ip_address(h), so a single address keeps the address predicates;
      • then ipaddress.ip_network(h, strict=False); a string that parses is a network and is classified by key question 3;
      • only what is left goes through httpx2.URL(scheme="http", host=h).host, then ip_address, then getaddrinfo(AI_NUMERICHOST), then dns.name.
    • Why networks come before httpx2: sandbox entries are often CIDRs, and _validate_public_whitelist_item passes CIDR text such as 169.254.0.0/15 to the predicates today. Measured: httpx2.URL(scheme="http", host="169.254.0.0/15").host is 169.254.0.0%2F15, which is neither an ip_address nor an AI_NUMERICHOST literal. It would be compared as a name, the metadata predicate would answer False, and 168.63.0.0/16 would be refused by nothing.
    • Both httpx2 forms strip brackets, lowercase, and map ideographic dots. Neither is pydantic (see the pitfalls above).
  • Addresses: ipaddress.ip_address(host). If that fails, use socket.getaddrinfo(host, None, flags=socket.AI_NUMERICHOST). That call never queries DNS, and it returns the address glibc dials for 2852039166, 0xa9fea9fe, 0251.0376.0251.0376 and 127.1. A socket.gaierror means the host is a name.
  • Names:
    • localhost and the metadata names are compared with dns.name.from_text() equality, which handles case and the trailing dot.
    • Host text that dns.name.from_text() rejects with a dns.exception.DNSException is not a DNS name. httpx2 accepts some of it; measured: a..b raises EmptyLabel, and a 64-character label raises LabelTooLong. The loopback predicate answers False for such text (no loopback exemption), and validate_url and validate_url_egress refuse it as an invalid host, so it never reaches a name comparison. Both URL checks allow it today under allow_private=True (measured).
    • Declare dnspython==2.8.0 directly. It is already locked via email-validator, it is ISC-licensed, and its repository is active (commits on 2026-10-03).
    • The metadata names (metadata, metadata.google.internal, metadata.goog) are published by the cloud vendor, and no maintained library lists them. They stay one constant of dns.name.Name objects, with the source cited.
  • Classification uses the stdlib is_loopback, is_link_local, is_global, is_reserved, is_multicast and is_unspecified, plus the classifier from key question 1:
    • Metadata means is_link_local, or the classifier's metadata test, or a metadata name.
    • Non-public means not is_global, or the classifier's private test. This combination only ever tightens today's result.
    • Networks follow key question 3.
  • validate_url_egress and validate_url:
    • Both parse with httpx2.
    • Both run the metadata predicate on the host before the allow_private branch.
    • Every DNS answer they already resolve is checked against both predicates, so no new lookup is added.
  • One loopback predicate, today's _is_loopback_host (validation.py:328-336) rebuilt on the above, replaces:
    • the MCP bind set (server.py:2895-2896);
    • networking.py:267, :296, :328 and :440-441;
    • pricing.py:91;
    • sandbox/metrics.py:398-411.
  • Cloudflare: ipaddress.ip_network(clean, strict=False).with_prefixlen, refusing any address with a scope_id. Every run already sends the full policy payload: create_or_update_access_policy PUTs when a policy of that name exists (client.py:731-738) and POSTs a new one otherwise (:739-744). Canonical text therefore adds no extra step.
  • sandbox/models.py: parse inside the try and classify after it, so no branch reads str(exc).
  • reference_extractor: key question 4.

Delivery: two PRs.

Acceptance criteria

  • Old-vs-new table test (offline, parametrised, DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK unset; "today" measured at dcafadf):
Input Call Today After
http://2852039166/, http://0xa9fea9fe/, http://0xa9.0xfe.0xa9.0xfe/, http://169。254。169。254/ validate_url(allow_private=True) allowed refused (metadata)
http://[64:ff9b::a9fe:a9fe]/, http://[64:ff9b:1::a9fe:a9fe]/, http://[2002:a9fe:a9fe::1]/ validate_url(allow_private=True) allowed refused (metadata)
http://168.63.129.16/, http://[fd20:ce::254]/, http://[fd00:ec2::23]/, http://metadata.goog./ validate_url(allow_private=True) and validate_configured_service_url(allow_private=True) allowed refused (metadata)
http://168.63.129.16/ validate_configured_service_url() (default allow_private=False) allowed refused (metadata)
http://example.com\@169.254.169.254/ validate_url(allow_private=True) refused refused
http://a..b/, and a URL whose first label is 64 characters ("x" * 64 + ".example.com"; the subdomain length is the point of this row) validate_url(allow_private=True) and validate_url_egress(allow_private=True) allowed refused (invalid host)
http://169.254.169.254/, http://[fd00:ec2::254]/, http://metadata.google.internal/ validate_url_egress(allow_private=True) allowed refused
http://[64:ff9b::a9fe:a9fe]/, http://[64:ff9b::7f00:1]/, http://[::127.0.0.1]/, http://168.63.129.16/ validate_url_egress(purpose="web_fetch", allow_private=False) allowed refused
http://example.com/ with getaddrinfo patched to answer 168.63.129.16 validate_url_egress and validate_url, allow_private=False allowed refused
http://[64:ff9b::808:808]/, http://8.8.8.8/ validate_url_egress(allow_private=False) allowed allowed
host 127.0.0.2, ::ffff:127.0.0.1, LOCALHOST. run_mcp_server(transport="sse") refused as non-loopback binds
existing http://192.0.2.10:3000, detected http://127.0.0.2:3000 _should_update_url overwrites keeps the existing URL
169.254.0.0/15 sandbox public_whitelist refused refused (via overlaps)
168.63.129.16, 168.63.0.0/16 sandbox public_whitelist ipBlock emitted refused
0.0.0.0/0, ::/0, 128.0.0.0/1, 160.0.0.0/4 sandbox public_whitelist ipBlock emitted refused (overlaps link-local)
8.0.0.0/5 sandbox public_whitelist ipBlock emitted ipBlock emitted (stays with #702)
2001:DB8::1 / fe80::1%eth0/128 Cloudflare bypass IPs 2001:DB8::1/128 / accepted 2001:db8::1/128 / refused
  • Classifier rows. test_is_non_public_ip (tests/test_validation.py:27-38) keeps its tuple-equality form.
    • These become non-public; each is public today: 64:ff9b::a9fe:a9fe, 64:ff9b::7f00:1, ::127.0.0.1, 64:ff9b::a9fe:0/112, 64:ff9b::/96 and 64:ff9b::/95.
    • These stay public: 64:ff9b::808:808, 64:ff9b::808:0/112 and ::ffff:8.8.8.8.
    • These stay non-public: 64:ff9b:1::808:808, 2002:a9fe:a9fe::, 2001:0:a9fe:a9fe::, :: and ::1.
  • Metadata predicate. test_is_cloud_metadata_host_superset (tests/test_validation.py:412-448):
    • gains 64:ff9b::a9fe:a9fe (as a string and as ipaddress.ip_address), 2852039166, 168.63.129.16, fd20:ce::254 and metadata.goog, all True;
    • gains 64:ff9b::808:808, False;
    • keeps its bracketed and trailing-dot rows True.
  • web_fetch never sends these hops. test_web_fetch_tool_never_requests_a_private_redirect_hop (tests/test_common_tools.py:95-114) gains a nat64-metadata hop (http://[64:ff9b::a9fe:a9fe]/latest/meta-data/) and a wireserver hop (http://168.63.129.16/machine?comp=goalstate). For each, only https://example.com/start is requested and the result has no # Final.
  • The broker keeps its own veto. test_http_broker_keeps_its_own_veto_under_a_request_egress_policy (tests/test_http_broker.py:171-187) is parametrised over _LINK_LOCAL and the NAT64 metadata hop, under HttpClientBroker(allow_private_networks=True) and DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK=true. client.send raises SSRFBlockedError, and stub_web.requested == [_START].
  • Sandbox oracle. _METADATA_ORACLE_TARGETS (tests/test_sandbox_policy_properties.py:254-266) gains 2852039166, 0xa9fea9fe, 64:ff9b::a9fe:a9fe, 168.63.129.16, fd20:ce::254 and metadata.goog. :269-315 passes for each target, and test_property_resolve_hosts_emit_correct_prefix_lengths (:180) keeps passing.
  • Contract test for the classifier. One test drives the classifier rows above through the single import site of key question 1, and asserts that the link-local networks read there are exactly 169.254.0.0/16 and fe80::/10. If a pydantic-ai bump moves or changes the classifier, the test fails by name.
  • Test DNS guard. The guard (tests/conftest.py:125-133) passes getaddrinfo calls that carry AI_NUMERICHOST, whether the flags come positionally or by keyword. It still refuses every other non-literal lookup, and a test covers both cases.
  • Valkey guard. A first test for _should_update_valkey shows that, with valkey.url unset, an existing remote valkey.host of example.com is kept when the detected URL's host is 127.0.0.2; today it is overwritten (measured). With valkey.url set to redis://example.com:6379, _should_update_url already returns False today and the entry is kept, so that case is not the regression row. The split(":")[0] host read (networking.py:436) and the writer at :486-490 are left to fix(valkey): one validated URL names every Valkey endpoint, valkey-py dials an address that one check vetted, and the unused cache layer goes #1113.
  • No construct left:
  • Tests updated:
    • tests/test_validation.py: :83-84, :234-260 and :290-302;
    • tests/test_cloudflare_commands.py:638-653;
    • tests/test_mcp.py:138-143;
    • tests/test_k8s_networking.py (the _should_update_url rows);
    • PR2: tests/test_sandbox_networking.py:174-197 and tests/test_reference_extractor.py:1019-1031 and :1093.
  • Offline. Every address above is a literal or a patched getaddrinfo answer. Redirect tests answer from the stub_web fixture (tests/conftest.py:190), and no test makes an unpatched non-numeric lookup.
  • Release.
    • uv run devops ci passes within the 5-minute budget.
    • A changelog.d/897.md fragment names:
      • the newly refused metadata spellings and provider addresses;
      • sandbox public_whitelist CIDRs that overlap link-local space (0.0.0.0/0, ::/0, 128.0.0.0/1, 160.0.0.0/4) or hold a metadata address (168.63.0.0/16), which are now refused instead of emitted as ipBlocks;
      • host text that is not a valid DNS name (a..b) refused as an invalid host;
      • the MCP bind accepting any loopback form;
      • Cloudflare's canonical CIDR output.
    • CHANGELOG.md and docs/ROADMAP.md are not edited.
  • Pending a person: ask pydantic/pydantic-ai upstream to export is_cloud_metadata_ip, is_private_ip and the metadata address sets from a public module.

Key questions

  • Where does the metadata and IPv6-wrapper classification come from?
    • Decided (owner, 2026-10-03): fixes use reputable libraries and tools, never new hand-rolled parsers, prefix tests, regex validators or literal address sets. That rules out decoding the IPv6 wrappers here and keeping a local list of provider addresses.
    • Proposed: pydantic-ai's pydantic_ai._ssrf.is_cloud_metadata_ip and is_private_ip, imported at one site in core/validation.py.
    • Why this classifier:
      • pydantic-ai is a direct, exact-pinned dependency (pyproject.toml:37, 2.35.0), maintained by the Pydantic team, and both functions are on upstream main.
      • Measured offline: is_cloud_metadata_ip flags 64:ff9b::a9fe:a9fe, 64:ff9b:1::a9fe:a9fe, 2002:a9fe:a9fe::1, ::a9fe:a9fe, the Teredo form 2001::5601:5601, fd20:ce::254 and 168.63.129.16. is_private_ip flags 64:ff9b::7f00:1 and ::127.0.0.1. Both keep 8.8.8.8 and 64:ff9b::808:808 public.
      • Its metadata list (169.254.169.254, 169.254.170.2, 169.254.170.23, 168.63.129.16, 100.100.100.200, 192.0.0.192, 169.254.42.42, fd00:ec2::254, fd00:ec2::23, fd20:ce::254, fd00:42::42) settles fix(security): ssrf guards admit azure's wireserver metadata address 168.63.129.16 #989's comparison criterion without a local list.
    • Networks: key question 3 reads the same module's _CLOUD_METADATA_IPV4, _CLOUD_METADATA_IPV6 and _PRIVATE_NETWORKS, from the same import site.
    • Cost: the module is private (__all__ = ['safe_download']). Mitigations:
      • the exact pin, so a pydantic-ai bump is a deliberate change that runs the contract test;
      • one import site;
      • the contract test;
      • the upstream export request (Pending a person, above).
    • If a bump moves or changes the classifier: the contract test fails by name, and the pin stays until the upstream export is available. Hand-written decoding or a local address list is not the fallback, under the decided rule above.
  • Which parser supplies the host? Proposed: httpx2.URL, the client that sends, never pydantic (the backslash case above). A bare host string is tried as an ipaddress address, then as an ipaddress network; only what is neither goes through httpx2.URL(scheme="http", host=h) (see "Host text" above).
  • How are networks classified? Proposed: a network is metadata if it overlaps a link-local network or contains a metadata address. A network is non-public if not net.is_global, or if either end address is non-public under the address predicate.
    • Why a network rule of its own: the classifier functions take one address.
    • The link-local networks are read, not written: they are the members of pydantic-ai's _PRIVATE_NETWORKS whose is_link_local is True, taken at key question 1's import site. Measured on 2.35.0: 169.254.0.0/16 and fe80::/10, the RFC 3927 and RFC 4291 ranges. The stdlib holds the same two ranges only in its private _constants.
    • The end rule satisfies the 64:ff9b::a9fe:0/112, 64:ff9b::/95 and 64:ff9b::808:0/112 rows.
    • The overlap rule changes four sandbox public_whitelist results. 0.0.0.0/0, ::/0, 128.0.0.0/1 and 160.0.0.0/4 each overlap link-local space, so they become refused; all four are emitted as ipBlocks today (measured). 0.0.0.0/0, ::/0 and 128.0.0.0/1 also fail the end rule (0.0.0.0, 255.255.255.255 and :: are non-public), and 160.0.0.0/4 is refused by the overlap alone.
    • A range whose ends are both public, that does not overlap link-local space and that holds no metadata address, but that contains private space, stays feat(telemetry): sandbox egress port scoping, collector lane & in-container trace context #702's. Example: 8.0.0.0/5, which holds an RFC 1918 block and is still emitted after this item.
  • What about reference_extractor's classification shift? (owner decision; PR2) Proposed:
    • Accept the stdlib classes, keeping not is_reserved and not is_multicast in is_public_ip.
    • Keep one closed constant of ip_network objects for the documentation and benchmarking ranges (RFC 5737, RFC 3849, RFC 9637, RFC 2544, RFC 5180), with the RFCs cited. The stdlib folds these ranges into is_private, and no library tells a documentation address from an RFC 1918 one. This is the one address constant the item would keep, so it is weighed against the rule decided in key question 1.
    • What shifts: without the tables, 0.0.0.0/8, 100.64/10, 192.0.0/24, 198.18/15, 240/4, 255.255.255.255, 100::/64, 2002::/16 and parts of 2001::/23 change report class. The RFC list in the message at ai/tools/builtin_tools.py:456 changes with them.
  • What happens to host.docker.internal and localhost in the local whitelist? (owner decision; PR2) Proposed:
    • Refuse host.docker.internal with a message asking for an IP or CIDR.
    • localhost emits no ipBlock, because pod loopback is never policed.
    • No DNS lookup is added to the policy builder.
  • Does fix(security): ssrf guards admit azure's wireserver metadata address 168.63.129.16 #989 close? Yes: it is closed as merged into this item (consolidation, 2026-10-03). Its cases are rows of the table above, and its comparison criterion is met by key question 1's classifier.

Strategic alignment

Readiness check (2026-10-04)

Checked at the release/v0.2.26 tip (b61e5a0):

  • pyproject.toml still pins httpx2 2.13.0 (:19) and pydantic-ai 2.35.0 (:37), and dnspython 2.8.0 is still locked.
  • pydantic_ai/_ssrf.py still has is_cloud_metadata_ip (:254), is_private_ip (:271), _PRIVATE_NETWORKS (:35) and _CLOUD_METADATA_IPV4/_IPV6 (:105/:117), with __all__ = ['safe_download'].
  • The "No construct left" searches print the same sites as before.

Moved lines

Rule alignment: one issue = one PR. The two-PR delivery above conflicts with this rule; the readiness check requires each PR body to close exactly one issue. This issue is delivered by the former PR1 only. The former PR2 scope moves to a separate issue, which the owner files and which lands after this one. That scope is:

  • the reference_extractor reclassification;
  • the host.docker.internal/localhost local-whitelist change;
  • key questions 4 and 5;
  • the PR2 test rows in tests/test_sandbox_networking.py and tests/test_reference_extractor.py.

Dependencies.

Open owner decisions. The Status stays unchanged until these are answered:

Amended criterion

Owner decisions (2026-10-04)

The owner accepted the recommendations for the open decisions in the readiness check above.

  1. Split approved. fix(security): egress guards refuse cloud metadata in every numeric, NAT64 and provider form, classified by ipaddress and one maintained library #897 is delivered by the former PR1 only. The former PR2 scope (the reference_extractor reclassification, the host.docker.internal/localhost local-whitelist change, key questions 4 and 5, and the PR2 test rows) moved to fix(security): reference_extractor classifies addresses with ipaddress, and the sandbox local whitelist stops mapping host.docker.internal to pod loopback #1174, which lands after this item. The PR2 parts above are superseded by fix(security): reference_extractor classifies addresses with ipaddress, and the sandbox local whitelist stops mapping host.docker.internal to pod loopback #1174.
  2. Key question 1 accepted. Import pydantic-ai's private _ssrf helpers (is_cloud_metadata_ip, is_private_ip, _PRIVATE_NETWORKS) at one site in core/validation.py, with the exact pydantic-ai pin, the contract test and the upstream export request.
  3. The order after feat(ci): hand-rolled URL and address parsing fails devops ci, AGENTS.md makes the library the rule, and reviews stop discarding the findings #1107 is soft. This item does not wait for feat(ci): hand-rolled URL and address parsing fails devops ci, AGENTS.md makes the library the rule, and reviews stop discarding the findings #1107; the marker criterion stays conditional ("Once feat(ci): hand-rolled URL and address parsing fails devops ci, AGENTS.md makes the library the rule, and reviews stop discarding the findings #1107 has landed").

Activity

  1. added
    type/security
    scope/security
    priority/p1-high
    on Oct 1, 2026
  2. modified the milestones: , v0.2.27 on Oct 1, 2026
  3. added a commit that references this issue on Oct 1, 2026
  4. changed the title [-]fix(security): egress checks classify NAT64 and IPv4-compatible IPv6 by their embedded IPv4 address[/-] [+]fix(security): egress guards refuse cloud metadata in every numeric, NAT64 and provider form, classified by ipaddress and one maintained library[/+] on Oct 3, 2026
  5. dan-petty-bot commented on Oct 5, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1239, merged into release/v0.2.27 as 13808c0.

    Summary

    Closes #897

    Refactors egress guards to refuse cloud metadata in every numeric, NAT64, and provider form, classified by ipaddress and maintained network safety classifiers rather than brittle ad-hoc string matching.

    • Adds dnspython==2.8.0 direct dependency for deterministic DNS name syntax and relative label parsing.
    • Refactors is_cloud_metadata_host to leverage pydantic_ai._ssrf network classifiers, dynamically derived link-local networks (169.254.0.0/16, fe80::/10), numeric host parsing via httpx2.URL and socket.AI_NUMERICHOST, and dns.name.from_text syntax parsing (failing closed on DNSException).
    • Refactors is_loopback_host to deterministically recognize loopback IP addresses (IPv4, IPv6, IPv4-mapped ::ffff:127.0.0.1), numeric forms, and localhost without DNS queries.
    • Refactors is_non_public_ip to inspect global reachability across single addresses and networks.
    • Hardens validate_url and validate_url_egress to check cloud metadata ahead of private network allowances, enforce syntax validation, and refuse metadata in numeric, NAT64, or DNS form even when allow_private=True.
    • Refactors _normalize_ip_cidr in cloudflare/client.py to reject scoped IP addresses (fe80::1%eth0/128) and normalize case.
    • Removes forbidden ad-hoc prefix/subset matching across src/ (CONST_CLOUD_METADATA_HOSTS, CONST_CLOUD_METADATA_IPS, CONST_LOCAL_HOSTNAMES, _LOOPBACK_AND_LOCAL_HOSTS, _ALLOWED_LOCAL_HOSTS, startswith("169.254."), strip("[]")).
    • Provides comprehensive unit and in…

    21 file(s) changed.

    How it was verified

    Check runs at cbfd776:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Dependency Freshness: skipping
    • Static Analysis: pass
    • Analyze (python): pass
    • Tests & Coverage: pass
    • Analyze (actions): pass
    • DevContainer Image: skipping
    • Service Image: skipping

    Acceptance Criteria of docs/agent/tasks/task-897-egress-guards-cloud-metadata.md:

    • Egress guards refuse IPv4 link-local (169.254.0.0/16), IPv6 link-local (fe80::/10), NAT64 metadata (64:ff9b::169.254.169.254), 6to4, Teredo, dword/hex/octal representations, fullwidth dot encodings, and provider metadata hostnames.
    • All classification uses ipaddress and pydantic_ai._ssrf without ad-hoc string prefixing or partial keyword subsets.
    • is_loopback_host recognizes localhost, 127.0.0.1, ::1, 127.0.0.2, ::ffff:127.0.0.1, LOCALHOST. without DNS resolution.
    • _normalize_ip_cidr rejects scoped IP addresses (fe80::1%eth0/128) and normalizes case.
    • Fast in-gate checks pass and changelog.d/897.md is provided.
    • Pending a person: uv run devops ci passes on this branch.
  6. added a commit that references this issue on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/securitytype/security

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions