import { test, after } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/childEnvGuard.test.ts // // THE CHILD-ENVIRONMENT HOOK (lane network-exits, reviews 2-3). Each case runs // lib/childEnvGuard.fixture.mjs as its own node process, with child_process // bindings taken before the guard is installed (ESM named imports and a // CommonJS require). const HERE = path.dirname(fileURLToPath(import.meta.url)); const DIR = mkdtempSync(path.join(os.tmpdir(), "child-env-guard-")); after(() => rmSync(DIR, { recursive: true, force: true })); function run(mode: string, settings: string | null | "unset") { let file = "-"; if (settings !== "unset") { file = path.join(DIR, `settings-${Math.random().toString(36).slice(2)}.json`); if (settings !== null) writeFileSync(file, settings); } const env: Record = {}; for (const [k, v] of Object.entries(process.env)) { if (v !== undefined && !/proxy$/i.test(k) && !["NODE_OPTIONS", "ARCHILYZER_NO_NETWORK", "SETTINGS_FILE"].includes(k)) env[k] = v; } return JSON.parse(execFileSync(process.execPath, [path.join(HERE, "childEnvGuard.fixture.mjs"), mode, file], { env, cwd: DIR }).toString()); } const POLICY = JSON.stringify({ network: { exits: [], platforms: { twitch: { exits: ["direct"], mode: "sticky" } } } }); const ASYNC = ["esmSpawn", "esmExec", "esmExecFile", "esmExecFileUndefined", "cjsSpawn", "cjsExecFile", "shellTrue", "fork", "execa", "execaOwnEnv"] as const; test("a credential leaves process.env at load and no child gets it, whatever API spawns it (review 2 finding 1)", () => { const r = run("apis", POLICY); assert.equal(r.parentHasCredential, false); for (const w of [...ASYNC, "routed", "spawnSync", "execFileSync"]) { assert.ok(!Object.keys(r[w]).some((k) => k.startsWith("NETWORK_EXIT_")), w); } }); test("any policy: every async spawn, bindings taken before install included, gets no proxy variable (review 3 test 1)", () => { const r = run("apis", POLICY); for (const w of ASYNC) assert.deepEqual(r[w], {}, w); assert.deepEqual(Object.keys(r.routed).sort(), ["ALL_PROXY", "HTTPS_PROXY", "HTTP_PROXY", "all_proxy", "http_proxy", "https_proxy"]); for (const v of Object.values(r.routed)) assert.equal(v, "socks5h://127.0.0.1:1"); }); test("the sync API is not hooked: it keeps main's environment (no networking spawn uses it — the gate holds that)", () => { const r = run("apis", POLICY); assert.deepEqual(r.spawnSync, { HTTPS_PROXY: "http://127.0.0.1:2", no_proxy: "*" }); assert.deepEqual(r.execFileSync, { HTTPS_PROXY: "http://127.0.0.1:2", no_proxy: "*" }); }); test("no policy is main, byte for byte: the whole environment, explicit env own and inherited keys, and the options (review 3 test 2)", () => { const r = run("main", JSON.stringify({ network: { exits: [], platforms: {} } })); assert.equal(r.sameWhole, true); assert.equal(r.explicitEnv.OWN_KEY, "own"); if (!r.noNetworkHarness) { assert.equal(r.explicitEnv.INHERITED_ONLY, "1"); assert.equal(r.explicitEnv.HTTPS_PROXY, "http://127.0.0.1:2"); } assert.equal(r.cwd, "/"); assert.equal(r.input, "piped-in"); assert.deepEqual(r.timeout, { timedOut: true, fast: true }); assert.equal(r.signal, true); assert.equal(r.detached, true); }); test("two copies of the module share one state: one patch, one map, a put-back credential reaches no child (review 3 test 3)", () => { const r = run("twocopies", POLICY); assert.equal(r.onePatch, true); assert.equal(r.bothRead, true); assert.deepEqual(r.putBack, {}); assert.equal(r.putBackLeftEnv, true); }); test("no settings file named: fail closed (a policy may exist), said once in the log (review 3 finding 2)", () => { const r = run("unresolved", "unset"); assert.deepEqual(r.child, {}); assert.equal(r.log.filter((l: string) => /childEnvGuard: no settings file was named/.test(l)).length, 1); }); test("unreadable settings strip too (fail safe), said once", () => { const r = run("apis", "{ not json"); for (const w of ASYNC) assert.deepEqual(r[w], {}, w); assert.equal(r.log.filter((l: string) => /could not be read/.test(l)).length, 1); }); test("an absent settings file at the named path is no policy: main's proxies", () => { const r = run("apis", null); assert.deepEqual(r.esmSpawn, { HTTPS_PROXY: "http://127.0.0.1:2", no_proxy: "*" }); }); test("the policy path is resolved once (review 3 nit 6): SETTINGS_FILE changed later is not re-read", () => { const src = readFileSync(path.join(HERE, "childEnvGuard.mjs"), "utf8"); assert.match(src, /resolvedSettingsFile/); assert.doesNotMatch(src, /process\.cwd\(\)/, "no cwd walk (review 3 findings 1-2)"); });