// A NETWORK EXIT'S CREDENTIALS (lane network-exits, review 1 finding 1; // review 2 finding 1 and nit 8), kept the way archive.org's S3 keys are // (lib/waybackPages/savePageNow-server.ts): the environment first // (`NETWORK_EXIT_`), else a mode-600 file whose PATH is the setting // `network.credentialsFile`, one line per exit: // `NETWORK_EXIT_=user:password`, each part percent-encoded. // // - The environment form is read from a PRIVATE in-process map: // lib/childEnvGuard.mjs moves every NETWORK_EXIT_* out of process.env when // it is installed (importing this module installs it), so no child process // ever inherits one. // - The file must sit under the user's config directory (`~/.config`, or // `$XDG_CONFIG_HOME`) or ARCHILYZER_CREDENTIALS_DIR, be a regular file // owned by this user, and have no group or other permission bits. It is // opened once and checked and read through the same descriptor. // // The credentials are never a setting, never logged, never in an error, a job // record or a result: every sentence here names the exit, the variable and // the file — never a value. import { closeSync, fstatSync, openSync, readFileSync, constants as fsConstants } from "node:fs"; import os from "node:os"; import path from "node:path"; import { networkExitEnvValue } from "./childEnvGuard.mjs"; import { DEFAULT_NETWORK_CREDENTIALS_FILE, credentialsEnvKey, parseCredentials, type ExitCredentials, type NetworkExit, } from "./networkExits"; export type ExitCredentialsRead = { ok: true; creds: ExitCredentials | null } | { ok: false; reason: string }; const expandHome = (p: string) => (p === "~" || p.startsWith("~/") ? os.homedir() + p.slice(1) : p); const unquote = (v: string) => v.trim().replace(/^(["'])(.*)\1$/, "$2").trim(); function allowedDirs(): string[] { const dirs = [path.join(os.homedir(), ".config")]; if (process.env.XDG_CONFIG_HOME && path.isAbsolute(process.env.XDG_CONFIG_HOME)) dirs.push(process.env.XDG_CONFIG_HOME); if (process.env.ARCHILYZER_CREDENTIALS_DIR && path.isAbsolute(process.env.ARCHILYZER_CREDENTIALS_DIR)) { dirs.push(process.env.ARCHILYZER_CREDENTIALS_DIR); } return dirs.map((d) => path.resolve(d)); } // Why `keyFile` may not hold credentials, or null. export function credentialsFileRefusal(keyFile: string): string | null { const expanded = expandHome(keyFile.trim()); if (!path.isAbsolute(expanded)) return `${keyFile} is not an absolute path (or ~/…); the credentials file is refused`; const resolved = path.resolve(expanded); const under = allowedDirs().some((d) => resolved === d || resolved.startsWith(d + path.sep)); return under ? null : `${keyFile} is not under the config directory (~/.config, $XDG_CONFIG_HOME) or ARCHILYZER_CREDENTIALS_DIR; the credentials file is refused`; } export function readExitCredentials( exit: Pick, o: { file?: string; env?: Readonly> } = {}, ): ExitCredentialsRead { if (!exit.credentials) return { ok: true, creds: null }; const key = credentialsEnvKey(exit.id); const keyFile = o.file?.trim() || DEFAULT_NETWORK_CREDENTIALS_FILE; const parsed = (value: string, from: string): ExitCredentialsRead => { const c = parseCredentials(value); return "error" in c ? { ok: false, reason: `exit "${exit.id}": ${key} in ${from}: ${c.error}` } : { ok: true, creds: c }; }; const envValue = (o.env ? o.env[key] : networkExitEnvValue(key))?.trim(); if (envValue) return parsed(envValue, "the environment"); const refused = credentialsFileRefusal(keyFile); if (refused) return { ok: false, reason: `${refused}; exit "${exit.id}" needs credentials.` }; let fd: number; try { fd = openSync(expandHome(keyFile), fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0)); } catch { return { ok: false, reason: `exit "${exit.id}" needs credentials: put ${key}=user:password (percent-encoded) in ${keyFile} (mode 600), or set ${key} in the environment`, }; } let text: string; try { const st = fstatSync(fd); if (!st.isFile()) return { ok: false, reason: `${keyFile} is not a regular file; exit "${exit.id}" is off.` }; if (typeof process.getuid === "function" && st.uid !== process.getuid()) { return { ok: false, reason: `${keyFile} is not owned by this user; exit "${exit.id}" is off.` }; } if (st.mode & 0o077) return { ok: false, reason: `${keyFile} is readable by others — chmod 600 it first; exit "${exit.id}" is off until then.` }; text = readFileSync(fd, "utf8"); } catch { return { ok: false, reason: `${keyFile} could not be read; exit "${exit.id}" is off.` }; } finally { closeSync(fd); } for (const raw of text.split("\n")) { const line = raw.trim().replace(/^export\s+/, ""); if (line.startsWith(`${key}=`)) return parsed(unquote(line.slice(key.length + 1)), keyFile); } return { ok: false, reason: `${keyFile} has no ${key}= line; exit "${exit.id}" needs credentials.` }; }