fix(javascript): confirm Electron renderer evidence with the static pass

The focused evidence check from 4a86bca9b also counted renderer calls in
bundler factory defaults that the bundler convention never runs, which
the static pass skips. Such a default switched a type-module file to
script context, so its unrelated unbound requires became imports again.

The focused check now only screens. It follows the static pass's own
traversal, so a negative result still rules evidence out. A positive
result is confirmed by the static pass's direct findings on the unscoped
parse, and those findings become the file's facts when they hold.
This commit is contained in:
RRFZ committed 2026-10-29 21:25:43 -04:00
1 parent 4a86bca9b0
commit f71236400f
4 files changed
+99 -50

No files matched your search

@@ -1,6 +1,7 @@
import * as t from "@babel/types";
import { ipcDescriptor } from "./electronStaticAnalysisIpc.js";
import type { ElectronStaticFindings } from "./electronStaticAnalysisTypes.js";
import { electronCalleePath } from "./electronStaticAnalysisValues.js";
import { contextBridgeExposureArguments } from "./javascriptContextBridgeExposure.js";
import { ELECTRON_MODULE } from "./javascriptElectronMemberWrites.js";
@@ -11,7 +12,7 @@ import {
import { hasPossibleCommonJsDelegateWrite } from "./javascriptSemanticRequireOrigin.js";
import {
traverseJavaScriptAst,
traverseJavaScriptValueAstSteps,
traverseJavaScriptAstSteps,
} from "./javascriptSemanticTraversal.js";
import { javaScriptRuntimeContext } from "./javascriptSourceParser.js";
import { stringValue } from "./javascriptStaticAnalysisHelpers.js";
@@ -26,7 +27,7 @@ import { stringValue } from "./javascriptStaticAnalysisHelpers.js";
* the source a module under every loader, so only scripts qualify. The
* source must also name `contextBridge` or `ipcRenderer`: Electron identity
* reaches a member only through an identifier, string, or template key, so a
* source naming neither cannot yield {@link electronRendererEvidenceSteps}.
* source naming neither cannot yield {@link hasElectronRendererEvidence}.
*/
export const mayRunUnderElectronScriptLoader = (
program: t.Program,
@@ -61,14 +62,16 @@ const rendererModules: ReadonlySet<string> = new Set([
]);
/**
* Whether a source directly calls a `contextBridge` exposure or a renderer IPC
* operation. Both are renderer-process Electron modules that the main process
* cannot obtain, so such a call places the source under a renderer-side
* loader. The check runs only the static pass's binding and Electron identity
* preamble and its exposure and IPC descriptors, without the rest of that
* pass; wrapper substitutions are inferred, so they are not expanded.
* Whether the static pass could directly observe a `contextBridge` exposure
* or a renderer IPC operation in this source. Both are renderer-process
* Electron modules that the main process cannot obtain. This screen runs the
* pass's binding and Electron identity preamble, source traversal and
* exposure and IPC descriptors, but not its bundler recognition. It therefore
* also counts factory defaults that the bundler convention never runs, which
* only that pass excludes: a false result rules evidence out, and a true one
* still needs {@link hasElectronRendererEvidence} on the pass's findings.
*/
export function* electronRendererEvidenceSteps(
export function* mayHaveElectronRendererEvidenceSteps(
program: t.Program,
): Generator<void, boolean> {
const index = yield* indexJavaScriptBindingsWithReceiversSteps(program);
@@ -81,23 +84,38 @@ export function* electronRendererEvidenceSteps(
);
const context = { electronBindings: identity.bindings };
let found = false;
yield* traverseJavaScriptValueAstSteps(
program,
index.bindings.erasedSyntaxNodes,
{
skip: () => found,
enter: (node) => {
if (!t.isCallExpression(node) && !t.isOptionalCallExpression(node))
return;
const path = electronCalleePath(node.callee, context);
found ||=
(path?.length === 2 &&
path[0] === "contextBridge" &&
contextBridgeExposureArguments(path[1] ?? "", node.arguments) !==
undefined) ||
ipcDescriptor(path)?.side === "renderer";
},
yield* traverseJavaScriptAstSteps(program, {
skip: () => found,
enter: (node) => {
if (
index.bindings.erasedSyntaxNodes.has(node) ||
(!t.isCallExpression(node) && !t.isOptionalCallExpression(node))
)
return;
const path = electronCalleePath(node.callee, context);
found ||=
(path?.length === 2 &&
path[0] === "contextBridge" &&
contextBridgeExposureArguments(path[1] ?? "", node.arguments) !==
undefined) ||
ipcDescriptor(path)?.side === "renderer";
},
);
});
return found;
}
/**
* Whether static findings directly observe a `contextBridge` exposure or a
* renderer IPC operation, which places their source under a renderer-side
* loader. Wrapper substitutions are inferred, so they do not count.
*/
export const hasElectronRendererEvidence = (
electron: ElectronStaticFindings,
): boolean =>
electron.context_bridge_apis.some(
({ resolution_basis }) => resolution_basis === undefined,
) ||
electron.ipc.some(
({ side, resolution_basis }) =>
side === "renderer" && resolution_basis === undefined,
);