fix(evidence): preserve literal keys and response body ownership

Retain exact empty and computed literal keys in JavaScript observations. Bind CDP response bodies to request occurrences, account for late responses, and validate the target after final progress callbacks.
This commit is contained in:
morluto
2026-10-12 16:17:29 +08:00
parent 79156e5bed
commit d1dcebce2c
10 changed files with 575 additions and 210 deletions
@@ -1,172 +1,200 @@
import { describe, expect, it } from "vitest";
import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
describe("Electron API identity", () => {
it("recovers APIs through an ES module default import of Electron", () => {
const analysis = analyzeJavaScriptStaticSource(`
import electron from "electron";
const { ipcMain } = electron;
new electron.BrowserWindow({ webPreferences: { preload: "./preload.js" } });
electron.contextBridge.exposeInMainWorld("api", { ping: true });
electron.ipcRenderer.invoke("default:ping");
ipcMain.handle("destructured:ping", () => "pong");
electron.default.ipcMain.handle("nested-default", () => "local");
`);
it("recovers APIs through an ES module default import of Electron", () => {
const analysis = analyzeJavaScriptStaticSource(`
import electron from "electron";
const { ipcMain } = electron;
new electron.BrowserWindow({ webPreferences: { preload: "./preload.js" } });
electron.contextBridge.exposeInMainWorld("api", { ping: true });
electron.ipcRenderer.invoke("default:ping");
ipcMain.handle("destructured:ping", () => "pong");
electron.default.ipcMain.handle("nested-default", () => "local");
`);
expect(analysis.electron.browser_windows).toMatchObject([
{ preload_path: "./preload.js" },
]);
expect(analysis.electron.context_bridge_apis).toMatchObject([
{ api_key: "api", members: ["ping"] },
]);
expect(
analysis.electron.ipc.map(({ side, operation, channel }) => ({
side,
operation,
channel,
})),
).toEqual([
{ side: "renderer", operation: "invoke", channel: "default:ping" },
{ side: "main", operation: "handle", channel: "destructured:ping" },
]);
expect(analysis.electron.browser_windows).toMatchObject([
{ preload_path: "./preload.js" },
]);
expect(analysis.electron.context_bridge_apis).toMatchObject([
{ api_key: "api", members: ["ping"] },
]);
expect(
analysis.electron.ipc.map(({ side, operation, channel }) => ({
side,
operation,
channel,
})),
).toEqual([
{ side: "renderer", operation: "invoke", channel: "default:ping" },
{ side: "main", operation: "handle", channel: "destructured:ping" },
]);
});
it("recovers APIs and sender checks written with optional chaining", () => {
const analysis = analyzeJavaScriptStaticSource(`
const { contextBridge, ipcMain, utilityProcess } = require("electron");
contextBridge?.exposeInMainWorld("api", {});
utilityProcess?.fork("./worker.js");
ipcMain?.handle("optional:ping", (event) => {
if (!event.senderFrame?.url.startsWith("file://")) return null;
return event.sender?.getURL() === "file:///index.html";
});
`);
expect(analysis.electron.context_bridge_apis).toMatchObject([
{ api_key: "api" },
]);
expect(analysis.electron.utility_processes).toMatchObject([
{ module_path: "./worker.js" },
]);
expect(analysis.electron.ipc).toMatchObject([
{ side: "main", operation: "handle", channel: "optional:ping" },
]);
expect(
analysis.electron.sender_validations.map(
({ subject, mechanism, expected }) => ({
subject,
mechanism,
expected: expected.value,
}),
),
).toEqual([
{
subject: "sender-url",
mechanism: "call:startsWith",
expected: "file://",
},
{
subject: "sender-url",
mechanism: "comparison:===",
expected: "file:///index.html",
},
]);
});
it("preserves empty and computed literal contextBridge member keys", () => {
const analysis = analyzeJavaScriptStaticSource(`
const { contextBridge } = require("electron");
const dynamicKey = getKey();
contextBridge.exposeInMainWorld("api", {
"": { nested: { "": true } },
["computed"]: true,
});
contextBridge.exposeInMainWorld("dynamic", {
[dynamicKey]: true,
});
`);
const finding = analysis.electron.context_bridge_apis.find(
({ api_key }) => api_key === "api",
);
expect(finding).toMatchObject({
api_status: "object-literal",
members: ["", ".nested", ".nested.", "computed"],
unknown_members: 0,
});
it("recovers APIs and sender checks written with optional chaining", () => {
const analysis = analyzeJavaScriptStaticSource(`
const { contextBridge, ipcMain, utilityProcess } = require("electron");
contextBridge?.exposeInMainWorld("api", {});
utilityProcess?.fork("./worker.js");
ipcMain?.handle("optional:ping", (event) => {
if (!event.senderFrame?.url.startsWith("file://")) return null;
return event.sender?.getURL() === "file:///index.html";
});
`);
expect(analysis.electron.context_bridge_apis).toMatchObject([
{ api_key: "api" },
]);
expect(analysis.electron.utility_processes).toMatchObject([
{ module_path: "./worker.js" },
]);
expect(analysis.electron.ipc).toMatchObject([
{ side: "main", operation: "handle", channel: "optional:ping" },
]);
expect(
analysis.electron.sender_validations.map(
({ subject, mechanism, expected }) => ({
subject,
mechanism,
expected: expected.value,
}),
),
).toEqual([
{
subject: "sender-url",
mechanism: "call:startsWith",
expected: "file://",
},
{
subject: "sender-url",
mechanism: "comparison:===",
expected: "file:///index.html",
},
]);
});
it("follows bundler namespace interop helpers without accepting other writes", () => {
const analysis = analyzeJavaScriptStaticSource(`
var __toESM = (mod) => mod;
var __importStar = (mod) => mod;
let electron = require("electron");
electron = __toESM(electron);
new electron.BrowserWindow({ webPreferences: { preload: "./rolldown.js" } });
var import_electron = __toESM(require("electron"));
new import_electron.BrowserWindow({ webPreferences: { preload: "./esbuild.js" } });
function _interopRequireWildcard(mod) { return mod; }
const Electron = __importStar(require("electron"));
new Electron.BrowserWindow({ webPreferences: { preload: "./tsc.js" } });
let replaced = require("electron");
replaced = __toESM(require("./fake"));
new replaced.BrowserWindow({ webPreferences: { preload: "./replaced.js" } });
let rewritten = require("electron");
rewritten = __toESM(rewritten);
rewritten = fake;
new rewritten.BrowserWindow({ webPreferences: { preload: "./rewritten.js" } });
const babel = _interopRequireWildcard(require("electron"));
new babel.BrowserWindow({ webPreferences: { preload: "./babel.js" } });
function nested() {
const __toESM = (mod) => mod;
const scoped = __toESM(require("electron"));
new scoped.BrowserWindow({ webPreferences: { preload: "./nested.js" } });
}
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./rolldown.js", "./esbuild.js", "./tsc.js", "./babel.js"]);
});
it("does not trust unbound helper names or non-function helper values", () => {
const analysis = analyzeJavaScriptStaticSource(`
const __toESM = makeLookalike();
const electron = __toESM(require("electron"));
new electron.BrowserWindow({});
let unbound = require("electron");
unbound = __importStar(unbound);
new unbound.BrowserWindow({});
`);
expect(analysis.electron.browser_windows).toEqual([]);
});
it("follows stable declaration aliases but not an assigned default parameter", () => {
const analysis = analyzeJavaScriptStaticSource(`
const direct = require("electron");
const constAlias = direct;
new constAlias.BrowserWindow({webPreferences:{preload:"./const.js"}});
let letAlias = require("electron");
new letAlias.BrowserWindow({webPreferences:{preload:"./let.js"}});
const { BrowserWindow } = require("electron");
new BrowserWindow({webPreferences:{preload:"./destructured.js"}});
function withDefault(electron = require("./not-electron")) {
if (flag) electron = require("electron");
new electron.BrowserWindow({webPreferences:{preload:"./default.js"}});
}
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./const.js", "./let.js", "./destructured.js"]);
});
it("preserves one initializer across variable redeclarations without accepting writes", () => {
const analysis = analyzeJavaScriptStaticSource(`
var electron;
var electron = require("electron");
var electron;
new electron.BrowserWindow({webPreferences:{preload:"./redeclared.js"}});
var replaced;
var replaced = require("electron");
replaced = fake;
new replaced.BrowserWindow({webPreferences:{preload:"./replaced.js"}});
var initializedTwice = require("electron");
var initializedTwice = fake;
new initializedTwice.BrowserWindow({});
var conditional;
if (flag) { var conditional = require("electron"); }
new conditional.BrowserWindow({});
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./redeclared.js"]);
});
expect(finding?.resolution_basis).toBeUndefined();
expect(analysis.electron.context_bridge_apis).toContainEqual(
expect.objectContaining({
api_key: "dynamic",
unknown_members: 1,
}),
);
});
it("follows bundler namespace interop helpers without accepting other writes", () => {
const analysis = analyzeJavaScriptStaticSource(`
var __toESM = (mod) => mod;
var __importStar = (mod) => mod;
let electron = require("electron");
electron = __toESM(electron);
new electron.BrowserWindow({ webPreferences: { preload: "./rolldown.js" } });
var import_electron = __toESM(require("electron"));
new import_electron.BrowserWindow({ webPreferences: { preload: "./esbuild.js" } });
function _interopRequireWildcard(mod) { return mod; }
const Electron = __importStar(require("electron"));
new Electron.BrowserWindow({ webPreferences: { preload: "./tsc.js" } });
let replaced = require("electron");
replaced = __toESM(require("./fake"));
new replaced.BrowserWindow({ webPreferences: { preload: "./replaced.js" } });
let rewritten = require("electron");
rewritten = __toESM(rewritten);
rewritten = fake;
new rewritten.BrowserWindow({ webPreferences: { preload: "./rewritten.js" } });
const babel = _interopRequireWildcard(require("electron"));
new babel.BrowserWindow({ webPreferences: { preload: "./babel.js" } });
function nested() {
const __toESM = (mod) => mod;
const scoped = __toESM(require("electron"));
new scoped.BrowserWindow({ webPreferences: { preload: "./nested.js" } });
}
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./rolldown.js", "./esbuild.js", "./tsc.js", "./babel.js"]);
});
it("does not trust unbound helper names or non-function helper values", () => {
const analysis = analyzeJavaScriptStaticSource(`
const __toESM = makeLookalike();
const electron = __toESM(require("electron"));
new electron.BrowserWindow({});
let unbound = require("electron");
unbound = __importStar(unbound);
new unbound.BrowserWindow({});
`);
expect(analysis.electron.browser_windows).toEqual([]);
});
it("follows stable declaration aliases but not an assigned default parameter", () => {
const analysis = analyzeJavaScriptStaticSource(`
const direct = require("electron");
const constAlias = direct;
new constAlias.BrowserWindow({webPreferences:{preload:"./const.js"}});
let letAlias = require("electron");
new letAlias.BrowserWindow({webPreferences:{preload:"./let.js"}});
const { BrowserWindow } = require("electron");
new BrowserWindow({webPreferences:{preload:"./destructured.js"}});
function withDefault(electron = require("./not-electron")) {
if (flag) electron = require("electron");
new electron.BrowserWindow({webPreferences:{preload:"./default.js"}});
}
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./const.js", "./let.js", "./destructured.js"]);
});
it("preserves one initializer across variable redeclarations without accepting writes", () => {
const analysis = analyzeJavaScriptStaticSource(`
var electron;
var electron = require("electron");
var electron;
new electron.BrowserWindow({webPreferences:{preload:"./redeclared.js"}});
var replaced;
var replaced = require("electron");
replaced = fake;
new replaced.BrowserWindow({webPreferences:{preload:"./replaced.js"}});
var initializedTwice = require("electron");
var initializedTwice = fake;
new initializedTwice.BrowserWindow({});
var conditional;
if (flag) { var conditional = require("electron"); }
new conditional.BrowserWindow({});
`);
expect(
analysis.electron.browser_windows.map(({ preload_path }) => preload_path),
).toEqual(["./redeclared.js"]);
});
@@ -4,6 +4,7 @@ import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import type { ElectronStaticValue } from "./electronStaticAnalysisTypes.js";
import {
readExactJavaScriptLiteral,
semanticStaticPropertyKey,
semanticStaticPropertyName,
} from "./javascriptAstValues.js";
import { sourceSlice } from "./javascriptStaticAnalysisHelpers.js";
@@ -124,7 +125,7 @@ export const collectContextBridgeMembers = (
members: [],
unknown: 0,
};
collectMembersAt(node, "", 0, state);
collectMembersAt(node, [], state);
const members = [...new Set(state.members)].sort(compareUnicodeCodePoints);
return {
status: "object-literal",
@@ -135,11 +136,10 @@ export const collectContextBridgeMembers = (
const collectMembersAt = (
object: t.ObjectExpression,
prefix: string,
depth: number,
prefix: readonly string[],
state: { members: string[]; unknown: number },
): void => {
if (depth >= 8) {
if (prefix.length >= 8) {
state.unknown += 1;
return;
}
@@ -148,15 +148,16 @@ const collectMembersAt = (
state.unknown += 1;
continue;
}
const name = semanticStaticPropertyName(property.key, property.computed);
if (name === "" || property.computed) {
const name = semanticStaticPropertyKey(property.key, property.computed);
if (name === null) {
state.unknown += 1;
continue;
}
const path = prefix === "" ? name : `${prefix}.${name}`;
const segments = [...prefix, name];
const path = segments.join(".");
state.members.push(path);
if (t.isObjectProperty(property) && t.isObjectExpression(property.value))
collectMembersAt(property.value, path, depth + 1, state);
collectMembersAt(property.value, segments, state);
}
};
@@ -503,6 +503,38 @@ it("resolves renamed default and namespace import helpers from their syntax", ()
.every((step) => step.label.length > 0),
).toBe(true);
});
it("resolves an empty bootstrap module key through an exported Electron bridge", () => {
const result = analyzeJavaScriptStaticSource(`(() => {
const modules = {
""(module) { module.exports = require("electron"); },
9(module, exports, load) {
const electron = load("");
electron.contextBridge.exposeInMainWorld("desktop", {
copy: () => electron.ipcRenderer.send("test:empty-module"),
});
},
};
function load(id) {
const module = { exports: {} };
modules[id].call(module.exports, module, module.exports, load);
return module.exports;
}
load(9);
})();`);
expect(result.bundler_registrations).toMatchObject([
{
entry_module_keys: ["9"],
modules: [{ module_key: "" }, { module_key: "9" }],
},
]);
expect(result.electron.call_paths).toContainEqual(
expect.objectContaining({
entry: "desktop.copy",
channel: "test:empty-module",
terminal: "electron.ipcRenderer.send",
}),
);
});
it("does not trust helper names or follow returns after a definite throw", () => {
for (const helper of [
`function __toESM() {return {ipcRenderer:{send(){}}}}`,
@@ -13,6 +13,7 @@ import type {
} from "./javascriptStaticAnalysisTypes.js";
import {
readExactJavaScriptLiteral,
semanticStaticPropertyKey,
semanticStaticPropertyName,
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
@@ -131,15 +132,15 @@ export const moduleFactory = (
/** Derive a literal or explicitly computed module key. */
export const modulePropertyName = (
property: t.ObjectMethod | t.ObjectProperty | t.SpreadElement,
): string =>
t.isObjectMethod(property) || t.isObjectProperty(property)
? property.computed &&
!t.isStringLiteral(property.key) &&
!t.isNumericLiteral(property.key)
? `[computed@${String(property.start ?? -1)}]`
: semanticStaticPropertyName(property.key, property.computed) ||
`[unknown@${String(property.start ?? -1)}]`
): string => {
if (!t.isObjectMethod(property) && !t.isObjectProperty(property))
return `[unknown@${String(property.start ?? -1)}]`;
const key = semanticStaticPropertyKey(property.key, property.computed);
if (key !== null) return key;
return property.computed
? `[computed@${String(property.start ?? -1)}]`
: `[unknown@${String(property.start ?? -1)}]`;
};
/** Read the factory-local bundler require parameter when declared. */
export const factoryRequireName = (