// WHICH HOSTS AN ARCHIVE MAY BE (demo epic D1 review, blocker 2c). // // Connecting an archive makes this machine read the origin's documents — and // umtool, which can connect one, is open on the LAN. So an origin whose host is // this machine or the local network (loopback, private, link-local, CGNAT, a // `.localhost`/`.local`/`.internal` name, or a name that RESOLVES to such an // address) is refused at connect, before its corpus.json is asked for — // unless the operator decided otherwise ON THIS MACHINE: // // - ARCHILYZER_ALLOW_PRIVATE_ARCHIVES=1 in the editor's own environment // (a stub archive in a test, an archive served on the LAN on purpose); // - the origin is TRANSCRIPT_SITE_URL's (the environment's archive); // - the origin is already in settings.json (written there by hand). // // Nothing else checks: once connected, an archive is read on its own origin // only (remoteArchives-server.ts fetchOnOrigin). A name re-pointed at a // private address after it was connected is not re-checked. import { lookup as dnsLookup } from "node:dns/promises"; import { isIP } from "node:net"; export type LookupAll = (host: string) => Promise<{ address: string }[]>; const defaultLookup: LookupAll = (host) => dnsLookup(host, { all: true }); function v4Private(a: string): boolean { const p = a.split(".").map(Number); if (p.length !== 4 || p.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return false; const [x, y] = p; return ( x === 0 || x === 10 || x === 127 || (x === 100 && y >= 64 && y <= 127) || (x === 169 && y === 254) || (x === 172 && y >= 16 && y <= 31) || (x === 192 && y === 168) || x >= 224 ); } /** * 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(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`), NAT64 (`64:ff9b::/96`), IPv4-translated * (`::ffff:0:a.b.c.d`) and 6to4 (`2002::/16`). NAT64's local-use block * `64:ff9b:1::/48` is private whole. */ 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; 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 if (g[0] === 0x64 && g[1] === 0xff9b && g[2] === 1) return true; // 64:ff9b:1::/48, NAT64's local-use block: never globally routable, whatever it carries if (zero(0, 4) && g[4] === 0xffff && g[5] === 0) return v4Private(v4Of(g[6], g[7])); // IPv4-translated ::ffff:0:a.b.c.d if (g[0] === 0x2002) return v4Private(v4Of(g[1], g[2])); // 6to4 2002:a.b.c.d::/48 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; /** * Why `origin`'s host may not be connected, or null: a private address * literally, a private name, or a name resolving to a private address. A name * that does not resolve is left to the probe, which says it did not answer. */ export async function privateHostRefusal(origin: string, opts: { lookup?: LookupAll } = {}): Promise { let host: string; try { host = new URL(origin).hostname.replace(/^\[|\]$/g, ""); } catch { return null; } const say = (why: string) => `${origin} is ${why} — an archive on this machine or its network is connected only when the editor runs with ARCHILYZER_ALLOW_PRIVATE_ARCHIVES=1, or names it in TRANSCRIPT_SITE_URL or settings.json`; if (isIP(host)) return isPrivateAddress(host) ? say("a local or private address") : null; if (PRIVATE_NAME.test(host)) return say("a local name"); const found = await (opts.lookup ?? defaultLookup)(host).catch(() => [] as { address: string }[]); const inside = found.find((f) => isPrivateAddress(f.address)); return inside ? say(`a name for a local or private address (${inside.address})`) : null; }