Skip to content

Commit 05374ab

Browse files
committed
fix Polynomial regular expression used on uncontrolled data
1 parent 7ad861f commit 05374ab

File tree

1 file changed

+4
-3
lines changed
  • packages/bundler-plugin-core/src

1 file changed

+4
-3
lines changed

packages/bundler-plugin-core/src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,12 @@ export function serializeIgnoreOptions(ignoreValue: string | string[] | undefine
443443
export function containsOnlyImports(code: string): boolean {
444444
const codeWithoutImports = code
445445
// Remove side effect imports: import '/path'; or import "./path";
446-
.replace(/^\s*import\s+(['"`]).*?\1\s*;?\s*$/gm, "")
446+
// Using explicit negated character classes to avoid polynomial backtracking
447+
.replace(/^\s*import\s+(?:'[^'\n]*'|"[^"\n]*"|`[^`\n]*`)[\s;]*$/gm, "")
447448
// Remove named/default imports: import x from '/path'; import { x } from '/path';
448-
.replace(/^\s*import\s+[\s\S]*?\s+from\s+(['"`]).*?\1\s*;?\s*$/gm, "")
449+
.replace(/^\s*import\b[^'"`\n]*\bfrom\s+(?:'[^'\n]*'|"[^"\n]*"|`[^`\n]*`)[\s;]*$/gm, "")
449450
// Remove re-exports: export * from '/path'; export { x } from '/path';
450-
.replace(/^\s*export\s+[\s\S]*?\s+from\s+(['"`]).*?\1\s*;?\s*$/gm, "")
451+
.replace(/^\s*export\b[^'"`\n]*\bfrom\s+(?:'[^'\n]*'|"[^"\n]*"|`[^`\n]*`)[\s;]*$/gm, "")
451452
// Remove block comments
452453
.replace(/\/\*[\s\S]*?\*\//g, "")
453454
// Remove line comments

0 commit comments

Comments
 (0)