Archilyzer · Source

archilyzer

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

commit b1bbf2cb0fdcd68b49193f49775d6bd246a370c0
parent 7580213d653ac8cea827966d4c3f5b3328febcc3
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 11 Nov 2026 12:15:55 -0500

archives: an archive on this machine or its network is connected only when decided here (demo epic D1 review, blocker 2c)

Connect read any origin's corpus.json, and umtool, which can connect one, is
open on the LAN. An origin whose host is loopback, private, link-local, CGNAT,
unspecified, a .localhost/.local/.internal/.lan name, or a name resolving to
such an address is now refused before anything is asked of it -- unless
the editor runs with ARCHILYZER_ALLOW_PRIVATE_ARCHIVES=1, or the origin is
TRANSCRIPT_SITE_URL's or already in settings.json (common/lib/archiveHosts-server.ts).
A public archive (the Docker demo's jeralyzer.pages.dev) is unaffected. The
editor e2e server sets the allowance (its stub archive is on loopback);
ENVIRONMENT.md regenerated. The controller's tests use a fake name lookup.

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

Diffstat:
MENVIRONMENT.md | 1+
Mcommon/controller/remoteArchives.test.ts | 23+++++++++++++++++++++++
Mcommon/controller/remoteArchives.ts | 13++++++++++++-
Acommon/lib/archiveHosts-server.test.ts | 21+++++++++++++++++++++
Acommon/lib/archiveHosts-server.ts | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon/lib/envVars.ts | 1+
Meditor/e2e/servers.ts | 3+++
7 files changed, 136 insertions(+), 1 deletion(-)

diff --git a/ENVIRONMENT.md b/ENVIRONMENT.md @@ -99,6 +99,7 @@ Tokens, credentials and knobs a running process reads. Most configuration is not | `SONG_REPORTS_DIR` | unset | umtool's um-song deliverables tree; when `REPORTS_DIR` is unset, its parent is the reports root a delete and `archilyzer case` read (as umtool does). | common/lib/report/delete.ts, common/bin/case.ts, umtool (song/paths.mjs) | | `UMTOOL_URL` | unset (no link; the MCP's `notes` off) | umtool's front door; when set, the video page links to it, its Find clips hands the recording to umtool's clip finder (the `find-clips` job posts to `/api/report/clips` with WORKER_TOKEN), and the MCP's `notes` tool reads the operator's notes there and writes a reply as the agent's (e.g. `http://localhost:3050`). Unset: Find clips is disabled. | editor/app/channels/[slug]/videos/[id]/page.tsx, common/lib/umtoolUrl.ts, mcp/src/archivalTools.ts | | `TRANSCRIPT_SITE_URL` | — | MCP server: one published archive to read over HTTP. Also the default connected archive (common/lib/remoteArchives.ts): the editor, umtool and the MCP offer it as a source when settings.remoteArchives does not list it, without writing it. | mcp/src/sources.ts | +| `ARCHILYZER_ALLOW_PRIVATE_ARCHIVES` | off | `1` lets the editor connect an archive whose host is this machine or its network (loopback, private, link-local, a `.local` name, or a name resolving to such an address), which is refused otherwise: umtool, which can connect one, is open on the LAN. TRANSCRIPT_SITE_URL's archive, and one already written in settings.json, are connected without it. | common/controller/remoteArchives.ts | | `ARCHIVE_CACHE_DIR` | `.archive-cache` beside EXPORT_BUILDS_DIR | Where a remote archive's documents (corpus.json, manifests, shard pages) are kept on disk, by the archive's build: a sweep over a published archive downloads a shard once per build, not once per sweep. In a container it is on the builds volume. | common/lib/archiveCache-server.ts | | `ARCHIVE_CACHE_MAX_MB` | 2048 | The disk cache of remote archive documents' size cap, MB: past it the least recently used are dropped. | common/lib/archiveCache-server.ts | | `ARCHIVE_CACHE` | on (off under node's test runner) | `0` reads remote archives without the disk cache (every read over the network, still one at a time); `1` turns it on under node's test runner, where it is off. | common/lib/archiveCache-server.ts | diff --git a/common/controller/remoteArchives.test.ts b/common/controller/remoteArchives.test.ts @@ -30,6 +30,8 @@ function world(initial: RemoteArchive[] = [], env: Record<string, string> = {}) fetch, now: () => new Date("2026-11-11T12:00:00Z"), env, + // No real DNS in a test: every name is a public address unless a test says. + lookup: async () => [{ address: "93.184.216.34" }], }; return { deps, writes, asked, stored: () => stored }; } @@ -71,3 +73,24 @@ test("remove by id or by origin; an unknown one is refused, naming what is conne assert.deepEqual(w.stored(), []); await assert.rejects(removeRemoteArchive("zzz", w.deps), /no connected archive "zzz" — connected: none/); }); + +// demo epic D1 review, blocker 2c: this machine or its network is not connected unless decided here. +test("a local or private archive host is refused before anything is asked of it; the operator's own word allows it", async () => { + for (const origin of ["http://127.0.0.1:4100", "http://[::1]:8080", "http://localhost:3000", "http://nas.local", "http://10.0.0.7", "http://192.168.1.20/archive"]) { + const w = world(); + await assert.rejects(connectRemoteArchive({ origin }, w.deps), /an archive on this machine or its network is connected only when/, origin); + assert.deepEqual(w.asked, [], `${origin}: nothing asked`); + } + // A public name that resolves to a private address. + const sneaky = world(); + sneaky.deps.lookup = async () => [{ address: "93.184.216.34" }, { address: "192.168.1.9" }]; + await assert.rejects(connectRemoteArchive({ origin: "https://rebind.example" }, sneaky.deps), /a name for a local or private address \(192\.168\.1\.9\)/); + assert.deepEqual(sneaky.asked, []); + // Decided on this machine: the editor's environment, TRANSCRIPT_SITE_URL, or settings.json. + const allowed = world([], { ARCHILYZER_ALLOW_PRIVATE_ARCHIVES: "1" }); + assert.equal((await connectRemoteArchive({ origin: "http://127.0.0.1:4100" }, allowed.deps)).already, false); + const fromEnv = world([], { TRANSCRIPT_SITE_URL: "http://127.0.0.1:4100" }); + assert.equal((await connectRemoteArchive({ origin: "http://127.0.0.1:4100" }, fromEnv.deps)).already, false); + const stored = world([{ id: "lan", origin: "http://10.0.0.7", title: "LAN", addedAt: "2026-11-11T00:00:00.000Z" }]); + assert.equal((await connectRemoteArchive({ origin: "http://10.0.0.7" }, stored.deps)).already, true); +}); diff --git a/common/controller/remoteArchives.ts b/common/controller/remoteArchives.ts @@ -12,7 +12,8 @@ // variable. import { connectedArchives, findConnectedArchive, freeArchiveId, sanitizeRemoteArchives, archiveOriginOf, type ConnectedArchive, type RemoteArchive } from "../lib/remoteArchives"; -import { probeArchive, type ArchiveProbe, type FetchLike } from "../lib/remoteArchives-server"; +import { ArchiveProbeError, probeArchive, type ArchiveProbe, type FetchLike } from "../lib/remoteArchives-server"; +import { privateHostRefusal, type LookupAll } from "../lib/archiveHosts-server"; export type RemoteArchiveDeps = { // The stored list (settings.remoteArchives). @@ -22,6 +23,8 @@ export type RemoteArchiveDeps = { fetch?: FetchLike; now?: () => Date; env?: Record<string, string | undefined>; + // The host check's name lookup (archiveHosts-server.ts); a test's fake. + lookup?: LookupAll; }; export type ConnectResult = { @@ -32,6 +35,14 @@ export type ConnectResult = { }; export async function connectRemoteArchive(o: { origin: string; dryRun?: boolean }, deps: RemoteArchiveDeps): Promise<ConnectResult> { + // This machine or its network: refused before anything is asked of it, + // unless decided here (archiveHosts-server.ts). + const origin = archiveOriginOf(o.origin); + const env = deps.env ?? process.env; + if (origin && env.ARCHILYZER_ALLOW_PRIVATE_ARCHIVES !== "1" && !findConnectedArchive(connectedArchives(deps.stored(), env), origin)) { + const refused = await privateHostRefusal(origin, { lookup: deps.lookup }); + if (refused) throw new ArchiveProbeError(refused); + } const probe = await probeArchive(o.origin, { fetch: deps.fetch }); const list = connectedArchives(deps.stored(), deps.env); const have = findConnectedArchive(list, probe.origin); diff --git a/common/lib/archiveHosts-server.test.ts b/common/lib/archiveHosts-server.test.ts @@ -0,0 +1,21 @@ +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("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); +}); + diff --git a/common/lib/archiveHosts-server.ts b/common/lib/archiveHosts-server.ts @@ -0,0 +1,75 @@ +// 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 + ); +} + +/** Whether an IP address is loopback, private, link-local, CGNAT, unspecified or multicast. */ +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 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<string | null> { + 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; +} diff --git a/common/lib/envVars.ts b/common/lib/envVars.ts @@ -135,6 +135,7 @@ const DECLARED: EnvVarDecl[] = [ { name: "SONG_REPORTS_DIR", audience: "runtime", default: "unset", readBy: "common/lib/report/delete.ts, common/bin/case.ts, umtool (song/paths.mjs)", doc: "umtool's um-song deliverables tree; when `REPORTS_DIR` is unset, its parent is the reports root a delete and `archilyzer case` read (as umtool does)." }, { name: "UMTOOL_URL", audience: "runtime", default: "unset (no link; the MCP's `notes` off)", readBy: "editor/app/channels/[slug]/videos/[id]/page.tsx, common/lib/umtoolUrl.ts, mcp/src/archivalTools.ts", doc: "umtool's front door; when set, the video page links to it, its Find clips hands the recording to umtool's clip finder (the `find-clips` job posts to `/api/report/clips` with WORKER_TOKEN), and the MCP's `notes` tool reads the operator's notes there and writes a reply as the agent's (e.g. `http://localhost:3050`). Unset: Find clips is disabled." }, { name: "TRANSCRIPT_SITE_URL", audience: "runtime", default: "—", readBy: "mcp/src/sources.ts", doc: "MCP server: one published archive to read over HTTP. Also the default connected archive (common/lib/remoteArchives.ts): the editor, umtool and the MCP offer it as a source when settings.remoteArchives does not list it, without writing it." }, + { name: "ARCHILYZER_ALLOW_PRIVATE_ARCHIVES", audience: "runtime", default: "off", readBy: "common/controller/remoteArchives.ts", doc: "`1` lets the editor connect an archive whose host is this machine or its network (loopback, private, link-local, a `.local` name, or a name resolving to such an address), which is refused otherwise: umtool, which can connect one, is open on the LAN. TRANSCRIPT_SITE_URL's archive, and one already written in settings.json, are connected without it." }, { name: "ARCHIVE_CACHE_DIR", audience: "runtime", default: "`.archive-cache` beside EXPORT_BUILDS_DIR", readBy: "common/lib/archiveCache-server.ts", doc: "Where a remote archive's documents (corpus.json, manifests, shard pages) are kept on disk, by the archive's build: a sweep over a published archive downloads a shard once per build, not once per sweep. In a container it is on the builds volume." }, { name: "ARCHIVE_CACHE_MAX_MB", audience: "runtime", default: "2048", readBy: "common/lib/archiveCache-server.ts", doc: "The disk cache of remote archive documents' size cap, MB: past it the least recently used are dropped." }, { name: "ARCHIVE_CACHE", audience: "runtime", default: "on (off under node's test runner)", readBy: "common/lib/archiveCache-server.ts", doc: "`0` reads remote archives without the disk cache (every read over the network, still one at a time); `1` turns it on under node's test runner, where it is off." }, diff --git a/editor/e2e/servers.ts b/editor/e2e/servers.ts @@ -40,6 +40,9 @@ export const E2E_SERVER_ENV: Record<string, string> = { E2E_AUDIO_CHECK_RECOVER_AFTER: "2", E2E_BACKOFF_BASE_MS: "20000", E2E_CLIP_WINDOW_GAP_MS: "2000", + // remote-archive.spec's stub archive is on loopback (demo epic D1): the + // editor connects a local archive only when told so on its own machine. + ARCHILYZER_ALLOW_PRIVATE_ARCHIVES: "1", E2E_AVAILABILITY_PROBE_GAP_MS: "0", // The pacing guard's BUILT-IN defaults (plans/pacing-guard.md) are off on // the test server — a spec's two fake X fetches would otherwise sit five