diff --git a/src/application/javascript/ElectronBoundaryGraphIpc.ts b/src/application/javascript/ElectronBoundaryGraphIpc.ts index 070259c9a3..7e88d6cb18 100644 --- a/src/application/javascript/ElectronBoundaryGraphIpc.ts +++ b/src/application/javascript/ElectronBoundaryGraphIpc.ts @@ -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.", + ] + : []), + ], }), }, ], diff --git a/src/domain/javascript/electronStaticAnalysisIpc.ts b/src/domain/javascript/electronStaticAnalysisIpc.ts index 561ce526c5..f24b086583 100644 --- a/src/domain/javascript/electronStaticAnalysisIpc.ts +++ b/src/domain/javascript/electronStaticAnalysisIpc.ts @@ -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, diff --git a/src/domain/javascript/electronStaticAnalysisTypes.ts b/src/domain/javascript/electronStaticAnalysisTypes.ts index aa9ce6f781..3ca486bc8f 100644 --- a/src/domain/javascript/electronStaticAnalysisTypes.ts +++ b/src/domain/javascript/electronStaticAnalysisTypes.ts @@ -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; diff --git a/src/domain/javascript/javascriptStaticAnalysis.ts b/src/domain/javascript/javascriptStaticAnalysis.ts index 5836caadfd..724d88cbb6 100644 --- a/src/domain/javascript/javascriptStaticAnalysis.ts +++ b/src/domain/javascript/javascriptStaticAnalysis.ts @@ -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); } diff --git a/src/domain/javascript/javascriptStaticAnalysisState.ts b/src/domain/javascript/javascriptStaticAnalysisState.ts index 099ef9f5a6..f180774f21 100644 --- a/src/domain/javascript/javascriptStaticAnalysisState.ts +++ b/src/domain/javascript/javascriptStaticAnalysisState.ts @@ -88,6 +88,7 @@ export interface JavaScriptAnalysisAccumulator { }[]; readonly contextBridgeApis: LocatedJavaScriptFinding[]; readonly ipc: LocatedJavaScriptFinding[]; + readonly pendingIpcChannels: Map; readonly senderValidations: LocatedJavaScriptFinding[]; readonly utilityProcesses: LocatedJavaScriptFinding[]; readonly nativeAddonBindings: LocatedJavaScriptFinding[]; @@ -202,6 +203,7 @@ export const createJavaScriptAnalysisAccumulator = ( pendingBrowserWindows: [], contextBridgeApis: [], ipc: [], + pendingIpcChannels: new Map(), senderValidations: [], utilityProcesses: [], nativeAddonBindings: [], diff --git a/src/javascript/analysis/JavaScriptAnalysisWorkerSchemas.ts b/src/javascript/analysis/JavaScriptAnalysisWorkerSchemas.ts index 4f2e319132..10b9afe588 100644 --- a/src/javascript/analysis/JavaScriptAnalysisWorkerSchemas.ts +++ b/src/javascript/analysis/JavaScriptAnalysisWorkerSchemas.ts @@ -94,6 +94,7 @@ const ipcChannel = (shape: Shape) => ...shape, channel: z.string(), channel_expression: z.null(), + channel_basis: z.literal("static-value").exactOptional(), }), z.strictObject({ ...electronFindingContext, diff --git a/tests/boundary/cli/electronScopedIpc.test.ts b/tests/boundary/cli/electronScopedIpc.test.ts index 6f0fc9eecc..7986efbc92 100644 --- a/tests/boundary/cli/electronScopedIpc.test.ts +++ b/tests/boundary/cli/electronScopedIpc.test.ts @@ -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", + }), + ]); + }, +);