Skip to content

Commit b9cc4d8

Browse files
cliffhallclaude
andcommitted
fix(fetch): refuse IPv4-compatible IPv6 addresses (::/96)
Python's is_global reads ::127.0.0.1 and other IPv4-compatible addresses as global, so the private-address guard let them through. RFC 4291 deprecated the prefix and nothing routes it, so the guard now refuses all of ::/96 (which also holds :: and ::1) rather than decoding it. The well-known NAT64 half of modelcontextprotocol#5181 was fixed by modelcontextprotocol#5238; the new tests pin both. Closes modelcontextprotocol#5181 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 336b2fd commit b9cc4d8

3 files changed

Lines changed: 119 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ By default, the server refuses any request, including a redirect hop or a robots
178178
address that is not globally routable: loopback (`127.0.0.0/8`, `::1`), private (`10.0.0.0/8`, `172.16.0.0/12`,
179179
`192.168.0.0/16`, `fc00::/7`), link-local (`169.254.0.0/16`, `fe80::/10`), shared address space (`100.64.0.0/10`),
180180
multicast, unspecified, documentation and the other IANA special-purpose ranges. An IPv4-mapped (`::ffff:0:0/96`) or
181-
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.
181+
well-known NAT64 (`64:ff9b::/96`) address is judged by the IPv4 address it carries; a deprecated IPv4-compatible
182+
address (`::/96`, such as `::127.0.0.1`) is refused whatever IPv4 it carries. A hostname is refused if any one of its addresses is in those ranges.
182183
The client receives an error naming the refused address.
183184

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

