Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/adr/0056-security-audit-hardening.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ and `transferRequestBytes` take `currentChallengeId`.
- Detection gaps left for follow-up (`npm run <name>` 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; `AUDIT_REVISION` is 3): `.`/`source` are not treated as interpreters, bare
check; #86 closed `.`/`source` as interpreters, pipes into an interpreter, pathed wrappers,
`tsx`/`ts-node`/`.ts` operands and `node <dir>`; `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
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 3;
export const AUDIT_REVISION = 4;

/** Run the heuristic rule pipeline over an already-extracted package. */
export function runRules(input: AuditInput): Finding[] {
Expand Down
90 changes: 76 additions & 14 deletions packages/core/src/rules/install-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,35 @@ export const installScriptsRule: Rule = {
// Pieces of a shell word that may hide a path (`${VAR}setup`, `--require=./x.js`, `a\b`).
const WORD_PIECES = /[${}\\=,'"`()<>]+/; // `<>`: `node <setup.js` reads setup.js
const SIMPLE_COMMAND_SPLIT = /&&|\|\||[;|&\n]/;
const PIPELINE_SPLIT = /&&|\|\||[;\n]|(?<!\|)&/; // like SIMPLE_COMMAND_SPLIT, but a single `|` or `|&` stays inside the pipeline

