/** * Lines the sandbox tooling itself emits, kept so a launch failure can say * why. Everything else the child writes is provider output and may carry * credentials, so it is counted and discarded as before. * * Without this a sandbox that never started reported only `exit=1`, and the * actual cause — `bwrap: loopback: Failed RTM_NEWADDR: Operation not * permitted` on a host with `kernel.apparmor_restrict_unprivileged_userns=1` * — was invisible. That turned a host restriction into an unexplained * product failure. */ const SANDBOX_TOOL = /^(?:bwrap|prlimit|systemd-run|systemctl):\s.*$/u; /** * Our own launch stages exit 125 before the provider runs: the cgroup gate * (provider-cgroup.ts ENFORCEMENT_GATE) and the scope wrapper * (provider-scope-wrapper.ts). Their lines are matched as whole templates, * not by prefix, so provider output that merely starts the same way is still * discarded. Only the liveness line carries free text, Node's own error for * descriptor 0. */ /** An errno or Node error name, the only free part of the scope wrapper's stop line. */ const ERROR_CODE = String.raw`(?:E[A-Z0-9]{1,15}|ERR_[A-Z0-9_]{1,40})`; const CGROUP_FILE = String.raw`(?:memory\.max|memory\.swap\.max|pids\.max|cpu\.max)`; const CGROUP_VALUE = String.raw`(?:max|[0-9]{1,20})(?: [0-9]{1,20})?`; const LAUNCH_STAGE = [ new RegExp( String.raw`^provider cgroup enforcement failed: (?:unified cgroup v2 membership is required|` + String.raw`unit membership is invalid|cannot read ${CGROUP_FILE}|` + String.raw`${CGROUP_FILE} is (?:${CGROUP_VALUE})?, expected ${CGROUP_VALUE})$`, "u", ), new RegExp( String.raw`^provider scope stopped before launch: (?:supervisor channel ended|` + String.raw`supervisor channel error (?:${ERROR_CODE}|UNKNOWN)|protocol failure)$`, "u", ), /^provider scope (?:received trailing protocol bytes|frame length is invalid|frame has trailing bytes)$/u, /^provider scope liveness channel is unavailable: [\x20-\x7e]{1,200}$/u, ] as const; const MAX_LINES = 8; /** A diagnostic line is short; anything longer is provider output. */ const MAX_LINE_CHARACTERS = 512; export type ProviderSandboxDiagnostics = Readonly<{ consume(stream: "stdout" | "stderr", chunk: Buffer | string): void; lines(): readonly string[]; }>; /** Whether the scope wrapper may name this error code in its stop line. */ export function isProviderScopeErrorCode(code: string): boolean { return new RegExp(`^${ERROR_CODE}$`, "u").test(code); } export function isProviderSandboxDiagnostic(line: string): boolean { return SANDBOX_TOOL.test(line) || LAUNCH_STAGE.some((pattern) => pattern.test(line)); } /** * Reassembles each stream into whole lines before matching, so a line split * across two reads is neither missed nor kept in a truncated form. A line is * only judged once its newline arrives. */ export function createProviderSandboxDiagnostics(): ProviderSandboxDiagnostics { const partial = { stdout: "", stderr: "" }; const overlong = { stdout: false, stderr: false }; const kept: string[] = []; const keep = (line: string): void => { if ( kept.length < MAX_LINES && line.length <= MAX_LINE_CHARACTERS && !kept.includes(line) && isProviderSandboxDiagnostic(line) ) { kept.push(line); } }; return Object.freeze({ consume: (stream: "stdout" | "stderr", chunk: Buffer | string) => { const pieces = String(chunk).split("\n"); const tail = pieces.pop() ?? ""; for (const piece of pieces) { if (!overlong[stream]) keep(partial[stream] + piece); partial[stream] = ""; overlong[stream] = false; } if (overlong[stream]) return; partial[stream] += tail; if (partial[stream].length > MAX_LINE_CHARACTERS) { partial[stream] = ""; overlong[stream] = true; } }, lines: () => Object.freeze([...kept]), }); }