import { test, after } from "node:test"; import assert from "node:assert/strict"; import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; process.env.NETWORK_EXIT_EXIT_CAPTURED = "captured-user:captured-value"; const { readExitCredentials, credentialsFileRefusal } = await import("./networkCredentials-server"); // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/networkCredentials-server.test.ts // // THE KEY-FILE CONVENTION (as archive.org's S3 keys, lib/waybackPages/ // savePageNow-server.ts): the environment first, else a mode-600 file whose // PATH is a setting. Temp files only. const DIR = mkdtempSync(path.join(os.tmpdir(), "net-creds-")); process.env.ARCHILYZER_CREDENTIALS_DIR = DIR; after(() => rmSync(DIR, { recursive: true, force: true })); const exitA = { id: "exit-a", label: "A", scheme: "socks5h" as const, host: "a.example.test", port: 1080, credentials: true, enabled: true }; function keyFile(text: string, mode = 0o600): string { const f = path.join(DIR, `keys-${Math.random().toString(36).slice(2)}.env`); writeFileSync(f, text); chmodSync(f, mode); return f; } test("an exit without credentials reads nothing", () => { assert.deepEqual(readExitCredentials({ ...exitA, credentials: false }, { file: "/nonexistent", env: {} }), { ok: true, creds: null }); }); test("the environment first, then the mode-600 file", () => { const f = keyFile("NETWORK_EXIT_EXIT_A=from-file:pw%21\n"); const env = readExitCredentials(exitA, { file: f, env: { NETWORK_EXIT_EXIT_A: "from-env:pw" } }); assert.ok(env.ok && env.creds?.username === "from-env"); const file = readExitCredentials(exitA, { file: f, env: {} }); assert.ok(file.ok); assert.deepEqual(file.creds, { username: "from-file", password: "pw!", encoded: "from-file:pw%21" }); }); test("a file others can read, a missing file or line, or a raw password is refused, never echoing it", () => { const open = readExitCredentials(exitA, { file: keyFile("NETWORK_EXIT_EXIT_A=u:fake-one\n", 0o644), env: {} }); assert.equal(open.ok, false); assert.match((open as { reason: string }).reason, /readable by others — chmod 600 it/); const none = readExitCredentials(exitA, { file: path.join(DIR, "absent.env"), env: {} }); assert.match((none as { reason: string }).reason, /exit "exit-a" needs credentials: put NETWORK_EXIT_EXIT_A=user:password .*absent\.env/); const noLine = readExitCredentials(exitA, { file: keyFile("OTHER=1\n"), env: {} }); assert.match((noLine as { reason: string }).reason, /has no NETWORK_EXIT_EXIT_A= line/); const raw = readExitCredentials(exitA, { file: keyFile("NETWORK_EXIT_EXIT_A=u:fake@value-two\n"), env: {} }); assert.equal(raw.ok, false); assert.match((raw as { reason: string }).reason, /percent-encode/); assert.ok(!(raw as { reason: string }).reason.includes("value-two")); }); test("NETWORK_EXIT_* is moved out of process.env at load into the private map (review 2 finding 1)", () => { assert.equal(process.env.NETWORK_EXIT_EXIT_CAPTURED, undefined); const r = readExitCredentials({ id: "exit-captured", credentials: true }, { file: path.join(DIR, "none.env") }); assert.ok(r.ok && r.creds?.username === "captured-user"); }); test("the key file must sit under the config directory or ARCHILYZER_CREDENTIALS_DIR, be ours and mode 600 (review 2 nit 8)", () => { assert.match(credentialsFileRefusal("/etc/network-exits.env") ?? "", /not under/); assert.match(credentialsFileRefusal("relative.env") ?? "", /absolute/); assert.equal(credentialsFileRefusal(path.join(DIR, "x.env")), null); assert.equal(credentialsFileRefusal("~/.config/archilyzer/network-exits.env"), null); const outside = readExitCredentials(exitA, { file: "/etc/network-exits.env", env: {} }); assert.match((outside as { reason: string }).reason, /not under/); const g = readExitCredentials(exitA, { file: keyFile("NETWORK_EXIT_EXIT_A=u:p\n", 0o640), env: {} }); assert.match((g as { reason: string }).reason, /readable by others/); });