/** Split on `&&`, `||`, `;`, `|`, `&`, newline outside quotes. An unterminated quote falls back to the plain split. */
function splitSimpleCommands(cmd: string): string[] {
/**
* Split on `&&`, `||`, `;`, `|`, `&`, newline outside quotes (`keepPipes`: a single `|` does not split, giving
* pipelines). An unterminated quote falls back to the plain split.
*/
function splitSimpleCommands(cmd: string, keepPipes = false): string[] {
const out: string[] = [];
let start = 0;
let quote = "";
let pipeEnd = -1; // index of the last char of the last unquoted, unescaped `|` / `|&`
for (let i = 0; i < cmd.length; i++) {
const c = cmd[i]!;
if (c === "\\" && quote !== "'") { i++; continue; }
if (quote) { if (c === quote) quote = ""; continue; }
if (c === "'" || c === '"') { quote = c; continue; }
if (c === "|" && keepPipes && cmd[i + 1] !== "|") { if (cmd[i + 1] === "&") i++; pipeEnd = i; continue; }
if (c === "\n" && keepPipes) { // a newline right after a pipe operator continues the pipeline
let j = i - 1;
while (j >= start && (cmd[j] === " " || cmd[j] === "\t")) j--;
if (j === pipeEnd) continue;
}
if (c === ";" || c === "|" || c === "&" || c === "\n") {
out.push(cmd.slice(start, i));
while (cmd[i + 1] === c && c !== ";" && c !== "\n") i++; // `&&`, `||`
start = i + 1;
}
}
if (quote) return cmd.split(SIMPLE_COMMAND_SPLIT);
if (quote) return cmd.split(keepPipes ? PIPELINE_SPLIT : SIMPLE_COMMAND_SPLIT);
out.push(cmd.slice(start));
return out;
}
Expand All @@ -140,18 +151,20 @@ function substitutionBodies(cmd: string): string[] {
}
return bodies;
}
const WRAPPERS = new Set(["env", "exec", "nohup", "time", "command", "npx"]);
const WRAPPERS = new Set(["env", "exec", "nohup", "time", "command", "npx", "sudo"]);
// 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(?:=.*)?)$/;
const NO_INLINE = /(?!)/; // never matches: these interpreters take no inline code
const INLINE_FLAG: Record<string, RegExp> = {
sh: SHELL_INLINE, bash: SHELL_INLINE, zsh: SHELL_INLINE, dash: SHELL_INLINE, ksh: SHELL_INLINE,
node: NODE_INLINE, nodejs: NODE_INLINE, bun: NODE_INLINE, deno: NODE_INLINE,
node: NODE_INLINE, nodejs: NODE_INLINE, bun: NODE_INLINE, deno: NODE_INLINE, tsx: NODE_INLINE, "ts-node": NODE_INLINE,
".": NO_INLINE, source: NO_INLINE, // `. ./setup` / `source setup`: the first operand is the sourced script
python: /^-c$/, python3: /^-c$/, perl: /^-e$/, ruby: /^-e$/, php: /^-r$/,
pwsh: /^-(?:c|command)$/i, powershell: /^-(?:c|command)$/i,
};
// Operands with these extensions are code wherever they appear (`sudo node x.js`, `timeout 5 sh x.sh`).
const CODE_WORD = /\.(?:c?js|mjs|ts|sh|py|pl|rb)\b/i;
const CODE_WORD = /\.(?:c?js|mjs|m?ts|cts|sh|py|pl|rb)\b/i;
// Flags whose value is also executed code (`node -r ./hook main.js`).
const CODE_VALUE_FLAG = /^--?(?:r|require|import|loader|experimental-loader)(?:=(.*))?$/;
const MAX_SUFFIX_DEPTH = 64;
Expand Down Expand Up @@ -204,17 +217,38 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
* A shell never appends `.js` to a command word, so only `script` retries.
*/
type Mode = "exact" | "path" | "script";
// `node <dir>`: the `main` file of <dir>/package.json, then <dir>/index.js. Resolved (or missed) once per directory.
const entries = new Map<string, PackageFile | null>();
const entry = (dir: string): PackageFile | undefined => {
let hit = entries.get(dir);
if (hit === undefined) {
let main: unknown;
try { main = JSON.parse(exact(`${dir}/package.json`)?.content ?? "null")?.main; } catch { /* no usable main */ }
const m = typeof main === "string" ? main.replace(/^\.\//, "") : "";
hit = (m ? exact(`${dir}/${m}`) ?? exact(`${dir}/${m}.js`) ?? exact(`${dir}/${m}/index.js`) : undefined) ?? exact(`${dir}/index.js`) ?? null;
entries.set(dir, hit);
}
return hit ?? undefined;
};
let spill: PackageFile[] = []; // extra candidates a resolution found besides the one it returned
const resolveRef = (raw: string, mode: Mode): PackageFile | undefined => {
const ref = raw.replace(/^\.\//, "");
const ref = raw.replace(/^\.\//, "").replace(/\/$/, "") || (raw === "./" ? "." : "");
if (!ref) return undefined;
if (ref === "." && mode === "script") return entry("package");
let hit = exact(`package/${ref}`);
if (hit || mode === "exact") return hit;
if (mode === "path") return suffix(ref);
const dotted = ref.slice(ref.lastIndexOf("/") + 1).includes(".");
// Node resolves `node setup` / `node scripts` as setup.js / scripts/index.js.
if (!dotted) hit = exact(`package/${ref}.js`) ?? exact(`package/${ref}/index.js`);
// After a `cd scripts &&` the file sits deeper than `package/<ref>`.
return hit ?? suffix(ref) ?? (dotted ? undefined : suffix(`${ref}.js`));
const nested = () => suffix(ref) ?? (dotted ? undefined : suffix(`${ref}.js`) ?? suffix(`${ref}.ts`));
// Node resolves `node setup` / `node scripts` as setup.js / scripts/<main or index.js>; TS runners also try setup.ts.
if (!dotted) hit = exact(`package/${ref}.js`);
hit ??= entry(`package/${ref}`); // a directory may have a dot in its name (`tools.v2`)
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);
return hit;
};
const resolveWord = (word: string, mode: Mode): PackageFile | undefined => {
const w = trimPunct(word);
Expand All @@ -226,8 +260,23 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
}
return undefined;
};
// 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 => {
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 isPrefix = (words: string[], i: number) =>
/^[A-Za-z_]\w*=/.test(words[i]!) || WRAPPERS.has(words[i]!) || (i > 0 && WRAPPERS.has(words[i - 1]!) && words[i]!.startsWith("-"));
/^[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("-"));
const isInterpreterCommand = (simple: string): boolean => {
const ws = simple.split(/\s+/).filter(Boolean).map(unquote);
let k = 0;
while (k < ws.length && isPrefix(ws, k)) k++;
return ws[k] !== undefined && Object.hasOwn(INLINE_FLAG, base(trimPunct(ws[k]!)));
};

// Package scripts reached from a command line, scanned from a worklist (not recursion): each name once, so
// the work is bounded by the number of scripts and a long chain cannot overflow the stack.
Expand Down Expand Up @@ -268,7 +317,12 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
// Quote tracking can only ADD coverage: a stray apostrophe (`# it's`) must not hide later commands,
// so the plain split is scanned too, and `$(...)` / backtick bodies are scanned as command lines.
if (depth < MAX_INLINE_DEPTH) for (const body of substitutionBodies(cmd)) scan(body, out, depth + 1);
for (const simple of new Set([...splitSimpleCommands(cmd), ...cmd.split(SIMPLE_COMMAND_SPLIT)])) {
for (const pipeline of new Set([...splitSimpleCommands(cmd, true), ...cmd.split(PIPELINE_SPLIT)])) {
const quoted = splitSimpleCommands(pipeline); // quote-aware, so `php -r 'a;b' README` is one stage
const stages = new Set([...quoted, ...pipeline.split(SIMPLE_COMMAND_SPLIT)]);
// Over-follow: the operands of every stage upstream of an interpreter stage (`cat x | sh`) are scripts.
const upstream = new Set(quoted.slice(0, Math.max(0, quoted.map(isInterpreterCommand).lastIndexOf(true))));
for (const simple of stages) {
const rawWords = simple.split(/\s+/).filter(Boolean);
const words = rawWords.map(unquote);
// Indexes of words ending in each quote char, built once per command so repeated inline flags
Expand All @@ -282,6 +336,8 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
};
let i = 0;
for (; i < words.length && isPrefix(words, i); i++) {
const local = localCommand(words[i]!);
if (local) out.add(local);
// `NODE_OPTIONS=--require=./hook.js node -v`: a code file inside an assignment value runs too.
const eq = words[i]!.indexOf("=");
if (eq < 0) continue;
Expand All @@ -293,7 +349,9 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
const command = words[i];
if (!command) continue;
const name = trimPunct(command);
const interpreter = name.slice(name.lastIndexOf("/") + 1);
const interpreter = base(name);
const local = localCommand(command);
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;
// The command word itself may be a package script (`./setup`).
Expand Down Expand Up @@ -355,27 +413,31 @@ function makeResolver(files: PackageFile[], scripts: Record<string, string>): (c
continue;
}
const isCode = CODE_WORD.test(w);
if (!isCode && (!inline || found)) continue;
if (!isCode && !(upstream.has(simple) && !inline) && (!inline || found)) continue;
const hit = resolveWord(w, "script");
if (hit) {
out.add(hit);
if (inline) found = true;
}
}
}
}
};

return (cmd) => {
const out = new Set<PackageFile>();
visited = new Set();
queue = [];
spill = [];
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);
return { files: [...out], scripts: queue };
};
}

const MAX_INLINE_DEPTH = 3;
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).
const INSTALL_SCRIPTS = ["preinstall", "install", "postinstall", "prepublish", "prepare"];
Expand Down
Loading
Loading