fix(browser): ignore duplicate frame navigations and read block-form source map directives (#546)

This commit is contained in:
morluto
2026-10-05 17:25:16 +08:00
committed by GitHub
parent 84e4361c09
commit 828395d510
6 changed files with 149 additions and 4 deletions
+5 -2
View File
@@ -273,11 +273,14 @@ export const addSourceMapDirectives = (
source: string,
accumulator: AnalysisAccumulator,
): void => {
// Minifiers emit either `//# sourceMappingURL=` or the block form
// `/*# sourceMappingURL=app.js.map */`. Matching only the line form silently
// dropped the block form, so a real footer produced no mapping at all.
for (const match of source.matchAll(
/\/\/[#@]\s*sourceMappingURL\s*=\s*([^\s]+)/gu,
/(?:\/\/|\/\*)[#@]\s*sourceMappingURL\s*=\s*([^\s*]+)/gu,
)) {
const declared = match[1];
if (declared === undefined) continue;
if (declared === undefined || declared.length === 0) continue;
const start = match.index;
const location = rangeForOffsets(source, start, start + match[0].length);
addFindingOnce(accumulator, `source-map\0${declared}`, () =>
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
const sourceMapUrls = (source: string): readonly string[] =>
analyzeJavaScriptStaticSource(source).source_map_urls.map(
({ declared_url: declaredUrl }) => declaredUrl,
);
describe("source map directives", () => {
it.each([
["line comment", "//# sourceMappingURL=app.js.map", "app.js.map"],
["legacy line comment", "//@ sourceMappingURL=app.js.map", "app.js.map"],
// Minifiers commonly emit the block form; it previously matched nothing.
["block comment", "/*# sourceMappingURL=app.js.map */", "app.js.map"],
[
"block comment after code",
"code();\n/*# sourceMappingURL=vendor.abc.map */",
"vendor.abc.map",
],
])("reads the %s form", (_label, source, expected) => {
expect(sourceMapUrls(source)).toEqual([expected]);
});
it("keeps the comment terminator out of the declared url", () => {
expect(sourceMapUrls("/*# sourceMappingURL=app.js.map */")).toEqual([
"app.js.map",
]);
});
it("retains every directive in a source", () => {
expect(
sourceMapUrls(
"//# sourceMappingURL=first.map\n/*# sourceMappingURL=second.map */",
),
).toEqual(["first.map", "second.map"]);
});
it("reports nothing when no directive is present", () => {
expect(sourceMapUrls("const value = 1;")).toEqual([]);
});
});