From 89ff156d00af7620e42fd594257b87f63d4576f0 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:13:47 +0200 Subject: [PATCH 1/6] fix(core): walk the loader-chain AST iteratively A file padded with a very deep expression (tens of thousands of chained members, inside `try { ... } catch {}`) overflowed acorn-walk's recursive walk. That dropped the dataflow analysis to a regex fallback that cannot tell a correlated chain from four unrelated mentions: a chain in a form the fallback does not recognise fell below critical, and a benign overflowing file that mentioned all four names became critical. `walkAncestor` replaces `walk.ancestor`. It runs acorn-walk's own base visitors, collects each node's children, and walks them from an explicit stack, so it visits the same nodes in the same order (a node's visitor runs after its children) with the same ancestor list. The scope chain is kept incrementally and handed to the visitor, so a deep expression does not make every callback O(depth). The walk is bounded by 2,000,000 nodes and 1,000 nested scopes; exceeding either is a RangeError, which keeps mapping to the existing `walkOverflow` path. A huge path argument is no longer re-sliced whole by every write (capped like snippets). A deep expression now costs time, not a stack overflow, and the full analysis runs. The regex fallback remains for genuine parse failures (including acorn's own "Not enough stack space" SyntaxError on deeply nested parens, arrays and binary chains) and for the node bound. Existing overflow-fallback tests padded with `x.b.b...` now pad with 2,000,001 statements, which still reaches the node bound. Bump AUDIT_REVISION to 6: rule semantics changed. Closes #88 --- docs/adr/0056-security-audit-hardening.md | 3 +- packages/core/src/audit.ts | 2 +- packages/core/src/detect/loader-chain.ts | 68 ++++++-- .../core/test/security-iterative-walk.test.ts | 155 ++++++++++++++++++ .../test/security-rule-robustness-10.test.ts | 2 +- .../test/security-rule-robustness-3.test.ts | 2 +- .../test/security-rule-robustness-4.test.ts | 2 +- .../test/security-rule-robustness-5.test.ts | 2 +- .../test/security-rule-robustness-6.test.ts | 2 +- .../test/security-rule-robustness-7.test.ts | 4 +- .../test/security-rule-robustness-9.test.ts | 2 +- .../test/security-rule-robustness.test.ts | 4 +- 12 files changed, 222 insertions(+), 26 deletions(-) create mode 100644 packages/core/test/security-iterative-walk.test.ts diff --git a/docs/adr/0056-security-audit-hardening.md b/docs/adr/0056-security-audit-hardening.md index 4a69566..9ad5709 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 (2,000,000-node bound, + 1,000 nested scopes), so the regex fallback now serves only genuine parse failures and that bound. 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..11dd388 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 { @@ -61,9 +61,52 @@ 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(); + +/** Total nodes the walk may visit; past it the walk throws a RangeError, which callers map to the overflow path. */ +const MAX_WALK_NODES = 2_000_000; +/** Scope nesting the walk follows (each analysis callback costs O(scope depth)); real code nests far less. */ +const MAX_SCOPE_DEPTH = 1000; + +type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => 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 and MAX_SCOPE_DEPTH (a RangeError). + */ +export function walkAncestor(root: any, visitors: Record): void { + const ancestors: any[] = []; + const scopes: any[] = []; // outermost → innermost + type Frame = { node: any; type: string; isNew: boolean; kids: [any, string | 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 RangeError("loader-chain: node bound exceeded"); + ancestors.push(node); + if (isScope(node)) { + if (scopes.length >= MAX_SCOPE_DEPTH) throw new RangeError("loader-chain: scope depth bound exceeded"); + scopes.push(node); + } + } + const visit = (walkBase as Record)[type]; + if (visit == null) throw new Error(`No walker function defined for node type ${type}`); + const kids: Frame["kids"] = []; + visit(node, undefined, (child: any, _st: any, o?: string) => { kids.push([child, o]); }); + stack.push({ node, type, isNew, kids, next: 0 }); + }; + enter(root); + while (stack.length) { + const f = stack[stack.length - 1]!; + if (f.next < f.kids.length) { const [child, o] = f.kids[f.next++]!; enter(child, o); continue; } + visitors[f.type]?.(f.node, ancestors, ancestors, scopes.slice().reverse()); + if (f.isNew) { ancestors.pop(); if (scopes[scopes.length - 1] === f.node) scopes.pop(); } + stack.pop(); + } } /** @@ -341,7 +384,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 +410,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 +426,23 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: } } }, - VariableDeclarator(node: any, _st: any, ancestors: any[]) { + VariableDeclarator(node: any, _st: any, _ancestors: any[], chain: any[]) { 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 }, - AssignmentExpression(node: any, _st: any, ancestors: any[]) { + AssignmentExpression(node: any, _st: any, _ancestors: any[], chain: any[]) { 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) }, - CallExpression(node: any, _st: any, ancestors: any[]) { - const chain = scopeChain(ancestors); + CallExpression(node: any, _st: any, _ancestors: any[], chain: any[]) { const cat = classifyCall(node); const args = node.arguments ?? []; 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..248a2e3 --- /dev/null +++ b/packages/core/test/security-iterative-walk.test.ts @@ -0,0 +1,155 @@ +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("deeply nested parens, arrays and binary chains overflow acorn's parser, not the walk", () => { + // acorn raises a SyntaxError ("Not enough stack space") for these, so they take the parse-failure path. + for (const pad of [`${"(".repeat(60000)}x${")".repeat(60000)};\n`, `${"[".repeat(60000)}${"]".repeat(60000)};\n`, `1${"+1".repeat(60000)};\n`]) { + assert.equal(loaderChain.analyzeLoaderChain(pad + CHAIN).parseFailed, true, pad.slice(0, 12)); + } + }); + + 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("the node bound is deterministic and still maps to the overflow path", () => { + const src = `var a = [${"0,".repeat(2_100_000)}];\n${CHAIN}`; + const a = loaderChain.analyzeLoaderChain(src); + assert.equal(a.parseFailed, true); + assert.equal(a.walkOverflow, true); + }); + + 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 trace = (run: (visitors: Record) => void) => { + const out: string[] = []; + const types = ["Program", "Identifier", "Literal", "CallExpression", "MemberExpression", "VariableDeclarator", "AssignmentExpression", + "FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression", "ClassBody", "PropertyDefinition", "TemplateLiteral", "ImportDeclaration", + "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..8ebc508 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(2_000_001)}\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..939a294 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(2_000_001)}\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..23f5c80 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(2_000_001)}\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..9c55008 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(2_000_001)}\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..2815be7 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(2_000_001)}\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..11e2ea3 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(2_000_001)}\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(2_000_001)}\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..d2aab05 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(2_000_001)}\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..2a24952 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(2_000_001); 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(2_000_001)); assert.equal(a.parseFailed, true); }); }); From eb7da27e54493b73a2c3ed903b8cd03a45f14555 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:15:06 +0200 Subject: [PATCH 2/6] fix(core): treat a parser stack overflow as an overflow, not a syntax error acorn's parser raises SyntaxError("Not enough stack space to parse input") on deep nesting (about 700 parens). That took the strict parse-failure fallback, so a correlated chain padded with nested parens capped at high. That error, like a RangeError, now maps to `walkOverflow` so the loose fallback applies. Genuine syntax errors keep the strict fallback. Refs #88 --- packages/core/src/detect/loader-chain.ts | 9 +++++-- .../core/test/security-iterative-walk.test.ts | 25 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/core/src/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index 11dd388..06f1f11 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -67,6 +67,11 @@ const MAX_WALK_NODES = 2_000_000; /** Scope nesting the walk follows (each analysis callback costs O(scope depth)); real code nests far less. */ const MAX_SCOPE_DEPTH = 1000; +/** 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")); +} + type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => void; /** @@ -125,7 +130,7 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: 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 }; + return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, walkOverflow: isStackOverflow(e) }; } } @@ -517,7 +522,7 @@ 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 }; + return { primitives: [], correlated: false, boosters: { tempOrHidden: false, chmod: false, detached: false, unref: false, moduleLoad: false }, parseFailed: true, walkOverflow: isStackOverflow(e) }; } return { primitives, correlated, boosters, parseFailed: false }; diff --git a/packages/core/test/security-iterative-walk.test.ts b/packages/core/test/security-iterative-walk.test.ts index 248a2e3..9d8efa3 100644 --- a/packages/core/test/security-iterative-walk.test.ts +++ b/packages/core/test/security-iterative-walk.test.ts @@ -80,11 +80,30 @@ describe("a deep expression no longer defeats the dataflow analysis", () => { } }); - test("deeply nested parens, arrays and binary chains overflow acorn's parser, not the walk", () => { - // acorn raises a SyntaxError ("Not enough stack space") for these, so they take the parse-failure path. + 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`]) { - assert.equal(loaderChain.analyzeLoaderChain(pad + CHAIN).parseFailed, true, pad.slice(0, 12)); + 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 700 nested parens stays critical (loose fallback), also with renamed primitives", () => { + const pad = `${"(".repeat(700)}x${")".repeat(700)};\n`; + 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("a padded benign file that only mentions the four names is not critical", () => { From 341767da639f436eb07483bb12a6ffbba229aea0 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:20:32 +0200 Subject: [PATCH 3/6] fix(core): keep overflow classification across parses and separate the node cap - If the module parse exhausts the stack and the script retry then fails on something else (`export`), the result still counts as a stack overflow: both parse errors are checked. - Hitting the node or scope bound is a distinct `walkTruncated` outcome that uses the strict fallback like a parse failure, never the loose overflow one. The node bound is 4,194,304: a retained file is at most 2 MiB and yields about one node per byte, so real input cannot reach it. - The walk enumerates children incrementally (an index into the node's child list per frame, and the `body` list itself for Program and block bodies) instead of copying every child up front. A flat 1.9M-statement file walks with about 21 MB of extra peak RSS instead of about 275 MB. The visit order is unchanged (the parity test still passes). - `atob(` is a decode signal in the loose overflow fallback, so a padded `atob` chain stays critical. - The overflow-fallback tests pad with 100,000 nested parens, which overflows acorn's parser on any supported Node, instead of a node-bound pad. Refs #88 --- docs/adr/0056-security-audit-hardening.md | 4 +- packages/core/src/detect/loader-chain.ts | 43 +++++++++++++------ .../core/src/rules/native-payload-loader.ts | 2 +- .../core/test/security-iterative-walk.test.ts | 30 ++++++++++--- .../test/security-rule-robustness-10.test.ts | 2 +- .../test/security-rule-robustness-3.test.ts | 2 +- .../test/security-rule-robustness-4.test.ts | 2 +- .../test/security-rule-robustness-5.test.ts | 2 +- .../test/security-rule-robustness-6.test.ts | 2 +- .../test/security-rule-robustness-7.test.ts | 4 +- .../test/security-rule-robustness-9.test.ts | 2 +- .../test/security-rule-robustness.test.ts | 4 +- 12 files changed, 66 insertions(+), 33 deletions(-) diff --git a/docs/adr/0056-security-audit-hardening.md b/docs/adr/0056-security-audit-hardening.md index 9ad5709..5f371b1 100644 --- a/docs/adr/0056-security-audit-hardening.md +++ b/docs/adr/0056-security-audit-hardening.md @@ -286,8 +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 6). #88 closed the walker overflow: the loader-chain walk is iterative (2,000,000-node bound, - 1,000 nested scopes), so the regex fallback now serves only genuine parse failures and that bound. 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/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index 06f1f11..9cf5980 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -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"; @@ -62,10 +65,13 @@ function isScope(n: any): boolean { return n.type === "Program" || n.type === "FunctionDeclaration" || n.type === "FunctionExpression" || n.type === "ArrowFunctionExpression"; } -/** Total nodes the walk may visit; past it the walk throws a RangeError, which callers map to the overflow path. */ -const MAX_WALK_NODES = 2_000_000; -/** Scope nesting the walk follows (each analysis callback costs O(scope depth)); real code nests far less. */ +/** + * 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; scope nesting costs O(depth) per callback and real code nests far less. + */ +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 { @@ -84,30 +90,36 @@ type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => void export function walkAncestor(root: any, visitors: Record): void { const ancestors: any[] = []; const scopes: any[] = []; // outermost → innermost - type Frame = { node: any; type: string; isNew: boolean; kids: [any, string | undefined][]; next: number }; + // 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 RangeError("loader-chain: node bound exceeded"); + if (++visited > MAX_WALK_NODES) throw new WalkBoundError("loader-chain: node bound exceeded"); ancestors.push(node); if (isScope(node)) { - if (scopes.length >= MAX_SCOPE_DEPTH) throw new RangeError("loader-chain: scope depth bound exceeded"); + if (scopes.length >= MAX_SCOPE_DEPTH) throw new WalkBoundError("loader-chain: scope depth bound exceeded"); scopes.push(node); } } const visit = (walkBase as Record)[type]; if (visit == null) throw new Error(`No walker function defined for node type ${type}`); - const kids: Frame["kids"] = []; - visit(node, undefined, (child: any, _st: any, o?: string) => { kids.push([child, o]); }); - stack.push({ node, type, isNew, kids, next: 0 }); + if (type === "Program" || type === "BlockStatement" || type === "StaticBlock") { // acorn-walk: every statement of `body`, as "Statement" + stack.push({ node, type, isNew, list: node.body, ov: "Statement", 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.kids.length) { const [child, o] = f.kids[f.next++]!; enter(child, o); continue; } + if (f.next < f.list.length) { const i = f.next++; enter(f.list[i], typeof f.ov === "string" ? f.ov : f.ov?.[i]); continue; } visitors[f.type]?.(f.node, ancestors, ancestors, scopes.slice().reverse()); if (f.isNew) { ancestors.pop(); if (scopes[scopes.length - 1] === f.node) scopes.pop(); } stack.pop(); @@ -126,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: isStackOverflow(e) }; + // 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) }; } } @@ -522,7 +535,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: isStackOverflow(e) }; + 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 index 9d8efa3..28672b8 100644 --- a/packages/core/test/security-iterative-walk.test.ts +++ b/packages/core/test/security-iterative-walk.test.ts @@ -90,8 +90,8 @@ describe("a deep expression no longer defeats the dataflow analysis", () => { assert.notEqual(loaderChain.analyzeLoaderChain("const x = ;\n" + "(".repeat(5000)).walkOverflow, true); }); - test("a chain padded with 700 nested parens stays critical (loose fallback), also with renamed primitives", () => { - const pad = `${"(".repeat(700)}x${")".repeat(700)};\n`; + 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"); @@ -106,6 +106,20 @@ go(o, [], { detached: true }); 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 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), []); @@ -122,11 +136,15 @@ go(o, [], { detached: true }); assert.ok(ms < 20_000, `took ${ms} ms`); }); - test("the node bound is deterministic and still maps to the overflow path", () => { - const src = `var a = [${"0,".repeat(2_100_000)}];\n${CHAIN}`; - const a = loaderChain.analyzeLoaderChain(src); + 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.walkOverflow, 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); }); test("genuine parse failures still use the parse-failure path", () => { diff --git a/packages/core/test/security-rule-robustness-10.test.ts b/packages/core/test/security-rule-robustness-10.test.ts index 8ebc508..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 = `${";".repeat(2_000_001)}\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 939a294..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 = `${";".repeat(2_000_001)}\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 23f5c80..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 = `${";".repeat(2_000_001)}\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 9c55008..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 = `${";".repeat(2_000_001)}\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 2815be7..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 = `${";".repeat(2_000_001)}\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 11e2ea3..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 = `${";".repeat(2_000_001)}\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 = `${";".repeat(2_000_001)}\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 d2aab05..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 = `${";".repeat(2_000_001)}\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 2a24952..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 = ";".repeat(2_000_001); + 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(";".repeat(2_000_001)); + const a = analyzeLoaderChain("(".repeat(100000) + "x" + ")".repeat(100000) + ";"); assert.equal(a.parseFailed, true); }); }); From 9b37513771159b6dca79644321ccf3bb6cea54ae Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:24:39 +0200 Subject: [PATCH 4/6] fix(core): never abort the loader-chain walk on scope depth - Past MAX_SCOPE_DEPTH, deeper functions join the deepest tracked scope instead of throwing, so the full dataflow analysis still runs on a file nested 1,001 functions deep (over-approximation: taint is never lost). Only the node bound throws a WalkBoundError. - Array elements (ArrayExpression and ArrayPattern) are iterated by index from the node's own list, skipping holes, in acorn-walk's order, so a wide array no longer buffers every child in two temporary lists. On a 2 MB benign array the walk's peak RSS is 228 MB against acorn-walk's 205 MB and 286 MB before. - The node-bound test now also asserts the exact strict-fallback result ("high" for strict read and write signals), and the walk-order parity sample covers array literals, holes and array patterns. Refs #88 --- packages/core/src/detect/loader-chain.ts | 20 +++++++++---------- .../core/test/security-iterative-walk.test.ts | 19 +++++++++++++++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/core/src/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index 9cf5980..d355ecb 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -67,7 +67,8 @@ function isScope(n: any): boolean { /** * 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; scope nesting costs O(depth) per callback and real code nests far less. + * 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; @@ -85,7 +86,7 @@ type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => void * 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 and MAX_SCOPE_DEPTH (a RangeError). + * 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[] = []; @@ -100,17 +101,14 @@ export function walkAncestor(root: any, visitors: Record): void if (isNew) { if (++visited > MAX_WALK_NODES) throw new WalkBoundError("loader-chain: node bound exceeded"); ancestors.push(node); - if (isScope(node)) { - if (scopes.length >= MAX_SCOPE_DEPTH) throw new WalkBoundError("loader-chain: scope depth bound exceeded"); - scopes.push(node); - } + // Past MAX_SCOPE_DEPTH a deeper function joins the deepest tracked scope (over-approximation: taint is never lost). + if (isScope(node) && scopes.length < MAX_SCOPE_DEPTH) scopes.push(node); } const visit = (walkBase as Record)[type]; if (visit == null) throw new Error(`No walker function defined for node type ${type}`); - if (type === "Program" || type === "BlockStatement" || type === "StaticBlock") { // acorn-walk: every statement of `body`, as "Statement" - stack.push({ node, type, isNew, list: node.body, ov: "Statement", next: 0 }); - return; - } + // 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); }); @@ -119,7 +117,7 @@ export function walkAncestor(root: any, visitors: Record): void enter(root); while (stack.length) { const f = stack[stack.length - 1]!; - if (f.next < f.list.length) { const i = f.next++; enter(f.list[i], typeof f.ov === "string" ? f.ov : f.ov?.[i]); continue; } + 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()); if (f.isNew) { ancestors.pop(); if (scopes[scopes.length - 1] === f.node) scopes.pop(); } stack.pop(); diff --git a/packages/core/test/security-iterative-walk.test.ts b/packages/core/test/security-iterative-walk.test.ts index 28672b8..bbead72 100644 --- a/packages/core/test/security-iterative-walk.test.ts +++ b/packages/core/test/security-iterative-walk.test.ts @@ -145,6 +145,22 @@ go(o, [], { detached: 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", () => { @@ -164,12 +180,13 @@ describe("the iterative walk visits what acorn-walk's ancestor visits", () => { 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", - "ForOfStatement", "TryStatement", "SwitchStatement", "ObjectExpression", "ArrayPattern", "ObjectPattern", "BinaryExpression", "ChainExpression", + "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(">")}`); From 725816341a3ac2676262cd0aa596734caffc0cce Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:28:47 +0200 Subject: [PATCH 5/6] fix(core): make bindings union-only in scopes merged past the bound Functions nested past MAX_SCOPE_DEPTH join the deepest tracked scope. A same-named inner local declared there replaced the enclosing binding's taint (kill-on-reassign), so the enclosing launch lost its `written-path` correlation and a critical chain dropped to high. The walker now tells the visitor when a node sits inside a merged function, and there a declaration, initialization or reassignment only adds taint to the existing entry. Merged scopes are "may" scopes: the analysis over-approximates and cannot lose a correlation a correct analysis would report. Scopes within the bound keep exact kill-on- reassign semantics, and the scope chain stays bounded. Refs #88 --- packages/core/src/detect/loader-chain.ts | 23 +++++++++----- .../core/test/security-iterative-walk.test.ts | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/core/src/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index d355ecb..558b44b 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -79,7 +79,8 @@ function isStackOverflow(e: unknown): boolean { return e instanceof RangeError || (e instanceof SyntaxError && e.message.startsWith("Not enough stack space")); } -type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => void; +/** `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 @@ -91,6 +92,7 @@ type Visitor = (node: any, st: any, ancestors: any[], scopeChain: any[]) => void 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[] = []; @@ -102,7 +104,7 @@ export function walkAncestor(root: any, visitors: Record): void 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) && scopes.length < MAX_SCOPE_DEPTH) scopes.push(node); + 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}`); @@ -118,8 +120,8 @@ export function walkAncestor(root: any, visitors: Record): void 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()); - if (f.isNew) { ancestors.pop(); if (scopes[scopes.length - 1] === f.node) scopes.pop(); } + 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(); } } @@ -165,6 +167,11 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: if (!m) { m = new Map(); taint.set(s, m); } return m; }; + /** Exact scopes REPLACE a binding's taint (kill-on-reassign); a merged scope is a "may" scope and only adds to it. */ + const putTaint = (m: Map>, name: string, tags: Set, merged: boolean): void => { + const cur = merged ? m.get(name) : undefined; + if (cur) for (const t of tags) cur.add(t); else m.set(name, tags); + }; 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; @@ -442,21 +449,21 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: } } }, - VariableDeclarator(node: any, _st: any, _ancestors: any[], chain: 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 relevant = new Set([...payloadTags(node.init, chain)].filter((t) => t === "read" || t === "decoded")); - scopeTaint(chain[0]).set(name, relevant); // declaration REPLACES + putTaint(scopeTaint(chain[0]), name, relevant, merged); // declaration REPLACES (UNION in a merged scope) }, - AssignmentExpression(node: any, _st: any, _ancestors: any[], chain: 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 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(scopeTaint(declaringScope(chain, name)), name, relevant, merged); // reassignment REPLACES (kill-on-reassign, S2; UNION in a merged scope) }, CallExpression(node: any, _st: any, _ancestors: any[], chain: any[]) { const cat = classifyCall(node); diff --git a/packages/core/test/security-iterative-walk.test.ts b/packages/core/test/security-iterative-walk.test.ts index bbead72..8633919 100644 --- a/packages/core/test/security-iterative-walk.test.ts +++ b/packages/core/test/security-iterative-walk.test.ts @@ -120,6 +120,37 @@ go(o, [], { detached: 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 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), []); From e3187e0761116feb055d0f9647aab8d0263c887a Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 15:33:32 +0200 Subject: [PATCH 6/6] fix(core): keep merged-scope taint in an overlay, not in tracked scopes A declaration inside a function nested past the scope bound wrote an empty taint entry into the deepest tracked scope. That entry then hid a binding of the same name in a farther-out tracked scope and stayed visible after the merged function exited, so a launch of the outer `written-path` variable lost its correlation. Merged declarations, assignments and written-path marks now go to a separate union-only `mergedTaint` overlay (name to tags) and never touch the tracked scopes. A lookup returns the lexical resolution unioned with the overlay's tags for that name, so the overlay only adds taint, never hides a binding, and may outlive the merged function. Exact scopes keep their kill-on-reassign semantics unchanged. Refs #88 --- packages/core/src/detect/loader-chain.ts | 30 ++++++++++++------- .../core/test/security-iterative-walk.test.ts | 26 ++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/core/src/detect/loader-chain.ts b/packages/core/src/detect/loader-chain.ts index 558b44b..f7d213d 100644 --- a/packages/core/src/detect/loader-chain.ts +++ b/packages/core/src/detect/loader-chain.ts @@ -167,14 +167,23 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: if (!m) { m = new Map(); taint.set(s, m); } return m; }; - /** Exact scopes REPLACE a binding's taint (kill-on-reassign); a merged scope is a "may" scope and only adds to it. */ - const putTaint = (m: Map>, name: string, tags: Set, merged: boolean): void => { - const cur = merged ? m.get(name) : undefined; - if (cur) for (const t of tags) cur.add(t); else m.set(name, tags); + // 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; } @@ -456,16 +465,16 @@ export function analyzeLoaderChain(source: string, opts: { moduleLoadReachable?: const name = node.id.name; declInit.set(name, node.init); const relevant = new Set([...payloadTags(node.init, chain)].filter((t) => t === "read" || t === "decoded")); - putTaint(scopeTaint(chain[0]), name, relevant, merged); // declaration REPLACES (UNION in a merged scope) + putTaint(chain[0], name, relevant, merged); // declaration REPLACES (UNION in a merged scope) }, 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 relevant = new Set([...payloadTags(node.right, chain)].filter((t) => t === "read" || t === "decoded")); - putTaint(scopeTaint(declaringScope(chain, name)), name, relevant, merged); // reassignment REPLACES (kill-on-reassign, S2; UNION in a merged scope) + 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[], chain: any[]) { + CallExpression(node: any, _st: any, _ancestors: any[], chain: any[], merged: boolean) { const cat = classifyCall(node); const args = node.arguments ?? []; @@ -487,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]); diff --git a/packages/core/test/security-iterative-walk.test.ts b/packages/core/test/security-iterative-walk.test.ts index 8633919..36a8ac7 100644 --- a/packages/core/test/security-iterative-walk.test.ts +++ b/packages/core/test/security-iterative-walk.test.ts @@ -151,6 +151,32 @@ go(out); 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), []);