fix(javascript): share source-map admission and path resolution (#12161)

* fix(source-map): share bounded structural inspection with local analysis

Refs #12135

* fix(javascript): retain relative URL traversal and decline unusable map paths

* fix(javascript): retain source content beyond coordinate admission
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 03:32:08 +00:00
1 parent 261c6d06d1
commit 27e04ea2ec
15 files changed
+794 -531

No files matched your search

@@ -5,14 +5,11 @@ import { setImmediate } from "node:timers/promises";
import type { JavaScriptPackageType } from "../../domain/javascript/javascriptSourceParser.js";
import { resolveJavaScriptSourceMapReferences } from "../../domain/javascript/javascriptSourceMapPaths.js";
import { hasValidSourceMapContents } from "../../domain/sourceMapContents.js";
import { resolveJavaScriptSourceMapReference } from "../../domain/javascript/javascriptSourceMapPaths.js";
import {
inspectSourceMapEnvelope,
sourceMapJsonText,
sourceMapMappingsSchema,
} from "../../domain/sourceMapEnvelope.js";
import type { SourceMapLeaf } from "../../domain/sourceMapEnvelope.js";
inspectLocalSourceMapDeclarations,
type LocalSourceMapDeclaration,
} from "../../domain/sourceMapFormat.js";
import type { JavaScriptStaticAnalysis } from "../../domain/javascript/javascriptStaticAnalysisTypes.js";
import type {
JavaScriptArtifactFile,
@@ -120,7 +117,7 @@ export const analyzeAndAdoptJavaScriptArtifactFiles = async (
analysisStarted = true;
if (file.kind !== "javascript" || !file.text.included) {
assertJavaScriptAnalysisActive(signal);
analyzeArtifactMetadata(file, state);
analyzeArtifactMetadata(file, state, signal);
assertJavaScriptAnalysisActive(signal);
continue;
}
@@ -392,6 +389,7 @@ const finalizeArtifactAnalysis = (
const analyzeArtifactMetadata = (
file: JavaScriptArtifactFile,
state: MutableArtifactAnalysis,
signal: AbortSignal | undefined,
): void => {
addStructuredObservations(file, state);
if (file.kind === "html" && file.text.included) {
@@ -401,7 +399,8 @@ const analyzeArtifactMetadata = (
if (html.inlineScripts !== null)
state.htmlInlineScripts.push(html.inlineScripts);
}
if (file.kind === "source-map") state.sourceMaps.push(parseSourceMap(file));
if (file.kind === "source-map")
state.sourceMaps.push(parseSourceMap(file, signal));
state.files.push({ file, javascript: null, semantic: null });
};
@@ -601,118 +600,33 @@ const parseHtmlScripts = (
const parseSourceMap = (
file: JavaScriptArtifactFile,
signal: AbortSignal | undefined,
): JavaScriptSourceMapObservation => {
if (!file.text.included) return unavailableSourceMap(file);
let value: unknown;
try {
value = JSON.parse(sourceMapJsonText(file.text.value));
} catch (cause: unknown) {
void cause;
return invalidSourceMap(file, "Source map is not valid JSON.");
}
const envelope = inspectSourceMapEnvelope(value);
if (envelope.status === "invalid")
return invalidSourceMap(file, envelope.reason);
const observation = collectSourceMapOriginals(file, envelope.leaves);
if (envelope.status === "included") return observation;
const envelopeLimitations = [
...envelope.invalid_sections.map(
const observed = inspectLocalSourceMapDeclarations(file.text.value, {
check: () => assertJavaScriptAnalysisActive(signal),
});
const sources = collectSourceMapOriginals(file, observed.declarations);
const limitations = [
...observed.limitations,
...observed.invalid_sections.map(
({ section_path, reason }) =>
`Indexed source-map section at [${section_path.join(",")}]: ${reason}`,
),
...envelope.external_sections.map(
...observed.external_sections.map(
({ url }) => `External source-map section URL is not fetched: ${url}`,
),
];
const limitation = [
...(observation.status === "invalid" ? [observation.limitation] : []),
...envelopeLimitations,
].join("; ");
return {
...observation,
status: "invalid",
limitation,
...(envelope.external_sections.length === 0
? {}
: { external_sections: envelope.external_sections }),
...(envelope.invalid_sections.length === 0
? {}
: { invalid_sections: envelope.invalid_sections }),
};
};
const collectSourceMapOriginals = (
file: JavaScriptArtifactFile,
leaves: readonly SourceMapLeaf[],
): JavaScriptSourceMapObservation => {
const sources: JavaScriptSourceMapOriginal[] = [];
const limitations: string[] = [];
let declarationIndex = 0;
for (const { map, section_path } of leaves) {
const location =
section_path.length === 0
? ""
: ` in section [${section_path.join(",")}]`;
const names = map.sources;
if (!Array.isArray(names)) {
limitations.push(`Source map has no sources array${location}.`);
continue;
}
const contents = map.sourcesContent;
if (!hasValidSourceMapContents(contents))
limitations.push(
`Source map sourcesContent must be an array of strings or null entries${location}.`,
);
if (map.sourceRoot != null && typeof map.sourceRoot !== "string") {
limitations.push(
`Source map sourceRoot must be a string or null${location}.`,
);
continue;
}
const root = typeof map.sourceRoot === "string" ? map.sourceRoot : null;
const sourceDeclarations: {
readonly index: number;
readonly name: string | null;
}[] = [];
for (const [index, raw] of names.entries()) {
if (raw !== null && typeof raw !== "string") {
limitations.push(
`Source map contains a non-string source name at index ${String(index)}${location}.`,
);
continue;
}
sourceDeclarations.push({ index, name: raw });
}
const references = resolveJavaScriptSourceMapReferences(
sourceDeclarations.map(({ name }) => name),
root,
file.path,
);
for (const [index, reference] of references.entries()) {
const declaration = sourceDeclarations[index];
if (declaration === undefined) continue;
const rawContent = Array.isArray(contents)
? contents[declaration.index]
: undefined;
const content = typeof rawContent === "string" ? rawContent : null;
sources.push({
source_index: declaration.index,
declaration_index: declarationIndex++,
reference,
content,
content_sha256: content === null ? null : sha256Hex(content),
section_path,
});
}
if (!sourceMapMappingsSchema.safeParse(map.mappings).success)
limitations.push(`Source map has no mappings string${location}.`);
}
if (limitations.length > 0)
return invalidSourceMap(
file,
[...new Set(limitations)].join("; "),
sources,
);
return {
...invalidSourceMap(file, [...new Set(limitations)].join("; "), sources),
...(observed.external_sections.length === 0
? {}
: { external_sections: observed.external_sections }),
...(observed.invalid_sections.length === 0
? {}
: { invalid_sections: observed.invalid_sections }),
};
return {
path: file.path,
sha256: file.sha256,
@@ -722,6 +636,26 @@ const collectSourceMapOriginals = (
};
};
const collectSourceMapOriginals = (
file: JavaScriptArtifactFile,
declarations: readonly LocalSourceMapDeclaration[],
): JavaScriptSourceMapOriginal[] =>
declarations.map(
(
{ sectionPath, sourceIndex, source, root, content },
declaration_index,
) => {
return {
source_index: sourceIndex,
declaration_index,
reference: resolveJavaScriptSourceMapReference(source, root, file.path),
content,
content_sha256: content === null ? null : sha256Hex(content),
section_path: sectionPath,
};
},
);
/** Name the HTML file and lines whose inline JavaScript was not analyzed. */
export const htmlInlineScriptLimitation = ({
path,