feat(javascript): add webpack and rspack runtime adapters
This commit is contained in:
@@ -57,6 +57,7 @@ export const buildJavaScriptArtifactGraph = (
|
||||
fileNodes: new Map(),
|
||||
assetNodes: new Map(),
|
||||
sourceModuleNodes: new Map(),
|
||||
chunkNodes: new Map(),
|
||||
moduleNodes: new Map(),
|
||||
containerNodes: new Map([[snapshot.manifest.root_sha256, root]]),
|
||||
};
|
||||
|
||||
@@ -35,6 +35,7 @@ export interface JavaScriptArtifactGraphContext {
|
||||
readonly fileNodes: Map<string, ApplicationNode>;
|
||||
readonly assetNodes: Map<string, ApplicationNode>;
|
||||
readonly sourceModuleNodes: Map<string, ApplicationNode>;
|
||||
readonly chunkNodes: Map<string, ApplicationNode>;
|
||||
readonly moduleNodes: Map<string, ApplicationNode>;
|
||||
readonly containerNodes: Map<string, ApplicationNode>;
|
||||
}
|
||||
@@ -315,6 +316,13 @@ export const linkElectronRoleToAsset = (
|
||||
export const moduleLookupKey = (path: string, moduleKey: string): string =>
|
||||
`${path}\0${moduleKey}`;
|
||||
|
||||
/** Deterministic key for one recovered bundler chunk inside a bundle asset. */
|
||||
export const chunkLookupKey = (
|
||||
path: string,
|
||||
runtime: string,
|
||||
chunkKey: string,
|
||||
): string => `${path}\0${runtime}\0${chunkKey}`;
|
||||
|
||||
/** Resolve a relative static specifier without escaping the artifact root. */
|
||||
export const resolveArtifactPath = (
|
||||
specifier: string,
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { JavaScriptArtifactFile } from "./JavaScriptArtifactFiles.js";
|
||||
import {
|
||||
addStaticInferenceEdge,
|
||||
artifactLocalIdentity,
|
||||
chunkLookupKey,
|
||||
createElectronRoleNode,
|
||||
javascriptAnalysisCoverage,
|
||||
linkElectronRoleToAsset,
|
||||
@@ -329,6 +330,8 @@ const resolveReference = (
|
||||
): ResolvedReference | null => {
|
||||
const specifier = reference.specifier;
|
||||
if (specifier === null) return null;
|
||||
const chunk = resolveBundlerChunkReference(context, sourcePath, reference);
|
||||
if (chunk !== null) return chunk;
|
||||
const module = context.moduleNodes.get(
|
||||
moduleLookupKey(sourcePath, specifier),
|
||||
);
|
||||
@@ -355,6 +358,43 @@ const resolveReference = (
|
||||
return file === undefined || node === undefined ? null : { path, file, node };
|
||||
};
|
||||
|
||||
const resolveBundlerChunkReference = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
sourcePath: string,
|
||||
reference: StaticReference,
|
||||
): ResolvedReference | null => {
|
||||
const key = reference.specifier?.startsWith("chunk:")
|
||||
? reference.specifier.slice("chunk:".length)
|
||||
: null;
|
||||
if (key === null) return null;
|
||||
const file = context.filesByPath.get(sourcePath);
|
||||
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(
|
||||
({ module_key: moduleKey }) => moduleKey === reference.module_key,
|
||||
),
|
||||
);
|
||||
if (registrations === undefined) return null;
|
||||
const candidates = registrations.flatMap(({ runtime }) => {
|
||||
const node = context.chunkNodes.get(
|
||||
chunkLookupKey(sourcePath, runtime, key),
|
||||
);
|
||||
return node === undefined ? [] : [node];
|
||||
});
|
||||
const unique = [
|
||||
...new Map(candidates.map((node) => [node.node_id, node])).values(),
|
||||
];
|
||||
if (unique.length !== 1) return null;
|
||||
const node = unique[0];
|
||||
return node === undefined
|
||||
? null
|
||||
: { path: `${sourcePath}#chunk:${key}`, file, node };
|
||||
};
|
||||
|
||||
const unresolvedReferenceNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
file: JavaScriptArtifactFile,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { ArtifactInventorySnapshot } from "./ArtifactInventory.js";
|
||||
import type { ApplicationNode } from "../domain/javascriptApplicationGraph.js";
|
||||
import type { JavaScriptBundlerRegistration } from "../domain/javascriptStaticAnalysisTypes.js";
|
||||
import type { JavaScriptArtifactAnalysis } from "./JavaScriptArtifactAnalysisTypes.js";
|
||||
import type {
|
||||
JavaScriptArtifactContainer,
|
||||
@@ -12,11 +13,13 @@ import {
|
||||
addUnavailableStaticParseScope,
|
||||
artifactFileNodeKind,
|
||||
artifactLocalIdentity,
|
||||
chunkLookupKey,
|
||||
createElectronRoleNode,
|
||||
javascriptAnalysisCoverage,
|
||||
linkElectronRoleToAsset,
|
||||
moduleLookupKey,
|
||||
type JavaScriptArtifactGraphContext,
|
||||
type JavaScriptArtifactGraphCoverage,
|
||||
} from "./JavaScriptArtifactGraphContext.js";
|
||||
import {
|
||||
artifactObservationEvidence,
|
||||
@@ -288,34 +291,15 @@ export const addJavaScriptBundlerNodes = (
|
||||
const asset = context.assetNodes.get(file.path);
|
||||
if (javascript === null || asset === undefined) continue;
|
||||
const coverage = javascriptAnalysisCoverage(javascript, context.input);
|
||||
const records: BundlerGraphRecord[] = [];
|
||||
for (const registration of javascript.bundler_registrations) {
|
||||
const chunkKey = `${registration.runtime}:${registration.chunk_keys.join(",")}`;
|
||||
const chunk = context.accumulator.addNode({
|
||||
kind: "javascript-chunk",
|
||||
identity: artifactLocalIdentity(file.sha256, "bundler-chunk", chunkKey),
|
||||
observations: [
|
||||
{
|
||||
label: chunkKey,
|
||||
properties: {
|
||||
path: file.path,
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
chunk_keys: registration.chunk_keys,
|
||||
omitted_chunk_keys: registration.omitted_chunk_keys,
|
||||
unknown_chunk_keys: registration.unknown_chunk_keys,
|
||||
module_count: registration.modules.length,
|
||||
},
|
||||
evidence: astObservationEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: registration.location,
|
||||
operation: "extract-bundler-registration",
|
||||
coverage,
|
||||
limitations: javascript.limitations,
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
const input = {
|
||||
file,
|
||||
registration,
|
||||
coverage,
|
||||
limitations: javascript.limitations,
|
||||
};
|
||||
const chunk = createBundlerChunkNode(context, input);
|
||||
addAstContainsEdge(context, {
|
||||
source: asset,
|
||||
target: chunk,
|
||||
@@ -324,60 +308,273 @@ export const addJavaScriptBundlerNodes = (
|
||||
coverage,
|
||||
properties: { bundler: registration.bundler },
|
||||
});
|
||||
for (const moduleValue of registration.modules) {
|
||||
const module = context.accumulator.addNode({
|
||||
kind: "javascript-module",
|
||||
identity: artifactLocalIdentity(
|
||||
file.sha256,
|
||||
`${registration.runtime}:module`,
|
||||
moduleValue.module_key,
|
||||
),
|
||||
observations: [
|
||||
{
|
||||
label: moduleValue.module_key,
|
||||
properties: {
|
||||
path: file.path,
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
module_key: moduleValue.module_key,
|
||||
source_sha256: moduleValue.source_sha256,
|
||||
structural_fingerprint_sha256:
|
||||
moduleValue.structural_fingerprint_sha256,
|
||||
structural_fingerprint_algorithm:
|
||||
moduleValue.structural_fingerprint_algorithm,
|
||||
structural_fingerprint_status:
|
||||
moduleValue.structural_fingerprint_status,
|
||||
exports: moduleValue.exports,
|
||||
exports_truncated: moduleValue.exports_truncated,
|
||||
},
|
||||
evidence: astObservationEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: moduleValue.location,
|
||||
operation: "extract-bundler-module",
|
||||
coverage,
|
||||
limitations: javascript.limitations,
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
context.moduleNodes.set(
|
||||
moduleLookupKey(file.path, moduleValue.module_key),
|
||||
module,
|
||||
for (const key of registration.chunk_keys)
|
||||
context.chunkNodes.set(
|
||||
chunkLookupKey(file.path, registration.runtime, key),
|
||||
chunk,
|
||||
);
|
||||
addAstContainsEdge(context, {
|
||||
source: chunk,
|
||||
target: module,
|
||||
file,
|
||||
range: moduleValue.location,
|
||||
coverage,
|
||||
properties: { module_key: moduleValue.module_key },
|
||||
});
|
||||
}
|
||||
records.push({ file, registration, chunk, coverage });
|
||||
addBundlerModuleNodes(context, { ...input, chunk });
|
||||
}
|
||||
for (const record of records) addBundlerRuntimeEdges(context, record);
|
||||
}
|
||||
};
|
||||
|
||||
interface BundlerProjectionInput {
|
||||
readonly file: JavaScriptArtifactFile;
|
||||
readonly registration: JavaScriptBundlerRegistration;
|
||||
readonly coverage: JavaScriptArtifactGraphCoverage;
|
||||
readonly limitations: readonly string[];
|
||||
}
|
||||
|
||||
interface BundlerModuleProjectionInput extends BundlerProjectionInput {
|
||||
readonly chunk: ApplicationNode;
|
||||
}
|
||||
|
||||
const createBundlerChunkNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
input: BundlerProjectionInput,
|
||||
): ApplicationNode => {
|
||||
const { file, registration, coverage, limitations } = input;
|
||||
const chunkKey = `${registration.runtime}:${registration.chunk_keys.join(",")}`;
|
||||
return context.accumulator.addNode({
|
||||
kind: "javascript-chunk",
|
||||
identity: artifactLocalIdentity(file.sha256, "bundler-chunk", chunkKey),
|
||||
observations: [
|
||||
{
|
||||
label: chunkKey,
|
||||
properties: {
|
||||
path: file.path,
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
chunk_keys: registration.chunk_keys,
|
||||
omitted_chunk_keys: registration.omitted_chunk_keys,
|
||||
unknown_chunk_keys: registration.unknown_chunk_keys,
|
||||
runtime_require_name: registration.runtime_require_name,
|
||||
runtime_module_cache_status: registration.runtime_module_cache_status,
|
||||
entry_module_keys: registration.entry_module_keys,
|
||||
omitted_entry_module_keys: registration.omitted_entry_module_keys,
|
||||
unknown_entry_module_keys: registration.unknown_entry_module_keys,
|
||||
async_chunk_keys: registration.async_chunk_keys,
|
||||
omitted_async_chunk_keys: registration.omitted_async_chunk_keys,
|
||||
unknown_async_chunk_keys: registration.unknown_async_chunk_keys,
|
||||
module_count: registration.modules.length,
|
||||
},
|
||||
evidence: astObservationEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: registration.location,
|
||||
operation: "extract-bundler-registration",
|
||||
coverage,
|
||||
limitations,
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const addBundlerModuleNodes = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
input: BundlerModuleProjectionInput,
|
||||
): void => {
|
||||
const { file, registration, chunk, coverage, limitations } = input;
|
||||
for (const moduleValue of registration.modules) {
|
||||
const module = context.accumulator.addNode({
|
||||
kind: "javascript-module",
|
||||
identity: artifactLocalIdentity(
|
||||
file.sha256,
|
||||
`${registration.runtime}:module`,
|
||||
moduleValue.module_key,
|
||||
),
|
||||
observations: [
|
||||
{
|
||||
label: moduleValue.module_key,
|
||||
properties: {
|
||||
path: file.path,
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
chunk_keys: registration.chunk_keys,
|
||||
module_key: moduleValue.module_key,
|
||||
factory_require_name: moduleValue.factory_require_name,
|
||||
runtime_entry: registration.entry_module_keys.includes(
|
||||
moduleValue.module_key,
|
||||
),
|
||||
source_sha256: moduleValue.source_sha256,
|
||||
structural_fingerprint_sha256:
|
||||
moduleValue.structural_fingerprint_sha256,
|
||||
structural_fingerprint_algorithm:
|
||||
moduleValue.structural_fingerprint_algorithm,
|
||||
structural_fingerprint_status:
|
||||
moduleValue.structural_fingerprint_status,
|
||||
exports: moduleValue.exports,
|
||||
exports_truncated: moduleValue.exports_truncated,
|
||||
},
|
||||
evidence: astObservationEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: moduleValue.location,
|
||||
operation: "extract-bundler-module",
|
||||
coverage,
|
||||
limitations,
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
context.moduleNodes.set(
|
||||
moduleLookupKey(file.path, moduleValue.module_key),
|
||||
module,
|
||||
);
|
||||
addAstContainsEdge(context, {
|
||||
source: chunk,
|
||||
target: module,
|
||||
file,
|
||||
range: moduleValue.location,
|
||||
coverage,
|
||||
properties: {
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
chunk_keys: registration.chunk_keys,
|
||||
module_key: moduleValue.module_key,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
interface BundlerGraphRecord {
|
||||
readonly file: JavaScriptArtifactFile;
|
||||
readonly registration: JavaScriptBundlerRegistration;
|
||||
readonly chunk: ApplicationNode;
|
||||
readonly coverage: JavaScriptArtifactGraphCoverage;
|
||||
}
|
||||
|
||||
const addBundlerRuntimeEdges = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
record: BundlerGraphRecord,
|
||||
): void => {
|
||||
const { file, registration, chunk, coverage } = record;
|
||||
for (const moduleKey of registration.entry_module_keys) {
|
||||
const resolved = context.moduleNodes.get(
|
||||
moduleLookupKey(file.path, moduleKey),
|
||||
);
|
||||
const target =
|
||||
resolved ?? unresolvedBundlerModuleNode(context, record, moduleKey);
|
||||
context.accumulator.addEdge({
|
||||
source_node_id: chunk.node_id,
|
||||
target_node_id: target.node_id,
|
||||
relation: "loads",
|
||||
properties: {
|
||||
kind: "bundler-entry-module",
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
module_key: moduleKey,
|
||||
resolution_status: resolved === undefined ? "not-found" : "resolved",
|
||||
},
|
||||
evidence: staticInferenceEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: registration.location,
|
||||
operation: "resolve-bundler-entry-module",
|
||||
coverage,
|
||||
confidence: resolved === undefined ? "low" : "high",
|
||||
}),
|
||||
});
|
||||
}
|
||||
for (const chunkKey of registration.async_chunk_keys) {
|
||||
const lookup = chunkLookupKey(file.path, registration.runtime, chunkKey);
|
||||
const resolved = context.chunkNodes.get(lookup);
|
||||
const target =
|
||||
resolved ?? unresolvedBundlerChunkNode(context, record, chunkKey);
|
||||
context.accumulator.addEdge({
|
||||
source_node_id: chunk.node_id,
|
||||
target_node_id: target.node_id,
|
||||
relation: "imports",
|
||||
properties: {
|
||||
kind: "bundler-async-chunk",
|
||||
bundler: registration.bundler,
|
||||
runtime: registration.runtime,
|
||||
chunk_key: chunkKey,
|
||||
resolution_status: resolved === undefined ? "not-found" : "resolved",
|
||||
},
|
||||
evidence: staticInferenceEvidence({
|
||||
sha256: file.sha256,
|
||||
path: file.path,
|
||||
range: registration.location,
|
||||
operation: "resolve-bundler-async-chunk",
|
||||
coverage,
|
||||
confidence: resolved === undefined ? "low" : "high",
|
||||
}),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const unresolvedBundlerModuleNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
record: BundlerGraphRecord,
|
||||
moduleKey: string,
|
||||
): ApplicationNode =>
|
||||
context.accumulator.addNode({
|
||||
kind: "javascript-module",
|
||||
identity: artifactLocalIdentity(
|
||||
record.file.sha256,
|
||||
`${record.registration.runtime}:unresolved-entry-module`,
|
||||
moduleKey,
|
||||
),
|
||||
observations: [
|
||||
{
|
||||
label: moduleKey,
|
||||
properties: {
|
||||
semantic_role: "bundler-module-reference",
|
||||
bundler: record.registration.bundler,
|
||||
runtime: record.registration.runtime,
|
||||
module_key: moduleKey,
|
||||
resolution_status: "not-found",
|
||||
},
|
||||
evidence: staticInferenceEvidence({
|
||||
sha256: record.file.sha256,
|
||||
path: record.file.path,
|
||||
range: record.registration.location,
|
||||
operation: "retain-unresolved-bundler-entry-module",
|
||||
coverage: record.coverage,
|
||||
confidence: "low",
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const unresolvedBundlerChunkNode = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
record: BundlerGraphRecord,
|
||||
chunkKey: string,
|
||||
): ApplicationNode =>
|
||||
context.accumulator.addNode({
|
||||
kind: "javascript-chunk",
|
||||
identity: artifactLocalIdentity(
|
||||
record.file.sha256,
|
||||
`${record.registration.runtime}:unresolved-async-chunk`,
|
||||
chunkKey,
|
||||
),
|
||||
observations: [
|
||||
{
|
||||
label: chunkKey,
|
||||
properties: {
|
||||
semantic_role: "bundler-chunk-reference",
|
||||
bundler: record.registration.bundler,
|
||||
runtime: record.registration.runtime,
|
||||
chunk_key: chunkKey,
|
||||
resolution_status: "not-found",
|
||||
},
|
||||
evidence: staticInferenceEvidence({
|
||||
sha256: record.file.sha256,
|
||||
path: record.file.path,
|
||||
range: record.registration.location,
|
||||
operation: "retain-unresolved-bundler-async-chunk",
|
||||
coverage: record.coverage,
|
||||
confidence: "low",
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const createFileTarget = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
file: JavaScriptArtifactFile,
|
||||
|
||||
@@ -229,24 +229,20 @@ const inspectCall = (
|
||||
const name = calleeName(node.callee);
|
||||
const first = stringValue(node.arguments[0]);
|
||||
const module = moduleAtOffset(node.start, accumulator.modules);
|
||||
const moduleRequire =
|
||||
module?.requireName !== null && name === module?.requireName;
|
||||
if (
|
||||
name === "require" ||
|
||||
name.endsWith(".require") ||
|
||||
name.includes("__webpack_require__") ||
|
||||
moduleRequire
|
||||
)
|
||||
const moduleRequireName = module?.requireName ?? null;
|
||||
const requireCall = requireCallSpecifier(node, name, moduleRequireName);
|
||||
if (requireCall !== null)
|
||||
addReference(context, {
|
||||
node,
|
||||
kind: "require",
|
||||
specifier: argumentValue(node.arguments[0]),
|
||||
specifier: requireCall.specifier,
|
||||
});
|
||||
if (name.endsWith(".e") && name.includes("require"))
|
||||
const chunkLoad = chunkLoadSpecifier(node, name, moduleRequireName);
|
||||
if (chunkLoad !== null)
|
||||
addReference(context, {
|
||||
node,
|
||||
kind: "dynamic-import",
|
||||
specifier: prefixedArgument("chunk:", node.arguments[0]),
|
||||
specifier: chunkLoad.specifier,
|
||||
});
|
||||
if (name === "Worker" || name.endsWith(".Worker"))
|
||||
addReference(context, { node, kind: "worker", specifier: first });
|
||||
@@ -284,6 +280,36 @@ const inspectCall = (
|
||||
});
|
||||
};
|
||||
|
||||
const requireCallSpecifier = (
|
||||
node: t.CallExpression | t.NewExpression,
|
||||
name: string,
|
||||
moduleRequireName: string | null,
|
||||
): { readonly specifier: string | undefined } | null => {
|
||||
if (name === "require" || name.endsWith(".require"))
|
||||
return { specifier: argumentValue(node.arguments[0]) };
|
||||
if (name === "__webpack_require__" || name === moduleRequireName)
|
||||
return { specifier: argumentValue(node.arguments[0]) };
|
||||
if (
|
||||
name === "__webpack_require__.bind" ||
|
||||
(moduleRequireName !== null && name === `${moduleRequireName}.bind`)
|
||||
)
|
||||
return { specifier: argumentValue(node.arguments[1]) };
|
||||
return null;
|
||||
};
|
||||
|
||||
const chunkLoadSpecifier = (
|
||||
node: t.CallExpression | t.NewExpression,
|
||||
name: string,
|
||||
moduleRequireName: string | null,
|
||||
): { readonly specifier: string | undefined } | null => {
|
||||
if (
|
||||
name !== "__webpack_require__.e" &&
|
||||
(moduleRequireName === null || name !== `${moduleRequireName}.e`)
|
||||
)
|
||||
return null;
|
||||
return { specifier: prefixedArgument("chunk:", node.arguments[0]) };
|
||||
};
|
||||
|
||||
const inspectBundlerRegistration = (
|
||||
source: string,
|
||||
call: t.CallExpression,
|
||||
@@ -295,32 +321,160 @@ const inspectBundlerRegistration = (
|
||||
if (runtime === undefined || !t.isArrayExpression(entry)) return;
|
||||
const chunkIds = entry.elements[0];
|
||||
const table = entry.elements[1];
|
||||
const runtimeValue = runtimeMetadata(entry.elements[2], limits.maxFindings);
|
||||
if (!t.isArrayExpression(chunkIds) || !t.isObjectExpression(table)) return;
|
||||
const recovered = recoverBundlerModules(source, table, accumulator, limits);
|
||||
const chunkKeys = staticArrayValues(
|
||||
chunkIds,
|
||||
Math.min(256, limits.maxFindings),
|
||||
);
|
||||
if (chunkKeys.omitted > 0) {
|
||||
accumulator.droppedFindings += chunkKeys.omitted;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
accumulator.unknownFindings += chunkKeys.unknown;
|
||||
if (
|
||||
runtimeValue.omittedEntryModuleKeys > 0 ||
|
||||
runtimeValue.omittedAsyncChunkKeys > 0 ||
|
||||
recovered.omittedAsyncChunkKeys > 0
|
||||
) {
|
||||
accumulator.droppedFindings +=
|
||||
runtimeValue.omittedEntryModuleKeys +
|
||||
runtimeValue.omittedAsyncChunkKeys +
|
||||
recovered.omittedAsyncChunkKeys;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
accumulator.unknownFindings +=
|
||||
runtimeValue.unknownEntryModuleKeys +
|
||||
runtimeValue.unknownAsyncChunkKeys +
|
||||
recovered.unknownAsyncChunkKeys;
|
||||
const asyncChunkKeys = boundedUniqueValues(
|
||||
[...runtimeValue.asyncChunkKeys, ...recovered.asyncChunkKeys],
|
||||
Math.min(256, limits.maxFindings),
|
||||
);
|
||||
if (asyncChunkKeys.omitted > 0) {
|
||||
accumulator.droppedFindings += asyncChunkKeys.omitted;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
const registration = {
|
||||
bundler: runtime.toLowerCase().includes("rspack")
|
||||
? ("rspack" as const)
|
||||
: ("webpack" as const),
|
||||
runtime,
|
||||
chunk_keys: chunkKeys.values,
|
||||
omitted_chunk_keys: chunkKeys.omitted,
|
||||
unknown_chunk_keys: chunkKeys.unknown,
|
||||
runtime_require_name: runtimeValue.requireName,
|
||||
runtime_module_cache_status: bundlerModuleCacheStatus(source),
|
||||
entry_module_keys: runtimeValue.entryModuleKeys,
|
||||
omitted_entry_module_keys: runtimeValue.omittedEntryModuleKeys,
|
||||
unknown_entry_module_keys: runtimeValue.unknownEntryModuleKeys,
|
||||
async_chunk_keys: asyncChunkKeys.values,
|
||||
omitted_async_chunk_keys:
|
||||
runtimeValue.omittedAsyncChunkKeys +
|
||||
recovered.omittedAsyncChunkKeys +
|
||||
asyncChunkKeys.omitted,
|
||||
unknown_async_chunk_keys:
|
||||
runtimeValue.unknownAsyncChunkKeys + recovered.unknownAsyncChunkKeys,
|
||||
modules: recovered.modules.sort((left, right) =>
|
||||
compareCodePoints(left.module_key, right.module_key),
|
||||
),
|
||||
location: range(call),
|
||||
};
|
||||
addBoundedFinding(
|
||||
accumulator,
|
||||
`registration\0${registrationKey(registration)}`,
|
||||
limits.maxFindings,
|
||||
() => accumulator.registrations.push(registration),
|
||||
);
|
||||
};
|
||||
|
||||
interface RecoveredBundlerModules {
|
||||
readonly modules: JavaScriptBundlerModule[];
|
||||
readonly asyncChunkKeys: readonly string[];
|
||||
readonly omittedAsyncChunkKeys: number;
|
||||
readonly unknownAsyncChunkKeys: number;
|
||||
}
|
||||
|
||||
const recoverBundlerModules = (
|
||||
source: string,
|
||||
table: t.ObjectExpression,
|
||||
accumulator: AnalysisAccumulator,
|
||||
limits: JavaScriptStaticAnalysisLimits,
|
||||
): RecoveredBundlerModules => {
|
||||
const modules: JavaScriptBundlerModule[] = [];
|
||||
const asyncChunkKeys: string[] = [];
|
||||
let omittedAsyncChunkKeys = 0;
|
||||
let unknownAsyncChunkKeys = 0;
|
||||
for (const property of table.properties) {
|
||||
const factory = moduleFactory(property);
|
||||
if (factory === undefined) continue;
|
||||
if (accumulator.moduleCount >= limits.maxModules) {
|
||||
accumulator.droppedFindings += 1;
|
||||
continue;
|
||||
}
|
||||
accumulator.moduleCount += 1;
|
||||
const key = modulePropertyName(property);
|
||||
if (key.startsWith("[computed@") || key.startsWith("[unknown@"))
|
||||
accumulator.unknownFindings += 1;
|
||||
const exactSource = sourceSlice(source, factory);
|
||||
const fingerprint = fingerprintJavaScriptAst(
|
||||
factory,
|
||||
Math.min(limits.maxAstNodes, 100_000),
|
||||
const recovered = recoverBundlerModule(
|
||||
source,
|
||||
property,
|
||||
accumulator,
|
||||
limits,
|
||||
);
|
||||
const exportsValue = collectJavaScriptExports(factory, 256);
|
||||
if (fingerprint.truncated || exportsValue.truncated) {
|
||||
accumulator.droppedFindings += 1;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
modules.push({
|
||||
if (recovered === null) continue;
|
||||
modules.push(recovered.module);
|
||||
asyncChunkKeys.push(...recovered.asyncChunkKeys);
|
||||
omittedAsyncChunkKeys += recovered.omittedAsyncChunkKeys;
|
||||
unknownAsyncChunkKeys += recovered.unknownAsyncChunkKeys;
|
||||
}
|
||||
return {
|
||||
modules,
|
||||
asyncChunkKeys,
|
||||
omittedAsyncChunkKeys,
|
||||
unknownAsyncChunkKeys,
|
||||
};
|
||||
};
|
||||
|
||||
const recoverBundlerModule = (
|
||||
source: string,
|
||||
property: t.ObjectMethod | t.ObjectProperty | t.SpreadElement,
|
||||
accumulator: AnalysisAccumulator,
|
||||
limits: JavaScriptStaticAnalysisLimits,
|
||||
): {
|
||||
readonly module: JavaScriptBundlerModule;
|
||||
readonly asyncChunkKeys: readonly string[];
|
||||
readonly omittedAsyncChunkKeys: number;
|
||||
readonly unknownAsyncChunkKeys: number;
|
||||
} | null => {
|
||||
const factory = moduleFactory(property);
|
||||
if (factory === undefined) return null;
|
||||
if (accumulator.moduleCount >= limits.maxModules) {
|
||||
accumulator.droppedFindings += 1;
|
||||
return null;
|
||||
}
|
||||
accumulator.moduleCount += 1;
|
||||
const key = modulePropertyName(property);
|
||||
if (key.startsWith("[computed@") || key.startsWith("[unknown@"))
|
||||
accumulator.unknownFindings += 1;
|
||||
const fingerprint = fingerprintJavaScriptAst(
|
||||
factory,
|
||||
Math.min(limits.maxAstNodes, 100_000),
|
||||
);
|
||||
const exportsValue = collectJavaScriptExports(factory, 256);
|
||||
const requireName = factoryRequireName(factory);
|
||||
const asyncChunks = collectBundlerAsyncChunkKeys(
|
||||
factory,
|
||||
requireName,
|
||||
limits.maxFindings,
|
||||
);
|
||||
if (fingerprint.truncated || exportsValue.truncated) {
|
||||
accumulator.droppedFindings += 1;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
if (typeof factory.start === "number" && typeof factory.end === "number")
|
||||
accumulator.modules.push({
|
||||
start: factory.start,
|
||||
end: factory.end,
|
||||
key,
|
||||
requireName,
|
||||
});
|
||||
return {
|
||||
module: {
|
||||
module_key: key,
|
||||
source_sha256: sha256Text(exactSource),
|
||||
factory_require_name: requireName,
|
||||
source_sha256: sha256Text(sourceSlice(source, factory)),
|
||||
structural_fingerprint_sha256: fingerprint.truncated
|
||||
? null
|
||||
: fingerprint.sha256,
|
||||
@@ -333,45 +487,141 @@ const inspectBundlerRegistration = (
|
||||
exports: exportsValue.values,
|
||||
exports_truncated: exportsValue.truncated,
|
||||
location: range(factory),
|
||||
});
|
||||
if (typeof factory.start === "number" && typeof factory.end === "number")
|
||||
accumulator.modules.push({
|
||||
start: factory.start,
|
||||
end: factory.end,
|
||||
key,
|
||||
requireName: factoryRequireName(factory),
|
||||
});
|
||||
}
|
||||
const chunkKeys = staticArrayValues(
|
||||
chunkIds,
|
||||
Math.min(256, limits.maxFindings),
|
||||
);
|
||||
if (chunkKeys.omitted > 0) {
|
||||
accumulator.droppedFindings += chunkKeys.omitted;
|
||||
accumulator.structuralTruncation = true;
|
||||
}
|
||||
accumulator.unknownFindings += chunkKeys.unknown;
|
||||
const registration = {
|
||||
bundler: runtime.toLowerCase().includes("rspack")
|
||||
? ("rspack" as const)
|
||||
: ("webpack" as const),
|
||||
runtime,
|
||||
chunk_keys: chunkKeys.values,
|
||||
omitted_chunk_keys: chunkKeys.omitted,
|
||||
unknown_chunk_keys: chunkKeys.unknown,
|
||||
modules: modules.sort((left, right) =>
|
||||
compareCodePoints(left.module_key, right.module_key),
|
||||
),
|
||||
location: range(call),
|
||||
},
|
||||
asyncChunkKeys: asyncChunks.values,
|
||||
omittedAsyncChunkKeys: asyncChunks.omitted,
|
||||
unknownAsyncChunkKeys: asyncChunks.unknown,
|
||||
};
|
||||
addBoundedFinding(
|
||||
accumulator,
|
||||
`registration\0${registrationKey(registration)}`,
|
||||
limits.maxFindings,
|
||||
() => accumulator.registrations.push(registration),
|
||||
);
|
||||
};
|
||||
|
||||
interface RuntimeMetadata {
|
||||
readonly requireName: string | null;
|
||||
readonly entryModuleKeys: readonly string[];
|
||||
readonly omittedEntryModuleKeys: number;
|
||||
readonly unknownEntryModuleKeys: number;
|
||||
readonly asyncChunkKeys: readonly string[];
|
||||
readonly omittedAsyncChunkKeys: number;
|
||||
readonly unknownAsyncChunkKeys: number;
|
||||
}
|
||||
|
||||
interface BoundedValues {
|
||||
readonly values: readonly string[];
|
||||
readonly omitted: number;
|
||||
readonly unknown: number;
|
||||
}
|
||||
|
||||
type BundlerFunction =
|
||||
| t.FunctionExpression
|
||||
| t.ArrowFunctionExpression
|
||||
| t.ObjectMethod;
|
||||
|
||||
const runtimeMetadata = (
|
||||
node: t.Node | null | undefined,
|
||||
maximum: number,
|
||||
): RuntimeMetadata => {
|
||||
if (!isBundlerFunction(node))
|
||||
return {
|
||||
requireName: null,
|
||||
entryModuleKeys: [],
|
||||
omittedEntryModuleKeys: 0,
|
||||
unknownEntryModuleKeys: node === null || node === undefined ? 0 : 1,
|
||||
asyncChunkKeys: [],
|
||||
omittedAsyncChunkKeys: 0,
|
||||
unknownAsyncChunkKeys: 0,
|
||||
};
|
||||
const parameter = node.params[0];
|
||||
const requireName = t.isIdentifier(parameter)
|
||||
? parameter.name.slice(0, 4_096)
|
||||
: null;
|
||||
if (requireName === null)
|
||||
return {
|
||||
requireName,
|
||||
entryModuleKeys: [],
|
||||
omittedEntryModuleKeys: 0,
|
||||
unknownEntryModuleKeys: 1,
|
||||
asyncChunkKeys: [],
|
||||
omittedAsyncChunkKeys: 0,
|
||||
unknownAsyncChunkKeys: 0,
|
||||
};
|
||||
const entries = collectBundlerEntryModuleKeys(node, requireName, maximum);
|
||||
const asyncChunks = collectBundlerAsyncChunkKeys(node, requireName, maximum);
|
||||
return {
|
||||
requireName,
|
||||
entryModuleKeys: entries.values,
|
||||
omittedEntryModuleKeys: entries.omitted,
|
||||
unknownEntryModuleKeys: entries.unknown,
|
||||
asyncChunkKeys: asyncChunks.values,
|
||||
omittedAsyncChunkKeys: asyncChunks.omitted,
|
||||
unknownAsyncChunkKeys: asyncChunks.unknown,
|
||||
};
|
||||
};
|
||||
|
||||
const collectBundlerEntryModuleKeys = (
|
||||
factory: BundlerFunction,
|
||||
requireName: string,
|
||||
maximum: number,
|
||||
): BoundedValues =>
|
||||
collectBundlerCallArgumentValues(factory, requireName, maximum);
|
||||
|
||||
const collectBundlerAsyncChunkKeys = (
|
||||
factory: BundlerFunction,
|
||||
requireName: string | null,
|
||||
maximum: number,
|
||||
): BoundedValues =>
|
||||
requireName === null
|
||||
? { values: [], omitted: 0, unknown: 0 }
|
||||
: collectBundlerCallArgumentValues(factory, `${requireName}.e`, maximum);
|
||||
|
||||
const collectBundlerCallArgumentValues = (
|
||||
factory: BundlerFunction,
|
||||
callee: string,
|
||||
maximum: number,
|
||||
): BoundedValues => {
|
||||
const values: string[] = [];
|
||||
let unknown = 0;
|
||||
t.traverseFast(factory, (node) => {
|
||||
if (!t.isCallExpression(node) && !t.isNewExpression(node)) return undefined;
|
||||
if (calleeName(node.callee) !== callee) return undefined;
|
||||
const value = argumentValue(node.arguments[0]);
|
||||
if (value === undefined) unknown += 1;
|
||||
else values.push(value);
|
||||
return undefined;
|
||||
});
|
||||
return boundedUniqueValues(values, Math.min(256, maximum), unknown);
|
||||
};
|
||||
|
||||
const isBundlerFunction = (
|
||||
node: t.Node | null | undefined,
|
||||
): node is BundlerFunction =>
|
||||
t.isFunctionExpression(node) ||
|
||||
t.isArrowFunctionExpression(node) ||
|
||||
t.isObjectMethod(node);
|
||||
|
||||
const boundedUniqueValues = (
|
||||
values: readonly string[],
|
||||
maximum: number,
|
||||
unknown = 0,
|
||||
): BoundedValues => {
|
||||
const bounded = [
|
||||
...new Set(values.map((value) => value.slice(0, 4_096))),
|
||||
].sort(compareCodePoints);
|
||||
const retained = bounded.slice(0, Math.max(0, maximum));
|
||||
return {
|
||||
values: retained,
|
||||
omitted: Math.max(0, bounded.length - retained.length),
|
||||
unknown,
|
||||
};
|
||||
};
|
||||
|
||||
const bundlerModuleCacheStatus = (
|
||||
source: string,
|
||||
): "observed" | "not-observed" =>
|
||||
/\b(?:__webpack_module_cache__|__rspack_module_cache__|installedModules)\b/u.test(
|
||||
source,
|
||||
)
|
||||
? "observed"
|
||||
: "not-observed";
|
||||
|
||||
const inspectEndpointCall = (
|
||||
node: t.CallExpression | t.NewExpression,
|
||||
name: string,
|
||||
|
||||
@@ -57,6 +57,7 @@ export interface JavaScriptStaticStorage {
|
||||
/** One exact module factory recovered from a Webpack/Rspack registration. */
|
||||
export interface JavaScriptBundlerModule {
|
||||
readonly module_key: string;
|
||||
readonly factory_require_name: string | null;
|
||||
readonly source_sha256: string;
|
||||
readonly structural_fingerprint_sha256: string | null;
|
||||
readonly structural_fingerprint_algorithm: "babel-ast-v1" | null;
|
||||
@@ -73,6 +74,14 @@ export interface JavaScriptBundlerRegistration {
|
||||
readonly chunk_keys: readonly string[];
|
||||
readonly omitted_chunk_keys: number;
|
||||
readonly unknown_chunk_keys: number;
|
||||
readonly runtime_require_name: string | null;
|
||||
readonly runtime_module_cache_status: "observed" | "not-observed";
|
||||
readonly entry_module_keys: readonly string[];
|
||||
readonly omitted_entry_module_keys: number;
|
||||
readonly unknown_entry_module_keys: number;
|
||||
readonly async_chunk_keys: readonly string[];
|
||||
readonly omitted_async_chunk_keys: number;
|
||||
readonly unknown_async_chunk_keys: number;
|
||||
readonly modules: readonly JavaScriptBundlerModule[];
|
||||
readonly location: JavaScriptSourceRange;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user