Skip to content

Commit 2cc19f7

Browse files
committed
fix(rsc): prevent catastrophic backtracking in directive regex
The regular expression used to preserve "use client" and "use server" directives was flawed and could backtrack into single-line comments that contained string literal characters. This resulted in the virtual CSS import being injected inside comments or invalid positions, causing syntax errors in files with URL queries like `?url` or `?raw` (e.g. static-assets tests). This fix changes the comment and string literal matching to prevent backtracking and ensures we only match true top-level string directives.
1 parent 2947f81 commit 2cc19f7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export const pluginTailwindCSS = (
181181
const importStr = `import "${pathToFileURL(VIRTUAL_UTILITIES_ID)}?${params.toString()}";\n`;
182182

183183
const match = code.match(
184-
/^(?:[\s]*|(?:\/\*[\s\S]*?\*\/)|(?:\/\/[^\n]*\n?))*(?:(?:(?:"[^"]*"|'[^']*')[ \t]*;?)(?:[ \t]*\/\/[^\n]*\n?|[ \t]*\/\*[\s\S]*?\*\/|[\s]*))+/,
184+
/^(?:[\s]*|(?:\/\*[\s\S]*?\*\/)|(?:\/\/[^\n]*(?:\n|$)))*(?:(?:(?:"[^"\n\r]*"|'[^'\n\r]*')[ \t]*;?)(?:[\s]*|(?:\/\*[\s\S]*?\*\/)|(?:\/\/[^\n]*(?:\n|$)))*)+/,
185185
);
186186

187187
if (match) {

0 commit comments

Comments
 (0)