diff --git a/docs/adr/0056-security-audit-hardening.md b/docs/adr/0056-security-audit-hardening.md index 4a69566..5f371b1 100644 --- a/docs/adr/0056-security-audit-hardening.md +++ b/docs/adr/0056-security-audit-hardening.md @@ -286,7 +286,8 @@ and `transferRequestBytes` take `currentChallengeId`. check; #86 closed `.`/`source` as interpreters, pipes into an interpreter, pathed wrappers, `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 + tarball only; `AUDIT_REVISION` is 6). #88 closed the walker overflow: the loader-chain walk is iterative (4,194,304-node safety bound, + 1,000 nested scopes; a truncation takes the strict fallback), so the loose overflow fallback serves only parser or walk stack overflows. 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 4f8b821..e699872 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 = 5; +export const AUDIT_REVISION = 6; /** Run the heuristic rule pipeline over an already-extracted package. */ export function runRules(input: AuditInput): Finding[] { diff --git a/packages/core/src/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index 715630f..f7d213d 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -1,5 +1,5 @@ import * as acorn from "acorn"; -import * as walk from "acorn-walk"; +import { base as walkBase } from "acorn-walk"; import { lineLocator } from "../rules/util.js"; export interface LoaderPrimitive { @@ -15,7 +15,10 @@ export interface LoaderPrimitive { export interface LoaderBoosters { tempOrHidden: boolean; chmod: boolean; detached: boolean; unref: boolean; moduleLoad: boolean } export interface LoaderAnalysis { primitives: LoaderPrimitive[]; correlated: boolean; boosters: LoaderBoosters; parseFailed: boolean; /** Stack overflow while parsing or walking (hostile nesting), not a syntax error: the caller must not downgrade a regex chain hit. */ - walkOverflow?: boolean } + walkOverflow?: boolean; + /** The walk hit its node or scope bound: unreachable for real input, so it is a safety stop, not a stack overflow. The + * caller uses the STRICT fallback (like a parse failure), never the loose overflow one. */ + walkTruncated?: boolean } /** Bounded, deterministic taint tags a variable can carry. */ type Tag = "read" | "decoded" | "written-path"; @@ -61,9 +64,66 @@ function snip(src: string, node: any): string { function isScope(n: any): boolean { return n.type === "Program" || n.type === "FunctionDeclaration" || n.type === "FunctionExpression" || n.type === "ArrowFunctionExpression"; } -/** Scope chain innermost → outermost (Program) from an acorn-walk ancestor list. */ -function scopeChain(ancestors: any[]): any[] { - return ancestors.filter(isScope).reverse(); + +/** + * Safety bounds of the walk. A retained file is at most 2 MiB and yields at most about one node per byte, so the node + * bound is unreachable for real input. Each callback costs O(scope depth), so scopes nested deeper than the scope + * bound are merged into the deepest tracked one (the walk goes on) rather than tracked. + */ +const MAX_WALK_NODES = 4_194_304; +const MAX_SCOPE_DEPTH = 1000; +class WalkBoundError extends Error {} + +/** Hostile nesting defeated the parser or the walk: a RangeError, or acorn's own "Not enough stack space" SyntaxError. A genuine syntax error is neither. */ +function isStackOverflow(e: unknown): boolean { + return e instanceof RangeError || (e instanceof SyntaxError && e.message.startsWith("Not enough stack space")); +} + +/** `merged`: the node sits inside a function nested past MAX_SCOPE_DEPTH, whose bindings are UNION-ONLY (never replace or kill). */ +type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[], merged: boolean) => void; + +/** + * acorn-walk's `ancestor` without recursion: the same nodes, in the same order (a node's visitor runs after its + * children), with the same ancestor list. acorn-walk's base visitors only call back for children, so each node's + * children are collected first and then walked from an explicit stack. The visitor also receives the scope chain + * (innermost → outermost), kept incrementally so a deep expression does not make every callback O(depth). + * Deterministic bounds: MAX_WALK_NODES throws a WalkBoundError; past MAX_SCOPE_DEPTH scopes merge instead of aborting. + */ +export function walkAncestor(root: any, visitors: Record): void { + const ancestors: any[] = []; + const scopes: any[] = []; // outermost → innermost + let merged = 0; // functions currently open beyond the scope bound + // A frame holds its node's child list and an index into it: only the active path is held, never every child at once. + type Frame = { node: any; type: string; isNew: boolean; list: any[]; ov: string | (string | undefined)[] | undefined; next: number }; + const stack: Frame[] = []; + let visited = 0; + const enter = (node: any, override?: string): void => { + const type = override || node.type; + const isNew = node !== ancestors[ancestors.length - 1]; // an override re-enters the same node + if (isNew) { + if (++visited > MAX_WALK_NODES) throw new WalkBoundError("loader-chain: node bound exceeded"); + ancestors.push(node); + // Past MAX_SCOPE_DEPTH a deeper function joins the deepest tracked scope (over-approximation: taint is never lost). + if (isScope(node)) { if (scopes.length < MAX_SCOPE_DEPTH) scopes.push(node); else merged++; } + } + const visit = (walkBase as Record)[type]; + if (visit == null) throw new Error(`No walker function defined for node type ${type}`); + // acorn-walk walks these lists in order: every statement of `body` ("Statement"), every non-hole array element. + if (type === "Program" || type === "BlockStatement" || type === "StaticBlock") { stack.push({ node, type, isNew, list: node.body, ov: "Statement", next: 0 }); return; } + if (type === "ArrayExpression" || type === "ArrayPattern") { stack.push({ node, type, isNew, list: node.elements, ov: type === "ArrayExpression" ? "Expression" : "Pattern", next: 0 }); return; } + const list: any[] = []; + const ov: (string | undefined)[] = []; + visit(node, undefined, (child: any, _st: any, o?: string) => { list.push(child); ov.push(o); }); + stack.push({ node, type, isNew, list, ov, next: 0 }); + }; + enter(root); + while (stack.length) { + const f = stack[stack.length - 1]!; + if (f.next < f.list.length) { const i = f.next++; if (f.list[i]) enter(f.list[i], typeof f.ov === "string" ? f.ov : f.ov?.[i]); continue; } // holes are skipped + visitors[f.type]?.(f.node, ancestors, ancestors, scopes.slice().reverse(), merged > 0); + if (f.isNew) { ancestors.pop(); if (scopes[scopes.length - 1] === f.node) scopes.pop(); else if (isScope(f.node)) merged--; } + stack.pop(); + } } /** @@ -78,11 +138,12 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: let ast: any; try { ast = acorn.parse(source, { ecmaVersion: "latest", sourceType: "module", allowReturnOutsideFunction: true, allowAwaitOutsideFunction: true, allowHashBang: true }); - } catch { + } catch (moduleError) { try { ast = acorn.parse(source, { ecmaVersion: "latest", sourceType: "script", allowReturnOutsideFunction: true, allowHashBang: true }); } catch (e) { - return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, walkOverflow: e instanceof RangeError }; + // Either attempt may have exhausted the stack (a module file with deep nesting then also fails the script retry on `export`). + return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, walkOverflow: isStackOverflow(moduleError) || isStackOverflow(e) }; } } @@ -106,9 +167,23 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: if (!m) { m = new Map(); taint.set(s, m); } return m; }; + // Functions nested past the scope bound are "may" scopes: their declarations and assignments never touch the tracked + // scopes. They only add to this name → tags overlay, which is never a shadowing entry and may outlive the function. + const mergedTaint = new Map>(); + const addMerged = (name: string, tags: Iterable): void => { + const cur = mergedTaint.get(name) ?? mergedTaint.set(name, new Set()).get(name)!; + for (const t of tags) cur.add(t); + }; + /** Exact scopes REPLACE a binding's taint (kill-on-reassign); a merged scope only adds to the overlay. */ + const putTaint = (scope: any, name: string, tags: Set, merged: boolean): void => { + if (merged) addMerged(name, tags); else scopeTaint(scope).set(name, tags); + }; + /** Lexical resolution unioned with the merged overlay for that name. */ const lookupTaint = (chain: any[], name: string): Set | undefined => { - for (const s of chain) { const t = taint.get(s)?.get(name); if (t) return t; } - return undefined; + let found: Set | undefined; + for (const s of chain) { const t = taint.get(s)?.get(name); if (t) { found = t; break; } } + const o = mergedTaint.get(name); + return !o ? found : !found ? o : new Set([...found, ...o]); }; const declaringScope = (chain: any[], name: string): any => { for (const s of chain) { if (taint.get(s)?.has(name)) return s; } @@ -341,7 +416,7 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: function isTempOrHidden(node: any): boolean { const resolved = node?.type === "Identifier" ? (declInit.get(node.name) ?? node) : node; if (!resolved) return false; - const txt = source.slice(resolved.start, resolved.end); + const txt = source.slice(resolved.start, Math.min(resolved.end, resolved.start + SNIP_SOURCE_CAP)); return /tmpdir\s*\(\s*\)|['"`]\/tmp\b|['"`]\.[A-Za-z0-9_]/.test(txt) || /\.tmpdir/.test(txt); } @@ -367,10 +442,10 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: } } - // A hostile file (50k chained members, absurd nesting) can overflow the walker's - // recursion; that must degrade to the parse-failure path, never throw out of the rule. + // The walk is iterative, so a deep expression costs time, not a stack overflow. Exceeding the node or scope bound + // (a RangeError) degrades to the parse-failure path, never throws out of the rule. try { - walk.ancestor(ast, { + walkAncestor(ast, { ImportDeclaration(node: any) { if (node.source?.type !== "Literal" || typeof node.source.value !== "string") return; const mod = stripNode(node.source.value); @@ -383,26 +458,23 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: } } }, - VariableDeclarator(node: any, _st: any, ancestors: any[]) { + VariableDeclarator(node: any, _st: any, _ancestors: any[], chain: any[], merged: boolean) { if (!node.init) { if (node.id?.type === "Identifier") declInit.delete(node.id.name); return; } bindFromInit(node.id, node.init); if (node.id?.type !== "Identifier") return; const name = node.id.name; declInit.set(name, node.init); - const chain = scopeChain(ancestors); const relevant = new Set([...payloadTags(node.init, chain)].filter((t) => t === "read" || t === "decoded")); - scopeTaint(chain[0]).set(name, relevant); // declaration REPLACES + putTaint(chain[0], name, relevant, merged); // declaration REPLACES (UNION in a merged scope) }, - AssignmentExpression(node: any, _st: any, ancestors: any[]) { + AssignmentExpression(node: any, _st: any, _ancestors: any[], chain: any[], merged: boolean) { if (node.operator !== "=" || node.left.type !== "Identifier") return; const name = node.left.name; declInit.set(name, node.right); - const chain = scopeChain(ancestors); const relevant = new Set([...payloadTags(node.right, chain)].filter((t) => t === "read" || t === "decoded")); - scopeTaint(declaringScope(chain, name)).set(name, relevant); // reassignment REPLACES (kill-on-reassign, S2) + putTaint(declaringScope(chain, name), name, relevant, merged); // reassignment REPLACES (kill-on-reassign, S2; UNION in a merged scope) }, - CallExpression(node: any, _st: any, ancestors: any[]) { - const chain = scopeChain(ancestors); + CallExpression(node: any, _st: any, _ancestors: any[], chain: any[], merged: boolean) { const cat = classifyCall(node); const args = node.arguments ?? []; @@ -424,7 +496,8 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: if (pathArg && decodeInScope && carriesPayload(payloadTags(dataArg, chain))) { if (pathArg.type === "Identifier") { const s = declaringScope(chain, pathArg.name); - (scopeTaint(s).get(pathArg.name) ?? scopeTaint(s).set(pathArg.name, new Set()).get(pathArg.name)!).add("written-path"); + if (merged) addMerged(pathArg.name, ["written-path"]); + else (scopeTaint(s).get(pathArg.name) ?? scopeTaint(s).set(pathArg.name, new Set()).get(pathArg.name)!).add("written-path"); } else { const key = structKey(pathArg); if (key !== null) (writtenPaths.get(key) ?? writtenPaths.set(key, new Set()).get(key)!).add(chain[0]); @@ -477,7 +550,9 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: }); boosters.moduleLoad = Boolean(opts.moduleLoadReachable) && topLevelExecution(ast); } catch (e) { - return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, walkOverflow: e instanceof RangeError }; + const truncated = e instanceof WalkBoundError; + return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, + walkOverflow: !truncated && isStackOverflow(e), ...(truncated ? { walkTruncated: true } : {}) }; } return { primitives, correlated, boosters, parseFailed: false }; diff --git a/packages/core/src/rules/native-payload-loader.ts b/packages/core/src/rules/native-payload-loader.ts index 8fca865..81cc7d3 100644 --- a/packages/core/src/rules/native-payload-loader.ts +++ b/packages/core/src/rules/native-payload-loader.ts @@ -31,7 +31,7 @@ const BASE64 = /['"]base64(?:url)?['"]/gi; */ const LOOSE: Record<"read" | "decode" | "write" | "launch", RegExp> = { read: /\b(?:readFileSync|readFile|createReadStream)\b/, - decode: /\b(?:gunzipSync|gunzip|inflateSync|inflate|inflateRaw(?:Sync)?|unzipSync|unzip|brotliDecompress(?:Sync)?)\b/, + decode: /\b(?:gunzipSync|gunzip|inflateSync|inflate|inflateRaw(?:Sync)?|unzipSync|unzip|brotliDecompress(?:Sync)?)\b|\batob\s*\(/, write: /\b(?:writeFileSync|writeFile|createWriteStream)\b/, launch: /\b(?:spawn|spawnSync|exec|execSync|execFile|execFileSync)\b|process\.dlopen/, }; diff --git a/packages/core/test/security-iterative-walk.test.ts b/packages/core/test/security-iterative-walk.test.ts new file mode 100644 index 0000000..36a8ac7 --- /dev/null +++ b/packages/core/test/security-iterative-walk.test.ts @@ -0,0 +1,266 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { describe, test } from "node:test"; +import * as acorn from "acorn"; +import * as walk from "acorn-walk"; +import { runRules } from "../src/index.js"; +import * as loaderChain from "../src/detect/loader-chain.js"; +import type { AuditInput, PackageFile } from "../src/types.js"; + +// The loader-chain walk is iterative: a deep expression costs time, not a stack overflow, so the full dataflow +// analysis runs and the regex fallback is only for genuine parse failures and the node bound. + +const PAD = (n: number) => `try { x${".b".repeat(n)}; } catch (e) {}\n`; + +/** A correlated chain in a form only the dataflow analysis recognises: renamed bindings, a decode (`atob`) the regex + * fallback has no pattern for, and the chain routed through a helper function. */ +const CHAIN = ` +const { readFileSync: rd, writeFileSync: wr } = require("fs"); +const { spawnSync: go } = require("child_process"); +const p = require("path"); +function stage() { + const blob = rd(p.join(__dirname, "intro.js")); + const raw = atob(blob.toString()); + const dst = p.join(require("os").tmpdir(), ".cache-x"); + wr(dst, raw); + go(dst); +} +stage(); +`; +/** Mentions all four primitive names, in places that are not a chain. */ +const BENIGN = ` +const api = { readFileSync() {}, gunzipSync() {}, writeFileSync() {}, spawn() {} }; +console.log("readFileSync gunzipSync writeFileSync spawn"); +function unrelated(x) { return x.readFileSync + x.gunzipSync + x.writeFileSync + x.spawn; } +`; + +function file(path: string, content: string): PackageFile { + return { path, content, size: Buffer.byteLength(content), changed: false }; +} +function input(js: string): AuditInput { + const files = [file("package/package.json", '{"name":"probe","version":"1.0.0"}'), file("package/index.js", js)]; + return { + meta: { + name: "probe", version: "1.0.0", author: null, maintainers: [], license: null, + hasInstallScripts: false, signature: "unknown", provenance: "unknown", integrity: null, + unpackedSize: 0, fileCount: files.length, + }, + files, + mode: "full", + }; +} +const loaderSeverities = (js: string) => runRules(input(js)).filter((f) => f.ruleId === "native-payload-loader").map((f) => f.severity); + +describe("a deep expression no longer defeats the dataflow analysis", () => { + test("the unpadded chain is correlated (baseline)", () => { + const a = loaderChain.analyzeLoaderChain(CHAIN); + assert.equal(a.correlated, true); + assert.deepEqual(loaderSeverities(CHAIN), ["critical"]); + }); + + test("a 60k-member pad does not stop the full analysis: still critical, walkOverflow false", () => { + const src = PAD(60000) + CHAIN; + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, false); + assert.equal(a.walkOverflow, undefined); + assert.equal(a.correlated, true); + assert.deepEqual(loaderSeverities(src), ["critical"]); + }); + + test("the pad may come after the chain, or sit inside a function", () => { + assert.deepEqual(loaderSeverities(CHAIN + PAD(60000)), ["critical"]); + assert.deepEqual(loaderSeverities(`function pad() {\n${PAD(60000)}}\n${CHAIN}`), ["critical"]); + }); + + test("other deep shapes the parser accepts: chained calls and index chains", () => { + for (const pad of [`f${"()".repeat(60000)};\n`, `a${"[0]".repeat(60000)};\n`]) { + const a = loaderChain.analyzeLoaderChain(pad + CHAIN); + assert.equal(a.walkOverflow, undefined, pad.slice(0, 12)); + assert.equal(a.correlated, true, pad.slice(0, 12)); + } + }); + + test("a parser stack overflow takes the overflow path; a genuine syntax error does not", () => { + // acorn raises a SyntaxError ("Not enough stack space") for deep parens, arrays and binary chains. + for (const pad of [`${"(".repeat(60000)}x${")".repeat(60000)};\n`, `${"[".repeat(60000)}${"]".repeat(60000)};\n`, `1${"+1".repeat(60000)};\n`]) { + const a = loaderChain.analyzeLoaderChain(pad + CHAIN); + assert.equal(a.parseFailed, true, pad.slice(0, 12)); + assert.equal(a.walkOverflow, true, pad.slice(0, 12)); + } + assert.notEqual(loaderChain.analyzeLoaderChain("const x = ;\n" + "(".repeat(5000)).walkOverflow, true); + }); + + test("a chain padded with deeply nested parens stays critical (loose fallback), also with renamed primitives", () => { + const pad = `${"(".repeat(100_000)}x${")".repeat(100_000)};\n`; // overflows acorn's parser on any supported Node + const RENAMED_ZLIB = ` +const { readFileSync: r, writeFileSync: w } = require("fs"), { gunzipSync: g } = require("zlib"), { spawn: go } = require("child_process"); +const path = require("path"), os = require("os"); +const o = path.join(os.tmpdir(), ".x"); +w(o, g(r(path.join(__dirname, "intro.js")))); +go(o, [], { detached: true }); +`; + assert.deepEqual(loaderSeverities(RENAMED_ZLIB), ["critical"]); // control + assert.equal(loaderChain.analyzeLoaderChain(pad + RENAMED_ZLIB).walkOverflow, true); + assert.deepEqual(loaderSeverities(pad + RENAMED_ZLIB), ["critical"]); + // and a benign file that only mentions one name is still not critical + assert.deepEqual(loaderSeverities(`${pad}const {readFileSync:r}=require("fs");module.exports=()=>r("a");`), []); + }); + + test("an atob chain padded to overflow the parser is critical (atob is a decode signal in the loose fallback)", () => { + const pad = `${"(".repeat(100_000)}x${")".repeat(100_000)};\n`; + assert.deepEqual(loaderSeverities(pad + CHAIN), ["critical"]); + }); + + test("a module-syntax file whose module parse overflows keeps the overflow classification across the script retry", () => { + const pad = `${"(".repeat(100_000)}x${")".repeat(100_000)};\n`; + const src = `export const marker = 1;\n${pad}${CHAIN}`; // `export` also fails the script retry + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, true); + assert.equal(a.walkOverflow, true); + assert.deepEqual(loaderSeverities(src), ["critical"]); + }); + + test("a same-named inner local past the scope bound does not kill the enclosing binding's taint", (t) => { + const encl = (innerDecl: string) => ` +const { readFileSync: rd, writeFileSync: wr } = require("fs"); +const { spawnSync: go } = require("child_process"); +const { gunzipSync: gz } = require("zlib"); +const p = require("path"); +const blob = rd(p.join(__dirname, "intro.js")); +const raw = gz(blob); +const out = p.join(require("os").tmpdir(), ".cache-x"); +wr(out, raw); +${innerDecl} +go(out); +`; + const nested = (depth: number, body: string) => `${"function f(){".repeat(depth)}${body}${"}".repeat(depth)}\n`; + try { acorn.parse(nested(1004, ""), { ecmaVersion: "latest", sourceType: "script" }); } catch { t.skip("the parser cannot nest this deep on this Node"); return; } + // the enclosing function sits just inside, at and past the scope bound; an inner function beyond it redeclares `out` + for (let depth = 996; depth <= 1003; depth++) { + for (const inner of ['function inner() { const out = "benign"; }', 'function inner() { var out = "benign"; out = "x"; }']) { + const src = nested(depth, encl(inner)); + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, false, `depth ${depth}`); + assert.equal(a.correlated, true, `depth ${depth}: ${inner}`); + } + } + assert.deepEqual(loaderSeverities(nested(999, encl('function inner() { const out = "benign"; }'))), ["critical"]); + // within the bound exact semantics are kept: a reassignment in the same scope still kills the taint (kill-on-reassign) + const killed = encl("").replace("go(out);", 'out = "benign";\ngo(out);').replace("const out =", "let out ="); + assert.equal(loaderChain.analyzeLoaderChain(killed).correlated, false); + assert.equal(loaderChain.analyzeLoaderChain(nested(50, killed)).correlated, false); + }); + + test("a merged declaration never hides an outer tracked binding, and leaves nothing that does", (t) => { + // `out` is written in an outer tracked function F (depth 5). Below it, functions nest to the scope bound, so the + // deepest tracked scope D lies between F's body and the merged function that redeclares `out`. The launch of `out` + // sits in D after the merged function: a merged declaration must not leave an entry in D that hides F's `out`. + const head = ` +const { readFileSync: rd, writeFileSync: wr } = require("fs"); +const { spawnSync: go } = require("child_process"); +const { gunzipSync: gz } = require("zlib"); +const p = require("path"); +`; + const write = `const blob = rd(p.join(__dirname, "intro.js")); const out = p.join(require("os").tmpdir(), ".cache-x"); wr(out, gz(blob));\n`; + const open = (n: number) => "function f(){".repeat(n); + const close = (n: number) => "}".repeat(n); + try { acorn.parse(open(1004) + close(1004), { ecmaVersion: "latest", sourceType: "script" }); } catch { t.skip("the parser cannot nest this deep on this Node"); return; } + for (const decl of ["let out;", 'const out = "x";', 'var out = "x"; out = "y";']) { + for (const total of [999, 1000, 1002]) { // the `total`-th function D (scopes are tracked up to the 999th, Program being the first) + // F is the 5th function; D is the `total`-th function (the deepest tracked one at 999); the merged function (depth total + 1) redeclares `out`. + const src = head + open(4) + `function F(){${write}${open(total - 6)}function D(){\n` + + `function merged(){ ${decl} }\ngo(out);\n}${close(total - 6)}}\n${close(4)}`; + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, false, `${total} ${decl}`); + assert.equal(a.correlated, true, `${total}: ${decl}`); + } + } + }); + + test("a padded benign file that only mentions the four names is not critical", () => { + assert.equal(loaderSeverities(PAD(60000) + BENIGN).includes("critical"), false); + assert.deepEqual(loaderSeverities(PAD(60000) + BENIGN), []); + assert.equal(loaderChain.analyzeLoaderChain(PAD(60000) + BENIGN).walkOverflow, undefined); + }); + + test("a 2 MiB padded file finishes within a generous bound", () => { + const src = PAD(1_050_000) + CHAIN; + assert.ok(src.length > 2 * 1024 * 1024 - 1024); + const t0 = performance.now(); + const a = loaderChain.analyzeLoaderChain(src); + const ms = performance.now() - t0; + assert.equal(a.correlated, true); + assert.ok(ms < 20_000, `took ${ms} ms`); + }); + + test("crossing the node cap is a truncation with the strict fallback, never the loose overflow one", () => { + const flat = ";".repeat(4_194_305); // one node per byte: just over the cap (a retained file is at most 2 MiB) + const a = loaderChain.analyzeLoaderChain(flat + CHAIN); + assert.equal(a.parseFailed, true); + assert.equal(a.walkTruncated, true); + assert.notEqual(a.walkOverflow, true); + // a benign flat file that crosses the cap is not critical, even though it mentions all four names + assert.equal(loaderSeverities(flat + BENIGN).includes("critical"), false); + assert.equal(loaderSeverities(flat + CHAIN).includes("critical"), false); + // the strict fallback still runs: strict read and write signals give exactly "high" (an empty result would mean it was skipped) + const strict = `const fs = require("fs");\nfs.writeFileSync("o", fs.readFileSync("i"));\n`; + assert.deepEqual(loaderSeverities(flat + strict), ["high"]); + }); + + test("the scope bound never aborts the walk: a chain inside or after 1,001 nested functions stays critical", (t) => { + const deep = (inner: string) => `${"function f(){".repeat(1001)}${inner}${"}".repeat(1001)}\n`; + // 1,001 nested scopes need a parser stack that can hold them (acorn's own limit is near 1,250 on a default Node stack) + try { acorn.parse(deep(""), { ecmaVersion: "latest", sourceType: "script" }); } catch { t.skip("the parser cannot nest 1,001 functions on this Node"); return; } + for (const src of [deep(CHAIN), deep("") + CHAIN, deep(CHAIN) + CHAIN]) { + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, false); + assert.equal(a.walkTruncated, undefined); + assert.equal(a.correlated, true); + } + assert.deepEqual(loaderSeverities(deep(CHAIN)), ["critical"]); + }); + + test("genuine parse failures still use the parse-failure path", () => { + const a = loaderChain.analyzeLoaderChain("const = ;"); + assert.equal(a.parseFailed, true); + assert.notEqual(a.walkOverflow, true); + }); +}); + +describe("the iterative walk visits what acorn-walk's ancestor visits", () => { + const SAMPLE = ` + import fs, { a as b } from "fs"; export const q = 1; export default function () {} + class A extends B { static #x = 1; get y() { return super.y } static { init() } constructor(...r) { super(...r) } } + async function* g(a = 1, { b, ...c }, [d, , e]) { for await (const z of a) yield* z; label: for (;;) { break label } } + const t = tag\`a\${1}b\`, o = { a, [b]: c, ...d, get e() { return 1 }, f() {} }, r = /x/g, n = 10n; + try { a?.b?.[c]?.(d) } catch { } finally { import.meta; } function nt() { return new.target; } + switch (x) { case 1: y(); default: z(); } do { w() } while (v); if (a) b; else c; + for (var i in o) ; for (const k of o) ; with_ = (a, b) => a ?? b; x ||= 1; delete a.b; void 0; typeof a; + (function () { "use strict"; return this })(); a = b ? c : d; throw new Error(\`e\`); + const arr = [1, , 2, ...r, [3, , 4]]; const [p2, , [q2, , ...s2]] = arr; + `; + const trace = (run: (visitors: Record) => void) => { + const out: string[] = []; + const types = ["Program", "Identifier", "Literal", "CallExpression", "MemberExpression", "VariableDeclarator", "AssignmentExpression", + "FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "ClassBody", "PropertyDefinition", "TemplateLiteral", "ImportDeclaration", + "ArrayExpression", "ForOfStatement", "TryStatement", "SwitchStatement", "ObjectExpression", "ArrayPattern", "ObjectPattern", "BinaryExpression", "ChainExpression", + "ExpressionStatement", "ReturnStatement", "StaticBlock", "MetaProperty", "NewExpression", "Property", "SpreadElement", "RestElement", "AssignmentPattern"]; + const v: Record = {}; + for (const ty of types) v[ty] = (n: any, _st: any, anc: any[]) => out.push(`${ty}@${n.start}-${n.end}/${anc.map((a) => a.type).join(">")}`); + run(v); + return out; + }; + const sources: [string, string, "module" | "script"][] = [["inline sample", SAMPLE, "module"]]; + for (const rel of ["../../../node_modules/acorn/dist/acorn.js", "../../../node_modules/acorn-walk/dist/walk.js"]) { + try { sources.push([rel.split("/").pop()!, readFileSync(new URL(rel, import.meta.url), "utf8"), "script"]); } catch { /* optional corpus file */ } + } + for (const [label, src, sourceType] of sources) { + test(`same visits, order and ancestors: ${label}`, () => { + const ast = acorn.parse(src, { ecmaVersion: "latest", sourceType, allowHashBang: true, allowReturnOutsideFunction: true }); + const expected = trace((v) => walk.ancestor(ast, v)); + const actual = trace((v) => (loaderChain as any).walkAncestor(ast, v)); + assert.ok(expected.length > 50); + assert.deepEqual(actual, expected); + }); + } +}); diff --git a/packages/core/test/security-rule-robustness-10.test.ts b/packages/core/test/security-rule-robustness-10.test.ts index 2b05864..7ecc746 100644 --- a/packages/core/test/security-rule-robustness-10.test.ts +++ b/packages/core/test/security-rule-robustness-10.test.ts @@ -65,7 +65,7 @@ describe("the suffix fallback does not match bare command words or module specif }); describe("overflow decode signal needs a whole identifier", () => { - const PAD = `try{x${".b".repeat(60000)}}catch(e){}\n`; + const PAD = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const loader = (src: string) => runRules(inputFor({ "package/index.js": src }, { name: "p", main: "index.js" })).filter((f) => f.ruleId === "native-payload-loader"); diff --git a/packages/core/test/security-rule-robustness-3.test.ts b/packages/core/test/security-rule-robustness-3.test.ts index ba50900..85b35ed 100644 --- a/packages/core/test/security-rule-robustness-3.test.ts +++ b/packages/core/test/security-rule-robustness-3.test.ts @@ -116,7 +116,7 @@ const fs=require('fs'),zlib=require('zlib'),cp=require('child_process'),os=requi })();`; test("`try{x.b.b...}catch{}` prepended to the chain stays critical and blocks", async () => { - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const tarball = tarBytes([ { name: "package/package.json", data: JSON.stringify({ name: "demo", version: "1.0.0", main: "index.js" }) }, { name: "package/index.js", data: pad + CORRELATED }, diff --git a/packages/core/test/security-rule-robustness-4.test.ts b/packages/core/test/security-rule-robustness-4.test.ts index 57298d9..a4dcd9e 100644 --- a/packages/core/test/security-rule-robustness-4.test.ts +++ b/packages/core/test/security-rule-robustness-4.test.ts @@ -111,7 +111,7 @@ const b=zlib.gunzipSync(c); const out=path.join(os.tmpdir(),'.x.node'); fs.writeFileSync(out,b); require(out);`; - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const tarball = tarBytes([ { name: "package/package.json", data: JSON.stringify({ name: "demo", version: "1.0.0", main: "index.js" }) }, { name: "package/index.js", data: pad + chain }, diff --git a/packages/core/test/security-rule-robustness-5.test.ts b/packages/core/test/security-rule-robustness-5.test.ts index 7cd329e..81b5c94 100644 --- a/packages/core/test/security-rule-robustness-5.test.ts +++ b/packages/core/test/security-rule-robustness-5.test.ts @@ -135,7 +135,7 @@ const c=fs.readFileSync(__dirname+'/intro.node'); const b=zlib.gunzipSync(c); fs.writeFileSync(dir+'/p',b); require(\`\${dir}/p\`);`; - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const tarball = tarBytes([ { name: "package/package.json", data: JSON.stringify({ name: "demo", version: "1.0.0", main: "index.js" }) }, { name: "package/index.js", data: pad + chain }, diff --git a/packages/core/test/security-rule-robustness-6.test.ts b/packages/core/test/security-rule-robustness-6.test.ts index 09044fc..7197747 100644 --- a/packages/core/test/security-rule-robustness-6.test.ts +++ b/packages/core/test/security-rule-robustness-6.test.ts @@ -90,7 +90,7 @@ const fs=require('fs'),zlib=require('zlib'),path=require('path'); const c=fs.readFileSync(path.join(__dirname,'intro.bin')); fs.writeFileSync(path.join(__dirname,'output.node'),zlib.gunzipSync(c)); require('./output.node');`; - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const report = await auditTarball({ meta, tarball: tarBytes([ diff --git a/packages/core/test/security-rule-robustness-7.test.ts b/packages/core/test/security-rule-robustness-7.test.ts index 04644f2..1781e1c 100644 --- a/packages/core/test/security-rule-robustness-7.test.ts +++ b/packages/core/test/security-rule-robustness-7.test.ts @@ -116,7 +116,7 @@ const fs=require('fs'),zlib=require('zlib'),path=require('path'); const c=fs.readFileSync(path.join(__dirname,'intro.bin')); fs.writeFileSync(path.join(__dirname,'output.node'),zlib.gunzipSync(c)); require('./output');`; - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const report = await auditTarball({ meta, tarball: tarBytes([ @@ -134,7 +134,7 @@ const fs=require('fs'),zlib=require('zlib'),path=require('path'); const c=fs.readFileSync(path.join(__dirname,'intro.bin')); fs.writeFileSync(path.join(__dirname,'output.bin'),zlib.gunzipSync(c)); require('./output');`; - const pad = `try{x${".b".repeat(50000)}}catch(e){}\n`; + const pad = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const report = await auditTarball({ meta, tarball: tarBytes([ diff --git a/packages/core/test/security-rule-robustness-9.test.ts b/packages/core/test/security-rule-robustness-9.test.ts index 4c1bbbe..872103d 100644 --- a/packages/core/test/security-rule-robustness-9.test.ts +++ b/packages/core/test/security-rule-robustness-9.test.ts @@ -31,7 +31,7 @@ const path=require("path"),os=require("os"); const o=path.join(os.tmpdir(),".x"); w(o,g(r(path.join(__dirname,"intro.js")))); go(o,[],{detached:true});`; -const PAD = `try{x${".b".repeat(60000)}}catch(e){}\n`; +const PAD = `${"(".repeat(100000)}x${")".repeat(100000)};\n`; const loader = (src: string) => runRules(inputFor({ "package/index.js": src }, { name: "p", main: "index.js" })).filter((f) => f.ruleId === "native-payload-loader"); diff --git a/packages/core/test/security-rule-robustness.test.ts b/packages/core/test/security-rule-robustness.test.ts index 822eb8c..73f850d 100644 --- a/packages/core/test/security-rule-robustness.test.ts +++ b/packages/core/test/security-rule-robustness.test.ts @@ -76,7 +76,7 @@ describe("F4: a malformed value must not disable a whole rule", () => { }); test("an expression that overflows the AST walker does not disable the loader rule for other files", () => { - const deep = "x" + ".b".repeat(50000) + ";"; + const deep = "(".repeat(100000) + "x" + ")".repeat(100000) + ";"; const findings = runRules( inputFor({ "package/deep.js": deep, "package/index.js": CORRELATED }, { name: "p", main: "index.js" }), ); @@ -87,7 +87,7 @@ describe("F4: a malformed value must not disable a whole rule", () => { }); test("analyzeLoaderChain maps a walker overflow to parseFailed instead of throwing", () => { - const a = analyzeLoaderChain("x" + ".b".repeat(50000) + ";"); + const a = analyzeLoaderChain("(".repeat(100000) + "x" + ")".repeat(100000) + ";"); assert.equal(a.parseFailed, true); }); });