import { test } from "node:test"; import assert from "node:assert/strict"; import { isPrivateAddress, privateHostRefusal } from "./archiveHosts-server"; // demo epic D1 review, blocker 2c: an archive host on this machine or its network. test("private addresses: loopback, private, link-local, CGNAT, unspecified, IPv6 local, v4-mapped", () => { for (const a of ["127.0.0.1", "10.1.2.3", "172.20.0.1", "192.168.0.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fd00::1", "fe80::1", "::ffff:127.0.0.1", "[::1]"]) { assert.equal(isPrivateAddress(a), true, a); } 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("the other ways an IPv6 literal carries a private IPv4: NAT64's local-use prefix, IPv4-translated, 6to4", () => { for (const a of ["::ffff:0:7f00:1", "::ffff:0:10.0.0.1", "2002:7f00:1::", "2002:c0a8:101::1", "2002:a9fe:a9fe::"]) { assert.equal(isPrivateAddress(a), true, a); } for (const a of ["::ffff:0:808:808", "2002:808:808::1"]) assert.equal(isPrivateAddress(a), false, a); // 64:ff9b:1::/48 is local-use whole: private wherever an IPv4 sits in it, and when it carries a public one. for (const a of ["64:ff9b:1::7f00:1", "64:ff9b:1::808:808", "64:ff9b:1:7f00:0:1::", "64:ff9b:1:a00:0:1::", "64:ff9b:1:ffff:ffff:ffff:ffff:ffff", "64:ff9b:1::"]) { assert.equal(isPrivateAddress(a), true, a); } // Its neighbours are not it. for (const a of ["64:ff9b:2::808:808", "64:ff9b:10::1"]) assert.equal(isPrivateAddress(a), false, a); }); test("every spelling of a local address in an origin is refused: decimal, hex, octal, short, a trailing dot, bracketed IPv6", async () => { const { archiveOriginOf } = await import("./remoteArchives"); // The names' answers are what a resolver gives for them; nothing here is asked of the network. const lookup = async (h: string) => (/^localhost\.?$/.test(h) || /\.local\.?$/.test(h) ? [{ address: "127.0.0.1" }] : [{ address: "93.184.216.34" }]); for (const typed of [ "http://[::ffff:7f00:1]", "http://[::ffff:127.0.0.1]", "http://[0:0:0:0:0:ffff:127.0.0.1]", "http://[::1]", "http://[::]", "http://0.0.0.0", "http://0", "http://2130706433", // 127.0.0.1 as one decimal number "http://3232235777", // 192.168.1.1 "http://0x7f000001", "http://0x7f.0.0.1", "http://017700000001", "http://0177.0.0.1", "http://127.1", "http://127.0.0.1.", "http://169.254.169.254.", "http://LOCALHOST", "http://localhost.", "http://nas.local.", "http://router.lan", "http://[64:ff9b:1::7f00:1]", "http://[2002:7f00:1::]", ]) { const origin = archiveOriginOf(typed); assert.ok(origin, `${typed} is an origin`); assert.match((await privateHostRefusal(origin, { lookup })) ?? "", /a local or private address|a local name/, typed); } }); 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/); assert.match((await privateHostRefusal("http://box.localhost:3000", { lookup }))!, /a local name/); assert.match((await privateHostRefusal("https://inside.example", { lookup }))!, /\(10\.0\.0\.2\)/); assert.equal(await privateHostRefusal("https://jeralyzer.pages.dev", { lookup }), null); });