|
5 | 5 | caller-supplied URL server-side, so the URL must be restricted to public |
6 | 6 | http(s) targets before any connection is opened. |
7 | 7 |
|
| 8 | +Also covers GHSA-cx77-j6h8-3382: the first fix classified addresses with a |
| 9 | +category denylist, which let 100.64.0.0/10 (RFC 6598 shared address space, used |
| 10 | +for GKE pod/service CIDRs and ISP CGNAT) through because it is neither private |
| 11 | +nor reserved. The guard now requires `is_global`. |
| 12 | +
|
8 | 13 | These tests use literal IPs for the block/allow matrix so they do not depend on |
9 | 14 | network DNS; only the "localhost" case exercises real name resolution (which is |
10 | 15 | universally available). |
11 | 16 | """ |
12 | 17 |
|
| 18 | +import ipaddress |
13 | 19 | import json |
14 | 20 | from unittest.mock import AsyncMock, patch |
15 | 21 |
|
|
18 | 24 |
|
19 | 25 | from meta_ads_mcp.core.utils import ( |
20 | 26 | BlockedURLError, |
| 27 | + _ip_is_disallowed, |
21 | 28 | validate_public_url, |
22 | 29 | download_image, |
23 | 30 | try_multiple_download_methods, |
|
41 | 48 | "http://[fe80::1]/", # IPv6 link-local |
42 | 49 | "http://[fc00::1]/", # IPv6 unique-local (private) |
43 | 50 | "https://169.254.169.254/", # https also blocked |
| 51 | + # GHSA-cx77-j6h8-3382 — non-public, but neither private nor reserved, so a |
| 52 | + # category denylist let these through. |
| 53 | + "http://100.64.1.1/internal", # RFC 6598 shared address space |
| 54 | + "http://100.127.255.254/", # top of the shared range |
| 55 | + "http://[::ffff:100.64.1.1]/", # IPv4-mapped shared address |
| 56 | + "http://192.0.0.1/", # IETF protocol assignments |
| 57 | + "http://198.18.0.5/", # benchmarking range |
| 58 | + "http://192.0.2.9/", # TEST-NET-1 |
| 59 | + "http://198.51.100.9/", # TEST-NET-2 |
| 60 | + "http://203.0.113.9/", # TEST-NET-3 |
| 61 | + "http://240.0.0.1/", # reserved (class E) |
| 62 | + "http://[2001:db8::1]/", # IPv6 documentation range |
44 | 63 | ] |
45 | 64 |
|
46 | 65 |
|
@@ -93,6 +112,43 @@ def test_validate_public_url_allows_public_ip_literals(url): |
93 | 112 | validate_public_url(url) |
94 | 113 |
|
95 | 114 |
|
| 115 | +# --- GHSA-cx77-j6h8-3382: shared address space ------------------------------- |
| 116 | + |
| 117 | +@pytest.mark.parametrize( |
| 118 | + "address", |
| 119 | + ["100.64.0.0", "100.64.1.1", "100.127.255.254", "100.127.255.255"], |
| 120 | +) |
| 121 | +def test_shared_address_space_is_not_public(address): |
| 122 | + """100.64.0.0/10 is is_global == False but is_private == False, which is |
| 123 | + exactly the gap the category denylist had.""" |
| 124 | + ip = ipaddress.ip_address(address) |
| 125 | + |
| 126 | + assert not ip.is_private, "test would pass for the wrong reason" |
| 127 | + assert _ip_is_disallowed(ip) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("address", ["100.63.255.255", "100.128.0.0", "8.8.8.8"]) |
| 131 | +def test_addresses_outside_the_shared_range_stay_reachable(address): |
| 132 | + """The bounds matter: 100.64.0.0/10 is not all of 100.0.0.0/8.""" |
| 133 | + assert not _ip_is_disallowed(ipaddress.ip_address(address)) |
| 134 | + |
| 135 | + |
| 136 | +async def test_redirect_into_shared_address_space_is_blocked(): |
| 137 | + def handler(request: httpx.Request) -> httpx.Response: |
| 138 | + if request.url.host == "8.8.8.8": |
| 139 | + return httpx.Response(302, headers={"Location": "http://100.64.1.1/internal"}) |
| 140 | + return httpx.Response(200, content=b"SHOULD-NOT-BE-FETCHED") |
| 141 | + |
| 142 | + transport = httpx.MockTransport(handler) |
| 143 | + async with httpx.AsyncClient( |
| 144 | + transport=transport, |
| 145 | + follow_redirects=True, |
| 146 | + event_hooks={"request": [_ssrf_guard_request_hook]}, |
| 147 | + ) as client: |
| 148 | + with pytest.raises(BlockedURLError): |
| 149 | + await client.get("https://8.8.8.8/start") |
| 150 | + |
| 151 | + |
96 | 152 | # --- request event hook (covers redirect hops) ------------------------------ |
97 | 153 |
|
98 | 154 | async def test_ssrf_guard_request_hook_blocks_internal(): |
|
0 commit comments