Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(electron): count bridge listener registrations by source callsite

morluto506400748eb1
diff --git a/src/domain/javascript/electronWrappedCallPaths.test.ts b/src/domain/javascript/electronWrappedCallPaths.test.ts
index 449ca776d..d52e8f57f 100644
--- a/src/domain/javascript/electronWrappedCallPaths.test.ts
+++ b/src/domain/javascript/electronWrappedCallPaths.test.ts
@@ -384,6 +384,66 @@ it("keeps a directly observed renderer operation singular when tracing its expos
expect.objectContaining({ entry: "desktop.ping", channel: "ping" }),
);
});
+it("counts one exposed renderer listener callsite once and retains its bridge path", () => {
+ const result = analyzeJavaScriptStaticSource(`
+ const {contextBridge,ipcRenderer}=require("electron");
+ contextBridge.exposeInMainWorld("desktop", {
+ onPong: callback => ipcRenderer.on("pong", callback),
+ });
+ `);
+ expect(
+ result.electron.ipc.filter(
+ ({ side, mode, channel }) =>
+ side === "renderer" && mode === "listen" && channel === "pong",
+ ),
+ ).toHaveLength(1);
+ expect(result.electron.call_paths).toContainEqual(
+ expect.objectContaining({
+ entry: "desktop.onPong",
+ channel: "pong",
+ terminal: "electron.ipcRenderer.on",
+ }),
+ );
+});
+it("keeps separate bridge callers while counting their shared listener callsite once", () => {
+ const result = analyzeJavaScriptStaticSource(`
+ const {contextBridge,ipcRenderer}=require("electron");
+ const subscribe = callback => ipcRenderer.once("pong", callback);
+ contextBridge.exposeInMainWorld("desktop", {
+ onPong: subscribe,
+ onPongAgain: subscribe,
+ });
+ `);
+ const listeners = result.electron.ipc.filter(
+ ({ side, mode, channel }) =>
+ side === "renderer" && mode === "listen" && channel === "pong",
+ );
+ expect(listeners).toHaveLength(1);
+ expect(listeners[0]?.operation).toBe("once");
+ expect(
+ result.electron.call_paths
+ ?.filter(({ channel }) => channel === "pong")
+ .map(({ entry }) => entry),
+ ).toEqual(["desktop.onPong", "desktop.onPongAgain"]);
+});
+it("keeps distinct exposed renderer listener callsites distinct", () => {
+ const result = analyzeJavaScriptStaticSource(`
+ const {contextBridge,ipcRenderer}=require("electron");
+ contextBridge.exposeInMainWorld("desktop", {
+ onPong: callback => ipcRenderer.on("pong", callback),
+ onPongAgain: callback => ipcRenderer.on("pong", callback),
+ });
+ `);
+ expect(
+ result.electron.ipc.filter(
+ ({ side, mode, channel }) =>
+ side === "renderer" && mode === "listen" && channel === "pong",
+ ),
+ ).toHaveLength(2);
+ expect(
+ result.electron.call_paths?.filter(({ channel }) => channel === "pong"),
+ ).toHaveLength(2);
+});
it("does not identify overwritten Electron namespaces or shadowed require", () => {
for (const source of [
`const e=require("electron");e.ipcMain={on(){}};e.ipcMain.on("bad",event=>event.sender.copy());`,
diff --git a/src/domain/javascript/electronWrappedCallPaths.ts b/src/domain/javascript/electronWrappedCallPaths.ts
index c32bca04b..9a9632850 100644
--- a/src/domain/javascript/electronWrappedCallPaths.ts
+++ b/src/domain/javascript/electronWrappedCallPaths.ts
@@ -50,6 +50,9 @@ const ipcEndpointKey = (value: ElectronIpcFinding): string =>
interface IpcEndpointFacts {
// A directly observed transmission stays one fact however it is reached.
observedTransmission: boolean;
+ // A bridge path to a directly observed renderer listener is provenance for
+ // that source registration, not another registration at the same callsite.
+ observedRendererListener: boolean;
readonly invocations: Set<string>;
}
@@ -63,12 +66,20 @@ const indexIpcFinding = (
const key = ipcEndpointKey(value);
let facts = index.get(key);
if (facts === undefined) {
- facts = { observedTransmission: false, invocations: new Set() };
+ facts = {
+ observedTransmission: false,
+ observedRendererListener: false,
+ invocations: new Set(),
+ };
index.set(key, facts);
}
facts.observedTransmission ||=
value.resolution_basis === undefined &&
(value.mode === "send" || value.mode === "invoke");
+ facts.observedRendererListener ||=
+ value.resolution_basis === undefined &&
+ value.side === "renderer" &&
+ value.mode === "listen";
facts.invocations.add(ipcInvocationKey(value.invocation_location));
};
@@ -397,6 +408,8 @@ export const collectElectronWrappedCallPaths = (
const duplicate =
recorded !== undefined &&
(recorded.observedTransmission ||
+ (context.entryKind === "bridge-member" &&
+ recorded.observedRendererListener) ||
recorded.invocations.has(ipcInvocationKey(invocationLocation)));
const findingGuarded =
context.conditions.length > 0 || hasConditions(first) || guardedDispatch;
@@ -451,6 +464,12 @@ export const collectElectronWrappedCallPaths = (
emit(channelContext, node, endpoint, channel);
return;
}
+ if (
+ descriptor.side === "renderer" &&
+ descriptor.mode === "listen" &&
+ context.entryKind === "bridge-member"
+ )
+ emit(channelContext, node, endpoint, channel);
if (descriptor.side === "main" && fn)
functionBody(
fn,
diff --git a/tests/acceptance/applications/electronIpcListenerCallsiteCli.test.ts b/tests/acceptance/applications/electronIpcListenerCallsiteCli.test.ts
new file mode 100644
index 000000000..5d8efcf2a
--- /dev/null
+++ b/tests/acceptance/applications/electronIpcListenerCallsiteCli.test.ts
@@ -0,0 +1,129 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { expect } from "vitest";
+import { z } from "zod";
+
+import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
+import { applicationFeatureTraceResultSchema } from "../../../src/domain/javascript/javascriptFeatureTraceSchemas.js";
+import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+cliTest.for(["CLI", "stdio MCP"] as const)(
+ "counts exposed IPC listener callsites once and retains bridge paths through %s",
+ async (surface, { cli }) => {
+ const root = await createTestTempDirectory("rea-electron-ipc-listener-");
+ await Promise.all([
+ writeFile(
+ join(root, "package.json"),
+ JSON.stringify({
+ name: "ipc-listener-fixture",
+ version: "1.0.0",
+ main: "main.js",
+ }),
+ ),
+ writeFile(
+ join(root, "main.js"),
+ `const {app,BrowserWindow,ipcMain}=require("electron");
+ ipcMain.handle("ping",()=>"pong");
+ app.whenReady().then(()=>new BrowserWindow({webPreferences:{preload:"./preload.js"}}));`,
+ ),
+ writeFile(
+ join(root, "preload.js"),
+ `const {contextBridge,ipcRenderer}=require("electron");
+ const subscribe = callback => ipcRenderer.on("pong",callback);
+ contextBridge.exposeInMainWorld("api",{
+ ping:()=>ipcRenderer.invoke("ping"),
+ onPong:subscribe,
+ onPongAgain:subscribe,
+ onPongSeparate:callback=>ipcRenderer.on("pong",callback),
+ });`,
+ ),
+ writeFile(join(root, "index.html"), "<!doctype html><html></html>\n"),
+ ]);
+
+ const mcp =
+ surface === "stdio MCP" ? await connectLocalToolsMcp() : undefined;
+ let applicationEvidence: unknown;
+ if (surface === "CLI") {
+ const response = await cli.run({
+ arguments: ["analyze-javascript-application", root, "--json"],
+ });
+ expect(response.exitCode, response.stderr).toBe(0);
+ applicationEvidence = response.json;
+ } else {
+ const response = await mcp!.call("analyze_javascript_application", {
+ input_path: root,
+ });
+ expect(response.isError, JSON.stringify(response)).not.toBe(true);
+ applicationEvidence = response.structuredContent;
+ }
+
+ const { normalized_result: application } = z
+ .object({ normalized_result: javascriptApplicationAnalysisResultSchema })
+ .parse(applicationEvidence);
+ expect(application.summary.ipc).toMatchObject({
+ operations: 4,
+ renderer_listeners: 2,
+ });
+ const pongChannels = application.graph.nodes.filter(
+ (node) =>
+ node.kind === "ipc-channel" &&
+ node.observations.some(
+ ({ properties }) => properties.channel === "pong",
+ ),
+ );
+ expect(pongChannels).toHaveLength(1);
+ expect(pongChannels[0]?.observations).toHaveLength(2);
+
+ const bridgePaths = application.graph.nodes.filter(
+ (node) =>
+ node.kind === "javascript-expression" &&
+ node.observations.some(
+ ({ properties }) =>
+ properties.entry === "api.onPong" ||
+ properties.entry === "api.onPongAgain" ||
+ properties.entry === "api.onPongSeparate",
+ ),
+ );
+ expect(
+ new Set(
+ bridgePaths.flatMap((node) =>
+ node.observations.flatMap(({ properties }) =>
+ typeof properties.entry === "string" ? [properties.entry] : [],
+ ),
+ ),
+ ),
+ ).toEqual(new Set(["api.onPong", "api.onPongAgain", "api.onPongSeparate"]));
+
+ const traceArguments = {
+ application: applicationEvidence,
+ seed: { kind: "channel", value: "pong", match: "exact" },
+ direction: "incoming",
+ };
+ let traceEvidence: unknown;
+ if (surface === "CLI") {
+ const response = await cli.run({
+ arguments: [
+ "trace-application-feature",
+ JSON.stringify(traceArguments),
+ "--json",
+ ],
+ });
+ expect(response.exitCode, response.stderr).toBe(0);
+ traceEvidence = response.json;
+ } else {
+ const response = await mcp!.call(
+ "trace_application_feature",
+ traceArguments,
+ );
+ expect(response.isError, JSON.stringify(response)).not.toBe(true);
+ traceEvidence = response.structuredContent;
+ }
+ const { normalized_result: trace } = z
+ .object({ normalized_result: applicationFeatureTraceResultSchema })
+ .parse(traceEvidence);
+ expect(trace.summary.matched_seeds).toBe(1);
+ },
+);