mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 06:25:44 +00:00
fix(electron): resolve exact local IPC channel values (#13005)
* fix(electron): resolve exact local IPC channel values * test(electron): cover ESM IPC channel overwrites * fix(electron): reconcile wrapper registrations before resolving channels * test(electron): require inference evidence for resolved handler channels * fix(electron): preserve inferred channels on handler evidence
This commit is contained in:
1 parent
fba5e44bf5
commit
e66a27c2f2
7 files changed
+250
-84
No files matched your search
@@ -121,6 +121,10 @@ const addIpcRecord = (
|
||||
});
|
||||
};
|
||||
|
||||
const ipcChannelBasis = (finding: ElectronIpcRecord["finding"]) =>
|
||||
finding.resolution_basis ??
|
||||
(finding.channel !== null ? finding.channel_basis : undefined);
|
||||
|
||||
const addChannelNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
record: ElectronIpcRecord,
|
||||
@@ -150,7 +154,7 @@ const addChannelNode = (
|
||||
return context.accumulator.addNode({
|
||||
kind: "ipc-channel",
|
||||
identity:
|
||||
finding.resolution_basis === "wrapper-substitution" ||
|
||||
ipcChannelBasis(finding) !== undefined ||
|
||||
finding.loader_authority === "candidate"
|
||||
? artifactLocalIdentity(
|
||||
file.sha256,
|
||||
@@ -184,69 +188,68 @@ const addChannelNode = (
|
||||
? {}
|
||||
: { invocation_location: finding.invocation_location }),
|
||||
},
|
||||
evidence:
|
||||
finding.resolution_basis !== "wrapper-substitution"
|
||||
? electronFindingEvidence(
|
||||
{
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: finding.location,
|
||||
operation: "observe-ipc-channel-expression",
|
||||
coverage,
|
||||
limitations: [
|
||||
...(finding.receiver === undefined
|
||||
? []
|
||||
: [
|
||||
"The receiver is a source-level allocation, not a runtime frame identity; registration and dispatch remain unverified.",
|
||||
]),
|
||||
...(finding.channel !== null
|
||||
? []
|
||||
: [
|
||||
"The channel expression is dynamic; its runtime value and pairings remain unknown.",
|
||||
]),
|
||||
],
|
||||
},
|
||||
finding.loader_authority,
|
||||
)
|
||||
: staticInferenceEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: finding.location,
|
||||
operation: "observe-ipc-channel-expression",
|
||||
coverage,
|
||||
...(finding.loader_authority === "candidate"
|
||||
? { confidence: "low" as const }
|
||||
: {}),
|
||||
limitations: [
|
||||
...(finding.loader_authority === "candidate"
|
||||
? [ELECTRON_CANDIDATE_LIMITATION]
|
||||
: []),
|
||||
...(finding.receiver === undefined
|
||||
? []
|
||||
: [
|
||||
"The receiver is a source-level allocation, not a runtime frame identity; registration and dispatch remain unverified.",
|
||||
]),
|
||||
...(finding.resolution_basis !== "wrapper-substitution"
|
||||
? []
|
||||
: [
|
||||
"The channel is inferred through lexical wrapper substitution; runtime registration and dispatch remain unverified.",
|
||||
]),
|
||||
...(finding.channel !== null
|
||||
? []
|
||||
: [
|
||||
"The channel expression is dynamic; its runtime value and pairings remain unknown.",
|
||||
]),
|
||||
],
|
||||
}),
|
||||
evidence: ipcChannelEvidence(file, finding, coverage),
|
||||
},
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const ipcChannelEvidence = (
|
||||
file: ElectronIpcRecord["file"],
|
||||
finding: ElectronIpcRecord["finding"],
|
||||
coverage: JavaScriptArtifactGraphCoverage,
|
||||
) => {
|
||||
const basis = ipcChannelBasis(finding);
|
||||
const input = {
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: finding.location,
|
||||
operation: "observe-ipc-channel-expression",
|
||||
coverage,
|
||||
limitations: [
|
||||
...(finding.receiver === undefined
|
||||
? []
|
||||
: [
|
||||
"The receiver is a source-level allocation, not a runtime frame identity; registration and dispatch remain unverified.",
|
||||
]),
|
||||
...(finding.channel === null
|
||||
? [
|
||||
"The channel expression is dynamic; its runtime value and pairings remain unknown.",
|
||||
]
|
||||
: []),
|
||||
],
|
||||
};
|
||||
return basis === undefined
|
||||
? electronFindingEvidence(input, finding.loader_authority)
|
||||
: staticInferenceEvidence({
|
||||
...input,
|
||||
...(finding.loader_authority === "candidate"
|
||||
? { confidence: "low" as const }
|
||||
: {}),
|
||||
limitations: [
|
||||
...input.limitations,
|
||||
...(finding.loader_authority === "candidate"
|
||||
? [ELECTRON_CANDIDATE_LIMITATION]
|
||||
: []),
|
||||
basis === "wrapper-substitution"
|
||||
? "The channel is inferred through lexical wrapper substitution; runtime registration and dispatch remain unverified."
|
||||
: "The channel is inferred from an exact static value at this call site; runtime registration and dispatch remain unverified.",
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const addHandlerNode = (input: IpcHandlerInput): ApplicationNode => {
|
||||
const { context, record, source, channel, coverage } = input;
|
||||
const { finding, file } = record;
|
||||
const handlerRange = finding.handler_location ?? finding.location;
|
||||
const basis = ipcChannelBasis(finding);
|
||||
const handlerEvidence = {
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: handlerRange,
|
||||
operation: "observe-ipc-handler",
|
||||
coverage,
|
||||
};
|
||||
const handlerKey = `${finding.side}:${finding.operation}:${finding.channel ?? finding.channel_expression}:${javascriptSourceRangeKey(handlerRange)}`;
|
||||
const identityKey =
|
||||
finding.receiver === undefined
|
||||
@@ -287,29 +290,23 @@ const addHandlerNode = (input: IpcHandlerInput): ApplicationNode => {
|
||||
: { invocation_location: finding.invocation_location }),
|
||||
},
|
||||
evidence:
|
||||
finding.resolution_basis !== "wrapper-substitution"
|
||||
? electronFindingEvidence(
|
||||
{
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: handlerRange,
|
||||
operation: "observe-ipc-handler",
|
||||
coverage,
|
||||
},
|
||||
finding.loader_authority,
|
||||
)
|
||||
basis === undefined
|
||||
? electronFindingEvidence(handlerEvidence, finding.loader_authority)
|
||||
: staticInferenceEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: handlerRange,
|
||||
operation: "observe-ipc-handler",
|
||||
coverage,
|
||||
...handlerEvidence,
|
||||
...(finding.loader_authority === "candidate"
|
||||
? {
|
||||
confidence: "low" as const,
|
||||
limitations: [ELECTRON_CANDIDATE_LIMITATION],
|
||||
}
|
||||
? { confidence: "low" as const }
|
||||
: {}),
|
||||
limitations: [
|
||||
...(finding.loader_authority === "candidate"
|
||||
? [ELECTRON_CANDIDATE_LIMITATION]
|
||||
: []),
|
||||
...(basis === "static-value"
|
||||
? [
|
||||
"The handler's channel is inferred from the registration's exact static value.",
|
||||
]
|
||||
: []),
|
||||
],
|
||||
}),
|
||||
},
|
||||
],
|
||||
|
||||
@@ -21,7 +21,12 @@ import {
|
||||
} from "./javascriptStaticAnalysisHelpers.js";
|
||||
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
|
||||
import { compositeKey } from "../unicodeCodePointOrder.js";
|
||||
import type { JavaScriptFindingContext } from "./javascriptStaticAnalysisState.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
import type {
|
||||
JavaScriptAnalysisAccumulator,
|
||||
JavaScriptFindingContext,
|
||||
} from "./javascriptStaticAnalysisState.js";
|
||||
|
||||
/** Inspect Electron IPC operations and validation candidates. */
|
||||
export const inspectElectronIpcNode = (
|
||||
@@ -45,21 +50,20 @@ const inspectIpcCall = (
|
||||
ipcDescriptor(electronCalleePath(node.callee, context));
|
||||
if (descriptor === undefined) return;
|
||||
const channelNode = argumentNode(node.arguments[0]);
|
||||
const channelValue = electronStaticValue(context.source, channelNode);
|
||||
const literal = electronStaticValue(context.source, channelNode);
|
||||
const channel =
|
||||
channelValue.status === "literal" && typeof channelValue.value === "string"
|
||||
? channelValue.value
|
||||
literal.status === "literal" && typeof literal.value === "string"
|
||||
? literal.value
|
||||
: null;
|
||||
const channelReference =
|
||||
channel === null
|
||||
? {
|
||||
channel: null,
|
||||
channel_expression:
|
||||
channelValue.status === "dynamic"
|
||||
? channelValue.expression
|
||||
: boundedExpression(context.source, channelNode),
|
||||
channel_expression: boundedExpression(context.source, channelNode),
|
||||
}
|
||||
: { channel, channel_expression: null };
|
||||
if (channel === null && channelNode !== undefined && node.start != null)
|
||||
context.accumulator.pendingIpcChannels.set(node.start, channelNode);
|
||||
const handlerNode =
|
||||
descriptor.mode === "listen" || descriptor.mode === "handle"
|
||||
? argumentNode(node.arguments[1])
|
||||
@@ -107,6 +111,46 @@ const inspectIpcCall = (
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve lexical values for direct sites left to the syntax pass. Wrapper
|
||||
* reconciliation owns replacements, guarded channels and per-invocation
|
||||
* registrations, so a known channel never absorbs one of those registrations.
|
||||
*/
|
||||
export const finishElectronIpcChannels = (
|
||||
accumulator: JavaScriptAnalysisAccumulator,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
const wrapperSites = new Set(
|
||||
accumulator.ipc
|
||||
.filter(({ value }) => value.resolution_basis === "wrapper-substitution")
|
||||
.map(({ offset }) => offset),
|
||||
);
|
||||
for (const [index, { offset, value }] of accumulator.ipc.entries()) {
|
||||
if (
|
||||
value.channel !== null ||
|
||||
value.invocation_channels !== undefined ||
|
||||
wrapperSites.has(offset)
|
||||
)
|
||||
continue;
|
||||
const argument = accumulator.pendingIpcChannels.get(offset);
|
||||
if (argument === undefined) continue;
|
||||
const channel = evaluateSemanticExpression(argument, state);
|
||||
if (channel.status !== "literal" || typeof channel.value !== "string")
|
||||
continue;
|
||||
accumulator.ipc[index] = {
|
||||
offset,
|
||||
value: {
|
||||
...value,
|
||||
channel: channel.value,
|
||||
channel_expression: null,
|
||||
channel_basis: "static-value",
|
||||
},
|
||||
};
|
||||
accumulator.unknownFindings -= 1;
|
||||
}
|
||||
accumulator.pendingIpcChannels.clear();
|
||||
};
|
||||
|
||||
const scopedIpcDescriptor = (
|
||||
node: t.CallExpression | t.OptionalCallExpression,
|
||||
context: JavaScriptFindingContext,
|
||||
|
||||
@@ -162,7 +162,11 @@ export type ElectronIpcFinding = ElectronIpcDescriptor &
|
||||
ElectronIpcHandler &
|
||||
ElectronIpcFindingContext &
|
||||
(
|
||||
| { readonly channel: string; readonly channel_expression: null }
|
||||
| {
|
||||
readonly channel: string;
|
||||
readonly channel_expression: null;
|
||||
readonly channel_basis?: "static-value";
|
||||
}
|
||||
| {
|
||||
readonly channel: null;
|
||||
readonly channel_expression: string;
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
} from "./electronStaticRenderer.js";
|
||||
import { finishElectronNativeBindingsSteps } from "./electronStaticAnalysisNative.js";
|
||||
import { finishElectronDynamicCodeSteps } from "./electronStaticAnalysisDynamicCode.js";
|
||||
import { finishElectronIpcChannels } from "./electronStaticAnalysisIpc.js";
|
||||
import { inspectElectronStaticNode } from "./electronStaticAnalysis.js";
|
||||
import {
|
||||
hasPossibleCommonJsDelegateWrite,
|
||||
@@ -234,6 +235,10 @@ export function* analyzeParsedJavaScriptStaticSourceSteps(
|
||||
electronIdentity,
|
||||
() => index.indexMemberMutationFactsSteps(),
|
||||
);
|
||||
if (accumulator.pendingIpcChannels.size > 0) {
|
||||
yield* index.indexMemberMutationFactsSteps();
|
||||
finishElectronIpcChannels(accumulator, bindings);
|
||||
}
|
||||
addSourceMapDirectives(source, file.comments ?? [], accumulator);
|
||||
return finalizeStaticAnalysis(source, file, accumulator, bindings, routes);
|
||||
}
|
||||
|
||||
@@ -88,6 +88,7 @@ export interface JavaScriptAnalysisAccumulator {
|
||||
}[];
|
||||
readonly contextBridgeApis: LocatedJavaScriptFinding<ElectronContextBridgeFinding>[];
|
||||
readonly ipc: LocatedJavaScriptFinding<ElectronIpcFinding>[];
|
||||
readonly pendingIpcChannels: Map<number, t.Node>;
|
||||
readonly senderValidations: LocatedJavaScriptFinding<ElectronSenderValidationFinding>[];
|
||||
readonly utilityProcesses: LocatedJavaScriptFinding<ElectronUtilityProcessFinding>[];
|
||||
readonly nativeAddonBindings: LocatedJavaScriptFinding<ElectronNativeAddonBindingFinding>[];
|
||||
@@ -202,6 +203,7 @@ export const createJavaScriptAnalysisAccumulator = (
|
||||
pendingBrowserWindows: [],
|
||||
contextBridgeApis: [],
|
||||
ipc: [],
|
||||
pendingIpcChannels: new Map(),
|
||||
senderValidations: [],
|
||||
utilityProcesses: [],
|
||||
nativeAddonBindings: [],
|
||||
|
||||
@@ -94,6 +94,7 @@ const ipcChannel = <Shape extends z.ZodRawShape>(shape: Shape) =>
|
||||
...shape,
|
||||
channel: z.string(),
|
||||
channel_expression: z.null(),
|
||||
channel_basis: z.literal("static-value").exactOptional(),
|
||||
}),
|
||||
z.strictObject({
|
||||
...electronFindingContext,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
@@ -253,3 +254,115 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"resolves selected local IPC channel values while retaining opaque writes through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-ipc-local-values-");
|
||||
const source = [
|
||||
'const { ipcRenderer } = require("electron");',
|
||||
'const channels = { listen: "autofill.listen", other: "original" };',
|
||||
"const captured = channels.listen;",
|
||||
'const stable = { listen: "autofill.stable" };',
|
||||
"const subscribe = () => ipcRenderer.on(stable.listen, () => {});",
|
||||
"channels.other = unknown;",
|
||||
"ipcRenderer.on(channels.other, () => {});",
|
||||
"channels.listen = unknown;",
|
||||
"ipcRenderer.send(captured);",
|
||||
].join("\n");
|
||||
await writeFixtureFiles(root, { "preload.cjs": source });
|
||||
const application = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
const observations = application.graph.nodes
|
||||
.filter(({ kind }) => kind === "ipc-channel")
|
||||
.flatMap(({ observations }) => observations);
|
||||
expect(observations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({ channel: "autofill.listen" }),
|
||||
evidence: expect.objectContaining({
|
||||
state: "inferred",
|
||||
artifact: expect.objectContaining({
|
||||
available: true,
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
source: "preload.cjs",
|
||||
start: expect.objectContaining({ line: 9 }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: "autofill.stable",
|
||||
operation: "on",
|
||||
}),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
channel: null,
|
||||
channel_expression: "channels.other",
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const handlers = application.graph.nodes
|
||||
.filter(({ kind }) => kind === "ipc-handler")
|
||||
.flatMap(({ observations }) => observations);
|
||||
expect(handlers).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({ channel: "autofill.stable" }),
|
||||
evidence: expect.objectContaining({
|
||||
state: "inferred",
|
||||
artifact: expect.objectContaining({
|
||||
available: true,
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
source: "preload.cjs",
|
||||
start: expect.objectContaining({ line: 5 }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"keeps an ESM channel unknown after its property is overwritten through %s",
|
||||
async (surface, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-ipc-mutated-channel-");
|
||||
await writeFixtureFiles(root, {
|
||||
"preload.mjs": [
|
||||
'import { ipcRenderer } from "electron";',
|
||||
'const channels = { send: "old" };',
|
||||
"const alias = channels;",
|
||||
"alias.send = unknown;",
|
||||
"ipcRenderer.send(channels.send);",
|
||||
].join("\n"),
|
||||
});
|
||||
const application = await analyzeJavaScriptDirectory(surface, cli, root);
|
||||
expect(application.summary.ipc).toMatchObject({
|
||||
operations: 1,
|
||||
literal_channels: 0,
|
||||
dynamic_channel_operations: 1,
|
||||
});
|
||||
expect(
|
||||
application.graph.nodes
|
||||
.filter(({ kind }) => kind === "ipc-channel")
|
||||
.flatMap(({ observations }) =>
|
||||
observations.map(({ properties }) => properties),
|
||||
),
|
||||
).toEqual([
|
||||
expect.objectContaining({
|
||||
channel: null,
|
||||
channel_expression: "channels.send",
|
||||
}),
|
||||
]);
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user