From 224856bf62c3f232a015e9823943303fc065c0b6 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Wed, 18 Nov 2026 23:07:25 +0800 Subject: [PATCH] fix(source-maps): reject empty comma-delimited mappings (#11970) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> --- src/domain/sourceMapFormat.ts | 14 ++++++++++--- .../sourceMaps/TraceSourceMap.test.ts | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/domain/sourceMapFormat.ts b/src/domain/sourceMapFormat.ts index e8146ef131..88b35724fc 100644 --- a/src/domain/sourceMapFormat.ts +++ b/src/domain/sourceMapFormat.ts @@ -268,9 +268,15 @@ const inspectMappingLayout = ( let segments = 0; let components = 0; let inSegment = false; + let afterComma = false; let continued = false; let digits = 0; - const finish = (): void => { + const finish = (requiresSegment: boolean): void => { + if (requiresSegment && !inSegment) + throw malformedInput( + SOURCE_MAP_FORMAT, + "Source-map mappings contain an empty mapping segment.", + ); if (continued) throw malformedInput( SOURCE_MAP_FORMAT, @@ -286,7 +292,9 @@ const inspectMappingLayout = ( for (const character of mappings) { if ((index++ & 0x3fff) === 0) check?.(); if (character === "," || character === ";") { - finish(); + const isComma = character === ","; + finish(isComma || afterComma); + afterComma = isComma; inSegment = false; components = 0; if (character === ";") lines += 1; @@ -317,7 +325,7 @@ const inspectMappingLayout = ( : limits.decodedRows, ); } - finish(); + finish(afterComma); return { lines, segments }; }; diff --git a/src/javascript/sourceMaps/TraceSourceMap.test.ts b/src/javascript/sourceMaps/TraceSourceMap.test.ts index 75d9976740..1fa63c9199 100644 --- a/src/javascript/sourceMaps/TraceSourceMap.test.ts +++ b/src/javascript/sourceMaps/TraceSourceMap.test.ts @@ -330,6 +330,26 @@ describe("source-map layout admission", () => { expect(() => trace({ ...leaf, mappings })).toThrow(); }, ); + it.each([",", ",AAAA", "AAAA,,A", "AAAA,", "AAAA,;A"])( + "rejects empty mapping segments around commas: %s", + (mappings) => { + expect(() => trace({ ...leaf, mappings })).toThrow( + /empty mapping segment/u, + ); + }, + ); + it("retains empty generated lines and generated-only mappings", () => { + const report = traceSourceMap( + JSON.stringify({ ...leaf, mappings: ";;A,CAAA;" }), + url, + { line: 3, column: 0 }, + ); + expect(report.lookup.matched_generated_position).toEqual({ + line: 3, + column: 0, + }); + expect(report.matches).toEqual([{ state: "generated-only" }]); + }); it("validates regular-map source indexes after reusing its decoded rows", () => { expect(() => trace({ ...leaf, mappings: "ACAA" })).toThrow(/index/u); });