diff --git a/src/domain/javascript/electronBundledRequireIdentity.test.ts b/src/domain/javascript/electronBundledRequireIdentity.test.ts index 15f3fea848..7db07d1808 100644 --- a/src/domain/javascript/electronBundledRequireIdentity.test.ts +++ b/src/domain/javascript/electronBundledRequireIdentity.test.ts @@ -53,6 +53,10 @@ describe("Electron identity through Webpack externals", () => { ["const key='45840';delete n.m[key];", 0], ["n.m[runtimeKey]=local;", 0], ["const key=45841;n.m[key]=local;", 1], + ["const key=flag?45841:'45842';n.m[key]=local;", 1], + ["const key=flag?45840:'45842';n.m[key]=local;", 0], + ["const key=flag?dynamicKey:'45842';n.m[key]=local;", 0], + ["const key=flag?'__proto__':'45842';n.m[key]=local;", 0], ] as const)( "preserves only unmodified external slots after %s", (write, expected) => { @@ -198,6 +202,21 @@ describe("Electron identity through Webpack externals", () => { }); }); +it.each([ + ["45841", 1], + ["45840", 0], +] as const)( + "scopes writes to cache slots when one key is %s", + (key, expected) => { + const source = bootstrap( + EXTERNAL, + CONSUMER, + `n.c=c;const key=flag?${key}:"45842";n.c[key]={exports:local};n(100);`, + ); + expect(electronOf(source).browser_windows).toHaveLength(expected); + }, +); + describe("Webpack loader alias bounds", () => { const getterSource = (initializers: readonly string[], selection = "") => bootstrap( diff --git a/src/domain/javascript/javascriptBootstrapIntegrity.ts b/src/domain/javascript/javascriptBootstrapIntegrity.ts index 8dc5737529..ff8703f57b 100644 --- a/src/domain/javascript/javascriptBootstrapIntegrity.ts +++ b/src/domain/javascript/javascriptBootstrapIntegrity.ts @@ -17,7 +17,7 @@ import { evaluateSemanticExpression } from "./javascriptSemanticValues.js"; type Bootstrap = NonNullable>; -/** Slots a visible literal write may have replaced; the rest stay as declared. */ +/** Slots a visible write may have replaced; the rest stay as declared. */ export interface JavaScriptBootstrapIntegrity { readonly replacedKeys: ReadonlySet; } @@ -38,8 +38,8 @@ interface LoaderShape { * `table[key]`: every normal completion of the loader returns the dispatched * module's exports or a cache entry it created for that module, the table has * only literal keys, and the table and cache are referenced only by the loader - * or through the runtime's `loader.m`/`loader.c` exposure. Literal-key writes - * through that exposure replace their slot. Returns undefined when any of this + * or through the runtime's `loader.m`/`loader.c` exposure. Writes with finite keys + * through that exposure invalidate each affected slot. Returns undefined when any of this * is unproven. */ export const proveJavaScriptBootstrapIntegrity = ( @@ -612,7 +612,7 @@ const exposedTableUse = (use: Use): "other" | "contained" | "escaped" => { return entryWritten ? "escaped" : "contained"; }; -/** Unknown computed writes may replace any existing slot; fresh installation is unproven. */ +/** Retain all finite written keys. An unknown key leaves every slot uncertain. */ const exposedSlotWrite = (use: Use, replacedKeys: Set): boolean => { const { parent, ancestors, state } = use; const slot = ancestors.at(-2); @@ -632,19 +632,28 @@ const exposedSlotWrite = (use: Use, replacedKeys: Set): boolean => { ) ) return true; - if (!slot.computed) { - const key = semanticStaticPropertyKey(slot.property, false); - if (key === null) return false; - replacedKeys.add(key); - return true; - } - const key = evaluateSemanticExpression(slot.property, state); + const key = slot.computed + ? evaluateSemanticExpression(slot.property, state) + : { + status: "literal" as const, + value: semanticStaticPropertyKey(slot.property, false), + }; + const keys = + key.status === "union" + ? key.values + : key.status === "literal" + ? [key.value] + : []; if ( - key.status !== "literal" || - (typeof key.value !== "string" && typeof key.value !== "number") + keys.length === 0 || + keys.some( + (key) => + (typeof key !== "string" && typeof key !== "number") || + key === "__proto__", + ) ) return false; - replacedKeys.add(String(key.value)); + for (const key of keys) replacedKeys.add(String(key)); return true; }; diff --git a/tests/boundary/cli/javascriptApplicationCoverage.test.ts b/tests/boundary/cli/javascriptApplicationCoverage.test.ts index 3bb21d6101..caeb4027e3 100644 --- a/tests/boundary/cli/javascriptApplicationCoverage.test.ts +++ b/tests/boundary/cli/javascriptApplicationCoverage.test.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -140,3 +141,55 @@ for (const surface of ["CLI", "stdio MCP"] as const) { }, ); } + +for (const surface of ["CLI", "stdio MCP"] as const) { + cliTest( + `retains a Webpack external outside the finite written slots through ${surface}`, + async ({ cli }) => { + const root = await createTestTempDirectory("rea-webpack-slot-coverage-"); + const source = `(() => { + const table = { + 1: module => { module.exports = require("electron"); }, + 2: (module, exports, load) => { + const electron = load(1); + new electron.BrowserWindow({}); + electron.ipcMain.handle("ready", () => {}); + } + }; + function load(key) { + const module = { exports: {} }; + table[key](module, module.exports, load); + return module.exports; + } + load.m = table; + const key = flag ? 3 : "4"; + load.m[key] = module => { module.exports = {}; }; + load(2); + })();`; + await writeFixtureFiles(root, { "main.cjs": source }); + const application = await analyzeJavaScriptDirectory(surface, cli, root); + expect(application.summary).toMatchObject({ browser_windows: 1 }); + const windows = application.graph.nodes + .filter(({ kind }) => kind === "browser-window") + .flatMap(({ observations }) => observations); + expect(windows).toContainEqual( + expect.objectContaining({ + evidence: expect.objectContaining({ + state: "observed", + artifact: expect.objectContaining({ + available: true, + sha256: createHash("sha256").update(source).digest("hex"), + }), + location: expect.objectContaining({ + available: true, + value: expect.objectContaining({ + source: "main.cjs", + start: expect.objectContaining({ line: 6 }), + }), + }), + }), + }), + ); + }, + ); +}