mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
feat(electron): resolve stable local constant window options
Resolve stable local constant BrowserWindow options with retained provenance and shared early-exit mutation queries. Preserve source ordering and unknown results for relevant mutations or escapes. Includes real Joplin correctness/cost receipts and regression coverage. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
1 parent
5a77acfbcd
commit
d72d856f58
20 files changed
+4544
-64
No files matched your search
@@ -30,40 +30,123 @@ import {
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascriptAstValues.js";
|
||||
import type { JavaScriptFindingContext } from "./javascriptStaticAnalysisState.js";
|
||||
import type { JavaScriptBindingIndex } from "./javascriptSemanticState.js";
|
||||
import {
|
||||
hasElectronOptionObjectCandidate,
|
||||
resolveElectronOptionObject,
|
||||
} from "./electronStaticOptionObjects.js";
|
||||
|
||||
/** Inspect BrowserWindow, contextBridge, and utility-process syntax. */
|
||||
export const inspectElectronBrowserNode = (
|
||||
node: t.Node,
|
||||
context: JavaScriptFindingContext,
|
||||
): void => {
|
||||
if (t.isNewExpression(node)) inspectBrowserWindow(node, context);
|
||||
if (t.isNewExpression(node)) {
|
||||
const path = electronCalleePath(node.callee, context);
|
||||
if (path?.length === 1 && path[0] === "BrowserWindow") {
|
||||
if (needsConstantOptions(node))
|
||||
context.accumulator.pendingBrowserWindows.push(node);
|
||||
else inspectBrowserWindow(node, context);
|
||||
}
|
||||
}
|
||||
if (!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) return;
|
||||
inspectContextBridge(node, context);
|
||||
inspectUtilityProcess(node, context);
|
||||
};
|
||||
|
||||
const needsConstantOptions = (node: t.NewExpression): boolean => {
|
||||
const argument = argumentNode(node.arguments[0]);
|
||||
const options = argument && unwrapJavaScriptExpression(argument).node;
|
||||
if (t.isIdentifier(options)) return true;
|
||||
const lookup = objectProperty(options, "webPreferences");
|
||||
return (
|
||||
lookup.status === "explicit" &&
|
||||
t.isIdentifier(unwrapJavaScriptExpression(lookup.property.value).node)
|
||||
);
|
||||
};
|
||||
|
||||
/** Batch deferred options after the shared mutation index can yield safely. */
|
||||
export function* finishElectronBrowserWindowsSteps(
|
||||
context: JavaScriptFindingContext,
|
||||
index: JavaScriptBindingIndex,
|
||||
): Generator<void, void> {
|
||||
const pending = context.accumulator.pendingBrowserWindows;
|
||||
if (pending.length === 0) return;
|
||||
if (
|
||||
pending.some((node) => {
|
||||
const argument = argumentNode(node.arguments[0]);
|
||||
const options = argument && unwrapJavaScriptExpression(argument).node;
|
||||
if (t.isIdentifier(options))
|
||||
return hasElectronOptionObjectCandidate(options, index);
|
||||
const lookup = objectProperty(options, "webPreferences");
|
||||
return (
|
||||
lookup.status === "explicit" &&
|
||||
hasElectronOptionObjectCandidate(lookup.property.value, index)
|
||||
);
|
||||
})
|
||||
)
|
||||
yield* index.indexMemberMutationFactsSteps();
|
||||
for (const node of pending) {
|
||||
inspectBrowserWindow(node, context, index);
|
||||
yield;
|
||||
}
|
||||
// Inline findings arrive during traversal; deferred findings join that same
|
||||
// source-ordered collection before module attribution and public projection.
|
||||
context.accumulator.browserWindows.sort(
|
||||
(left, right) => left.offset - right.offset,
|
||||
);
|
||||
pending.length = 0;
|
||||
}
|
||||
|
||||
const inspectBrowserWindow = (
|
||||
node: t.NewExpression,
|
||||
context: JavaScriptFindingContext,
|
||||
index?: JavaScriptBindingIndex,
|
||||
): void => {
|
||||
const path = electronCalleePath(node.callee, context);
|
||||
if (path?.length !== 1 || path[0] !== "BrowserWindow") return;
|
||||
const argument = argumentNode(node.arguments[0]);
|
||||
const options =
|
||||
argument === undefined
|
||||
? undefined
|
||||
: unwrapJavaScriptExpression(argument).node;
|
||||
const collected = collectWindowOptions(context.source, options);
|
||||
const resolvedOptions =
|
||||
(index &&
|
||||
resolveElectronOptionObject(options, node, index, ["webPreferences"])) ??
|
||||
options;
|
||||
const lookup = objectProperty(resolvedOptions, "webPreferences");
|
||||
const declaredPreferences =
|
||||
lookup.status === "explicit"
|
||||
? unwrapJavaScriptExpression(lookup.property.value).node
|
||||
: undefined;
|
||||
const resolvedPreferences =
|
||||
(index && resolveElectronOptionObject(declaredPreferences, node, index)) ??
|
||||
declaredPreferences;
|
||||
const resolutionSources = [
|
||||
...(resolvedOptions !== options && resolvedOptions !== undefined
|
||||
? [range(resolvedOptions)]
|
||||
: []),
|
||||
...(resolvedPreferences !== declaredPreferences &&
|
||||
resolvedPreferences !== undefined
|
||||
? [range(resolvedPreferences)]
|
||||
: []),
|
||||
];
|
||||
const collected = collectWindowOptions(
|
||||
context.source,
|
||||
lookup.status,
|
||||
resolvedPreferences,
|
||||
);
|
||||
context.accumulator.unknownFindings += collected.unknown;
|
||||
const finding: ElectronBrowserWindowFinding = {
|
||||
options_status:
|
||||
options === undefined
|
||||
resolvedOptions === undefined
|
||||
? "missing"
|
||||
: t.isObjectExpression(options)
|
||||
: t.isObjectExpression(resolvedOptions)
|
||||
? "object-literal"
|
||||
: "dynamic",
|
||||
web_preferences_status: collected.status,
|
||||
web_preferences: collected.preferences,
|
||||
...(resolutionSources.length > 0
|
||||
? { resolution_sources: resolutionSources }
|
||||
: {}),
|
||||
...collected.preload,
|
||||
module_key: null,
|
||||
location: range(node),
|
||||
@@ -78,24 +161,20 @@ const inspectBrowserWindow = (
|
||||
|
||||
const collectWindowOptions = (
|
||||
source: string,
|
||||
options: t.Node | undefined,
|
||||
lookupStatus: "explicit" | "missing" | "dynamic",
|
||||
preferences: t.Node | undefined,
|
||||
): {
|
||||
readonly status: ElectronBrowserWindowFinding["web_preferences_status"];
|
||||
readonly preferences: readonly ElectronWebPreference[];
|
||||
readonly preload: ElectronBrowserWindowPreload;
|
||||
readonly unknown: number;
|
||||
} => {
|
||||
const lookup = objectProperty(options, "webPreferences");
|
||||
const preferences =
|
||||
lookup.status === "explicit"
|
||||
? unwrapJavaScriptExpression(lookup.property.value).node
|
||||
: undefined;
|
||||
if (lookup.status !== "explicit" || !t.isObjectExpression(preferences))
|
||||
if (lookupStatus !== "explicit" || !t.isObjectExpression(preferences))
|
||||
return {
|
||||
status: lookup.status === "missing" ? "missing" : "dynamic",
|
||||
status: lookupStatus === "missing" ? "missing" : "dynamic",
|
||||
preferences: [],
|
||||
preload: { preload_path: null, preload_resolution_context: null },
|
||||
unknown: lookup.status === "missing" ? 0 : 1,
|
||||
unknown: lookupStatus === "missing" ? 0 : 1,
|
||||
};
|
||||
return collectWebPreferences(source, preferences);
|
||||
};
|
||||
|
||||
@@ -2,6 +2,128 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
||||
|
||||
describe("Electron constant option evidence", () => {
|
||||
it("keeps mixed constant, inline and unresolved windows in construction order", () => {
|
||||
const analysis = analyzeJavaScriptStaticSource(
|
||||
[
|
||||
'const { BrowserWindow } = require("electron");',
|
||||
"const options = { webPreferences: { sandbox: true } };",
|
||||
"new BrowserWindow(options);",
|
||||
"new BrowserWindow({ webPreferences: { sandbox: false } });",
|
||||
"new BrowserWindow(dynamic);",
|
||||
"new BrowserWindow(options);",
|
||||
].join("\n"),
|
||||
);
|
||||
expect(
|
||||
analysis.electron.browser_windows.map(
|
||||
({ location }) => location.start.line,
|
||||
),
|
||||
).toEqual([3, 4, 5, 6]);
|
||||
expect(
|
||||
analysis.electron.browser_windows.map(
|
||||
({ options_status }) => options_status,
|
||||
),
|
||||
).toEqual([
|
||||
"object-literal",
|
||||
"object-literal",
|
||||
"dynamic",
|
||||
// The first constructor already received this object; later contents
|
||||
// remain unknown even though the declarations retain their source order.
|
||||
"dynamic",
|
||||
]);
|
||||
});
|
||||
|
||||
it("resolves local constant options despite an unrelated option write", () => {
|
||||
const analysis = analyzeJavaScriptStaticSource(`
|
||||
const { BrowserWindow } = require("electron");
|
||||
function open() {
|
||||
const preferences = { nodeIntegration: true, contextIsolation: false };
|
||||
const options = { webPreferences: preferences };
|
||||
if (linux) options.icon = "icon.png";
|
||||
new BrowserWindow(options);
|
||||
}
|
||||
`);
|
||||
expect(analysis.electron.browser_windows).toMatchObject([
|
||||
{
|
||||
options_status: "object-literal",
|
||||
web_preferences_status: "object-literal",
|
||||
web_preferences: [
|
||||
{
|
||||
name: "contextIsolation",
|
||||
value: { status: "literal", value: false },
|
||||
},
|
||||
{
|
||||
name: "nodeIntegration",
|
||||
value: { status: "literal", value: true },
|
||||
},
|
||||
],
|
||||
resolution_sources: [expect.any(Object), expect.any(Object)],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"options.webPreferences.sandbox = false;",
|
||||
"const alias = options.webPreferences; alias.sandbox = false;",
|
||||
"mutate(options);",
|
||||
"const { webPreferences: alias } = options; mutate(alias);",
|
||||
"Object.assign(options, unknown);",
|
||||
"const box = {}; box.options = options; box.options.webPreferences.sandbox = false;",
|
||||
"options = replacement;",
|
||||
])("keeps changed or escaped constant options unknown: %s", (mutation) => {
|
||||
const analysis = analyzeJavaScriptStaticSource(`
|
||||
const { BrowserWindow } = require("electron");
|
||||
const options = { webPreferences: { sandbox: true } };
|
||||
${mutation}
|
||||
new BrowserWindow(options);
|
||||
`);
|
||||
expect(analysis.electron.browser_windows).toMatchObject([
|
||||
{
|
||||
options_status: "dynamic",
|
||||
web_preferences_status: "dynamic",
|
||||
web_preferences: [],
|
||||
preload_path: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"new BrowserWindow(options); const options = { webPreferences: { sandbox: true } };",
|
||||
"const options = alias; const alias = { webPreferences: { sandbox: true } }; new BrowserWindow(options);",
|
||||
"const options = alias; const alias = options; new BrowserWindow(options);",
|
||||
"const options = { webPreferences: { sandbox: true } }; { const options = unknown; new BrowserWindow(options); }",
|
||||
"let options = { webPreferences: { sandbox: true } }; new BrowserWindow(options);",
|
||||
])("does not invent a constant initializer: %s", (source) => {
|
||||
const analysis = analyzeJavaScriptStaticSource(`
|
||||
const { BrowserWindow } = require("electron"); ${source}
|
||||
`);
|
||||
expect(analysis.electron.browser_windows).toMatchObject([
|
||||
{
|
||||
options_status: "dynamic",
|
||||
web_preferences_status: "dynamic",
|
||||
web_preferences: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves constants selected before later mutation and resolves preload context", () => {
|
||||
const analysis = analyzeJavaScriptStaticSource(`
|
||||
const { BrowserWindow } = require("electron");
|
||||
const original = { webPreferences: { preload: require("path").join(__dirname, "preload.js"), sandbox: true } };
|
||||
const options = original;
|
||||
new BrowserWindow(options);
|
||||
original.webPreferences.sandbox = false;
|
||||
`);
|
||||
expect(analysis.electron.browser_windows).toMatchObject([
|
||||
{
|
||||
options_status: "object-literal",
|
||||
preload_path: "preload.js",
|
||||
preload_resolution_context: "filesystem-expression",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Electron option evidence", () => {
|
||||
it.each([
|
||||
'({ webPreferences: { preload: "./preload.js", sandbox: true } } as const)',
|
||||
|
||||
@@ -38,6 +38,8 @@ export type ElectronBrowserWindowFinding = ElectronBrowserWindowPreload & {
|
||||
readonly options_status: "object-literal" | "dynamic" | "missing";
|
||||
readonly web_preferences_status: "object-literal" | "dynamic" | "missing";
|
||||
readonly web_preferences: readonly ElectronWebPreference[];
|
||||
/** Object syntax supporting constant substitution, separate from construction. */
|
||||
readonly resolution_sources?: readonly JavaScriptSourceRange[];
|
||||
readonly module_key: string | null;
|
||||
/** Recovered factory runtime; absent in older or unbundled findings. */
|
||||
readonly module_runtime?: string;
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import * as t from "@babel/types";
|
||||
import { unwrapJavaScriptExpression } from "./javascriptAstValues.js";
|
||||
import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js";
|
||||
import {
|
||||
semanticStableInitializer,
|
||||
type JavaScriptBindingIndex,
|
||||
} from "./javascriptSemanticState.js";
|
||||
|
||||
/** Lexical candidates alone do not establish that object contents are intact. */
|
||||
const optionObjectCandidate = (
|
||||
input: t.Node | undefined,
|
||||
index: JavaScriptBindingIndex,
|
||||
):
|
||||
| {
|
||||
readonly object: t.ObjectExpression;
|
||||
readonly references: readonly t.Identifier[];
|
||||
}
|
||||
| undefined => {
|
||||
if (input === undefined) return undefined;
|
||||
let node = unwrapJavaScriptExpression(input).node;
|
||||
const seen = new Set<t.Node>();
|
||||
const references: t.Identifier[] = [];
|
||||
while (t.isIdentifier(node)) {
|
||||
if (seen.has(node)) return undefined;
|
||||
seen.add(node);
|
||||
const binding = index.bindingForReferenceAt(node);
|
||||
if (binding === undefined) return undefined;
|
||||
const initializer = semanticStableInitializer(binding, index.bindings);
|
||||
if (
|
||||
binding.mutable ||
|
||||
binding.declarationKinds.size !== 1 ||
|
||||
!binding.declarationKinds.has("const") ||
|
||||
initializer === undefined ||
|
||||
initializer.projection.length !== 0 ||
|
||||
!semanticBindingAtRead(binding, node, index.bindings.parentsByNode)
|
||||
.hasDefiniteInitializer
|
||||
)
|
||||
return undefined;
|
||||
references.push(node);
|
||||
node = unwrapJavaScriptExpression(initializer.node).node;
|
||||
}
|
||||
return t.isObjectExpression(node) ? { object: node, references } : undefined;
|
||||
};
|
||||
|
||||
/** Avoid building mutation facts for parameters, missing or unresolved objects. */
|
||||
export const hasElectronOptionObjectCandidate = (
|
||||
input: t.Node | undefined,
|
||||
index: JavaScriptBindingIndex,
|
||||
): boolean => optionObjectCandidate(input, index) !== undefined;
|
||||
|
||||
/** Select an inert const object using the shared lexical and mutation owner. */
|
||||
export const resolveElectronOptionObject = (
|
||||
input: t.Node | undefined,
|
||||
site: t.Node,
|
||||
index: JavaScriptBindingIndex,
|
||||
propertyPath: readonly string[] = [],
|
||||
): t.ObjectExpression | undefined => {
|
||||
const candidate = optionObjectCandidate(input, index);
|
||||
if (candidate === undefined) return undefined;
|
||||
for (const reference of candidate.references) {
|
||||
if (index.referenceMayHaveChangedAt(site, reference, propertyPath))
|
||||
return undefined;
|
||||
}
|
||||
return candidate.object;
|
||||
};
|
||||
@@ -175,13 +175,16 @@ function* indexSemanticMemberMutationFactsSteps(
|
||||
indexedMemberMutationStates.add(state);
|
||||
}
|
||||
|
||||
/** Resolve one identifier or member use and summarize its source-site effects. */
|
||||
const semanticReferenceEffectsAtSite = (
|
||||
/** One owner projects source-site effects for complete reports or early exit. */
|
||||
const visitSemanticReferenceEffectsAtSite = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
site: t.Node,
|
||||
referenceNode: t.Node,
|
||||
visit: (
|
||||
effect: JavaScriptSemanticReferenceEffectsAtSite["effects"][number],
|
||||
) => boolean,
|
||||
requestedPath?: JavaScriptSemanticPropertyPath,
|
||||
): JavaScriptSemanticReferenceEffectsAtSite => {
|
||||
): Omit<JavaScriptSemanticReferenceEffectsAtSite, "effects"> => {
|
||||
const { root, path: inferredPath } = semanticReferenceRoot(referenceNode);
|
||||
const memberEffectsIndexed = indexedMemberMutationStates.has(state);
|
||||
if (!t.isIdentifier(root))
|
||||
@@ -189,7 +192,6 @@ const semanticReferenceEffectsAtSite = (
|
||||
bindingId: null,
|
||||
resolution: "unknown",
|
||||
memberEffectsIndexed,
|
||||
effects: [],
|
||||
};
|
||||
const lookup = semanticBindingLookupAt(state, root, root.name);
|
||||
const binding = "binding" in lookup ? lookup.binding : undefined;
|
||||
@@ -197,16 +199,19 @@ const semanticReferenceEffectsAtSite = (
|
||||
const resolution =
|
||||
lookup.kind === "binding" ? "resolved" : blocked ? "unknown" : "unbound";
|
||||
const path = requestedPath ?? inferredPath;
|
||||
const effects: JavaScriptSemanticReferenceEffectsAtSite["effects"][number][] =
|
||||
[];
|
||||
const metadata: Omit<JavaScriptSemanticReferenceEffectsAtSite, "effects"> = {
|
||||
bindingId: binding?.bindingId ?? null,
|
||||
resolution,
|
||||
memberEffectsIndexed,
|
||||
};
|
||||
const retain = (
|
||||
kind: JavaScriptSemanticReferenceEffectsAtSite["effects"][number]["kind"],
|
||||
node: t.Node | undefined,
|
||||
effectPath?: JavaScriptSemanticPropertyPath,
|
||||
): void => {
|
||||
): boolean => {
|
||||
if (effectPath !== undefined && !semanticPathsOverlap(path, effectPath))
|
||||
return;
|
||||
effects.push({
|
||||
return true;
|
||||
return visit({
|
||||
kind,
|
||||
...(effectPath === undefined ? {} : { path: effectPath }),
|
||||
timing: semanticEffectTiming(site, node, state),
|
||||
@@ -214,23 +219,40 @@ const semanticReferenceEffectsAtSite = (
|
||||
};
|
||||
if (binding !== undefined) {
|
||||
for (const node of state.bindingWrites.get(binding.bindingId) ?? [])
|
||||
retain("binding-write", node);
|
||||
if (!retain("binding-write", node)) return metadata;
|
||||
if (indexedMemberMutationStates.has(state)) {
|
||||
for (const effect of binding.memberWrites)
|
||||
retain("member-write", effect.node, effect.path);
|
||||
if (!retain("member-write", effect.node, effect.path)) return metadata;
|
||||
for (const effect of binding.escapedPaths)
|
||||
retain("escape", effect.node, effect.path);
|
||||
if (!retain("escape", effect.node, effect.path)) return metadata;
|
||||
}
|
||||
} else if (lookup.kind === "ambient" || lookup.kind === "uncertain-ambient") {
|
||||
for (const node of state.ambientBindingWrites.get(root.name) ?? [])
|
||||
retain("ambient-write", node);
|
||||
if (!retain("ambient-write", node)) return metadata;
|
||||
}
|
||||
return {
|
||||
bindingId: binding?.bindingId ?? null,
|
||||
resolution,
|
||||
memberEffectsIndexed,
|
||||
effects,
|
||||
};
|
||||
return metadata;
|
||||
};
|
||||
|
||||
/** Preserve the complete diagnostic contract for consumers needing every effect. */
|
||||
const semanticReferenceEffectsAtSite = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
site: t.Node,
|
||||
referenceNode: t.Node,
|
||||
requestedPath?: JavaScriptSemanticPropertyPath,
|
||||
): JavaScriptSemanticReferenceEffectsAtSite => {
|
||||
const effects: JavaScriptSemanticReferenceEffectsAtSite["effects"][number][] =
|
||||
[];
|
||||
const metadata = visitSemanticReferenceEffectsAtSite(
|
||||
state,
|
||||
site,
|
||||
referenceNode,
|
||||
(effect) => {
|
||||
effects.push(effect);
|
||||
return true;
|
||||
},
|
||||
requestedPath,
|
||||
);
|
||||
return { ...metadata, effects };
|
||||
};
|
||||
|
||||
const semanticReferenceRoot = (
|
||||
@@ -421,6 +443,12 @@ export function* indexJavaScriptBindingsWithReceiversSteps(
|
||||
*indexMemberMutationFactsSteps() {
|
||||
yield* indexSemanticMemberMutationFactsSteps(state);
|
||||
},
|
||||
bindingForReferenceAt(referenceNode) {
|
||||
const { root } = semanticReferenceRoot(referenceNode);
|
||||
return t.isIdentifier(root)
|
||||
? resolveSemanticBindingState(state, root, root.name)
|
||||
: undefined;
|
||||
},
|
||||
effectsForReferenceAt(site, referenceNode, propertyPath) {
|
||||
return semanticReferenceEffectsAtSite(
|
||||
state,
|
||||
@@ -429,6 +457,21 @@ export function* indexJavaScriptBindingsWithReceiversSteps(
|
||||
propertyPath,
|
||||
);
|
||||
},
|
||||
referenceMayHaveChangedAt(site, referenceNode, propertyPath) {
|
||||
if (!indexedMemberMutationStates.has(state)) return true;
|
||||
let changed = false;
|
||||
const metadata = visitSemanticReferenceEffectsAtSite(
|
||||
state,
|
||||
site,
|
||||
referenceNode,
|
||||
(effect) => {
|
||||
changed = effect.timing !== "after";
|
||||
return !changed;
|
||||
},
|
||||
propertyPath,
|
||||
);
|
||||
return metadata.resolution !== "resolved" || changed;
|
||||
},
|
||||
*referencesSteps() {
|
||||
references ??= yield* collectSemanticReferencesSteps(program, state);
|
||||
return references;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { expect, it } from "vitest";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
import {
|
||||
analyzeParsedJavaScriptSemantics,
|
||||
@@ -15,6 +16,51 @@ import {
|
||||
traverseJavaScriptAstSteps,
|
||||
} from "./javascriptSemanticTraversal.js";
|
||||
|
||||
it("bounds repeated capture checks while preserving unindexed and escaped unknowns", () => {
|
||||
const count = 128;
|
||||
const parsed = parseJavaScriptSource(
|
||||
[
|
||||
"const options = {webPreferences: {sandbox: true}};",
|
||||
...Array.from({ length: count }, () => "new BrowserWindow(options);"),
|
||||
].join("\n"),
|
||||
);
|
||||
if (parsed === null) throw new Error("Expected parsed source");
|
||||
const index = completeSemanticSteps(
|
||||
indexJavaScriptBindingsWithReceiversSteps(parsed.program),
|
||||
);
|
||||
const sites = parsed.program.body.flatMap((node) =>
|
||||
t.isExpressionStatement(node) && t.isNewExpression(node.expression)
|
||||
? [node.expression]
|
||||
: [],
|
||||
);
|
||||
const first = sites[0];
|
||||
if (first === undefined || !t.isIdentifier(first.arguments[0]))
|
||||
throw new Error("Expected capture");
|
||||
expect(index.referenceMayHaveChangedAt(first, first.arguments[0])).toBe(true);
|
||||
completeSemanticSteps(index.indexMemberMutationFactsSteps());
|
||||
const binding = index.bindingForReferenceAt(first.arguments[0]);
|
||||
if (binding === undefined) throw new Error("Expected local binding");
|
||||
let reads = 0;
|
||||
const effects = new Proxy(binding.escapedPaths, {
|
||||
get(target, key, receiver) {
|
||||
if (typeof key === "string" && /^\d+$/u.test(key)) reads += 1;
|
||||
return Reflect.get(target, key, receiver);
|
||||
},
|
||||
});
|
||||
Object.defineProperty(binding, "escapedPaths", { value: effects });
|
||||
const changed = sites.map((site) => {
|
||||
const reference = site.arguments[0];
|
||||
if (!t.isIdentifier(reference)) throw new Error("Expected local reference");
|
||||
return index.referenceMayHaveChangedAt(site, reference, ["webPreferences"]);
|
||||
});
|
||||
expect(changed).toEqual([
|
||||
false,
|
||||
...Array.from({ length: count - 1 }, () => true),
|
||||
]);
|
||||
// Allow implementation headroom while rejecting a full N-by-N effect report.
|
||||
expect(reads).toBeLessThanOrEqual(count * 4);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"checkpoints suppressed GraphQL sources, including alias proof (aliases: %s)",
|
||||
(aliased) => {
|
||||
|
||||
@@ -163,11 +163,21 @@ export interface JavaScriptBindingIndex {
|
||||
readonly bindings: JavaScriptSemanticAnalysisState;
|
||||
indexAmbientWritesSteps(): Generator<void, void>;
|
||||
indexMemberMutationFactsSteps(): Generator<void, void>;
|
||||
/** Resolve certain lexical ownership without collecting mutation reports. */
|
||||
bindingForReferenceAt(
|
||||
referenceNode: t.Node,
|
||||
): JavaScriptSemanticBindingState | undefined;
|
||||
effectsForReferenceAt(
|
||||
site: t.Node,
|
||||
referenceNode: t.Node,
|
||||
propertyPath?: JavaScriptSemanticPropertyPath,
|
||||
): JavaScriptSemanticReferenceEffectsAtSite;
|
||||
/** Stop at the first relevant prior/uncertain effect without retaining a report. */
|
||||
referenceMayHaveChangedAt(
|
||||
site: t.Node,
|
||||
referenceNode: t.Node,
|
||||
propertyPath?: JavaScriptSemanticPropertyPath,
|
||||
): boolean;
|
||||
referencesSteps(): Generator<void, readonly JavaScriptSemanticReference[]>;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from "./javascriptSemanticTraversal.js";
|
||||
|
||||
import { inspectElectronStaticNode } from "./electronStaticAnalysis.js";
|
||||
import { finishElectronBrowserWindowsSteps } from "./electronStaticAnalysisBrowser.js";
|
||||
import {
|
||||
detectVendors,
|
||||
failedJavaScriptStaticAnalysis,
|
||||
@@ -99,6 +100,7 @@ export function* analyzeParsedJavaScriptStaticSourceSteps(
|
||||
rpcScan: createJavaScriptRpcScan(source, receivers.rpcClientRoots, index),
|
||||
};
|
||||
yield* traverseStaticSourceSteps(file, findings, bindings);
|
||||
yield* finishElectronBrowserWindowsSteps(findings, index);
|
||||
if (accumulator.pendingNetworkCalls.length > 0) {
|
||||
yield* index.indexMemberMutationFactsSteps();
|
||||
finishPartialNetworkEndpoints(findings, bindings, index);
|
||||
|
||||
@@ -571,8 +571,7 @@ const resolvedRequestTarget = (
|
||||
// A toString result is selected before the enclosing request invocation.
|
||||
const expression = unwrapJavaScriptExpression(node).node;
|
||||
const capture = reference === expression ? site : expression;
|
||||
if (referenceMayHaveChangedAtSite(reference, capture, index))
|
||||
return undefined;
|
||||
if (index.referenceMayHaveChangedAt(capture, reference)) return undefined;
|
||||
if (!requestInitializerPrecedesSite(reference, capture, bindings, index))
|
||||
return undefined;
|
||||
const evaluated = evaluateSemanticExpression(node, bindings);
|
||||
@@ -587,9 +586,7 @@ const requestInitializerPrecedesSite = (
|
||||
bindings: JavaScriptSemanticAnalysisState,
|
||||
index: JavaScriptBindingIndex,
|
||||
): boolean => {
|
||||
const effects = index.effectsForReferenceAt(site, reference);
|
||||
if (effects.bindingId === null) return false;
|
||||
const binding = bindings.bindingsById.get(effects.bindingId);
|
||||
const binding = index.bindingForReferenceAt(reference);
|
||||
const initializer = semanticStableInitializer(binding, bindings);
|
||||
return (
|
||||
initializer !== undefined &&
|
||||
@@ -601,19 +598,6 @@ const requestInitializerPrecedesSite = (
|
||||
);
|
||||
};
|
||||
|
||||
const referenceMayHaveChangedAtSite = (
|
||||
reference: t.Node,
|
||||
site: t.Node,
|
||||
index: JavaScriptBindingIndex,
|
||||
): boolean => {
|
||||
const effects = index.effectsForReferenceAt(site, reference);
|
||||
return (
|
||||
!effects.memberEffectsIndexed ||
|
||||
effects.resolution !== "resolved" ||
|
||||
effects.effects.some(({ timing }) => timing !== "after")
|
||||
);
|
||||
};
|
||||
|
||||
const requestReferenceNode = (node: t.Node): t.Node => {
|
||||
const target = unwrapJavaScriptExpression(node).node;
|
||||
if (
|
||||
@@ -652,16 +636,9 @@ const urlObjectRequestTarget = (
|
||||
)
|
||||
target = unwrapJavaScriptExpression(callee.object).node;
|
||||
}
|
||||
if (
|
||||
!t.isIdentifier(target) ||
|
||||
referenceMayHaveChangedAtSite(target, site, index)
|
||||
)
|
||||
if (!t.isIdentifier(target) || index.referenceMayHaveChangedAt(site, target))
|
||||
return undefined;
|
||||
const effects = index.effectsForReferenceAt(site, target);
|
||||
const binding =
|
||||
effects.bindingId === null
|
||||
? undefined
|
||||
: bindings.bindingsById.get(effects.bindingId);
|
||||
const binding = index.bindingForReferenceAt(target);
|
||||
if (binding === undefined || seen.has(binding.bindingId)) return undefined;
|
||||
seen.add(binding.bindingId);
|
||||
const stable = semanticStableInitializer(binding, bindings);
|
||||
|
||||
@@ -55,6 +55,7 @@ export interface JavaScriptAnalysisAccumulator {
|
||||
readonly sourceMaps: JavaScriptStaticAnalysis["source_map_urls"][number][];
|
||||
readonly registrations: JavaScriptBundlerRegistration[];
|
||||
readonly browserWindows: LocatedJavaScriptFinding<ElectronBrowserWindowFinding>[];
|
||||
readonly pendingBrowserWindows: t.NewExpression[];
|
||||
readonly contextBridgeApis: LocatedJavaScriptFinding<ElectronContextBridgeFinding>[];
|
||||
readonly ipc: LocatedJavaScriptFinding<ElectronIpcFinding>[];
|
||||
readonly senderValidations: LocatedJavaScriptFinding<ElectronSenderValidationFinding>[];
|
||||
@@ -120,6 +121,7 @@ export const createJavaScriptAnalysisAccumulator = (
|
||||
sourceMaps: [],
|
||||
registrations: [],
|
||||
browserWindows: [],
|
||||
pendingBrowserWindows: [],
|
||||
contextBridgeApis: [],
|
||||
ipc: [],
|
||||
senderValidations: [],
|
||||
|
||||
Reference in new issue
Block a user