Skip to content

Commit 8a0b061

Browse files
committed
fix(@angular/build): handle regex literals in source-map comment scanner
Previously, `removeSourceMappingURL` did not track regular expression literals in its state machine. A quote character (`"`, `'`, or `` ` ``) inside a regex literal (such as `/"/`) was misinterpreted as the start of a string literal, inverting string boundaries for the remainder of the file and corrupting valid JavaScript when a subsequent string contained `//# sourceMappingURL=`. Closes #34590
1 parent 47bc9c3 commit 8a0b061

2 files changed

Lines changed: 190 additions & 3 deletions

File tree

‎packages/angular/build/src/utils/source-map.ts‎

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export function findTrailingSourceMapComment(
5959

6060
/**
6161
* Removes `//# sourceMappingURL=` comments safely from the given JavaScript code,
62-
* ignoring any occurrences that are inside string literals, template literals, or block comments.
62+
* ignoring any occurrences that are inside string literals, template literals, regex literals,
63+
* or block comments.
6364
*
6465
* For raw Uint8Array / Buffer inputs, it optimizes performance by inspecting trailing byte sequences
6566
* to slice the buffer directly without full string decoding.
@@ -93,6 +94,59 @@ export function removeSourceMappingURL(code: string | Uint8Array): string | Uint
9394
return stripped === text ? code : Buffer.from(stripped, 'utf-8');
9495
}
9596

97+
const REGEX_PRECEDING_KEYWORDS = new Set([
98+
'await',
99+
'case',
100+
'delete',
101+
'do',
102+
'else',
103+
'in',
104+
'instanceof',
105+
'new',
106+
'of',
107+
'return',
108+
'throw',
109+
'typeof',
110+
'void',
111+
'yield',
112+
]);
113+
114+
const REGEX_PRECEDING_PUNCTUATION = new Set([
115+
'(',
116+
',',
117+
'=',
118+
':',
119+
'[',
120+
'!',
121+
'&',
122+
'|',
123+
'?',
124+
'{',
125+
'}',
126+
';',
127+
'+',
128+
'-',
129+
'*',
130+
'%',
131+
'~',
132+
'^',
133+
'<',
134+
'>',
135+
'/',
136+
]);
137+
138+
function isIdentifierChar(char: string): boolean {
139+
const code = char.charCodeAt(0);
140+
141+
return (
142+
(code >= 97 && code <= 122) || // a-z
143+
(code >= 65 && code <= 90) || // A-Z
144+
(code >= 48 && code <= 57) || // 0-9
145+
code === 95 || // _
146+
code === 36 // $
147+
);
148+
}
149+
96150
function removeSourceMappingURLFromString(code: string): string {
97151
if (!code.includes(SOURCEMAP_COMMENT_PREFIX)) {
98152
return code;
@@ -106,8 +160,15 @@ function removeSourceMappingURLFromString(code: string): string {
106160
// Stack to track template literal state and curly brace depth for nested interpolations.
107161
const stack: { type: 'template'; braceDepth: number }[] = [];
108162
let currentState:
109-
'normal' | 'string_double' | 'string_single' | 'template' | 'comment_block' | 'comment_line' =
110-
'normal';
163+
| 'normal'
164+
| 'string_double'
165+
| 'string_single'
166+
| 'template'
167+
| 'regex'
168+
| 'comment_block'
169+
| 'comment_line' = 'normal';
170+
let regexAllowed = true;
171+
let inRegexCharClass = false;
111172

112173
while (i < len) {
113174
const char = code[i];
@@ -156,17 +217,20 @@ function removeSourceMappingURLFromString(code: string): string {
156217
}
157218
if (char === '"') {
158219
currentState = 'string_double';
220+
regexAllowed = false;
159221
i++;
160222
continue;
161223
}
162224
if (char === "'") {
163225
currentState = 'string_single';
226+
regexAllowed = false;
164227
i++;
165228
continue;
166229
}
167230
if (char === '`') {
168231
currentState = 'template';
169232
stack.push({ type: 'template', braceDepth: 0 });
233+
regexAllowed = false;
170234
i++;
171235
continue;
172236
}
@@ -175,6 +239,7 @@ function removeSourceMappingURLFromString(code: string): string {
175239
if (top) {
176240
top.braceDepth++;
177241
}
242+
regexAllowed = true;
178243
i++;
179244
continue;
180245
}
@@ -186,14 +251,53 @@ function removeSourceMappingURLFromString(code: string): string {
186251
// Exiting a template literal interpolation ${ ... }
187252
stack.pop();
188253
currentState = 'template';
254+
regexAllowed = false;
189255
i++;
190256
continue;
191257
}
192258
}
259+
regexAllowed = true;
260+
i++;
261+
continue;
262+
}
263+
if (char === '/') {
264+
if (regexAllowed) {
265+
currentState = 'regex';
266+
inRegexCharClass = false;
267+
regexAllowed = false;
268+
} else {
269+
regexAllowed = true;
270+
}
271+
i++;
272+
continue;
273+
}
274+
if (char === ' ' || char === '\t' || char === '\n' || char === '\r') {
275+
i++;
276+
continue;
277+
}
278+
if (isIdentifierChar(char)) {
279+
const start = i;
193280
i++;
281+
while (i < len && isIdentifierChar(code[i])) {
282+
i++;
283+
}
284+
const word = code.slice(start, i);
285+
let prevIdx = start - 1;
286+
while (
287+
prevIdx >= 0 &&
288+
(code[prevIdx] === ' ' ||
289+
code[prevIdx] === '\t' ||
290+
code[prevIdx] === '\n' ||
291+
code[prevIdx] === '\r')
292+
) {
293+
prevIdx--;
294+
}
295+
regexAllowed =
296+
(prevIdx < 0 || code[prevIdx] !== '.') && REGEX_PRECEDING_KEYWORDS.has(word);
194297
continue;
195298
}
196299

300+
regexAllowed = REGEX_PRECEDING_PUNCTUATION.has(char);
197301
i++;
198302
} else if (currentState === 'string_double') {
199303
if (char === '\\') {
@@ -202,6 +306,7 @@ function removeSourceMappingURLFromString(code: string): string {
202306
}
203307
if (char === '"') {
204308
currentState = 'normal';
309+
regexAllowed = false;
205310
}
206311
i++;
207312
} else if (currentState === 'string_single') {
@@ -211,6 +316,7 @@ function removeSourceMappingURLFromString(code: string): string {
211316
}
212317
if (char === "'") {
213318
currentState = 'normal';
319+
regexAllowed = false;
214320
}
215321
i++;
216322
} else if (currentState === 'template') {
@@ -222,12 +328,40 @@ function removeSourceMappingURLFromString(code: string): string {
222328
// Entering template literal interpolation context
223329
currentState = 'normal';
224330
stack.push({ type: 'template', braceDepth: 0 });
331+
regexAllowed = true;
225332
i += 2;
226333
continue;
227334
}
228335
if (char === '`') {
229336
stack.pop();
230337
currentState = 'normal';
338+
regexAllowed = false;
339+
}
340+
i++;
341+
} else if (currentState === 'regex') {
342+
if (char === '\\') {
343+
if (nextChar === '\n' || nextChar === '\r') {
344+
currentState = 'normal';
345+
inRegexCharClass = false;
346+
regexAllowed = true;
347+
}
348+
i += 2;
349+
continue;
350+
}
351+
if (char === '\n' || char === '\r') {
352+
currentState = 'normal';
353+
inRegexCharClass = false;
354+
regexAllowed = true;
355+
i++;
356+
continue;
357+
}
358+
if (char === '[') {
359+
inRegexCharClass = true;
360+
} else if (char === ']' && inRegexCharClass) {
361+
inRegexCharClass = false;
362+
} else if (char === '/' && !inRegexCharClass) {
363+
currentState = 'normal';
364+
regexAllowed = false;
231365
}
232366
i++;
233367
} else if (currentState === 'comment_block') {

‎packages/angular/build/src/utils/source-map_spec.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,64 @@ describe('removeSourceMappingURL', () => {
8383
expect(removeSourceMappingURL(code)).toBe(code);
8484
});
8585

86+
it('should not corrupt strings following a regex literal containing a double quote', () => {
87+
const code = 'const pattern = /"/; const text = "//# sourceMappingURL=not-a-comment";';
88+
expect(removeSourceMappingURL(code)).toBe(code);
89+
});
90+
91+
it('should not corrupt strings following a regex literal containing single quotes or backticks', () => {
92+
const singleQuoteCode =
93+
"const pattern = /'/; const text = '//# sourceMappingURL=not-a-comment';";
94+
expect(removeSourceMappingURL(singleQuoteCode)).toBe(singleQuoteCode);
95+
96+
const backtickCode = 'const pattern = /`/; const text = `//# sourceMappingURL=not-a-comment`;';
97+
expect(removeSourceMappingURL(backtickCode)).toBe(backtickCode);
98+
});
99+
100+
it('should handle regex character classes containing slashes and quotes', () => {
101+
const code =
102+
'const pattern = /[/"\'`]/; const text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
103+
expect(removeSourceMappingURL(code)).toBe(
104+
'const pattern = /[/"\'`]/; const text = "//# sourceMappingURL=not-a-comment";\n',
105+
);
106+
107+
const charClassComment = 'const re = /[//# sourceMappingURL=foo.js.map]/;';
108+
expect(removeSourceMappingURL(charClassComment)).toBe(charClassComment);
109+
});
110+
111+
it('should handle escaped slashes, brackets, and quotes inside regex literals', () => {
112+
const code =
113+
'const pattern = /\\/"\\[/; const text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
114+
expect(removeSourceMappingURL(code)).toBe(
115+
'const pattern = /\\/"\\[/; const text = "//# sourceMappingURL=not-a-comment";\n',
116+
);
117+
118+
const escapedBracketInClass =
119+
'const pattern = /[\\]/"\']/; const text = "//# sourceMappingURL=not-a-comment";';
120+
expect(removeSourceMappingURL(escapedBracketInClass)).toBe(escapedBracketInClass);
121+
});
122+
123+
it('should handle regex literals after keywords', () => {
124+
const code =
125+
'function test() { return /"/; }\nconst text = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
126+
expect(removeSourceMappingURL(code)).toBe(
127+
'function test() { return /"/; }\nconst text = "//# sourceMappingURL=not-a-comment";\n',
128+
);
129+
});
130+
86131
it('should not affect normal division operators', () => {
87132
const code = 'const ratio = 10 / 2;\n//# sourceMappingURL=main.js.map';
88133
expect(removeSourceMappingURL(code)).toBe('const ratio = 10 / 2;\n');
89134
});
90135

136+
it('should handle division expressions followed by strings and comments', () => {
137+
const code =
138+
'const x = a / b; const y = "//# sourceMappingURL=not-a-comment";\n//# sourceMappingURL=main.js.map';
139+
expect(removeSourceMappingURL(code)).toBe(
140+
'const x = a / b; const y = "//# sourceMappingURL=not-a-comment";\n',
141+
);
142+
});
143+
91144
it('should only remove exact sourceMappingURL prefix comments', () => {
92145
const code = '// # sourceMappingURL=main.js.map\n//# sourceMappingURL=main.js.map';
93146
expect(removeSourceMappingURL(code)).toBe('// # sourceMappingURL=main.js.map\n');

0 commit comments

Comments
 (0)