Skip to content

Commit 24ce09f

Browse files
cliffhallclaude
andcommitted
fix(fetch): harden address classification
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 69b53e9 commit 24ce09f

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/fetch/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ the request was user initiated (via a prompt). This can be disabled by adding th
176176
By default, the server refuses any request, including a redirect hop or a robots.txt fetch, whose host resolves to an
177177
address that is not globally routable: loopback (`127.0.0.0/8`, `::1`), private (`10.0.0.0/8`, `172.16.0.0/12`,
178178
`192.168.0.0/16`, `fc00::/7`), link-local (`169.254.0.0/16`, `fe80::/10`), shared address space (`100.64.0.0/10`),
179-
multicast, unspecified, documentation and the other IANA special-purpose ranges. A hostname is refused if any one of its addresses is in those ranges.
179+
multicast, unspecified, documentation and the other IANA special-purpose ranges. An IPv4-mapped (`::ffff:0:0/96`) or
180+
well-known NAT64 (`64:ff9b::/96`) address is judged by the IPv4 address it carries. A hostname is refused if any one of its addresses is in those ranges.
180181
The client receives an error naming the refused address.
181182

182183
To fetch from local or internal hosts (a development server, an intranet page), add the argument

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,25 @@ async def _resolve_host(host: str) -> list[str]:
110110
)
111111

112112

113+
# The well-known NAT64 prefix (RFC 6052). A NAT64 gateway forwards an address
114+
# in it to the IPv4 held in its low 32 bits, so that IPv4 is the destination.
115+
_NAT64_WELL_KNOWN = ipaddress.IPv6Network("64:ff9b::/96")
116+
117+
113118
def _is_public_address(address: str) -> bool:
114119
"""True when ``address`` is a globally routable unicast address.
115120
116121
Refused: loopback, RFC 1918, link-local (169.254.0.0/16, which holds most
117122
cloud metadata endpoints), shared address space (100.64.0.0/10, which holds
118123
Alibaba Cloud's 100.100.100.200), unique local IPv6 (AWS's fd00:ec2::254),
119124
multicast, unspecified, documentation and other special-purpose ranges.
125+
An IPv4-mapped or well-known NAT64 address is judged by the IPv4 it wraps.
120126
"""
121127
ip = ipaddress.ip_address(address)
122128
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
123129
ip = ip.ipv4_mapped
130+
elif isinstance(ip, ipaddress.IPv6Address) and ip in _NAT64_WELL_KNOWN:
131+
ip = ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF)
124132
if any(ip in network for network in _PUBLIC_EXCEPTIONS):
125133
return True
126134
if any(ip in network for network in _NON_PUBLIC_NETWORKS):

‎src/fetch/tests/test_protocol.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ async def test_redirect_to_private_address_is_refused(web: FakeWeb) -> None:
756756
"192.0.0.8",
757757
"64:ff9b:1::1",
758758
"2002:7f00:1::",
759+
# The well-known NAT64 prefix (64:ff9b::/96) wraps an IPv4 in its low
760+
# 32 bits, and a NAT64 gateway forwards to that IPv4.
761+
"64:ff9b::a9fe:a9fe",
762+
"64:ff9b::7f00:1",
763+
"64:ff9b::a00:1",
759764
# Inside 2001::/23 but outside its global exceptions (benchmarking).
760765
"2001:2::1",
761766
# Multicast, which is_global does not exclude.
@@ -797,6 +802,17 @@ async def test_global_exceptions_in_special_ranges_are_allowed(
797802
assert web.urls() == [url]
798803

799804

805+
async def test_nat64_address_wrapping_a_public_ipv4_is_allowed(web: FakeWeb) -> None:
806+
# Only the embedded IPv4 decides: 64:ff9b::808:808 is 8.8.8.8 through NAT64.
807+
web.dns["nat64.example.com"] = ["64:ff9b::808:808"]
808+
url = "https://nat64.example.com/"
809+
web.add(url, plain("ok"))
810+
async with connect(ignore_robots_txt=True) as (session, _):
811+
result = await call(session, {"url": url})
812+
assert wire(result)["isError"] is False
813+
assert web.urls() == [url]
814+
815+
800816
async def test_hostname_resolving_to_a_private_address_is_refused(
801817
web: FakeWeb,
802818
) -> None:

0 commit comments

Comments
 (0)