‎src/mcp_server_fetch/server.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ async def _resolve_host(host: str) -> list[str]:
8080
"198.51.100.0/24",
8181
"203.0.113.0/24",
8282
"240.0.0.0/4",
83-
"::/128",
84-
"::1/128",
83+
# IPv4-compatible IPv6 (RFC 4291 2.5.5.1), deprecated and never routed;
84+
# it also holds the unspecified (::) and loopback (::1) addresses.
85+
"::/96",
8586
"64:ff9b:1::/48",
8687
"100::/64",
8788
"2001::/23",
@@ -122,7 +123,8 @@ def _is_public_address(address: str) -> bool:
122123
cloud metadata endpoints), shared address space (100.64.0.0/10, which holds
123124
Alibaba Cloud's 100.100.100.200), unique local IPv6 (AWS's fd00:ec2::254),
124125
multicast, unspecified, documentation and other special-purpose ranges.
125-
An IPv4-mapped or well-known NAT64 address is judged by the IPv4 it wraps.
126+
An IPv4-mapped or well-known NAT64 address is judged by the IPv4 it wraps;
127+
a deprecated IPv4-compatible address (``::/96``) is refused whatever it holds.
126128
"""
127129
ip = ipaddress.ip_address(address)
128130
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:

‎tests/test_translated_addresses.py‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""The private-address guard on IPv6 forms that carry an IPv4 address (#5181).
2+
3+
Two /96 prefixes embed an IPv4 in their low 32 bits, and Python's
4+
``is_global`` reads both as global whatever that IPv4 is:
5+
6+
- the well-known NAT64 prefix ``64:ff9b::/96`` (RFC 6052), which a NAT64
7+
gateway forwards to the embedded IPv4, so the guard judges it by that IPv4
8+
(fixed by #5238; pinned here so #5181 is covered end to end);
9+
- the IPv4-compatible prefix ``::/96`` (RFC 4291 2.5.5.1), deprecated and
10+
never routed, so the guard refuses it outright, even around a public IPv4.
11+
12+
Each case is driven through the real server over the MCP protocol (see
13+
``harness.connect``): once as an IP literal in the URL and once as a hostname
14+
that resolves to the address, so both the literal and the DNS path are covered.
15+
"""
16+
17+
from __future__ import annotations
18+
19+
from typing import Any
20+
21+
import httpx
22+
import pytest
23+
from mcp.types import CallToolResult
24+
25+
from .harness import FakeWeb, connect
26+
27+
TOOL_ERROR = "Error executing tool fetch: "
28+
29+
# IPv4-compatible (::/96) addresses: loopback, link-local metadata, RFC 1918,
30+
# and a public IPv4, which is refused too because ::/96 is never routed.
31+
IPV4_COMPATIBLE = ["::7f00:1", "::a9fe:a9fe", "::a00:1", "::808:808"]
32+
33+
# Well-known NAT64 (64:ff9b::/96) addresses wrapping non-public IPv4s.
34+
NAT64_NON_PUBLIC = ["64:ff9b::7f00:1", "64:ff9b::a9fe:a9fe", "64:ff9b::a00:1"]
35+
36+
37+
def wire(result: CallToolResult) -> dict[str, Any]:
38+
return result.model_dump(
39+
by_alias=True, mode="json", exclude_none=True, exclude_unset=True
40+
)
41+
42+
43+
def text_of(result: CallToolResult) -> str:
44+
data = wire(result)
45+
assert len(data["content"]) == 1
46+
return data["content"][0]["text"]
47+
48+
49+
@pytest.mark.parametrize("address", IPV4_COMPATIBLE + NAT64_NON_PUBLIC)
50+
async def test_ip_literal_is_refused_before_any_request(
51+
web: FakeWeb, address: str
52+
) -> None:
53+
url = f"http://[{address}]/admin"
54+
async with connect(ignore_robots_txt=True) as (session, _):
55+
result = await session.call_tool("fetch", {"url": url})
56+
assert wire(result)["isError"] is True
57+
assert text_of(result) == (
58+
f"{TOOL_ERROR}Refused to fetch {url}: {address} resolves to {address}, "
59+
"which is not a public address. Start the server with "
60+
"--allow-private-ips to allow private, loopback and link-local addresses."
61+
)
62+
assert web.requests == []
63+
64+
65+
@pytest.mark.parametrize("address", IPV4_COMPATIBLE + NAT64_NON_PUBLIC)
66+
async def test_hostname_resolving_to_the_address_is_refused(
67+
web: FakeWeb, address: str
68+
) -> None:
69+
web.dns["internal.example.com"] = [address]
70+
url = "https://internal.example.com/"
71+
async with connect(ignore_robots_txt=True) as (session, _):
72+
result = await session.call_tool("fetch", {"url": url})
73+
assert text_of(result) == (
74+
f"{TOOL_ERROR}Refused to fetch {url}: internal.example.com resolves to "
75+
f"{address}, which is not a public address. Start the server with "
76+
"--allow-private-ips to allow private, loopback and link-local addresses."
77+
)
78+
assert web.requests == []
79+
80+
81+
async def test_nat64_address_wrapping_a_public_ipv4_is_allowed(
82+
web: FakeWeb,
83+
) -> None:
84+
# The contrast with ::808:808 above: through NAT64, 8.8.8.8 is reachable.
85+
web.dns["nat64.example.com"] = ["64:ff9b::808:808"]
86+
url = "https://nat64.example.com/"
87+
web.add(
88+
url,
89+
httpx.Response(200, content=b"ok", headers={"content-type": "text/plain"}),
90+
)
91+
async with connect(ignore_robots_txt=True) as (session, _):
92+
result = await session.call_tool("fetch", {"url": url})
93+
assert wire(result)["isError"] is False
94+
assert web.urls() == [url]
95+
96+
97+
@pytest.mark.parametrize("address", IPV4_COMPATIBLE)
98+
async def test_allow_private_ips_lets_ipv4_compatible_addresses_through(
99+
web: FakeWeb, address: str
100+
) -> None:
101+
# The opt-out still covers the newly refused range.
102+
url = f"http://[{address}]/"
103+
web.add(
104+
url,
105+
httpx.Response(200, content=b"ok", headers={"content-type": "text/plain"}),
106+
)
107+
async with connect(ignore_robots_txt=True, allow_private_ips=True) as (
108+
session,
109+
_,
110+
):
111+
result = await session.call_tool("fetch", {"url": url})
112+
assert wire(result)["isError"] is False

0 commit comments

Comments
 (0)