fix(javascript): retain runtime scope in bundled module relationships
This commit is contained in:
@@ -2,9 +2,10 @@ import type { ApplicationNode } from "../../domain/javascript/javascriptApplicat
|
||||
import { sha256Text } from "../../domain/javascript/javascriptStaticAnalysisHelpers.js";
|
||||
import type { JavaScriptSourceRange } from "../../domain/javascript/javascriptStaticAnalysisTypes.js";
|
||||
import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js";
|
||||
import type {
|
||||
JavaScriptArtifactGraphContext,
|
||||
JavaScriptArtifactGraphCoverage,
|
||||
import {
|
||||
sourceNodeFor,
|
||||
type JavaScriptArtifactGraphContext,
|
||||
type JavaScriptArtifactGraphCoverage,
|
||||
} from "./JavaScriptArtifactGraphContext.js";
|
||||
import {
|
||||
astObservationEvidence,
|
||||
@@ -38,10 +39,11 @@ export const electronFindingSourceNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
file: JavaScriptArtifactFile,
|
||||
moduleKey: string | null,
|
||||
runtime?: string,
|
||||
): ApplicationNode | undefined =>
|
||||
moduleKey === null
|
||||
? (context.assetNodes.get(file.path) ?? context.fileNodes.get(file.path))
|
||||
: context.moduleNodes.get(`${file.path}\0${moduleKey}`);
|
||||
: sourceNodeFor(context, file.path, moduleKey, runtime);
|
||||
|
||||
/** Observation-scoped identity shared safely across different artifact files. */
|
||||
export const electronObservationIdentity = (
|
||||
|
||||
@@ -73,6 +73,7 @@ const addIpcRecord = (
|
||||
context,
|
||||
record.file,
|
||||
record.finding.module_key,
|
||||
record.finding.module_runtime,
|
||||
);
|
||||
if (source === undefined) return;
|
||||
state.sources.set(record.key, source);
|
||||
@@ -255,6 +256,7 @@ const addValidationObservations = (state: IpcGraphState): void => {
|
||||
state.context,
|
||||
file,
|
||||
validation.module_key,
|
||||
validation.module_runtime,
|
||||
)
|
||||
: state.handlers.get(handlerRecord.key);
|
||||
if (target !== undefined)
|
||||
|
||||
@@ -38,7 +38,12 @@ export const addElectronNativeBoundaries = (
|
||||
|
||||
const addNativeBinding = (input: NativeBindingInput): void => {
|
||||
const { context, file, value, coverage } = input;
|
||||
const source = electronFindingSourceNode(context, file, value.module_key);
|
||||
const source = electronFindingSourceNode(
|
||||
context,
|
||||
file,
|
||||
value.module_key,
|
||||
value.module_runtime,
|
||||
);
|
||||
if (source === undefined) return;
|
||||
const resolved = resolveArtifactPath(
|
||||
value.specifier,
|
||||
|
||||
@@ -39,17 +39,32 @@ export const addElectronWindowBoundaries = (
|
||||
if (javascript === null) continue;
|
||||
const coverage = javascriptAnalysisCoverage(javascript);
|
||||
for (const value of javascript.electron.browser_windows) {
|
||||
const source = electronFindingSourceNode(context, file, value.module_key);
|
||||
const source = electronFindingSourceNode(
|
||||
context,
|
||||
file,
|
||||
value.module_key,
|
||||
value.module_runtime,
|
||||
);
|
||||
if (source !== undefined)
|
||||
addBrowserWindow({ context, file, source, value, coverage });
|
||||
}
|
||||
for (const value of javascript.electron.context_bridge_apis) {
|
||||
const source = electronFindingSourceNode(context, file, value.module_key);
|
||||
const source = electronFindingSourceNode(
|
||||
context,
|
||||
file,
|
||||
value.module_key,
|
||||
value.module_runtime,
|
||||
);
|
||||
if (source !== undefined)
|
||||
addContextBridge({ context, file, source, value, coverage });
|
||||
}
|
||||
for (const value of javascript.electron.utility_processes) {
|
||||
const source = electronFindingSourceNode(context, file, value.module_key);
|
||||
const source = electronFindingSourceNode(
|
||||
context,
|
||||
file,
|
||||
value.module_key,
|
||||
value.module_runtime,
|
||||
);
|
||||
if (source !== undefined)
|
||||
addUtilityProcess({ context, file, source, value, coverage });
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ const addBundlerModuleNodes = (
|
||||
],
|
||||
});
|
||||
context.moduleNodes.set(
|
||||
moduleLookupKey(file.path, moduleValue.module_key),
|
||||
moduleLookupKey(file.path, registration.runtime, moduleValue.module_key),
|
||||
module,
|
||||
);
|
||||
addAstContainsEdge(context, {
|
||||
@@ -195,7 +195,7 @@ const addBundlerRuntimeEdges = (
|
||||
const { file, registration, chunk, coverage } = record;
|
||||
for (const moduleKey of registration.entry_module_keys) {
|
||||
const resolved = context.moduleNodes.get(
|
||||
moduleLookupKey(file.path, moduleKey),
|
||||
moduleLookupKey(file.path, registration.runtime, moduleKey),
|
||||
);
|
||||
const target =
|
||||
resolved ?? unresolvedBundlerModuleNode(context, record, moduleKey);
|
||||
|
||||
@@ -323,8 +323,11 @@ export const selfReferenceOmissions = (
|
||||
];
|
||||
|
||||
/** Deterministic key for one recovered module inside a bundle asset. */
|
||||
export const moduleLookupKey = (path: string, moduleKey: string): string =>
|
||||
`${path}\0${moduleKey}`;
|
||||
export const moduleLookupKey = (
|
||||
path: string,
|
||||
runtime: string,
|
||||
moduleKey: string,
|
||||
): string => compositeKey([path, runtime, moduleKey]);
|
||||
|
||||
/** Deterministic key for one recovered bundler chunk inside a bundle asset. */
|
||||
export const chunkLookupKey = (
|
||||
@@ -353,7 +356,10 @@ export const sourceNodeFor = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
path: string,
|
||||
moduleKey: string | null,
|
||||
runtime?: string,
|
||||
): ApplicationNode | undefined =>
|
||||
moduleKey === null
|
||||
? context.sourceModuleNodes.get(path)
|
||||
: context.moduleNodes.get(moduleLookupKey(path, moduleKey));
|
||||
: runtime === undefined
|
||||
? undefined
|
||||
: context.moduleNodes.get(moduleLookupKey(path, runtime, moduleKey));
|
||||
|
||||
@@ -78,8 +78,12 @@ const addReference = (
|
||||
omissions: { selfReferences: number },
|
||||
): void => {
|
||||
const source =
|
||||
sourceNodeFor(context, input.file.path, input.value.module_key) ??
|
||||
input.asset;
|
||||
sourceNodeFor(
|
||||
context,
|
||||
input.file.path,
|
||||
input.value.module_key,
|
||||
input.value.module_runtime,
|
||||
) ?? input.asset;
|
||||
const resolved =
|
||||
input.value.specifier === null
|
||||
? null
|
||||
@@ -162,8 +166,12 @@ const addEndpoint = (
|
||||
input: FindingInput<StaticEndpoint>,
|
||||
): void => {
|
||||
const source =
|
||||
sourceNodeFor(context, input.file.path, input.value.module_key) ??
|
||||
input.asset;
|
||||
sourceNodeFor(
|
||||
context,
|
||||
input.file.path,
|
||||
input.value.module_key,
|
||||
input.value.module_runtime,
|
||||
) ?? input.asset;
|
||||
const key = compactStringIdentityKey(input.value.value);
|
||||
const label = `${input.value.kind} endpoint`;
|
||||
const node = context.accumulator.addNode({
|
||||
@@ -204,8 +212,12 @@ const addStorage = (
|
||||
input: FindingInput<StaticStorage>,
|
||||
): void => {
|
||||
const source =
|
||||
sourceNodeFor(context, input.file.path, input.value.module_key) ??
|
||||
input.asset;
|
||||
sourceNodeFor(
|
||||
context,
|
||||
input.file.path,
|
||||
input.value.module_key,
|
||||
input.value.module_runtime,
|
||||
) ?? input.asset;
|
||||
const node = context.accumulator.addNode({
|
||||
kind: "storage",
|
||||
identity: artifactLocalIdentity(
|
||||
@@ -262,8 +274,12 @@ const addDiscoveredRole = (
|
||||
coverage: input.coverage,
|
||||
});
|
||||
const source =
|
||||
sourceNodeFor(context, input.file.path, input.value.module_key) ??
|
||||
input.asset;
|
||||
sourceNodeFor(
|
||||
context,
|
||||
input.file.path,
|
||||
input.value.module_key,
|
||||
input.value.module_runtime,
|
||||
) ?? input.asset;
|
||||
addStaticInferenceEdge(context, {
|
||||
source,
|
||||
target: role,
|
||||
@@ -359,9 +375,12 @@ const resolveReference = (
|
||||
if (specifier === null) return null;
|
||||
const chunk = resolveBundlerChunkReference(context, sourcePath, reference);
|
||||
if (chunk !== null) return chunk;
|
||||
const module = context.moduleNodes.get(
|
||||
moduleLookupKey(sourcePath, specifier),
|
||||
);
|
||||
const module =
|
||||
reference.module_runtime === undefined
|
||||
? undefined
|
||||
: context.moduleNodes.get(
|
||||
moduleLookupKey(sourcePath, reference.module_runtime, specifier),
|
||||
);
|
||||
if (module !== undefined) {
|
||||
const file = context.filesByPath.get(sourcePath);
|
||||
return file === undefined
|
||||
@@ -407,10 +426,11 @@ const resolveBundlerChunkReference = (
|
||||
if (file === undefined) return null;
|
||||
const registrations = context.analysis.files
|
||||
.find(({ file: analyzedFile }) => analyzedFile.path === sourcePath)
|
||||
?.javascript?.bundler_registrations.filter(({ modules }) =>
|
||||
reference.module_key === null
|
||||
? true
|
||||
: modules.some(
|
||||
?.javascript?.bundler_registrations.filter(({ runtime, modules }) =>
|
||||
reference.module_runtime === undefined
|
||||
? reference.module_key === null
|
||||
: runtime === reference.module_runtime &&
|
||||
modules.some(
|
||||
({ module_key: moduleKey }) => moduleKey === reference.module_key,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user