import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync, readdirSync, statSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/childEnvGuard.gate.test.ts // // THE SPAWN-SITE GATE (review 2, finding 2). The environment a child gets is // decided in ONE place: lib/childEnvGuard.mjs, a hook on Node's own spawn // (ChildProcess.prototype.spawn, which spawn/execFile/exec/fork/execa all // reach) and on spawnSync/execSync/execFileSync. A site cannot build its // environment another way, because whatever it passes is filtered there. So // the gate holds what the hook depends on: // 1. every process ENTRY that can start network work installs it first // (editor, umtool, the archilyzer CLI) — a child of a guarded process // inherits the filtered environment, so grandchildren are covered; // 2. every common/ module that spawns a networking tool (yt-dlp, gallery-dl, // streamlink, curl, wget, a browser) imports it too, so a script that // loads one without an entry is still guarded; // 3. no source file reaches a spawn the hook does not see (process.binding, // internalBinding, a second ChildProcess.prototype.spawn patch); // 4. no source file sets a proxy variable for a child except the guard's // routedChildEnv. const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); const read = (rel: string) => readFileSync(path.join(ROOT, rel), "utf8"); const GUARD_IMPORT = /childEnvGuard(\.mjs)?["']/; function walk(dir: string, out: string[] = []): string[] { let names: string[]; try { names = readdirSync(path.join(ROOT, dir)); } catch { return out; } for (const n of names) { if (n === "node_modules" || n === ".next" || n === "e2e" || n.startsWith(".")) continue; const rel = path.join(dir, n); if (statSync(path.join(ROOT, rel)).isDirectory()) walk(rel, out); else if (/\.(ts|tsx|mjs|js)$/.test(n) && !/\.(test|spec|fixture|unit)\./.test(n)) out.push(rel); } return out; } const SOURCES = [...walk("common"), ...walk("editor/app"), ...walk("editor/lib"), "editor/instrumentation.ts", ...walk("umtool"), ...walk("scripts"), ...walk("mcp/src")]; test("1. every process entry installs the guard before anything else can spawn", () => { for (const entry of ["editor/instrumentation.ts", "umtool/instrumentation.ts", "common/bin/archilyzer.ts", "umtool/bin/umtool.mjs"]) { assert.match(read(entry), GUARD_IMPORT, `${entry} does not install lib/childEnvGuard`); } }); // X code (and headedDisplay, which builds the X browser's launch) is another // lane's: covered by the process entries above, not edited here. const X_FILES = new Set([ "common/social/xGalleryDlFetcher.ts", "common/social/xTrendingRead.ts", "common/social/xSessionBroker.ts", "common/social/xBrowserLogin.ts", "common/social/headedDisplay.ts", ]); const NETWORK_TOOL = /ytdlpBin|galleryDlBin|["'`]yt-dlp["'`]|["'`]gallery-dl["'`]|streamlink|["'`]curl["'`]|["'`]wget["'`]|chromium\.launch|firefox\.launch|launchPersistentContext/; const SPAWNS = /\b(execa|spawn|execFile|fork|spawnSync|execFileSync|execSync)\s*\(/; test("2. every common/ module that spawns a networking tool imports the guard", () => { const missing = SOURCES.filter( (f) => f.startsWith("common/") && !f.endsWith("childEnvGuard.mjs") && !X_FILES.has(f), ).filter((f) => { const s = read(f); return SPAWNS.test(s) && NETWORK_TOOL.test(s) && !GUARD_IMPORT.test(s); }); assert.deepEqual(missing, []); }); test("3. nothing reaches a spawn the guard does not see", () => { const raw = SOURCES.filter((f) => !f.endsWith("childEnvGuard.mjs") && !f.endsWith("noNetwork.mjs")).filter((f) => /process\.binding\(|internalBinding\(|ChildProcess\.prototype\.spawn\s*=/.test(read(f)), ); assert.deepEqual(raw, []); }); test("4. no source sets a proxy variable for a child but the guard", () => { const setters = SOURCES.filter((f) => !f.endsWith("childEnvGuard.mjs") && !f.endsWith("noNetwork.mjs")).filter((f) => /\b(HTTPS?_PROXY|ALL_PROXY|https?_proxy|all_proxy|NO_PROXY|no_proxy)\s*:/.test(read(f)), ); assert.deepEqual(setters, []); }); // REVIEW 3 finding 3: the sync API is not hooked, so no networking spawn may // use it. A sync call naming a networking tool (or a URL) fails the gate. test("5. no networking tool is spawned through the sync API", () => { const SYNC = /\b(spawnSync|execSync|execFileSync|execaSync)\s*\(([\s\S]{0,240})/g; const NET = /yt-?dlp|ytdlpBin|gallery-?dl|galleryDlBin|streamlink|\bcurl\b|\bwget\b|aria2c|chromium|playwright|https?:\/\//i; const hits: string[] = []; for (const f of SOURCES) { for (const m of read(f).matchAll(SYNC)) if (NET.test(m[2]!)) hits.push(`${f}: ${m[0].slice(0, 80)}`); } assert.deepEqual(hits, []); }); // REVIEW 3 finding 2: every entry names the settings file the editor uses. test("6. every process entry tells the guard which settings file holds the policy", () => { for (const entry of ["editor/instrumentation.ts", "umtool/instrumentation.ts", "common/bin/archilyzer.ts", "umtool/bin/umtool.mjs"]) { assert.match(read(entry), /setNetworkPolicySettingsFile\(/, entry); } // The umtool CLI names the file getPaths() reads (umtool/lib/paths.mjs editorSettingsFile). assert.match(read("umtool/bin/umtool.mjs"), /setNetworkPolicySettingsFile\(editorSettingsFile\(\)\)/); }); // REVIEW 3 nit 4: a guard that does not install stops the server. test("7. a server whose guard does not install refuses to start", () => { for (const entry of ["editor/instrumentation.ts", "umtool/instrumentation.ts"]) { const s = read(entry); const at = s.indexOf("setNetworkPolicySettingsFile("); const block = s.slice(at, at + 900); assert.match(block, /catch[\s\S]*refuses to start[\s\S]*process\.exit\(1\)/, entry); // …and does not leave a credential behind for whatever runs before the exit. assert.match(block, /catch[\s\S]*NETWORK_EXIT_[\s\S]*process\.exit\(1\)/, `${entry}: the catch clears NETWORK_EXIT_* first`); } });