diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index c5902568ff..36938002b8 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -56,11 +56,23 @@ def validate_crawl_url(raw): raise ValueError( f"refusing to crawl address {host} embedding non-public IPv4 {embedded}" ) + elif isinstance(ip, ipaddress.IPv6Address) and ip not in _GLOBAL_UNICAST_V6: + # ipaddress.is_global is True for any IPv6 address outside its + # narrow private list — multicast (ff00::/8), site-local + # (fec0::/10), SRv6 SIDs (5f00::/16) and the reserved space + # outside global unicast all pass it (#474). Global unicast + # lives in 2000::/3; require that first, as the Worker's + # validateCrawlUrl does, then let is_global judge within it + # (it knows the 2001::/23 special-purpose block). + raise ValueError(f"refusing to crawl non-public address {host}") elif not ip.is_global: raise ValueError(f"refusing to crawl non-public address {host}") return raw +_GLOBAL_UNICAST_V6 = ipaddress.IPv6Network("2000::/3") + + _NAT64_WELL_KNOWN = ipaddress.IPv6Network("64:ff9b::/96") diff --git a/pkgs/lab-ops-hub/src/lib.ts b/pkgs/lab-ops-hub/src/lib.ts index f84047a7ad..09b751733e 100644 --- a/pkgs/lab-ops-hub/src/lib.ts +++ b/pkgs/lab-ops-hub/src/lib.ts @@ -94,6 +94,27 @@ function ipv6IsGlobal(host: string): boolean { if (g[0] >= 0xfc00 && g[0] <= 0xfdff) return false; // unique-local fc00::/7 if (g[0] >= 0xff00) return false; // multicast ff00::/8 if (g[0] === 0x2001 && g[1] === 0x0db8) return false; // documentation 2001:db8::/32 + // The 2001::/23 IANA special-purpose block is non-global except six + // entries (#474, mirroring the Python validator's ipaddress verdicts): + // 2001:1::1 and 2001:1::2 (PCP anycast), 2001:3::/32 (AMT), + // 2001:4:112::/48 (AS112), 2001:20::/28 and 2001:30::/28 (ORCHIDv2). + if (g[0] === 0x2001 && (g[1] & 0xfe00) === 0) { + if (g[1] === 0x0003) return true; + if (g[1] === 0x0004 && g[2] === 0x0112) return true; + if (g[1] >= 0x0020 && g[1] <= 0x003f) return true; + if ( + g[1] === 0x0001 && + g[2] === 0 && + g[3] === 0 && + g[4] === 0 && + g[5] === 0 && + g[6] === 0 && + (g[7] === 1 || g[7] === 2) + ) { + return true; + } + return false; + } // Global unicast lives in 2000::/3 (first hextet 2xxx or 3xxx). return g[0] >= 0x2000 && g[0] <= 0x3fff; } diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index 22b51722ca..08c11a92a3 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -63,6 +63,41 @@ test("validateCrawlUrl judges NAT64 / 6to4 literals by their embedded IPv4 (#472 } }); +test("validateCrawlUrl agrees with the container validator on special IPv6 ranges (#474)", () => { + // Verdicts must match the Python validator's suite case for case: + // multicast/reserved/site-local/SRv6 space is not global unicast, and + // the 2001::/23 IANA special-purpose block is non-global except its + // six global entries. + const blocked = [ + "https://[ff00::1]/", // multicast + "https://[4000::1]/", // reserved, outside 2000::/3 + "https://[fec0::1]/", // site-local (deprecated) + "https://[5f00::1]/", // SRv6 SIDs + "https://[100:1::1]/", // outside the 100::/64 discard prefix + "https://[2001::1]/", // Teredo 2001::/32 + "https://[2001:2::1]/", // benchmarking 2001:2::/48 + "https://[2001:10::1]/", // ORCHID 2001:10::/28 + "https://[2001:40::1]/", // 2001::/23 special-purpose + "https://[2001:50::1]/", // 2001::/23 special-purpose + "https://[2001:1::3]/", // 2001:1::/48 is global only at ::1 and ::2 + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + const allowed = [ + "https://[2001:1::1]/", // PCP anycast + "https://[2001:1::2]/", // PCP anycast + "https://[2001:3::1]/", // AMT 2001:3::/32 + "https://[2001:4:112::1]/", // AS112 2001:4:112::/48 + "https://[2001:20::1]/", // ORCHIDv2 2001:20::/28 + "https://[2001:30::1]/", // ORCHIDv2 2001:30::/28 + "https://[2001:4860:4860::8888]/", // ordinary global unicast + ]; + for (const u of allowed) { + assert.equal(validateCrawlUrl(u).ok, true, u); + } +}); + test("validateCrawlUrl refuses trailing-dot spellings of blocked hosts (#466)", () => { const blocked = ["http://localhost./", "http://foo.localhost./", "http://printer.local./", "http://nas.internal./"]; for (const u of blocked) { diff --git a/pkgs/lab-ops-hub/test/test_container_validate.py b/pkgs/lab-ops-hub/test/test_container_validate.py index c4b1db98cf..ecd874f099 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -85,6 +85,36 @@ def test_judges_translation_prefixes_by_embedded_ipv4(self): ): self.assertAccepted(url) + def test_agrees_with_worker_validator_on_special_ipv6_ranges(self): + # Verdicts must match the TypeScript validator's suite case for + # case (#474): ipaddress.is_global alone passes multicast, + # site-local, SRv6 and reserved IPv6 space, and the 2001::/23 + # IANA special-purpose block is non-global except six entries. + for url in ( + "https://[ff00::1]/", # multicast + "https://[4000::1]/", # reserved, outside 2000::/3 + "https://[fec0::1]/", # site-local (deprecated) + "https://[5f00::1]/", # SRv6 SIDs + "https://[100:1::1]/", # outside the 100::/64 discard prefix + "https://[2001::1]/", # Teredo 2001::/32 + "https://[2001:2::1]/", # benchmarking 2001:2::/48 + "https://[2001:10::1]/", # ORCHID 2001:10::/28 + "https://[2001:40::1]/", # 2001::/23 special-purpose + "https://[2001:50::1]/", # 2001::/23 special-purpose + "https://[2001:1::3]/", # 2001:1::/48 global only at ::1/::2 + ): + self.assertRefused(url) + for url in ( + "https://[2001:1::1]/", # PCP anycast + "https://[2001:1::2]/", # PCP anycast + "https://[2001:3::1]/", # AMT 2001:3::/32 + "https://[2001:4:112::1]/", # AS112 2001:4:112::/48 + "https://[2001:20::1]/", # ORCHIDv2 2001:20::/28 + "https://[2001:30::1]/", # ORCHIDv2 2001:30::/28 + "https://[2001:4860:4860::8888]/", # ordinary global unicast + ): + self.assertAccepted(url) + def test_accepts_public_targets(self): for url in ( "https://example.com/page",