fix(javascript): retain runtime scope in bundled module relationships

This commit is contained in:
dakjdakd
2026-10-11 01:36:12 +08:00
committed by Ysopwnable
parent 0d89978333
commit c1217e711b
14 changed files with 394 additions and 42 deletions
@@ -39,6 +39,8 @@ export type ElectronBrowserWindowFinding = ElectronBrowserWindowPreload & {
readonly web_preferences_status: "object-literal" | "dynamic" | "missing";
readonly web_preferences: readonly ElectronWebPreference[];
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
};
@@ -56,6 +58,8 @@ export type ElectronContextBridgeFinding = ElectronContextBridgeApiKey & {
readonly members: readonly string[];
readonly unknown_members: number;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
};
@@ -109,6 +113,8 @@ type ElectronIpcHandler =
interface ElectronIpcFindingContext {
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -134,12 +140,16 @@ export interface ElectronSenderValidationFinding {
readonly expected: ElectronStaticValue;
readonly enforcement: "unknown";
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
interface ElectronUtilityProcessFindingState {
readonly service_name: string | null;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -169,6 +179,8 @@ export interface ElectronNativeAddonBindingFinding {
readonly namespace_access?: boolean;
readonly dynamic_member_access?: boolean;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -40,7 +40,7 @@ export const inspectBundlerRegistration = (
const table = entry.elements[1];
const runtimeValue = runtimeMetadata(entry.elements[2]);
if (!t.isArrayExpression(chunkIds) || !t.isObjectExpression(table)) return;
const recovered = recoverBundlerModules(source, table, accumulator);
const recovered = recoverBundlerModules(source, table, runtime, accumulator);
const chunkKeys = staticArrayValues(chunkIds);
accumulator.unknownFindings += chunkKeys.unknown;
accumulator.unknownFindings +=
@@ -92,12 +92,13 @@ export const inspectEsbuildWrapper = (
: null;
const table = call.arguments[0];
if (wrapperKind === null || !t.isObjectExpression(table)) return;
const recovered = recoverBundlerModules(source, table, accumulator);
const runtime = `esbuild-${wrapperKind}`;
const recovered = recoverBundlerModules(source, table, runtime, accumulator);
if (recovered.modules.length === 0) return;
accumulator.unknownFindings += recovered.unknownAsyncChunkKeys;
const registration: JavaScriptBundlerRegistration = {
bundler: "esbuild",
runtime: `esbuild-${wrapperKind}`,
runtime,
chunk_keys: [`${wrapperKind}@${String(call.start ?? 0)}`],
unknown_chunk_keys: 0,
runtime_require_name: null,
@@ -127,13 +128,19 @@ interface RecoveredBundlerModules {
const recoverBundlerModules = (
source: string,
table: t.ObjectExpression,
runtime: string,
accumulator: AnalysisAccumulator,
): RecoveredBundlerModules => {
const modules: JavaScriptBundlerModule[] = [];
const asyncChunkKeys: string[] = [];
let unknownAsyncChunkKeys = 0;
for (const property of table.properties) {
const recovered = recoverBundlerModule(source, property, accumulator);
const recovered = recoverBundlerModule(
source,
property,
runtime,
accumulator,
);
if (recovered === null) continue;
modules.push(recovered.module);
asyncChunkKeys.push(...recovered.asyncChunkKeys);
@@ -149,6 +156,7 @@ const recoverBundlerModules = (
const recoverBundlerModule = (
source: string,
property: t.ObjectMethod | t.ObjectProperty | t.SpreadElement,
runtime: string,
accumulator: AnalysisAccumulator,
): {
readonly module: JavaScriptBundlerModule;
@@ -169,6 +177,7 @@ const recoverBundlerModule = (
start: factory.start,
end: factory.end,
key,
runtime,
requireName,
};
accumulator.moduleRangeIndex.add(moduleRange);
@@ -130,7 +130,11 @@ export const finalizeLocatedFindings = <
values: readonly LocatedJavaScriptFinding<Value>[],
modules: JavaScriptModuleRangeIndex,
): Value[] =>
values.map(({ offset, value }) => ({
...value,
module_key: modules.find(offset)?.key ?? null,
}));
values.map(({ offset, value }) => {
const module = modules.find(offset);
return {
...value,
module_key: module?.key ?? null,
...(module === undefined ? {} : { module_runtime: module.runtime }),
};
});
@@ -6,9 +6,27 @@ import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
it("preserves first insertion precedence and inclusive interval bounds", () => {
const nested = new JavaScriptModuleRangeIndex(100);
expect(nested.find(0)).toBeUndefined();
nested.add({ start: 10, end: 90, key: "outer-first", requireName: null });
nested.add({ start: 20, end: 30, key: "inner-second", requireName: null });
nested.add({ start: 30, end: 40, key: "later-overlap", requireName: null });
nested.add({
start: 10,
end: 90,
key: "outer-first",
runtime: "webpackChunkApp",
requireName: null,
});
nested.add({
start: 20,
end: 30,
key: "inner-second",
runtime: "webpackChunkApp",
requireName: null,
});
nested.add({
start: 30,
end: 40,
key: "later-overlap",
runtime: "webpackChunkApp",
requireName: null,
});
expect(nested.find(9)).toBeUndefined();
expect(nested.find(10)?.key).toBe("outer-first");
expect(nested.find(20)?.key).toBe("outer-first");
@@ -20,7 +38,13 @@ it("preserves first insertion precedence and inclusive interval bounds", () => {
it("rejects missing and out-of-range lookup offsets", () => {
const index = new JavaScriptModuleRangeIndex(20);
index.add({ start: 5, end: 10, key: "bounded", requireName: null });
index.add({
start: 5,
end: 10,
key: "bounded",
runtime: "webpackChunkApp",
requireName: null,
});
expect(index.find(undefined)).toBeUndefined();
expect(index.find(null)).toBeUndefined();
expect(index.find(-1)).toBeUndefined();
@@ -58,8 +82,11 @@ it("keeps module references attributed across many real factory ranges", () => {
expect(
analysis.references.find(
({ specifier }) => specifier === `dependency-${index}`,
)?.module_key,
).toBe(`module-${index}`);
),
).toMatchObject({
module_key: `module-${index}`,
module_runtime: "webpackChunkApp",
});
}
expect(
analysis.references.find(
@@ -23,6 +23,7 @@ export interface JavaScriptModuleRange {
readonly start: number;
readonly end: number;
readonly key: string;
readonly runtime: string;
readonly requireName: string | null;
}
@@ -25,6 +25,8 @@ interface JavaScriptStaticReferenceState {
| "worker"
| "service-worker";
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -41,6 +43,8 @@ export interface JavaScriptStaticEndpoint {
readonly value: string;
readonly mechanism: string;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -55,6 +59,8 @@ export interface JavaScriptStaticStorage {
readonly name: string | null;
readonly mechanism: string;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}
@@ -95,6 +101,8 @@ export interface JavaScriptRolePath {
readonly resolution_context: JavaScriptStaticPathContext;
readonly mechanism: string;
readonly module_key: string | null;
/** Recovered factory runtime; absent in older or unbundled findings. */
readonly module_runtime?: string;
readonly location: JavaScriptSourceRange;
}