Skip to content

Commit c1f6f8e

Browse files
cliffhallclaude
andcommitted
fix(fetch): list special-purpose ranges explicitly, not only via is_global (modelcontextprotocol#4838)
ipaddress.is_global's tables were corrected only in recent Python releases, so on older 3.10-3.12 patch releases ranges such as 192.0.0.0/24 and 64:ff9b:1::/48 read as global and passed the guard. The IANA special-purpose ranges are now listed explicitly, so the answer is the same on every supported Python. Multicast and the two ranges are added to the tests. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent fd5958f commit c1f6f8e

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎src/mcp_server_fetch/server.py‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,53 @@ async def _resolve_host(host: str) -> list[str]:
3838
return [str(info[4][0]) for info in infos]
3939

4040

41+
# IANA special-purpose ranges that are not public destinations. ``is_global``
42+
# covers most of them, but its tables were corrected only in recent releases
43+
# (3.13, and patch releases of 3.11 and 3.12: before them 192.0.0.0/24 and
44+
# 64:ff9b:1::/48, for example, read as global), so the ranges are listed here
45+
# too, to give the same answer on every supported Python.
46+
_NON_PUBLIC_NETWORKS = tuple(
47+
ipaddress.ip_network(network)
48+
for network in (
49+
"0.0.0.0/8",
50+
"10.0.0.0/8",
51+
"100.64.0.0/10",
52+
"127.0.0.0/8",
53+
"169.254.0.0/16",
54+
"172.16.0.0/12",
55+
"192.0.0.0/24",
56+
"192.0.2.0/24",
57+
"192.88.99.0/24",
58+
"192.168.0.0/16",
59+
"198.18.0.0/15",
60+
"198.51.100.0/24",
61+
"203.0.113.0/24",
62+
"240.0.0.0/4",
63+
"::/128",
64+
"::1/128",
65+
"64:ff9b:1::/48",
66+
"100::/64",
67+
"2001::/23",
68+
"2001:db8::/32",
69+
"fc00::/7",
70+
"fe80::/10",
71+
)
72+
)
73+
74+
4175
def _is_public_address(address: str) -> bool:
4276
"""True when ``address`` is a globally routable unicast address.
4377
44-
``is_global`` is False for loopback, RFC 1918, link-local (169.254.0.0/16,
45-
which holds most cloud metadata endpoints), shared address space
46-
(100.64.0.0/10, which holds Alibaba Cloud's 100.100.100.200), unique local
47-
IPv6 (AWS's fd00:ec2::254), unspecified and reserved ranges.
78+
Refused: loopback, RFC 1918, link-local (169.254.0.0/16, which holds most
79+
cloud metadata endpoints), shared address space (100.64.0.0/10, which holds
80+
Alibaba Cloud's 100.100.100.200), unique local IPv6 (AWS's fd00:ec2::254),
81+
multicast, unspecified, documentation and other special-purpose ranges.
4882
"""
4983
ip = ipaddress.ip_address(address)
5084
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
5185
ip = ip.ipv4_mapped
86+
if any(ip in network for network in _NON_PUBLIC_NETWORKS):
87+
return False
5288
return ip.is_global and not ip.is_multicast
5389

5490

‎tests/test_protocol.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@ async def test_redirect_to_private_address_is_refused(web: FakeWeb) -> None:
660660
"fe80::1",
661661
"fd00:ec2::254",
662662
"::ffff:127.0.0.1",
663+
# Read as global by is_global on older Python patch releases.
664+
"192.0.0.8",
665+
"64:ff9b:1::1",
666+
# Multicast, which is_global does not exclude.
667+
"224.0.0.251",
663668
],
664669
)
665670
async def test_private_address_is_refused_before_any_request(

0 commit comments

Comments
 (0)