// The fixture of childEnvGuard.test.ts, run as its own node process: // `node childEnvGuard.fixture.mjs `. Bindings to // child_process are taken BEFORE the guard is installed — an ESM named import // here and a CommonJS require below — as code loaded earlier than an entry's // import would hold them. Prints JSON. import { spawn as eSpawn, exec as eExec, execFile as eExecFile, fork as eFork, spawnSync as eSpawnSync, execFileSync as eExecFileSync } from "node:child_process"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; const require = createRequire(import.meta.url); const cjs = require("node:child_process"); const { spawn: rSpawn, execFile: rExecFile } = cjs; const SELF = fileURLToPath(import.meta.url); const [mode, settingsFile] = process.argv.slice(2); const ENV_SCRIPT = "const o={};for(const [k,v] of Object.entries(process.env)){if(/proxy$/i.test(k)||/^NETWORK_EXIT_/.test(k))o[k]=v;}process.stdout.write(JSON.stringify(o));"; const WHOLE_ENV = "process.stdout.write(JSON.stringify(Object.entries(process.env).filter(([k])=>k!=='_')))"; if (mode === "fork-child") { const o = {}; for (const [k, v] of Object.entries(process.env)) if (/proxy$/i.test(k) || /^NETWORK_EXIT_/.test(k)) o[k] = v; process.stdout.write(JSON.stringify(o)); process.exit(0); } const collect = (child) => new Promise((resolve) => { let s = ""; child.stdout.on("data", (d) => (s += d)); child.on("close", () => resolve(JSON.parse(s || "{}"))); }); const node = process.execPath; Object.assign(process.env, { HTTPS_PROXY: "http://127.0.0.1:2", no_proxy: "*", NETWORK_EXIT_EXIT_A: "fixture-user:fixture-value" }); if (settingsFile && settingsFile !== "-") process.env.SETTINGS_FILE = settingsFile; else delete process.env.SETTINGS_FILE; const errs = []; const origError = console.error; console.error = (...a) => errs.push(a.join(" ")); const guard = await import("./childEnvGuard.mjs"); const out = {}; if (mode === "apis") { out.parentHasCredential = "NETWORK_EXIT_EXIT_A" in process.env; out.esmSpawn = await collect(eSpawn(node, ["-e", ENV_SCRIPT])); out.esmExec = await collect(eExec(`"${node}" -e '${ENV_SCRIPT}'`)); out.esmExecFile = await collect(eExecFile(node, ["-e", ENV_SCRIPT])); out.esmExecFileUndefined = await collect(eExecFile(node, ["-e", ENV_SCRIPT], undefined)); out.cjsSpawn = await collect(rSpawn(node, ["-e", ENV_SCRIPT])); out.cjsExecFile = await collect(rExecFile(node, ["-e", ENV_SCRIPT])); out.shellTrue = await collect(eSpawn(`"${node}" -e '${ENV_SCRIPT}'`, { shell: true })); out.fork = await collect(eFork(SELF, ["fork-child"], { silent: true })); const { execa } = await import("execa"); out.execa = JSON.parse((await execa(node, ["-e", ENV_SCRIPT])).stdout || "{}"); out.execaOwnEnv = JSON.parse((await execa(node, ["-e", ENV_SCRIPT], { env: { ...process.env, ALL_PROXY: "http://127.0.0.1:3" } })).stdout || "{}"); out.routed = JSON.parse((await execa(node, ["-e", ENV_SCRIPT], { env: guard.routedChildEnv(), extendEnv: false })).stdout || "{}"); // The sync API is NOT hooked (no networking spawn uses it; the gate holds // that): it gets main's environment — minus the credential, which left // process.env at install. out.spawnSync = JSON.parse(eSpawnSync(node, ["-e", ENV_SCRIPT]).stdout.toString() || "{}"); out.execFileSync = JSON.parse(eExecFileSync(node, ["-e", ENV_SCRIPT]).toString() || "{}"); out.log = errs; } else if (mode === "main") { // No policy: the WHOLE environment a child sees is the parent's, in order. const parent = Object.entries(process.env).filter(([k]) => k !== "_"); const child = await collect(eSpawn(node, ["-e", WHOLE_ENV])); out.sameWhole = JSON.stringify(child) === JSON.stringify(parent); const base = { PATH: process.env.PATH ?? "", HTTPS_PROXY: "http://127.0.0.1:2", INHERITED_ONLY: "1" }; const ownAndInherited = Object.create(base); ownAndInherited.OWN_KEY = "own"; const viaSpawn = await collect(eSpawn(node, ["-e", WHOLE_ENV], { env: ownAndInherited })); out.explicitEnv = Object.fromEntries(viaSpawn); // The test harness re-adds its no-network preload to a child given its own // environment (common/lib/noNetwork.mjs guardedChildEnv), which copies own // keys only; the inherited-key case is then the harness's, not the guard's. out.noNetworkHarness = process.env.ARCHILYZER_NO_NETWORK === "1"; // Options reach Node unchanged. const { execa } = await import("execa"); out.cwd = (await execa(node, ["-e", "process.stdout.write(process.cwd())"], { cwd: "/" })).stdout; out.input = (await execa(node, ["-e", "process.stdin.pipe(process.stdout)"], { input: "piped-in" })).stdout; const t0 = Date.now(); const timedOut = await execa(node, ["-e", "setTimeout(()=>{},5000)"], { timeout: 200, reject: false }); out.timeout = { timedOut: timedOut.timedOut, fast: Date.now() - t0 < 3000 }; const ac = new AbortController(); const aborted = execa(node, ["-e", "setTimeout(()=>{},5000)"], { cancelSignal: ac.signal, reject: false }); setTimeout(() => ac.abort(), 100); out.signal = (await aborted).isCanceled; const det = eSpawn(node, ["-e", "process.stdout.write(String(process.pid))"], { detached: true, stdio: ["ignore", "pipe", "ignore"] }); out.detached = (await new Promise((r) => { let s = ""; det.stdout.on("data", (d) => (s += d)); det.on("close", () => r(s)); })) !== ""; } else if (mode === "twocopies") { const { ChildProcess } = cjs; const first = ChildProcess.prototype.spawn; const copy = await import("./childEnvGuard.mjs?copy=2"); out.onePatch = ChildProcess.prototype.spawn === first; out.bothRead = guard.networkExitEnvValue("NETWORK_EXIT_EXIT_A") === "fixture-user:fixture-value" && copy.networkExitEnvValue("NETWORK_EXIT_EXIT_A") === "fixture-user:fixture-value"; // A credential put back (Next's dev .env reload does this) reaches no child. const PUT_BACK = "NETWORK_EXIT_EXIT_B"; process.env[PUT_BACK] = "fixture-user:put-back"; out.putBack = await collect(eSpawn(node, ["-e", ENV_SCRIPT])); out.putBackLeftEnv = !(PUT_BACK in process.env); } else if (mode === "unresolved") { out.child = await collect(eSpawn(node, ["-e", ENV_SCRIPT])); await collect(eSpawn(node, ["-e", ENV_SCRIPT])); out.log = errs; } console.error = origError; process.stdout.write(JSON.stringify(out));