From 5ff66f534ca310fc7c155a30ef9f9caaaefae57d Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Wed, 21 Oct 2026 05:44:40 -0500 Subject: [PATCH] fix(lab-ops-hub): judge NAT64/6to4/mapped crawl literals by embedded IPv4 (#472) --- pkgs/lab-ops-hub/container/app.py | 32 +++++++++- pkgs/lab-ops-hub/src/lib.ts | 64 +++++++++++++++---- pkgs/lab-ops-hub/test/lib.test.ts | 22 +++++++ .../test/test_container_validate.py | 21 ++++++ 4 files changed, 126 insertions(+), 13 deletions(-) diff --git a/pkgs/lab-ops-hub/container/app.py b/pkgs/lab-ops-hub/container/app.py index 71b77781fe..c5902568ff 100644 --- a/pkgs/lab-ops-hub/container/app.py +++ b/pkgs/lab-ops-hub/container/app.py @@ -48,11 +48,41 @@ def validate_crawl_url(raw): ip = _inet_aton_ipv4(host) if ip is None: return raw # a public DNS name; literal-IP policy does not apply - if not ip.is_global: + embedded = _embedded_ipv4(ip) + if embedded is not None: + # Translation/mapped prefixes are judged by the IPv4 they embed, + # not by ipaddress's wholesale verdict on the prefix (#472). + if not embedded.is_global: + raise ValueError( + f"refusing to crawl address {host} embedding non-public IPv4 {embedded}" + ) + elif not ip.is_global: raise ValueError(f"refusing to crawl non-public address {host}") return raw +_NAT64_WELL_KNOWN = ipaddress.IPv6Network("64:ff9b::/96") + + +def _embedded_ipv4(ip): + # Prefixes that embed an IPv4 address are judged by that address, the + # same policy as plain IPv4 (#472). ipaddress folds the IPv4-mapped + # case into is_global, but its verdicts on the translation prefixes + # are wholesale: the whole NAT64 well-known prefix 64:ff9b::/96 is + # "global" even when it embeds 127.0.0.1, and the whole 6to4 range + # 2002::/16 (IPv6Address.sixtofour) is "private" even when it embeds + # a public address. + if not isinstance(ip, ipaddress.IPv6Address): + return None + if ip.ipv4_mapped is not None: + return ip.ipv4_mapped + if ip.sixtofour is not None: + return ip.sixtofour + if ip in _NAT64_WELL_KNOWN: + return ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF) + return None + + def _inet_aton_ipv4(host): # ipaddress only accepts canonical dotted-quad, but the resolver the # fetcher uses (getaddrinfo, inet_aton semantics) also interprets the diff --git a/pkgs/lab-ops-hub/src/lib.ts b/pkgs/lab-ops-hub/src/lib.ts index d24a337c1e..f84047a7ad 100644 --- a/pkgs/lab-ops-hub/src/lib.ts +++ b/pkgs/lab-ops-hub/src/lib.ts @@ -41,21 +41,61 @@ function ipv4IsGlobal(n: number): boolean { return true; } +// Expand an IPv6 literal to its eight 16-bit groups, or null if it does +// not parse. A trailing dotted-quad counts as the last two groups. +function ipv6Groups(host: string): number[] | null { + let h = host; + const v4tail = h.match(/(\d+\.\d+\.\d+\.\d+)$/); + if (v4tail) { + const v4 = ipv4ToInt(v4tail[1]); + if (v4 === null) return null; + h = `${h.slice(0, -v4tail[1].length)}${Math.floor(v4 / 65536).toString(16)}:${v4 % 65536}`; + } + const halves = h.split("::"); + if (halves.length > 2) return null; + const parseSide = (s: string): number[] | null => { + if (s === "") return []; + const groups: number[] = []; + for (const part of s.split(":")) { + if (!/^[0-9a-f]{1,4}$/.test(part)) return null; + groups.push(parseInt(part, 16)); + } + return groups; + }; + const left = parseSide(halves[0]); + if (left === null) return null; + if (halves.length === 1) return left.length === 8 ? left : null; + const right = parseSide(halves[1]); + if (right === null) return null; + const missing = 8 - left.length - right.length; + if (missing < 1) return null; + return [...left, ...new Array(missing).fill(0), ...right]; +} + function ipv6IsGlobal(host: string): boolean { - const h = host.toLowerCase(); - // IPv4-mapped address: judge the embedded IPv4 address. - const mapped = h.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/); - if (mapped) { - const v4 = ipv4ToInt(mapped[1]); - return v4 !== null && ipv4IsGlobal(v4); + const g = ipv6Groups(host.toLowerCase()); + if (g === null) return false; + // Prefixes that embed an IPv4 address are judged by that address, the + // same policy as plain IPv4: IPv4-mapped ::ffff:0/96, the NAT64 + // well-known prefix 64:ff9b::/96, and 6to4 2002::/16 (#472). (WHATWG + // URL normalises the mapped form to hex groups, e.g. ::ffff:7f00:1, + // so this must work on groups, not the dotted spelling.) + const embedded = (hi: number, lo: number) => hi * 65536 + lo; + if (g[0] === 0 && g[1] === 0 && g[2] === 0 && g[3] === 0 && g[4] === 0 && g[5] === 0xffff) { + return ipv4IsGlobal(embedded(g[6], g[7])); + } + if (g[0] === 0x64 && g[1] === 0xff9b && g[2] === 0 && g[3] === 0 && g[4] === 0 && g[5] === 0) { + return ipv4IsGlobal(embedded(g[6], g[7])); } - if (h === "::" || h === "::1") return false; // unspecified, loopback - if (/^fe[89ab]/.test(h)) return false; // link-local fe80::/10 - if (/^f[cd]/.test(h)) return false; // unique-local fc00::/7 - if (/^ff/.test(h)) return false; // multicast - if (h.startsWith("2001:db8:")) return false; // documentation 2001:db8::/32 + if (g[0] === 0x2002) return ipv4IsGlobal(embedded(g[1], g[2])); + if (g.every((x) => x === 0)) return false; // unspecified :: + if (g[7] === 1 && g.slice(0, 7).every((x) => x === 0)) return false; // loopback ::1 + if (g[0] >= 0xfe80 && g[0] <= 0xfebf) return false; // link-local fe80::/10 + 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 // Global unicast lives in 2000::/3 (first hextet 2xxx or 3xxx). - return /^[23]/.test(h); + return g[0] >= 0x2000 && g[0] <= 0x3fff; } const BLOCKED_HOST_SUFFIXES = [".localhost", ".local", ".internal", ".lan", ".home.arpa"]; diff --git a/pkgs/lab-ops-hub/test/lib.test.ts b/pkgs/lab-ops-hub/test/lib.test.ts index bd98d7df71..22b51722ca 100644 --- a/pkgs/lab-ops-hub/test/lib.test.ts +++ b/pkgs/lab-ops-hub/test/lib.test.ts @@ -41,6 +41,28 @@ test("validateCrawlUrl refuses loopback / private / link-local targets (#441)", } }); +test("validateCrawlUrl judges NAT64 / 6to4 literals by their embedded IPv4 (#472)", () => { + const blocked = [ + "https://[64:ff9b::7f00:1]/", // NAT64 embedding 127.0.0.1 + "https://[64:ff9b::a9fe:a9fe]/", // NAT64 embedding 169.254.169.254 (cloud metadata) + "https://[64:ff9b::c0a8:101]/", // NAT64 embedding 192.168.1.1 + "https://[2002:7f00:1::]/", // 6to4 embedding 127.0.0.1 + "https://[2002:a9fe:a9fe::]/", // 6to4 embedding 169.254.169.254 + "https://[::ffff:7f00:1]/", // IPv4-mapped loopback, hex-group spelling + ]; + for (const u of blocked) { + assert.equal(validateCrawlUrl(u).ok, false, u); + } + const allowed = [ + "https://[64:ff9b::808:808]/", // NAT64 embedding 8.8.8.8 + "https://[2002:808:808::]/", // 6to4 embedding 8.8.8.8 + "https://[::ffff:8.8.8.8]/", // IPv4-mapped public + ]; + 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 0ee7317cf9..c4b1db98cf 100644 --- a/pkgs/lab-ops-hub/test/test_container_validate.py +++ b/pkgs/lab-ops-hub/test/test_container_validate.py @@ -64,6 +64,27 @@ def test_refuses_inet_aton_spellings_of_non_public_addresses(self): ): self.assertRefused(url) + def test_judges_translation_prefixes_by_embedded_ipv4(self): + # NAT64 (64:ff9b::/96) and 6to4 (2002::/16) literals embed an IPv4 + # address; ipaddress marks both prefixes global as a whole, so the + # embedded address must be judged explicitly (#472). Verdicts must + # match the TypeScript validator's suite case for case. + for url in ( + "https://[64:ff9b::7f00:1]/", # NAT64 embedding 127.0.0.1 + "https://[64:ff9b::a9fe:a9fe]/", # NAT64 embedding 169.254.169.254 + "https://[64:ff9b::c0a8:101]/", # NAT64 embedding 192.168.1.1 + "https://[2002:7f00:1::]/", # 6to4 embedding 127.0.0.1 + "https://[2002:a9fe:a9fe::]/", # 6to4 embedding 169.254.169.254 + "https://[::ffff:7f00:1]/", # IPv4-mapped loopback, hex spelling + ): + self.assertRefused(url) + for url in ( + "https://[64:ff9b::808:808]/", # NAT64 embedding 8.8.8.8 + "https://[2002:808:808::]/", # 6to4 embedding 8.8.8.8 + "https://[::ffff:8.8.8.8]/", # IPv4-mapped public + ): + self.assertAccepted(url) + def test_accepts_public_targets(self): for url in ( "https://example.com/page",