|
| 1 | +"""``--allowed-hosts`` wildcards are for domain names, never addresses (#5521). |
| 2 | +
|
| 3 | +``*.0.0.1`` used to be accepted and then matched, as a suffix, every IPv4 |
| 4 | +literal ending ``.0.0.1``: 10.0.0.1 and 127.0.0.1 alike. These tests pin both |
| 5 | +halves of the fix: such an entry is refused when the list is read, and an |
| 6 | +IP-literal host is never matched by a wildcard entry, even one that reached |
| 7 | +``host_is_allowed`` without going through ``normalize_allowed_host``. The |
| 8 | +last test builds the real server, which refuses to start on such a list. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import argparse |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from mcp_server_fetch.allowlist import ( |
| 18 | + host_is_allowed, |
| 19 | + normalize_allowed_host, |
| 20 | + normalize_allowed_hosts, |
| 21 | + parse_allowed_host, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + "entry", |
| 27 | + [ |
| 28 | + "*.10.0.0.1", |
| 29 | + "*.0.0.1", |
| 30 | + "*.1", |
| 31 | + "*.0.0.1.", |
| 32 | + "*.[::1]", |
| 33 | + "*.::1", |
| 34 | + "*.0x7f.1", |
| 35 | + "*.0x", |
| 36 | + "*.example.123", |
| 37 | + " *.0.0.1 ", |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_wildcard_address_entries_are_rejected(entry: str) -> None: |
| 41 | + with pytest.raises(ValueError): |
| 42 | + normalize_allowed_host(entry) |
| 43 | + |
| 44 | + |
| 45 | +def test_the_rejection_says_what_a_wildcard_takes() -> None: |
| 46 | + with pytest.raises(argparse.ArgumentTypeError, match="wildcard on an IP address"): |
| 47 | + parse_allowed_host("*.0.0.1") |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + ("entry", "normalized"), |
| 52 | + [ |
| 53 | + ("*.example.com", "*.example.com"), |
| 54 | + ("*.1.example.com", "*.1.example.com"), |
| 55 | + ("*.123abc", "*.123abc"), |
| 56 | + ("*.0xyz", "*.0xyz"), |
| 57 | + ("*.xn--p1ai", "*.xn--p1ai"), |
| 58 | + ("10.0.0.1", "10.0.0.1"), |
| 59 | + ("[::1]", "::1"), |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_domain_wildcards_and_exact_addresses_are_still_accepted( |
| 63 | + entry: str, normalized: str |
| 64 | +) -> None: |
| 65 | + assert normalize_allowed_host(entry) == normalized |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "host", ["127.0.0.1", "10.0.0.1", "10.0.0.1.", "0x7f.0.0.1", "8.0.0.1"] |
| 70 | +) |
| 71 | +def test_a_wildcard_entry_never_matches_an_address(host: str) -> None: |
| 72 | + # Entries given already-normalized, as a caller bypassing the parser could. |
| 73 | + assert host_is_allowed(host, ("*.0.0.1",)) is False |
| 74 | + |
| 75 | + |
| 76 | +def test_a_wildcard_entry_never_matches_an_ipv6_address() -> None: |
| 77 | + assert host_is_allowed("2001:db8::7", ("*.db8::7", "*.7")) is False |
| 78 | + |
| 79 | + |
| 80 | +def test_an_address_still_matches_its_exact_entry() -> None: |
| 81 | + entries = normalize_allowed_hosts(["*.example.com", "10.0.0.1", "[2001:db8::7]"]) |
| 82 | + assert host_is_allowed("10.0.0.1", entries) is True |
| 83 | + assert host_is_allowed("2001:DB8:0::7", entries) is True |
| 84 | + assert host_is_allowed("127.0.0.1", entries) is False |
| 85 | + assert host_is_allowed("a.example.com", entries) is True |
| 86 | + |
| 87 | + |
| 88 | +def test_create_server_rejects_a_wildcard_address_entry() -> None: |
| 89 | + from mcp_server_fetch.server import create_server |
| 90 | + |
| 91 | + with pytest.raises(ValueError, match="wildcard on an IP address"): |
| 92 | + create_server(allowed_hosts=["*.0.0.1"]) |
0 commit comments