fix(javascript): retain guards without duplicating observed IPC calls

Include enclosing control-flow predicates when deciding whether symbolic
wrapper facts can be reported without guards. Preserve the guarded paths
and unresolved coverage instead of inventing unconditional exposures or
channel pairing candidates.

Keep fully observed renderer transmissions singular when tracing an exposed
callback. Distinct wrapper registrations still retain invocation identities.
This fixes the Electron package baseline's doubled transmission count.

Validation: check; 43 exact source and public CLI/MCP tests; existing artifact,
extraction, Electron analysis and routed-analysis verifier against the built
CLI. Installed-package platform coverage remains with CI.
This commit is contained in:
morluto
2026-10-12 10:08:39 +08:00
parent b8fab74297
commit 42b0ed126d
3 changed files with 61 additions and 4 deletions
@@ -257,6 +257,21 @@ it("retains distinct wrapper invocation ranges for one IPC API source", () => {
findings[1]?.invocation_location,
);
});
it("keeps a directly observed renderer operation singular when tracing its exposed callback", () => {
const result = analyzeJavaScriptStaticSource(`
const {contextBridge,ipcRenderer}=require("electron");
contextBridge.exposeInMainWorld("desktop",{ping:()=>ipcRenderer.invoke("ping")});
`);
expect(result.electron.ipc).toHaveLength(1);
expect(result.electron.ipc[0]).toMatchObject({
channel: "ping",
operation: "invoke",
});
expect(result.electron.ipc[0]).not.toHaveProperty("resolution_basis");
expect(result.electron.call_paths).toContainEqual(
expect.objectContaining({ entry: "desktop.ping", channel: "ping" }),
);
});
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());`,
@@ -327,6 +342,37 @@ it("keeps conditional IPC channels unknown instead of reporting them uncondition
expect(result.electron.call_paths ?? []).toEqual([]);
expect(result.parse_status).toBe("partial");
});
it("retains enclosing branch predicates without emitting unconditional wrapper findings", () => {
const result = analyzeJavaScriptStaticSource(`
const e=require("electron");
function expose(key,api){e.contextBridge.exposeInMainWorld(key,api);}
function register(channel,handler){e.ipcMain.on(channel,handler);}
if(flag){
expose("conditional",{copy:()=>navigator.clipboard.writeText("copy")});
register("only:flag",event=>event.sender.copy());
}
`);
expect(result.electron.context_bridge_apis).not.toContainEqual(
expect.objectContaining({
api_key: "conditional",
resolution_basis: "wrapper-substitution",
}),
);
expect(result.electron.ipc).not.toContainEqual(
expect.objectContaining({
channel: "only:flag",
resolution_basis: "wrapper-substitution",
}),
);
for (const entry of ["conditional.copy", "only:flag"])
expect(result.electron.call_paths).toContainEqual(
expect.objectContaining({
entry,
conditions: [expect.objectContaining({ expression: "flag" })],
}),
);
expect(result.parse_status).toBe("partial");
});
it("preserves the raw bridge observation for a guarded object value", () => {
const result = analyzeJavaScriptStaticSource(`
const e=require("electron");
@@ -240,16 +240,21 @@ export const collectElectronWrappedCallPaths = (
handler_location: null,
...findingContext,
};
// A fully observed transmission stays one source fact when its callback is
// traced. Registrations retain invocation identity because handlers compete.
const duplicate = accumulator.ipc.some(
({ value }) =>
value.side === finding.side &&
value.operation === finding.operation &&
value.channel === channel &&
JSON.stringify(value.location) === JSON.stringify(location) &&
JSON.stringify(value.invocation_location) ===
JSON.stringify(invocationLocation),
((value.resolution_basis === undefined &&
(value.mode === "send" || value.mode === "invoke")) ||
JSON.stringify(value.invocation_location) ===
JSON.stringify(invocationLocation)),
);
const findingGuarded = hasConditions(first) || guardedDispatch;
const findingGuarded =
context.conditions.length > 0 || hasConditions(first) || guardedDispatch;
if (!duplicate && !findingGuarded) {
accumulator.ipc.push({
offset: node.start ?? -1,
@@ -315,7 +320,11 @@ export const collectElectronWrappedCallPaths = (
// cannot be recorded as an unconditional exposure fact.
const guardedKey = hasConditions(key);
const guardedObject = hasConditions(object);
const guarded = guardedKey || guardedObject || guardedDispatch;
const guarded =
context.conditions.length > 0 ||
guardedKey ||
guardedObject ||
guardedDispatch;
if (guarded) accumulator.unknownFindings += 1;
// Existing entries are raw syntax observations and remain valid even when
// symbolic substitution can only describe this call under a predicate.
@@ -139,6 +139,7 @@ for (const surface of ["CLI", "stdio MCP"] as const) {
choose:choose(),
ambiguous:()=>e.ipcRenderer.send("test:ambiguous"),
competing:()=>e.ipcRenderer.send("test:competing"),
conditional:()=>e.ipcRenderer.send("test:conditional"),
fallback:()=>e.ipcRenderer.send("test:fallback"),
});`,
"main.js": `const e=require("electron");
@@ -148,6 +149,7 @@ for (const surface of ["CLI", "stdio MCP"] as const) {
register("test:ambiguous",flag?copy:cut);
register("test:competing",copy);
register("test:competing",flag?copy:unknownHandler);
if(flag) register("test:conditional",copy);
register("test:fallback",event=>{false ?? event.sender.copy()});`,
});
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);