Skip to content

Commit 04672f2

Browse files
committed
fix(javascript): preserve builtin identity and share alias traversal
Track module writes and escapes at their owner, preserve capture and reassignment ordering, and share CommonJS/default namespace identity without erasing parent-slot changes. Carry write sites into ambient alias resolution and use parsed write targets across consumers. Deduplicate returned-reference work with cycle context; retain unchanged temporal selection identity so provenance memoization stays effective. Reviewed fixes include #2041, #2465, #2830, #2881, and #2948. Remove superseded mutation representations and the ineffective per-markValue dedup layer. Validation: 380 focused JavaScript tests passed under a 384 MiB heap cap, including the previously aborting 30-level/128-call alias regression. Six public CLI/MCP builtin tests passed. Exact pdf.js completed-workflow performance remains unmeasured.
1 parent 0f4c484 commit 04672f2

34 files changed

Lines changed: 3771 additions & 314 deletions

‎src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ it("projects closure and direct interprocedural flow without execution", () => {
6565
);
6666
});
6767

68+
it("projects a destructuring assignment member target as a property write", () => {
69+
const graph = graphFor(`
70+
const source = { count: 1 };
71+
[source.count] = [2];
72+
`);
73+
const operations = graph.nodes.filter(
74+
({ properties }) => properties.operation_kind !== undefined,
75+
);
76+
expect(operations.map(({ label }) => label)).toEqual(["write:/count"]);
77+
expect(graph.relations).not.toContainEqual(
78+
expect.objectContaining({ relation: "reads-property" }),
79+
);
80+
});
81+
6882
it("keeps dynamic calls explicit and returns complete deterministic results", () => {
6983
const graph = graphFor(`
7084
const handlers = { ready() { return 1; } };

‎src/application/javascript/JavaScriptSemanticGraphResources.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ const emptyAnalysis = (): JavaScriptArtifactAnalysis => ({
351351
packages: [],
352352
json_modules: [],
353353
html_scripts: [],
354+
html_inline_scripts: [],
354355
source_maps: [],
355356
visited_ast_nodes: 0,
356357
findings: 0,
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
4+
5+
// Webpack 5 standalone bootstrap with an Electron external, as emitted for
6+
// `target: "electron-main"`.
7+
const bootstrap = (external: string, consumer: string, entry = "n(100);") =>
8+
`(()=>{var m={45840:${external},100:(e,t,n)=>{"use strict";${consumer}}},c={};` +
9+
`function n(t){var r=c[t];if(void 0!==r)return r.exports;var o=c[t]={exports:{}};` +
10+
`return m[t](o,o.exports,n),o.exports}${entry}})();`;
11+
12+
const EXTERNAL = 'e=>{"use strict";e.exports=require("electron")}';
13+
const CONSUMER =
14+
'const s=n(45840);new s.BrowserWindow({webPreferences:{preload:"./preload.js",contextIsolation:!1}});' +
15+
's.ipcMain.handle("get-connections",async()=>[]);';
16+
17+
const electronOf = (source: string) =>
18+
analyzeJavaScriptStaticSource(source).electron;
19+
20+
describe("Electron identity through Webpack externals", () => {
21+
it("follows the loader passed to a module factory", () => {
22+
const electron = electronOf(bootstrap(EXTERNAL, CONSUMER));
23+
expect(electron.browser_windows).toMatchObject([
24+
{ preload_path: "./preload.js" },
25+
]);
26+
expect(electron.ipc).toMatchObject([
27+
{ channel: "get-connections", side: "main" },
28+
]);
29+
});
30+
31+
it("follows a direct member read from the loader call", () => {
32+
expect(
33+
electronOf(
34+
bootstrap(
35+
EXTERNAL,
36+
'new(n(45840).BrowserWindow)({webPreferences:{preload:"./p.js"}});',
37+
),
38+
).browser_windows,
39+
).toMatchObject([{ preload_path: "./p.js" }]);
40+
});
41+
42+
it("follows the runtime loader called from the bootstrap body", () => {
43+
expect(
44+
electronOf(
45+
bootstrap(
46+
EXTERNAL,
47+
"",
48+
'const s=n(45840);new s.BrowserWindow({webPreferences:{preload:"./p.js"}});',
49+
),
50+
).browser_windows,
51+
).toMatchObject([{ preload_path: "./p.js" }]);
52+
});
53+
54+
it.each([
55+
[
56+
"an external overwritten after the require",
57+
bootstrap('e=>{e.exports=require("electron");e.exports=other}', CONSUMER),
58+
],
59+
[
60+
"a local module table entry",
61+
bootstrap("e=>{e.exports={BrowserWindow:class{}}}", CONSUMER),
62+
],
63+
[
64+
"a shadowed require inside the external",
65+
bootstrap(
66+
'e=>{const require=x=>x;e.exports=require("electron")}',
67+
CONSUMER,
68+
),
69+
],
70+
[
71+
"a reassigned loader parameter",
72+
bootstrap(EXTERNAL, `n=other;${CONSUMER}`),
73+
],
74+
[
75+
"a loader-shaped call outside a proven bootstrap",
76+
`var m={45840:${EXTERNAL}};function n(t){return other(t)}${CONSUMER}`,
77+
],
78+
[
79+
"a different table key",
80+
bootstrap(EXTERNAL, CONSUMER.replace("n(45840)", "n(45841)")),
81+
],
82+
])("keeps %s unresolved", (_name, source) => {
83+
const electron = electronOf(source);
84+
expect(electron.browser_windows).toEqual([]);
85+
// Only direct observations come from this identity; the separate
86+
// wrapper-substitution path keeps its own resolution basis.
87+
expect(
88+
electron.ipc.filter(
89+
({ resolution_basis: basis }) => basis !== "wrapper-substitution",
90+
),
91+
).toEqual([]);
92+
});
93+
});
94+
95+
describe("Webpack externals with a tampered runtime", () => {
96+
it.each([
97+
[
98+
"a replaced bootstrap loader",
99+
bootstrap(
100+
EXTERNAL,
101+
CONSUMER,
102+
"const o=n;n=k=>k===100?o(k):local;n(100);",
103+
),
104+
],
105+
[
106+
"a generator external",
107+
bootstrap('function*(e){e.exports=require("electron")}', CONSUMER),
108+
],
109+
[
110+
"an async external",
111+
bootstrap('async e=>{e.exports=require("electron")}', CONSUMER),
112+
],
113+
[
114+
"a loader that returns something else",
115+
bootstrap(EXTERNAL, CONSUMER).replace(
116+
"return m[t](o,o.exports,n),o.exports}",
117+
"return m[t](o,o.exports,n),{BrowserWindow:class{}}}",
118+
),
119+
],
120+
[
121+
"a table slot replaced before loading",
122+
bootstrap(
123+
EXTERNAL,
124+
CONSUMER,
125+
"m[45840]=e=>{e.exports={BrowserWindow:class{}}};n(100);",
126+
),
127+
],
128+
[
129+
"a table slot replaced through an alias",
130+
bootstrap(EXTERNAL, CONSUMER, "const a=m;a[45840]=local;n(100);"),
131+
],
132+
[
133+
"a slot replaced through the runtime exposure",
134+
bootstrap(EXTERNAL, CONSUMER, "n.m=m;n.m[45840]=local;n(100);"),
135+
],
136+
[
137+
"a cache entry planted before loading",
138+
bootstrap(EXTERNAL, CONSUMER, "c[45840]={exports:local};n(100);"),
139+
],
140+
[
141+
"a computed table key that may collide",
142+
bootstrap(EXTERNAL, CONSUMER).replace(
143+
"c={};",
144+
"c={};m={...m,[key]:e=>{e.exports=local}};",
145+
),
146+
],
147+
[
148+
"an unresolved computed key in the table",
149+
bootstrap(EXTERNAL, CONSUMER).replace(
150+
`45840:${EXTERNAL},`,
151+
`45840:${EXTERNAL},[key]:e=>{e.exports=local},`,
152+
),
153+
],
154+
])("keeps %s unresolved", (_name, source) => {
155+
const electron = electronOf(source);
156+
expect(electron.browser_windows).toEqual([]);
157+
// Only direct observations come from this identity; the separate
158+
// wrapper-substitution path keeps its own resolution basis.
159+
expect(
160+
electron.ipc.filter(
161+
({ resolution_basis: basis }) => basis !== "wrapper-substitution",
162+
),
163+
).toEqual([]);
164+
});
165+
});
166+
167+
describe("Webpack externals with a tampered loader or module object", () => {
168+
it.each([
169+
[
170+
"cached exports rewritten through the runtime exposure",
171+
bootstrap(
172+
EXTERNAL,
173+
CONSUMER,
174+
"n.c=c;n(45840);n.c[45840].exports=local;n(100);",
175+
),
176+
],
177+
[
178+
"a pre-populated cache",
179+
bootstrap(EXTERNAL, CONSUMER).replace(
180+
"c={};",
181+
"c={45840:{exports:local}};",
182+
),
183+
],
184+
[
185+
"exports rewritten in the returned sequence",
186+
bootstrap(EXTERNAL, CONSUMER).replace(
187+
"return m[t](o,o.exports,n),o.exports}",
188+
"return m[t](o,o.exports,n),o.exports=local,o.exports}",
189+
),
190+
],
191+
[
192+
"exports rewritten before the return",
193+
bootstrap(EXTERNAL, CONSUMER).replace(
194+
"return m[t](o,o.exports,n),o.exports}",
195+
"m[t](o,o.exports,n);o.exports=local;return o.exports}",
196+
),
197+
],
198+
[
199+
"an alias of the exposed table",
200+
bootstrap(EXTERNAL, CONSUMER, "n.m=m;const a=n.m;a[45840]=local;n(100);"),
201+
],
202+
[
203+
"a rewritten module key",
204+
bootstrap(EXTERNAL, CONSUMER).replace(
205+
"function n(t){",
206+
"function n(t){if(t===45840)t=100;",
207+
),
208+
],
209+
[
210+
"a dispatch behind a guard",
211+
bootstrap(EXTERNAL, CONSUMER).replace(
212+
"return m[t](o,o.exports,n),o.exports}",
213+
"if(t===100)m[t](o,o.exports,n);return o.exports}",
214+
),
215+
],
216+
[
217+
"duplicate factory parameter names",
218+
bootstrap('function(e,e,n){e.exports=require("electron")}', CONSUMER),
219+
],
220+
[
221+
"an accessor for module exports",
222+
bootstrap(EXTERNAL, CONSUMER).replace(
223+
"var o=c[t]={exports:{}}",
224+
"var o=c[t]={get exports(){return local},set exports(v){}}",
225+
),
226+
],
227+
])("keeps %s unresolved", (_name, source) => {
228+
const electron = electronOf(source);
229+
expect(electron.browser_windows).toEqual([]);
230+
// Only direct observations come from this identity; the separate
231+
// wrapper-substitution path keeps its own resolution basis.
232+
expect(
233+
electron.ipc.filter(
234+
({ resolution_basis: basis }) => basis !== "wrapper-substitution",
235+
),
236+
).toEqual([]);
237+
});
238+
239+
it.each([
240+
[
241+
"a cache cleared when the factory throws",
242+
bootstrap(EXTERNAL, CONSUMER).replace(
243+
"return m[t](o,o.exports,n),o.exports}",
244+
"var i=!0;try{m[t].call(o.exports,o,o.exports,n),i=!1}finally{i&&delete c[t]}return o.exports}",
245+
),
246+
],
247+
[
248+
"the runtime exposing its cache",
249+
bootstrap(EXTERNAL, CONSUMER, "n.c=c;n(100);"),
250+
],
251+
[
252+
"chunk installation through the runtime table",
253+
bootstrap(
254+
EXTERNAL,
255+
CONSUMER,
256+
"n.m=m;n.i=e=>{for(var k in e)n.m[k]=e[k]};n(100);",
257+
),
258+
],
259+
[
260+
"a loaded flag set before returning",
261+
bootstrap(EXTERNAL, CONSUMER).replace(
262+
"return m[t](o,o.exports,n),o.exports}",
263+
"return m[t].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}",
264+
),
265+
],
266+
])("still resolves with %s", (_name, source) => {
267+
expect(electronOf(source).browser_windows).toMatchObject([
268+
{ preload_path: "./preload.js" },
269+
]);
270+
});
271+
});

‎src/domain/javascript/javascriptAstValues.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,41 @@ export const semanticObjectPatternKeys = (
9494
return key === null ? [] : [key];
9595
});
9696

97+
/** Return the syntactic node whose target this construct writes. */
98+
export const semanticWriteSiteTarget = (node: t.Node): t.Node | null => {
99+
if (t.isAssignmentExpression(node)) return node.left;
100+
if (t.isUpdateExpression(node)) return node.argument;
101+
if (t.isUnaryExpression(node, { operator: "delete" })) return node.argument;
102+
if (t.isForOfStatement(node) || t.isForInStatement(node)) return node.left;
103+
return null;
104+
};
105+
106+
/** Whether a write site reads the previous target value before writing. */
107+
export const semanticWriteSiteReadsFirst = (site: t.Node): boolean =>
108+
(t.isAssignmentExpression(site) && site.operator !== "=") ||
109+
t.isUpdateExpression(site);
110+
111+
/** Find the owning write site through inert TypeScript and parenthesis wrappers. */
112+
export const semanticWrappedWriteSite = (
113+
node: t.Node,
114+
parent: t.Node,
115+
ancestors: readonly t.Node[],
116+
): t.Node | null => {
117+
const chain =
118+
ancestors.at(-1) === parent ? ancestors : [...ancestors, parent];
119+
for (let index = chain.length - 1; index >= 0; index -= 1) {
120+
const candidate = chain[index];
121+
if (candidate === undefined) return null;
122+
const target = semanticWriteSiteTarget(candidate);
123+
if (target !== null)
124+
return unwrapJavaScriptExpression(target).node === node
125+
? candidate
126+
: null;
127+
if (unwrapJavaScriptExpression(candidate).depth === 0) return null;
128+
}
129+
return null;
130+
};
131+
97132
/** Visit binding and member leaves in direct and destructuring write targets. */
98133
export function* semanticWriteTargets(
99134
root: t.Node,

0 commit comments

Comments
 (0)