|
| 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