Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit b8d96d6587d21cb085bbf6f9c0cfd4ae42d9d4cc
parent bb57002e2628d60cce5a7edf32e5e0b4a27e7d3d
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 11 Nov 2026 23:39:01 -0500

archives: an IPv6 literal carrying a private IPv4 is a private host -- IPv4-mapped in the hex spelling the URL parser writes, IPv4-compatible and NAT64; site-local fec0::/10 too (demo epic D1 re-review, must-fix 2)

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mcommon/lib/archiveHosts-server.test.ts | 44++++++++++++++++++++++++++++++++++++++++++++
Mcommon/lib/archiveHosts-server.ts | 51++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/common/lib/archiveHosts-server.test.ts b/common/lib/archiveHosts-server.test.ts @@ -11,6 +11,50 @@ test("private addresses: loopback, private, link-local, CGNAT, unspecified, IPv6 for (const a of ["93.184.216.34", "172.32.0.1", "2606:4700::1", "8.8.8.8"]) assert.equal(isPrivateAddress(a), false, a); }); +test("an IPv6 address carrying a private IPv4 is private, in the spelling the URL parser writes (re-review, must-fix 2)", () => { + // What `new URL("http://[::ffff:127.0.0.1]")` makes of its host. + assert.equal(new URL("http://[::ffff:127.0.0.1]/").hostname, "[::ffff:7f00:1]"); + for (const a of [ + "[::ffff:7f00:1]", // 127.0.0.1 + "[::ffff:a00:1]", // 10.0.0.1 + "[::ffff:a9fe:a9fe]", // 169.254.169.254 + "::ffff:c0a8:1", // 192.168.0.1 + "0:0:0:0:0:ffff:7f00:1", + "::ffff:10.0.0.1", + "::7f00:1", // IPv4-compatible 127.0.0.1 + "64:ff9b::7f00:1", // NAT64 of 127.0.0.1 + "64:ff9b::10.0.0.1", + "64:ff9b::a9fe:a9fe", + "fec0::1", // site-local + "feff::1", + "fe80::1%eth0", + "ff02::1", + "fc00::1", + "::", + "0:0:0:0:0:0:0:1", + ]) { + assert.equal(isPrivateAddress(a), true, a); + } + for (const a of [ + "[::ffff:5db8:d822]", // 93.184.216.34 + "::ffff:8.8.8.8", + "64:ff9b::808:808", // NAT64 of 8.8.8.8 + "2606:4700::1", + "2001:db8::fe80:1", + "fe00::1", + ]) { + assert.equal(isPrivateAddress(a), false, a); + } +}); + +test("a mapped literal in an origin is refused at connect, and a name resolving to one too", async () => { + const lookup = async (h: string) => (h === "mapped.example" ? [{ address: "::ffff:a00:2" }] : [{ address: "93.184.216.34" }]); + assert.match((await privateHostRefusal("http://[::ffff:127.0.0.1]:3001", { lookup }))!, /a local or private address/); + assert.match((await privateHostRefusal("http://[64:ff9b::a9fe:a9fe]", { lookup }))!, /a local or private address/); + assert.match((await privateHostRefusal("https://mapped.example", { lookup }))!, /\(::ffff:a00:2\)/); + assert.equal(await privateHostRefusal("http://[::ffff:5db8:d822]", { lookup }), null); +}); + test("a host's refusal: literal, by name, by what the name resolves to; a public one passes", async () => { const lookup = async (h: string) => (h === "inside.example" ? [{ address: "10.0.0.2" }] : [{ address: "93.184.216.34" }]); assert.match((await privateHostRefusal("http://169.254.169.254", { lookup }))!, /a local or private address/); diff --git a/common/lib/archiveHosts-server.ts b/common/lib/archiveHosts-server.ts @@ -39,16 +39,57 @@ function v4Private(a: string): boolean { ); } -/** Whether an IP address is loopback, private, link-local, CGNAT, unspecified or multicast. */ +/** + * An IPv6 address as its eight 16-bit groups, or null. The URL parser writes + * an IPv4-mapped literal in hex (`[::ffff:127.0.0.1]` becomes + * `[::ffff:7f00:1]`), so the groups are what is compared, never the text. + */ +function v6Groups(address: string): number[] | null { + let a = address; + // A dotted IPv4 tail is two groups. + const tail = /^(.*:)(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(a); + if (tail) { + const b = tail.slice(2).map(Number); + if (b.some((n) => n > 255)) return null; + a = `${tail[1]}${((b[0] << 8) | b[1]).toString(16)}:${((b[2] << 8) | b[3]).toString(16)}`; + } + const halves = a.split("::"); + if (halves.length > 2) return null; + const parse = (part: string) => (part === "" ? [] : part.split(":").map((g) => (/^[0-9a-f]{1,4}$/.test(g) ? parseInt(g, 16) : NaN))); + const head = parse(halves[0]); + const rest = halves.length === 2 ? parse(halves[1]) : []; + const fill = halves.length === 2 ? 8 - head.length - rest.length : 0; + if (fill < 0) return null; + const groups = [...head, ...new Array<number>(fill).fill(0), ...rest]; + return groups.length === 8 && groups.every((g) => Number.isInteger(g)) ? groups : null; +} + +const v4Of = (hi: number, lo: number) => `${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`; + +/** + * Whether an IP address is loopback, private, link-local, site-local, CGNAT, + * unspecified or multicast — in IPv6 also when it only CARRIES such an IPv4 + * address: IPv4-mapped (`::ffff:a.b.c.d`, in either spelling), the old + * IPv4-compatible form (`::a.b.c.d`) and NAT64 (`64:ff9b::/96`). + */ export function isPrivateAddress(address: string): boolean { const a = address.replace(/^\[|\]$/g, "").toLowerCase(); const kind = isIP(a); if (kind === 4) return v4Private(a); if (kind !== 6) return false; - if (a === "::" || a === "::1") return true; - const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(a); - if (mapped) return v4Private(mapped[1]); - return /^(fc|fd|fe8|fe9|fea|feb|ff)/.test(a); + const g = v6Groups(a.split("%")[0]); + if (!g) return true; // an IPv6 literal this cannot read is not vouched for + const zero = (from: number, to: number) => g.slice(from, to).every((n) => n === 0); + if (zero(0, 7) && (g[7] === 0 || g[7] === 1)) return true; // ::, ::1 + if (zero(0, 5) && g[5] === 0xffff) return v4Private(v4Of(g[6], g[7])); // IPv4-mapped + if (zero(0, 6)) return v4Private(v4Of(g[6], g[7])); // IPv4-compatible (deprecated) + if (g[0] === 0x64 && g[1] === 0xff9b && zero(2, 6)) return v4Private(v4Of(g[6], g[7])); // NAT64 + return ( + (g[0] & 0xfe00) === 0xfc00 || // fc00::/7 unique local + (g[0] & 0xffc0) === 0xfe80 || // fe80::/10 link-local + (g[0] & 0xffc0) === 0xfec0 || // fec0::/10 site-local (deprecated) + (g[0] & 0xff00) === 0xff00 // ff00::/8 multicast + ); } const PRIVATE_NAME = /(^|\.)(localhost|local|internal|lan|home\.arpa)$/i;