fix(javascript): trace nested Electron bridge members (#2745)
* fix(javascript): trace nested Electron bridge members * fix(javascript): preserve shallow Electron bridge paths * fix(javascript): preserve exact Electron bridge member paths * fix(javascript): preserve exact Electron bridge paths * fix(javascript): preserve bridge entry ownership * fix(javascript): preserve bridge path identity * fix(javascript): normalize typed feature seeds before tracing * test(javascript): normalize direct selector seed inputs * refactor(javascript): consume normalized feature seed defaults --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -23,7 +23,7 @@ const analyze = async (cli: TestCli, extra = "") => {
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
expect(output.exitCode, output.stderr).toBe(0);
|
||||
const { normalized_result: result } = z
|
||||
.object({
|
||||
normalized_result: javascriptApplicationAnalysisResultSchema,
|
||||
@@ -122,6 +122,190 @@ cliTest(
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"traces a nested contextBridge member through a unique IPC channel",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-nested-bridge-");
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
|
||||
contextBridge.exposeInMainWorld("api", {
|
||||
nested: { write: (value) => ipcRenderer.invoke("nested:write", value) }
|
||||
});`,
|
||||
"main.js": `const { ipcMain, clipboard } = require("electron");
|
||||
ipcMain.handle("nested:write", (event, value) => clipboard.writeText(value));`,
|
||||
});
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode, output.stderr).toBe(0);
|
||||
const { normalized_result: application } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(output.json);
|
||||
const { graph } = application;
|
||||
const paths = graph.nodes
|
||||
.flatMap((node) => node.observations)
|
||||
.filter(
|
||||
(observation) =>
|
||||
observation.properties.entry === "api.nested.write" &&
|
||||
observation.properties.entry_kind === "bridge-member",
|
||||
);
|
||||
expect(
|
||||
paths.some(
|
||||
(observation) => observation.properties.step_kind === "exposure",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
paths.some(
|
||||
(observation) => observation.properties.step_kind === "terminal",
|
||||
),
|
||||
).toBe(true);
|
||||
const joins = graph.edges.filter(
|
||||
(edge) =>
|
||||
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
|
||||
);
|
||||
expect(joins.map((edge) => edge.properties.channel)).toEqual([
|
||||
"nested:write",
|
||||
]);
|
||||
|
||||
const traceInput = join(root, "trace-nested-input.json");
|
||||
await writeFile(
|
||||
traceInput,
|
||||
JSON.stringify({
|
||||
application: output.json,
|
||||
seed: { kind: "api", value: "api.nested.write", match: "exact" },
|
||||
direction: "outgoing",
|
||||
}),
|
||||
);
|
||||
const traced = await cli.run({
|
||||
arguments: ["trace-application-feature", traceInput, "--json"],
|
||||
});
|
||||
expect(traced.exitCode).toBe(0);
|
||||
const { normalized_result: trace } = z
|
||||
.object({ normalized_result: applicationFeatureTraceResultSchema })
|
||||
.parse(traced.json);
|
||||
expect(trace.seed_matches).toHaveLength(1);
|
||||
expect(trace.paths.some((path) => path.contains_inference)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves exact bridge member segments through the public CLI",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-bridge-segments-");
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
|
||||
const write = () => ipcRenderer.send("write");
|
||||
contextBridge.exposeInMainWorld("desktop", {
|
||||
"nested.write": write,
|
||||
nested: { write },
|
||||
__proto__: { inherited: write },
|
||||
["__proto__"]: { own: write },
|
||||
});`,
|
||||
"main.js": `const { ipcMain } = require("electron");
|
||||
ipcMain.on("write", (event) => event.sender.copy());`,
|
||||
});
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
const { normalized_result: application } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(output.json);
|
||||
const observations = application.graph.nodes.flatMap((node) =>
|
||||
node.observations.filter(
|
||||
(observation) =>
|
||||
observation.properties.entry_kind === "bridge-member" &&
|
||||
observation.properties.step_kind === "exposure",
|
||||
),
|
||||
);
|
||||
const bridge = application.graph.nodes
|
||||
.filter((node) => node.kind === "context-bridge-api")
|
||||
.flatMap((node) => node.observations)
|
||||
.find((observation) => observation.properties.api_key === "desktop");
|
||||
expect(bridge?.properties.members).not.toContain("__proto__.inherited");
|
||||
expect(bridge?.properties.members).toContain("__proto__.own");
|
||||
const paths = observations.map(
|
||||
(observation) => observation.properties.entry_path,
|
||||
);
|
||||
expect(paths).toEqual(
|
||||
expect.arrayContaining([
|
||||
["desktop", "nested.write"],
|
||||
["desktop", "nested", "write"],
|
||||
["desktop", "__proto__", "own"],
|
||||
]),
|
||||
);
|
||||
expect(paths).not.toContainEqual(["desktop", "__proto__", "inherited"]);
|
||||
const handlerPaths = application.graph.nodes
|
||||
.flatMap((node) => node.observations)
|
||||
.filter(
|
||||
(observation) => observation.properties.entry_kind === "ipc-handler",
|
||||
);
|
||||
expect(handlerPaths.length).toBeGreaterThan(0);
|
||||
expect(
|
||||
handlerPaths.every(
|
||||
(observation) => !("entry_path" in observation.properties),
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"retains a nested bridge path when its IPC handler has no external terminal",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-nested-return-");
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
|
||||
contextBridge.exposeInMainWorld("api", {
|
||||
nested: { write: (value) => ipcRenderer.invoke("nested:write", value) }
|
||||
});`,
|
||||
"main.js": `const { ipcMain } = require("electron");
|
||||
ipcMain.handle("nested:write", (event, value) => value);`,
|
||||
});
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
const { normalized_result: application } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(output.json);
|
||||
const { graph } = application;
|
||||
expect(
|
||||
graph.nodes.some((node) =>
|
||||
node.observations.some(
|
||||
(observation) =>
|
||||
observation.properties.entry === "api.nested.write" &&
|
||||
observation.properties.step_kind === "exposure",
|
||||
),
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
graph.edges.some(
|
||||
(edge) =>
|
||||
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
|
||||
),
|
||||
).toBe(false);
|
||||
|
||||
const traceInput = join(root, "trace-nested-input.json");
|
||||
await writeFile(
|
||||
traceInput,
|
||||
JSON.stringify({
|
||||
application: output.json,
|
||||
seed: { kind: "api", value: "api.nested.write", match: "exact" },
|
||||
direction: "outgoing",
|
||||
}),
|
||||
);
|
||||
const traced = await cli.run({
|
||||
arguments: ["trace-application-feature", traceInput, "--json"],
|
||||
});
|
||||
expect(traced.exitCode).toBe(0);
|
||||
const { normalized_result: trace } = z
|
||||
.object({ normalized_result: applicationFeatureTraceResultSchema })
|
||||
.parse(traced.json);
|
||||
expect(trace.seed_matches).toHaveLength(1);
|
||||
expect(trace.graph?.nodes.length).toBeGreaterThan(1);
|
||||
},
|
||||
);
|
||||
|
||||
for (const surface of ["CLI", "stdio MCP"] as const) {
|
||||
cliTest(
|
||||
`preserves handler ambiguity and short-circuit factory paths through ${surface}`,
|
||||
@@ -225,3 +409,89 @@ cliTest(
|
||||
expect(joins.map((e) => e.properties.channel)).toEqual(["test:cut"]);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"keeps shallow bridge members when a sibling exceeds the nesting limit",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-electron-deep-bridge-");
|
||||
let deepMember = '() => e.ipcRenderer.send("deep:run")';
|
||||
for (let index = 0; index < 129; index += 1)
|
||||
deepMember = `{ level${index}: ${deepMember} }`;
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.js": `const e=require("electron");
|
||||
e.contextBridge.exposeInMainWorld("desktop", {
|
||||
deep: ${deepMember},
|
||||
shallow: () => e.ipcRenderer.send("shallow:run")
|
||||
});`,
|
||||
"main.js": `const e=require("electron");
|
||||
e.ipcMain.on("shallow:run", event => event.sender.copy());
|
||||
e.ipcMain.on("deep:run", event => event.sender.copy());`,
|
||||
});
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
const { normalized_result: application } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(output.json);
|
||||
const { graph } = application;
|
||||
const joins = graph.edges.filter(
|
||||
(edge) =>
|
||||
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
|
||||
);
|
||||
expect(joins.map((edge) => edge.properties.channel)).toEqual([
|
||||
"shallow:run",
|
||||
]);
|
||||
expect(
|
||||
graph.nodes
|
||||
.flatMap((node) => node.observations)
|
||||
.some(
|
||||
(observation) =>
|
||||
observation.properties.entry === "desktop.shallow" &&
|
||||
observation.properties.step_kind === "exposure",
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves empty bridge member segments in nested paths",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory(
|
||||
"rea-electron-empty-bridge-key-",
|
||||
);
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
|
||||
contextBridge.exposeInMainWorld("desktop", {
|
||||
"": { nested: { call: () => ipcRenderer.send("empty:key") } }
|
||||
});`,
|
||||
"main.js": `const { ipcMain } = require("electron");
|
||||
ipcMain.on("empty:key", event => event.sender.copy());`,
|
||||
});
|
||||
const output = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--json"],
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
const { normalized_result: application } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(output.json);
|
||||
const observations = application.graph.nodes.flatMap(
|
||||
(node) => node.observations,
|
||||
);
|
||||
expect(
|
||||
observations.some(
|
||||
(observation) =>
|
||||
observation.properties.entry === "desktop..nested.call" &&
|
||||
observation.properties.step_kind === "exposure",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
application.graph.edges
|
||||
.filter(
|
||||
(edge) =>
|
||||
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
|
||||
)
|
||||
.map((edge) => edge.properties.channel),
|
||||
).toEqual(["empty:key"]);
|
||||
},
|
||||
);
|
||||
|
||||
+68
-2
@@ -9,7 +9,10 @@ import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { matchJavaScriptApplicationVersions } from "../../../../src/domain/javascript/javascriptApplicationVersionKeys.js";
|
||||
import { findApplicationFeatureSeeds } from "../../../../src/domain/javascript/javascriptFeatureSeed.js";
|
||||
import { traceApplicationFeatureInputSchema } from "../../../../src/domain/javascript/javascriptFeatureTraceSchemas.js";
|
||||
import {
|
||||
applicationFeatureSeedSchema,
|
||||
traceApplicationFeatureInputSchema,
|
||||
} from "../../../../src/domain/javascript/javascriptFeatureTraceSchemas.js";
|
||||
import { analyzeJavaScriptApplication } from "../../../support/javascriptApplicationScope.js";
|
||||
|
||||
const analyzeSource = async (
|
||||
@@ -76,6 +79,59 @@ it("retains empty literal evidence across the application boundary surfaces", as
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps bridge entry paths stable through worker validation and API seed matching", async () => {
|
||||
const output = await analyzeSource(`
|
||||
const { contextBridge, ipcRenderer } = require("electron");
|
||||
const literal = () => ipcRenderer.send("literal");
|
||||
const nested = () => ipcRenderer.send("nested");
|
||||
contextBridge.exposeInMainWorld("desktop", {
|
||||
"nested.write": literal,
|
||||
nested: { write: nested },
|
||||
});
|
||||
`);
|
||||
const semanticPathIds = new Set(
|
||||
(output.semantic_graph?.nodes ?? [])
|
||||
.map(({ properties }) => properties.path_id)
|
||||
.filter((pathId): pathId is string => typeof pathId === "string"),
|
||||
);
|
||||
const applicationPathIds = new Set(
|
||||
output.graph.nodes.flatMap(({ observations }) =>
|
||||
observations
|
||||
.map(({ properties }) => properties.path_id)
|
||||
.filter((pathId): pathId is string => typeof pathId === "string"),
|
||||
),
|
||||
);
|
||||
expect(semanticPathIds).toEqual(applicationPathIds);
|
||||
|
||||
const matches = findApplicationFeatureSeeds(
|
||||
output.graph.nodes,
|
||||
applicationFeatureSeedSchema.parse({
|
||||
kind: "api",
|
||||
value: "desktop.nested.write",
|
||||
entry_path: ["desktop", "nested.write"],
|
||||
}),
|
||||
);
|
||||
expect(matches).toHaveLength(1);
|
||||
expect(
|
||||
output.graph.nodes
|
||||
.find(({ node_id: nodeId }) => nodeId === matches[0]?.node_id)
|
||||
?.observations.some(
|
||||
({ properties }) =>
|
||||
Array.isArray(properties.entry_path) &&
|
||||
properties.entry_path.join("\0") === "desktop\0nested.write",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
findApplicationFeatureSeeds(
|
||||
output.graph.nodes,
|
||||
applicationFeatureSeedSchema.parse({
|
||||
kind: "api",
|
||||
value: "desktop.nested.write",
|
||||
}),
|
||||
),
|
||||
).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("analyzes a plain long-line loader without package metadata", async () => {
|
||||
const output = await analyzeSource(
|
||||
`var Module = {}; fetch(""); new Worker(""); var payload = "${"x".repeat(65_536)}";`,
|
||||
@@ -240,8 +296,18 @@ it.each([
|
||||
[{ kind: "channel", value: "" }, false],
|
||||
[{ kind: "channel", value: "", match: "contains" }, false],
|
||||
[{ kind: "node-id", value: "", match: "exact" }, false],
|
||||
[{ kind: "api", value: "desktop.nested.write" }, true],
|
||||
[
|
||||
{
|
||||
kind: "api",
|
||||
value: "desktop.nested.write",
|
||||
entry_path: ["desktop", "nested.write"],
|
||||
},
|
||||
true,
|
||||
],
|
||||
[{ kind: "channel", value: "channel", entry_path: ["channel"] }, false],
|
||||
] as const)(
|
||||
"validates empty seed %j at runtime and in the advertised schema",
|
||||
"validates seed %j at runtime and in the advertised schema",
|
||||
(seed, accepted) => {
|
||||
expect(seedSchema.safeParse(seed).success).toBe(accepted);
|
||||
expect(validateAdvertisedSeed(seed)).toBe(accepted);
|
||||
|
||||
Reference in New Issue
Block a user