From 1e1a611012fc15d33a4338edf5fc484b5d595030 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 12 Oct 2026 00:33:00 +0800 Subject: [PATCH 1/3] fix(javascript): retain bound intrinsic mutation effects --- .../javascript/electronModuleInterop.test.ts | 39 ++++++ .../javascriptElectronMemberWrites.ts | 4 +- .../javascriptIntrinsicInvocation.ts | 27 ++-- .../javascript/javascriptModuleInterop.ts | 117 +++++++++++++++++- .../javascript/javascriptSemanticAnalysis.ts | 6 +- 5 files changed, 171 insertions(+), 22 deletions(-) diff --git a/src/domain/javascript/electronModuleInterop.test.ts b/src/domain/javascript/electronModuleInterop.test.ts index a9f436a82..1ececa18a 100644 --- a/src/domain/javascript/electronModuleInterop.test.ts +++ b/src/domain/javascript/electronModuleInterop.test.ts @@ -73,8 +73,36 @@ describe("Electron module interop identity", () => { }); }); +const BOUND_INTRINSIC_MUTATIONS: [string, string][] = [ + [ + "intrinsic changed through aliased prebound arguments", + `const objectAlias = Object; const key = "create"; const define = Object.defineProperty; const mutate = define.bind(null, objectAlias, key); mutate({value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], + [ + "intrinsic changed through nested prebinding", + `const first = Object.defineProperty.bind(null, Object); const second = first.bind(null, "create"); second({value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], + [ + "a replaced bound mutator bind method is unknown", + `const first = Object.defineProperty.bind(null, Object); first.bind = customBind; const second = first.bind(null, "create"); second({value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], + [ + "intrinsic changed through a computed prebound call", + `const mutate = Object.defineProperty.bind(null, Object, "create"); mutate["call"](null, {value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], + [ + "intrinsic changed through a computed prebound apply", + `const mutate = Object.defineProperty.bind(null, Object, "create"); mutate["apply"](null, [{value:customCreate}]); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], + [ + "a replaced bind intrinsic makes a prebound mutator unknown", + `Function.prototype.bind = customBind; const mutate = Object.defineProperty.bind(null, Object, "create"); mutate({value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, + ], +]; + describe("Electron module interop uncertainty", () => { it.each([ + ...BOUND_INTRINSIC_MUTATIONS, [ "intrinsic changed through an unresolved mutator target", `Object.defineProperty(getTarget(), "create", {value:customCreate}); ${ROLLDOWN_MODULE_INTEROP}\n${IMPORT} ${WRAP}`, @@ -547,3 +575,14 @@ describe("Electron module interop bound mutators", () => { ).toHaveLength(1); }); }); + +it("keeps unrelated exports after an aliased prebound key write", () => { + const analysis = analyzeJavaScriptStaticSource(` + const electron = require("electron"); + const key = "Unrelated"; + const mutate = Object.defineProperty.bind(null, electron, key); + mutate({value: custom}); + new electron.BrowserWindow({}); + `); + expect(analysis.electron.browser_windows).toHaveLength(1); +}); diff --git a/src/domain/javascript/javascriptElectronMemberWrites.ts b/src/domain/javascript/javascriptElectronMemberWrites.ts index a265b94fa..ef3e7f22a 100644 --- a/src/domain/javascript/javascriptElectronMemberWrites.ts +++ b/src/domain/javascript/javascriptElectronMemberWrites.ts @@ -6,7 +6,6 @@ import { unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { traverseJavaScriptAstSteps } from "./javascriptSemanticTraversal.js"; -import { stringValue } from "./javascriptStaticAnalysisHelpers.js"; /** Match Electron's supported module entry points. */ export const ELECTRON_MODULE = @@ -60,6 +59,7 @@ export function* collectJavaScriptIdentityWritesSteps( intrinsicInvocation: ( node: t.CallExpression | t.OptionalCallExpression, ) => JavaScriptIntrinsicInvocation | undefined, + staticString: (node: t.Node | undefined) => string | undefined, freshObject: (node: t.Node) => boolean, retain: (node: t.Node, path: readonly (string | null)[]) => void, ): Generator { @@ -100,7 +100,7 @@ export function* collectJavaScriptIdentityWritesSteps( const exported = origin(root, "member"); if (exported !== undefined) { const key = keyedMutators.has(method ?? "") - ? [stringValue(arguments_?.[1]) ?? null] + ? [staticString(arguments_?.[1]) ?? null] : []; retain(node, [...exported, ...members, ...key]); } else if (!freshObject(target)) { diff --git a/src/domain/javascript/javascriptIntrinsicInvocation.ts b/src/domain/javascript/javascriptIntrinsicInvocation.ts index 32bab79f7..f8eb6d2fd 100644 --- a/src/domain/javascript/javascriptIntrinsicInvocation.ts +++ b/src/domain/javascript/javascriptIntrinsicInvocation.ts @@ -37,6 +37,7 @@ export const resolveJavaScriptIntrinsicInvocation = ( node: t.CallExpression | t.OptionalCallExpression, state: JavaScriptSemanticAnalysisState, intrinsicPath: (node: t.Node) => readonly string[] | undefined, + boundIntrinsic: (node: t.Node) => JavaScriptIntrinsicInvocation | undefined, ): JavaScriptIntrinsicInvocation | undefined => { const visited = new Set(); let callee: t.Node = node.callee; @@ -70,23 +71,15 @@ export const resolveJavaScriptIntrinsicInvocation = ( continue; } } - if (t.isCallExpression(callee) || t.isOptionalCallExpression(callee)) { - const binder: t.Node = unwrapJavaScriptExpression(callee.callee).node; - if ( - (!t.isMemberExpression(binder) && - !t.isOptionalMemberExpression(binder)) || - semanticStaticPropertyKey(binder.property, binder.computed) !== "bind" - ) - return undefined; - const prefix = callee.arguments.slice(1); - arguments_ = t.isSpreadElement(callee.arguments[0]) - ? undefined - : prefix.length === 0 - ? arguments_ - : [...prefix, ...(arguments_ ?? [])]; - callee = binder.object; - continue; - } + const bound = boundIntrinsic(callee); + if (bound !== undefined) + return { + path: bound.path, + arguments: + bound.arguments === undefined + ? undefined + : [...bound.arguments, ...(arguments_ ?? [])], + }; if (!t.isIdentifier(callee)) return undefined; const binding = resolveSemanticBindingState(state, callee, callee.name); const initializer = semanticStableInitializer(binding, state); diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index 542fbbd39..bee2f4318 100644 --- a/src/domain/javascript/javascriptModuleInterop.ts +++ b/src/domain/javascript/javascriptModuleInterop.ts @@ -15,7 +15,10 @@ import { childNodes, javaScriptAstChildKeys, } from "./javascriptSemanticTraversal.js"; -import { electronMemberPath } from "./javascriptElectronMemberWrites.js"; +import { + electronMemberPath, + type JavaScriptIntrinsicInvocation, +} from "./javascriptElectronMemberWrites.js"; import { resolveJavaScriptIntrinsicInvocation } from "./javascriptIntrinsicInvocation.js"; import { SemanticPropertyPathCoverage } from "./javascriptSemanticPropertyPaths.js"; import { @@ -173,6 +176,109 @@ export const createSemanticModuleInterop = ( } }; + const staticString = (node: t.Node | undefined): string | undefined => { + if (node === undefined) return undefined; + const visited = new Set(); + let current = node; + for (;;) { + const literal = readExactJavaScriptLiteral(current); + if (literal.found) + return typeof literal.value === "string" ? literal.value : undefined; + const expression = unwrapJavaScriptExpression(current).node; + if (!t.isIdentifier(expression)) return undefined; + const binding = resolveSemanticBindingState( + state, + expression, + expression.name, + ); + const initializer = semanticStableInitializer(binding, state); + if ( + binding === undefined || + initializer === undefined || + initializer.projection.length !== 0 || + (initializer.node.end ?? Infinity) >= (expression.start ?? -1) || + visited.has(binding.bindingId) + ) + return undefined; + visited.add(binding.bindingId); + current = initializer.node; + } + }; + + const boundIntrinsic = ( + node: t.Node, + ): JavaScriptIntrinsicInvocation | undefined => { + const visited = new Set(); + const argumentGroups: t.CallExpression["arguments"][] = []; + const bindReads: t.Node[] = []; + let opaqueArguments = false; + let current = node; + for (;;) { + current = unwrapJavaScriptExpression(current).node; + if (t.isIdentifier(current)) { + const binding = resolveSemanticBindingState( + state, + current, + current.name, + ); + const initializer = semanticStableInitializer(binding, state); + if ( + binding === undefined || + initializer === undefined || + initializer.projection.length !== 0 || + (initializer.node.end ?? Infinity) >= (current.start ?? -1) || + visited.has(binding.bindingId) + ) + return undefined; + visited.add(binding.bindingId); + current = initializer.node; + continue; + } + if (!t.isCallExpression(current)) return undefined; + const { root: bindTarget, members } = electronMemberPath(current.callee); + if (members.at(-1) !== "bind") return undefined; + const captured = capturedIntrinsic(current.callee); + const terminal = captured?.path.at(-1) === "bind"; + // Only a direct .bind can continue a previously bound callable. Keeping + // intervening members would require a separate proof of their identity. + if (!terminal && members.length !== 1) return undefined; + bindReads.push(captured?.capturedAt ?? current.callee); + const thisArgument = current.arguments[0]; + const ownArguments = current.arguments.slice(1); + opaqueArguments ||= + thisArgument !== undefined && + !readExactJavaScriptLiteral(thisArgument).found; + opaqueArguments ||= ownArguments.some((argument) => + t.isSpreadElement(argument), + ); + argumentGroups.push(ownArguments); + if (!terminal || captured === undefined) { + current = bindTarget; + continue; + } + const path = captured.path.slice(0, -1); + const boundPath = [...path, "bind"]; + const functionBind = ["intrinsic", "Function", "prototype", "bind"]; + opaqueArguments ||= bindReads.some((capture) => { + const scope = opaqueMutationPath(capture).join("."); + return writes.some( + (write) => + (write.scope === scope || write.scope === "intrinsic") && + write.offset < (capture.start ?? -1) && + (write.coverage.covers(boundPath) || + write.coverage.covers(functionBind)), + ); + }); + return { + path, + // Rebinding appends arguments to the earlier bound argument list. + arguments: opaqueArguments + ? undefined + : argumentGroups.toReversed().flat(), + }; + } + }; + const isFreshObject = (node: t.Node): boolean => { const visited = new Set(); let current = node; @@ -443,7 +549,14 @@ export const createSemanticModuleInterop = ( callOrigin, intrinsicPath, intrinsicInvocation: (node: t.CallExpression | t.OptionalCallExpression) => - resolveJavaScriptIntrinsicInvocation(node, state, intrinsicPath), + resolveJavaScriptIntrinsicInvocation( + node, + state, + intrinsicPath, + boundIntrinsic, + ), + boundIntrinsic, + staticString, isFreshObject, opaqueMutationPath, retainWrite: (node: t.Node, path: readonly (string | null)[]) => { diff --git a/src/domain/javascript/javascriptSemanticAnalysis.ts b/src/domain/javascript/javascriptSemanticAnalysis.ts index 6c07340cd..517ed5836 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.ts @@ -407,10 +407,14 @@ export function* classifyParsedJavaScriptElectronBindingsSteps( if (kind === "opaque-call") return interop.opaqueMutationPath(root); const exported = kind === "binding" ? undefined : rootExport(root); return exported === undefined - ? interop.intrinsicPath(root) + ? (interop.intrinsicPath(root) ?? + (kind === "member" + ? interop.boundIntrinsic(root)?.path + : undefined)) : ["electron", ...exported]; }, interop.intrinsicInvocation, + interop.staticString, interop.isFreshObject, (node, path) => { if (path[0] === "electron") writes.retain(path); From 3e9bce624922150efd5d5ce5dea29d30d2e4c38e Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 12 Oct 2026 00:39:01 +0800 Subject: [PATCH 2/3] fix(javascript): preserve bound receivers for intrinsic invocation --- docs/javascript-artifact-reconstruction.md | 3 +- .../javascript/electronModuleInterop.test.ts | 8 ++ .../javascriptElectronMemberWrites.ts | 2 + .../javascriptIntrinsicInvocation.ts | 98 +++++++++++-------- .../javascript/javascriptModuleInterop.ts | 15 +-- .../electronModuleInteropCli.test.ts | 39 +++++--- 6 files changed, 95 insertions(+), 70 deletions(-) diff --git a/docs/javascript-artifact-reconstruction.md b/docs/javascript-artifact-reconstruction.md index 192a650f9..405f93945 100644 --- a/docs/javascript-artifact-reconstruction.md +++ b/docs/javascript-artifact-reconstruction.md @@ -344,7 +344,8 @@ reassignments, dynamic selections, and visibly overwritten namespace members are unresolved. Known Object/Reflect mutators remain recognizable through literal destructuring of intrinsic objects and stable bound-function aliases. Calls retain bound -argument prefixes through `call`, `apply`, and `Reflect.apply`; unexamined +argument prefixes and the original bound receiver through `call`, `apply`, and +`Reflect.apply`, including generic `Function.prototype` invocation; unexamined targets remain unresolved. Creating an unused binding does not itself mutate an intrinsic. Variable redeclarations retain a single unconditional initializer when they introduce no assignment or second initializer. diff --git a/src/domain/javascript/electronModuleInterop.test.ts b/src/domain/javascript/electronModuleInterop.test.ts index 1ececa18a..2fa971b83 100644 --- a/src/domain/javascript/electronModuleInterop.test.ts +++ b/src/domain/javascript/electronModuleInterop.test.ts @@ -546,6 +546,12 @@ describe("Electron module interop bound mutators", () => { 'const change = Object.defineProperty.bind(null,...args); change(Object,"create",{value:customCreate});', 'const metadata = {}; Object.defineProperty.call(...args,metadata,"x",{value:1});', 'const metadata = {}; Object.defineProperty.apply(...args,[metadata,"x",{value:1}]);', + 'const change = Function.prototype.call.bind(Object.defineProperty,null); change(Object,"create",{value:customCreate});', + 'const first = Function.prototype.call.bind(Object.defineProperty,null); const change = first.bind(customReceiver,Object,"create"); change({value:customCreate});', + 'const change = Function.prototype.apply.bind(Object.defineProperty,null); change([Object,"create",{value:customCreate}]);', + 'Function.prototype.call.call(Object.defineProperty,null,Object,"create",{value:customCreate});', + 'Function.prototype.apply.call(Object.defineProperty,null,[Object,"create",{value:customCreate}]);', + 'const uncurry = Function.prototype.call.bind(Function.prototype.call); uncurry(Object.defineProperty,null,Object,"create",{value:customCreate});', 'const change = Reflect.set.bind(null,Object,"create"); change(customCreate);', 'const change = Object.defineProperty.call; change(null,Object,"create",{value:customCreate});', 'const {apply:change} = Object.defineProperty; change(null,[Object,"create",{value:customCreate}]);', @@ -562,6 +568,8 @@ describe("Electron module interop bound mutators", () => { 'const change = Object.defineProperty.bind(null); change(metadata,"x",{value:1});', 'const change = Object.defineProperty.bind(null,metadata,"x"); change({value:1});', 'const first = Object.defineProperty.bind(null,metadata); const change = first.bind(null,"x"); change({value:1});', + 'const change = Function.prototype.call.bind(Object.defineProperty,null); change(metadata,"x",{value:1});', + 'const change = Function.prototype.apply.bind(Object.defineProperty,null); change([metadata,"x",{value:1}]);', 'const change = Object.defineProperty.bind(null,Object,"create");', 'Object.defineProperty.call(null,metadata,"x",{value:1});', 'Object.defineProperty.apply(null,[metadata,"x",{value:1}]);', diff --git a/src/domain/javascript/javascriptElectronMemberWrites.ts b/src/domain/javascript/javascriptElectronMemberWrites.ts index ef3e7f22a..90f79bf7c 100644 --- a/src/domain/javascript/javascriptElectronMemberWrites.ts +++ b/src/domain/javascript/javascriptElectronMemberWrites.ts @@ -43,6 +43,8 @@ export const electronMemberPath = ( /** A known intrinsic callable and its examined positional arguments. */ export interface JavaScriptIntrinsicInvocation { + /** The original bound receiver; rebinding cannot replace it. */ + readonly receiver?: t.Node; readonly path: readonly string[]; readonly arguments: | readonly t.CallExpression["arguments"][number][] diff --git a/src/domain/javascript/javascriptIntrinsicInvocation.ts b/src/domain/javascript/javascriptIntrinsicInvocation.ts index f8eb6d2fd..22b1eddfc 100644 --- a/src/domain/javascript/javascriptIntrinsicInvocation.ts +++ b/src/domain/javascript/javascriptIntrinsicInvocation.ts @@ -5,11 +5,6 @@ import { semanticStaticPropertyKey, unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; -import { - resolveSemanticBindingState, - semanticStableInitializer, - type JavaScriptSemanticAnalysisState, -} from "./javascriptSemanticState.js"; const arrayArguments = (array: t.Node | undefined) => t.isArrayExpression(array) && @@ -35,62 +30,79 @@ const invocationArguments = ( */ export const resolveJavaScriptIntrinsicInvocation = ( node: t.CallExpression | t.OptionalCallExpression, - state: JavaScriptSemanticAnalysisState, intrinsicPath: (node: t.Node) => readonly string[] | undefined, boundIntrinsic: (node: t.Node) => JavaScriptIntrinsicInvocation | undefined, ): JavaScriptIntrinsicInvocation | undefined => { - const visited = new Set(); + const visited = new Set(); let callee: t.Node = node.callee; let arguments_: JavaScriptIntrinsicInvocation["arguments"] = node.arguments; + let receiver: t.Node | undefined; for (;;) { callee = unwrapJavaScriptExpression(callee).node; + if (visited.has(callee)) return undefined; + visited.add(callee); let path = intrinsicPath(callee); - // Static projections can capture call/apply themselves as aliases. - while (path !== undefined && path.length > 3) { + if (path === undefined) { + if ( + t.isMemberExpression(callee) || + t.isOptionalMemberExpression(callee) + ) { + const method = semanticStaticPropertyKey( + callee.property, + callee.computed, + ); + if (method === "call" || method === "apply") { + receiver = arguments_?.[0]; + callee = callee.object; + arguments_ = invocationArguments(method, arguments_); + continue; + } + } + const bound = boundIntrinsic(callee); + if (bound === undefined) return undefined; + path = bound.path; + receiver = bound.receiver; + arguments_ = + bound.arguments === undefined + ? undefined + : [...bound.arguments, ...(arguments_ ?? [])]; + } + // A projected .call/.apply invokes the preceding callable. The actual + // Function.prototype methods instead invoke their receiver, retained by bind. + while (path.length > 3 && !genericInvocationMethod(path)) { const method = path.at(-1); if (method !== "call" && method !== "apply") break; + receiver = arguments_?.[0]; arguments_ = invocationArguments(method, arguments_); path = path.slice(0, -1); } - if (path?.join(".") === "intrinsic.Reflect.apply") { + const genericMethod = genericInvocationMethod(path); + if (genericMethod !== undefined) { + if (!t.isExpression(receiver)) return undefined; + callee = receiver; + receiver = arguments_?.[0]; + arguments_ = invocationArguments(genericMethod, arguments_); + continue; + } + if (path.join(".") === "intrinsic.Reflect.apply") { const callable = arguments_?.[0]; if (!t.isExpression(callable)) return undefined; callee = callable; + receiver = arguments_?.[1]; arguments_ = arrayArguments(arguments_?.[2]); continue; } - if (path !== undefined) return { path, arguments: arguments_ }; - if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) { - const method = semanticStaticPropertyKey( - callee.property, - callee.computed, - ); - if (method === "call" || method === "apply") { - callee = callee.object; - arguments_ = invocationArguments(method, arguments_); - continue; - } - } - const bound = boundIntrinsic(callee); - if (bound !== undefined) - return { - path: bound.path, - arguments: - bound.arguments === undefined - ? undefined - : [...bound.arguments, ...(arguments_ ?? [])], - }; - if (!t.isIdentifier(callee)) return undefined; - const binding = resolveSemanticBindingState(state, callee, callee.name); - const initializer = semanticStableInitializer(binding, state); - if ( - binding === undefined || - initializer === undefined || - initializer.projection.length !== 0 || - visited.has(binding.bindingId) - ) - return undefined; - visited.add(binding.bindingId); - callee = initializer.node; + return { path, arguments: arguments_ }; } }; + +const genericInvocationMethod = ( + path: readonly string[], +): "call" | "apply" | undefined => + path.length === 4 && + path[0] === "intrinsic" && + path[1] === "Function" && + path[2] === "prototype" && + (path[3] === "call" || path[3] === "apply") + ? path[3] + : undefined; diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index bee2f4318..c55527f99 100644 --- a/src/domain/javascript/javascriptModuleInterop.ts +++ b/src/domain/javascript/javascriptModuleInterop.ts @@ -212,6 +212,7 @@ export const createSemanticModuleInterop = ( const argumentGroups: t.CallExpression["arguments"][] = []; const bindReads: t.Node[] = []; let opaqueArguments = false; + let receiver: t.Node | undefined; let current = node; for (;;) { current = unwrapJavaScriptExpression(current).node; @@ -243,11 +244,9 @@ export const createSemanticModuleInterop = ( // intervening members would require a separate proof of their identity. if (!terminal && members.length !== 1) return undefined; bindReads.push(captured?.capturedAt ?? current.callee); - const thisArgument = current.arguments[0]; + receiver = current.arguments[0]; const ownArguments = current.arguments.slice(1); - opaqueArguments ||= - thisArgument !== undefined && - !readExactJavaScriptLiteral(thisArgument).found; + opaqueArguments ||= t.isSpreadElement(receiver); opaqueArguments ||= ownArguments.some((argument) => t.isSpreadElement(argument), ); @@ -271,6 +270,7 @@ export const createSemanticModuleInterop = ( }); return { path, + ...(receiver === undefined ? {} : { receiver }), // Rebinding appends arguments to the earlier bound argument list. arguments: opaqueArguments ? undefined @@ -549,12 +549,7 @@ export const createSemanticModuleInterop = ( callOrigin, intrinsicPath, intrinsicInvocation: (node: t.CallExpression | t.OptionalCallExpression) => - resolveJavaScriptIntrinsicInvocation( - node, - state, - intrinsicPath, - boundIntrinsic, - ), + resolveJavaScriptIntrinsicInvocation(node, intrinsicPath, boundIntrinsic), boundIntrinsic, staticString, isFreshObject, diff --git a/tests/acceptance/applications/electronModuleInteropCli.test.ts b/tests/acceptance/applications/electronModuleInteropCli.test.ts index ed988df0f..d16051c1f 100644 --- a/tests/acceptance/applications/electronModuleInteropCli.test.ts +++ b/tests/acceptance/applications/electronModuleInteropCli.test.ts @@ -10,6 +10,26 @@ import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +const INTRINSIC_MUTATIONS = [ + ["poisoned.cjs", "({Object} = replacements);"], + [ + "projected.cjs", + 'const {defineProperty:change} = Object; change(Object,"create",{value:customCreate});', + ], + [ + "decorated.cjs", + '@(() => Object) class metadata {}; Object.defineProperty(metadata,"create",{value:customCreate});', + ], + [ + "uncurried.cjs", + 'const change = Function.prototype.call.bind(Object.defineProperty,null); change(Object,"create",{value:customCreate});', + ], + [ + "bound.cjs", + 'const change = Object.defineProperty.bind(null,Object,"create"); change({value:customCreate});', + ], +] as const; + cliTest.for(["CLI", "stdio MCP"] as const)( "retains converted Electron windows and preload evidence through %s", async (surface, { cli }) => { @@ -22,21 +42,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( join(root, "preload.js"), 'throw new Error("must stay inert");', ); - for (const [name, mutation] of [ - ["poisoned.cjs", "({Object} = replacements);"], - [ - "projected.cjs", - 'const {defineProperty:change} = Object; change(Object,"create",{value:customCreate});', - ], - [ - "decorated.cjs", - '@(() => Object) class metadata {}; Object.defineProperty(metadata,"create",{value:customCreate});', - ], - [ - "bound.cjs", - 'const change = Object.defineProperty.bind(null,Object,"create"); change({value:customCreate});', - ], - ] as const) + for (const [name, mutation] of INTRINSIC_MUTATIONS) await writeFile( join(root, name), `${mutation} @@ -86,6 +92,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( require("./projected.cjs"); require("./decorated.cjs"); require("./bound.cjs"); + require("./uncurried.cjs"); throw new Error("must not execute main"); `; await writeFile(join(root, "main.cjs"), source); @@ -112,7 +119,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( preload_entrypoints: 1, }); expect(result.statistics.parse_failures).toBe(0); - expect(result.statistics.parsed_javascript_files).toBe(6); + expect(result.statistics.parsed_javascript_files).toBe(7); const windows = result.graph.nodes.filter( ({ kind }) => kind === "browser-window", ); From ad27b8b4ff871deb60c607d14694f5faa93ecb11 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 12 Oct 2026 00:55:02 +0800 Subject: [PATCH 3/3] fix(javascript): prove generic binding and invocation effects --- docs/javascript-artifact-reconstruction.md | 5 +- .../javascript/electronModuleInterop.test.ts | 12 +++ .../javascriptElectronMemberWrites.ts | 6 ++ .../javascriptIntrinsicInvocation.ts | 17 +++- .../javascript/javascriptModuleInterop.ts | 96 ++++++++++++++++--- .../electronModuleInteropCli.test.ts | 12 ++- 6 files changed, 130 insertions(+), 18 deletions(-) diff --git a/docs/javascript-artifact-reconstruction.md b/docs/javascript-artifact-reconstruction.md index 405f93945..b8a6e5c6d 100644 --- a/docs/javascript-artifact-reconstruction.md +++ b/docs/javascript-artifact-reconstruction.md @@ -345,7 +345,10 @@ are unresolved. Known Object/Reflect mutators remain recognizable through literal destructuring of intrinsic objects and stable bound-function aliases. Calls retain bound argument prefixes and the original bound receiver through `call`, `apply`, and -`Reflect.apply`, including generic `Function.prototype` invocation; unexamined +`Reflect.apply`, including generic `Function.prototype` invocation and binding +creation. Visibly replaced invocation intrinsics produce unknown effects instead +of using their former call semantics; captured function aliases retain their +earlier identity. Unexamined targets remain unresolved. Creating an unused binding does not itself mutate an intrinsic. Variable redeclarations retain a single unconditional initializer when they introduce no assignment or second initializer. diff --git a/src/domain/javascript/electronModuleInterop.test.ts b/src/domain/javascript/electronModuleInterop.test.ts index 2fa971b83..c06b87ac3 100644 --- a/src/domain/javascript/electronModuleInterop.test.ts +++ b/src/domain/javascript/electronModuleInterop.test.ts @@ -552,6 +552,13 @@ describe("Electron module interop bound mutators", () => { 'Function.prototype.call.call(Object.defineProperty,null,Object,"create",{value:customCreate});', 'Function.prototype.apply.call(Object.defineProperty,null,[Object,"create",{value:customCreate}]);', 'const uncurry = Function.prototype.call.bind(Function.prototype.call); uncurry(Object.defineProperty,null,Object,"create",{value:customCreate});', + 'const change = Function.prototype.bind.call(Object.defineProperty,null); change(Object,"create",{value:customCreate});', + 'const bind = Function.prototype.call.bind(Function.prototype.bind); const change = bind(Object.defineProperty,null); change(Object,"create",{value:customCreate});', + 'const change = Function.prototype.bind.apply(Object.defineProperty,[null,Object,"create"]); change({value:customCreate});', + 'const change = Reflect.apply(Function.prototype.bind,Object.defineProperty,[null,Object,"create"]); change({value:customCreate});', + 'const first = Object.defineProperty.bind(null,Object); const change = Function.prototype.bind.call(first,null,"create"); change({value:customCreate});', + 'Reflect.apply = Object.defineProperty; Reflect.apply(Object,"create",{value:customCreate});', + 'const apply = Reflect.apply; Reflect.apply = Object.defineProperty; Reflect.apply(Object,"create",{value:customCreate});', 'const change = Reflect.set.bind(null,Object,"create"); change(customCreate);', 'const change = Object.defineProperty.call; change(null,Object,"create",{value:customCreate});', 'const {apply:change} = Object.defineProperty; change(null,[Object,"create",{value:customCreate}]);', @@ -570,6 +577,11 @@ describe("Electron module interop bound mutators", () => { 'const first = Object.defineProperty.bind(null,metadata); const change = first.bind(null,"x"); change({value:1});', 'const change = Function.prototype.call.bind(Object.defineProperty,null); change(metadata,"x",{value:1});', 'const change = Function.prototype.apply.bind(Object.defineProperty,null); change([metadata,"x",{value:1}]);', + 'const change = Function.prototype.bind.call(Object.defineProperty,null,metadata,"x"); change({value:1});', + 'const bind = Function.prototype.call.bind(Function.prototype.bind); const change = bind(Object.defineProperty,null,metadata,"x"); change({value:1});', + 'const change = Function.prototype.bind.apply(Object.defineProperty,[null,metadata,"x"]); change({value:1});', + 'const change = Reflect.apply(Function.prototype.bind,Object.defineProperty,[null,metadata,"x"]); change({value:1});', + 'const apply = Reflect.apply; Reflect.apply = replacement; apply(Object.defineProperty,null,[metadata,"x",{value:1}]);', 'const change = Object.defineProperty.bind(null,Object,"create");', 'Object.defineProperty.call(null,metadata,"x",{value:1});', 'Object.defineProperty.apply(null,[metadata,"x",{value:1}]);', diff --git a/src/domain/javascript/javascriptElectronMemberWrites.ts b/src/domain/javascript/javascriptElectronMemberWrites.ts index 90f79bf7c..3ee0b5044 100644 --- a/src/domain/javascript/javascriptElectronMemberWrites.ts +++ b/src/domain/javascript/javascriptElectronMemberWrites.ts @@ -43,6 +43,7 @@ export const electronMemberPath = ( /** A known intrinsic callable and its examined positional arguments. */ export interface JavaScriptIntrinsicInvocation { + readonly kind: "known" | "opaque"; /** The original bound receiver; rebinding cannot replace it. */ readonly receiver?: t.Node; readonly path: readonly string[]; @@ -82,6 +83,11 @@ export function* collectJavaScriptIdentityWritesSteps( enter: (node) => { if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) { const invocation = intrinsicInvocation(node); + if (invocation?.kind === "opaque") { + const scope = origin(node, "opaque-call"); + if (scope !== undefined) retain(node, scope); + return; + } const callee = invocation?.path; const arguments_ = invocation?.arguments; const method = callee?.at(-1); diff --git a/src/domain/javascript/javascriptIntrinsicInvocation.ts b/src/domain/javascript/javascriptIntrinsicInvocation.ts index 22b1eddfc..e34efe04a 100644 --- a/src/domain/javascript/javascriptIntrinsicInvocation.ts +++ b/src/domain/javascript/javascriptIntrinsicInvocation.ts @@ -32,6 +32,7 @@ export const resolveJavaScriptIntrinsicInvocation = ( node: t.CallExpression | t.OptionalCallExpression, intrinsicPath: (node: t.Node) => readonly string[] | undefined, boundIntrinsic: (node: t.Node) => JavaScriptIntrinsicInvocation | undefined, + intrinsicReadChanged: (node: t.Node, path: readonly string[]) => boolean, ): JavaScriptIntrinsicInvocation | undefined => { const visited = new Set(); let callee: t.Node = node.callee; @@ -42,6 +43,8 @@ export const resolveJavaScriptIntrinsicInvocation = ( if (visited.has(callee)) return undefined; visited.add(callee); let path = intrinsicPath(callee); + if (path !== undefined && intrinsicReadChanged(callee, path)) + return { kind: "opaque", path, arguments: undefined }; if (path === undefined) { if ( t.isMemberExpression(callee) || @@ -52,6 +55,12 @@ export const resolveJavaScriptIntrinsicInvocation = ( callee.computed, ); if (method === "call" || method === "apply") { + const bound = boundIntrinsic(callee.object); + if ( + bound !== undefined && + intrinsicReadChanged(callee, [...bound.path, method]) + ) + return { kind: "opaque", path: bound.path, arguments: undefined }; receiver = arguments_?.[0]; callee = callee.object; arguments_ = invocationArguments(method, arguments_); @@ -60,6 +69,7 @@ export const resolveJavaScriptIntrinsicInvocation = ( } const bound = boundIntrinsic(callee); if (bound === undefined) return undefined; + if (bound.kind === "opaque") return bound; path = bound.path; receiver = bound.receiver; arguments_ = @@ -92,7 +102,12 @@ export const resolveJavaScriptIntrinsicInvocation = ( arguments_ = arrayArguments(arguments_?.[2]); continue; } - return { path, arguments: arguments_ }; + return { + kind: "known", + path, + arguments: arguments_, + ...(receiver === undefined ? {} : { receiver }), + }; } }; diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index c55527f99..35e1c9ef1 100644 --- a/src/domain/javascript/javascriptModuleInterop.ts +++ b/src/domain/javascript/javascriptModuleInterop.ts @@ -205,7 +205,31 @@ export const createSemanticModuleInterop = ( } }; - const boundIntrinsic = ( + const intrinsicReadChanged = ( + node: t.Node, + path: readonly string[], + ): boolean => { + const capture = capturedIntrinsic(node)?.capturedAt ?? node; + const scope = opaqueMutationPath(capture).join("."); + const method = path.at(-1); + const invocationMethod = + method === "bind" || method === "call" || method === "apply"; + return writes.some( + (write) => + (write.scope === scope || write.scope === "intrinsic") && + write.offset < (capture.start ?? -1) && + (write.coverage.covers(path) || + (invocationMethod && + write.coverage.covers([ + "intrinsic", + "Function", + "prototype", + method, + ]))), + ); + }; + + const resolveBoundIntrinsic = ( node: t.Node, ): JavaScriptIntrinsicInvocation | undefined => { const visited = new Set(); @@ -236,26 +260,49 @@ export const createSemanticModuleInterop = ( continue; } if (!t.isCallExpression(current)) return undefined; - const { root: bindTarget, members } = electronMemberPath(current.callee); - if (members.at(-1) !== "bind") return undefined; - const captured = capturedIntrinsic(current.callee); - const terminal = captured?.path.at(-1) === "bind"; - // Only a direct .bind can continue a previously bound callable. Keeping - // intervening members would require a separate proof of their identity. - if (!terminal && members.length !== 1) return undefined; - bindReads.push(captured?.capturedAt ?? current.callee); - receiver = current.arguments[0]; - const ownArguments = current.arguments.slice(1); - opaqueArguments ||= t.isSpreadElement(receiver); + const { root, members } = electronMemberPath(current.callee); + let bindTarget = root; + let bindingArguments: JavaScriptIntrinsicInvocation["arguments"] = + current.arguments; + let captured = capturedIntrinsic(current.callee); + let terminal = captured?.path.at(-1) === "bind"; + let path: readonly string[] | undefined = captured?.path.slice(0, -1); + if (members.at(-1) === "bind") { + // A direct .bind may continue a bound callable, without discarding members. + if (!terminal && members.length !== 1) return undefined; + bindReads.push(captured?.capturedAt ?? current.callee); + } else { + // Reuse the invocation owner for generic call/apply/Reflect.apply of bind. + const invocation = resolveJavaScriptIntrinsicInvocation( + current, + intrinsicPath, + boundIntrinsic, + intrinsicReadChanged, + ); + if ( + invocation?.kind !== "known" || + invocation.path.join(".") !== "intrinsic.Function.prototype.bind" || + invocation.receiver === undefined + ) + return undefined; + bindTarget = invocation.receiver; + bindingArguments = invocation.arguments; + captured = capturedIntrinsic(bindTarget); + terminal = captured !== undefined; + path = captured?.path; + } + receiver = bindingArguments?.[0]; + const ownArguments = bindingArguments?.slice(1) ?? []; + opaqueArguments ||= + bindingArguments === undefined || t.isSpreadElement(receiver); opaqueArguments ||= ownArguments.some((argument) => t.isSpreadElement(argument), ); argumentGroups.push(ownArguments); - if (!terminal || captured === undefined) { + if (!terminal || path === undefined) { current = bindTarget; continue; } - const path = captured.path.slice(0, -1); const boundPath = [...path, "bind"]; const functionBind = ["intrinsic", "Function", "prototype", "bind"]; opaqueArguments ||= bindReads.some((capture) => { @@ -269,6 +316,7 @@ export const createSemanticModuleInterop = ( ); }); return { + kind: opaqueArguments ? "opaque" : "known", path, ...(receiver === undefined ? {} : { receiver }), // Rebinding appends arguments to the earlier bound argument list. @@ -279,6 +327,19 @@ export const createSemanticModuleInterop = ( } }; + const activeBoundValues = new WeakSet(); + const boundIntrinsic = ( + node: t.Node, + ): JavaScriptIntrinsicInvocation | undefined => { + if (activeBoundValues.has(node)) return undefined; + activeBoundValues.add(node); + try { + return resolveBoundIntrinsic(node); + } finally { + activeBoundValues.delete(node); + } + }; + const isFreshObject = (node: t.Node): boolean => { const visited = new Set(); let current = node; @@ -549,7 +610,12 @@ export const createSemanticModuleInterop = ( callOrigin, intrinsicPath, intrinsicInvocation: (node: t.CallExpression | t.OptionalCallExpression) => - resolveJavaScriptIntrinsicInvocation(node, intrinsicPath, boundIntrinsic), + resolveJavaScriptIntrinsicInvocation( + node, + intrinsicPath, + boundIntrinsic, + intrinsicReadChanged, + ), boundIntrinsic, staticString, isFreshObject, diff --git a/tests/acceptance/applications/electronModuleInteropCli.test.ts b/tests/acceptance/applications/electronModuleInteropCli.test.ts index d16051c1f..9e6387c98 100644 --- a/tests/acceptance/applications/electronModuleInteropCli.test.ts +++ b/tests/acceptance/applications/electronModuleInteropCli.test.ts @@ -24,6 +24,14 @@ const INTRINSIC_MUTATIONS = [ "uncurried.cjs", 'const change = Function.prototype.call.bind(Object.defineProperty,null); change(Object,"create",{value:customCreate});', ], + [ + "generic-bind.cjs", + 'const change = Function.prototype.bind.call(Object.defineProperty,null); change(Object,"create",{value:customCreate});', + ], + [ + "replaced-apply.cjs", + 'Reflect.apply = Object.defineProperty; Reflect.apply(Object,"create",{value:customCreate});', + ], [ "bound.cjs", 'const change = Object.defineProperty.bind(null,Object,"create"); change({value:customCreate});', @@ -93,6 +101,8 @@ cliTest.for(["CLI", "stdio MCP"] as const)( require("./decorated.cjs"); require("./bound.cjs"); require("./uncurried.cjs"); + require("./generic-bind.cjs"); + require("./replaced-apply.cjs"); throw new Error("must not execute main"); `; await writeFile(join(root, "main.cjs"), source); @@ -119,7 +129,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)( preload_entrypoints: 1, }); expect(result.statistics.parse_failures).toBe(0); - expect(result.statistics.parsed_javascript_files).toBe(7); + expect(result.statistics.parsed_javascript_files).toBe(9); const windows = result.graph.nodes.filter( ({ kind }) => kind === "browser-window", );