diff --git a/docs/javascript-artifact-reconstruction.md b/docs/javascript-artifact-reconstruction.md index 1bbbb6af0..76d3af8b2 100644 --- a/docs/javascript-artifact-reconstruction.md +++ b/docs/javascript-artifact-reconstruction.md @@ -344,11 +344,15 @@ 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 -targets remain unresolved. Creating an unused binding does not itself mutate -an intrinsic. Generic `Function.prototype.call` and `apply` follow the callable -receiver, including a bound receiver; rebinding does not replace an earlier -bound receiver. Variable redeclarations retain a single unconditional initializer +argument prefixes and the original bound receiver through `call`, `apply`, and +`Reflect.apply`, including generic `Function.prototype` invocation and binding +creation. Arguments follow each actual callable invocation rather than property +chain depth; rebinding retains the original bound receiver. +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. Stable identifier chains to fresh object, array, function, undecorated class or RegExp syntax keep those targets separate from intrinsic diff --git a/src/domain/javascript/electronModuleInterop.test.ts b/src/domain/javascript/electronModuleInterop.test.ts index 8dcb71587..146bd849f 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}`, @@ -564,6 +592,19 @@ 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 = 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.call(Object.defineProperty,null,Object,"create",{value:customCreate});', 'const {apply:change} = Object.defineProperty; change.call(Object.defineProperty,null,[Object,"create",{value:customCreate}]);', @@ -580,6 +621,13 @@ 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 = 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}]);', @@ -593,3 +641,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..3ee0b5044 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 = @@ -44,6 +43,9 @@ 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[]; readonly arguments: | readonly t.CallExpression["arguments"][number][] @@ -60,6 +62,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 { @@ -80,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); @@ -100,7 +108,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 e36f85d6f..20885069f 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,88 +30,86 @@ 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, + intrinsicReadChanged: (node: t.Node, path: readonly string[]) => boolean, ): JavaScriptIntrinsicInvocation | undefined => { - const visited = new Set(); + const visited = new Set(); let callee: t.Node = node.callee; let arguments_: JavaScriptIntrinsicInvocation["arguments"] = node.arguments; - let thisArgument: t.Node | undefined; + let receiver: t.Node | undefined; let explicitReceiver = false; for (;;) { callee = unwrapJavaScriptExpression(callee).node; - const path = intrinsicPath(callee); - const method = - path !== undefined && path.length > 3 ? path.at(-1) : undefined; + 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) || + t.isOptionalMemberExpression(callee) + ) { + const method = semanticStaticPropertyKey( + callee.property, + 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]; + explicitReceiver = true; + callee = callee.object; + arguments_ = invocationArguments(method, arguments_); + continue; + } + } + const bound = boundIntrinsic(callee); + if (bound === undefined) return undefined; + if (bound.kind === "opaque") return bound; + path = bound.path; + receiver = bound.receiver; + explicitReceiver = true; + arguments_ = + bound.arguments === undefined + ? undefined + : [...bound.arguments, ...(arguments_ ?? [])]; + } + const method = path.length > 3 ? path.at(-1) : undefined; if (method === "call" || method === "apply") { // These properties hold generic function values. Their receiver selects // the callable; property-chain depth does not consume more arguments. const callable: t.Node | undefined = explicitReceiver - ? thisArgument + ? receiver : t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee) ? callee.object : undefined; if (!t.isExpression(callable)) return undefined; callee = callable; - thisArgument = arguments_?.[0]; + receiver = arguments_?.[0]; explicitReceiver = true; arguments_ = invocationArguments(method, arguments_); continue; } - if (path?.join(".") === "intrinsic.Reflect.apply") { + if (path.join(".") === "intrinsic.Reflect.apply") { const callable = arguments_?.[0]; if (!t.isExpression(callable)) return undefined; callee = callable; - thisArgument = arguments_?.[1]; + receiver = arguments_?.[1]; explicitReceiver = true; 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; - thisArgument = arguments_?.[0]; - explicitReceiver = true; - arguments_ = invocationArguments(method, arguments_); - 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); - // An inner bind owns the receiver even when the result is rebound. - thisArgument = callee.arguments[0]; - explicitReceiver = true; - arguments_ = t.isSpreadElement(callee.arguments[0]) - ? undefined - : prefix.length === 0 - ? arguments_ - : [...prefix, ...(arguments_ ?? [])]; - callee = binder.object; - continue; - } - 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 { + kind: "known", + path, + arguments: arguments_, + ...(receiver === undefined ? {} : { receiver }), + }; } }; diff --git a/src/domain/javascript/javascriptModuleInterop.ts b/src/domain/javascript/javascriptModuleInterop.ts index 542fbbd39..35e1c9ef1 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,170 @@ 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 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(); + 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; + 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, 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 || path === undefined) { + current = bindTarget; + continue; + } + 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 { + kind: opaqueArguments ? "opaque" : "known", + path, + ...(receiver === undefined ? {} : { receiver }), + // Rebinding appends arguments to the earlier bound argument list. + arguments: opaqueArguments + ? undefined + : argumentGroups.toReversed().flat(), + }; + } + }; + + 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; @@ -443,7 +610,14 @@ export const createSemanticModuleInterop = ( callOrigin, intrinsicPath, intrinsicInvocation: (node: t.CallExpression | t.OptionalCallExpression) => - resolveJavaScriptIntrinsicInvocation(node, state, intrinsicPath), + resolveJavaScriptIntrinsicInvocation( + node, + intrinsicPath, + boundIntrinsic, + intrinsicReadChanged, + ), + 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); diff --git a/tests/acceptance/applications/electronModuleInteropCli.test.ts b/tests/acceptance/applications/electronModuleInteropCli.test.ts index 73e6943e4..8c0b8a40d 100644 --- a/tests/acceptance/applications/electronModuleInteropCli.test.ts +++ b/tests/acceptance/applications/electronModuleInteropCli.test.ts @@ -10,34 +10,44 @@ 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});', + ], + [ + "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});', + ], +] as const; + const writePoisonedModules = async (root: string): Promise => { - 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});', - ], - [ - "uncurried.cjs", - 'const change = Function.prototype.call.bind(Object.defineProperty,null,Object,"create"); change({value:customCreate});', - ], - ] as const) + for (const [name, mutation] of INTRINSIC_MUTATIONS) await writeFile( join(root, name), `${mutation} - ${ROLLDOWN_MODULE_INTEROP} - let poisoned = require("electron"); - poisoned = __toESM(poisoned, 1); - new poisoned.BrowserWindow({}); - throw new Error("must not execute poisoned source");`, + ${ROLLDOWN_MODULE_INTEROP} + let poisoned = require("electron"); + poisoned = __toESM(poisoned, 1); + new poisoned.BrowserWindow({}); + throw new Error("must not execute poisoned source");`, ); }; @@ -97,6 +107,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); @@ -123,7 +135,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", );