Skip to content

Commit c53ef0b

Browse files
committed
revert: remove unnecessary ReDoS validation and array copying
- Remove ReDoS validation: user-provided regex patterns are trusted (core config values, user's responsibility) - Revert array copying in sortedImports: matches old TypeScript Hero behavior (groups are reset between calls anyway) - Add docstring noting regex patterns are trusted without validation The agents gave false warnings about these being bugs.
1 parent 8b26e72 commit c53ef0b

3 files changed

Lines changed: 7 additions & 34 deletions

File tree

src/imports/import-grouping/keyword-import-group.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export class KeywordImportGroup implements ImportGroup {
1111
public readonly imports: Import[] = [];
1212

1313
public get sortedImports(): Import[] {
14-
// IMPORTANT: Copy array before sorting to avoid mutating the original
15-
return [...this.imports].sort((i1, i2) => importSort(i1, i2, this.order));
14+
return this.imports.sort((i1, i2) => importSort(i1, i2, this.order));
1615
}
1716

1817
constructor(

src/imports/import-grouping/regex-import-group.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@ import { importSort } from '../import-utilities';
33
import { ImportGroup } from './import-group';
44
import { ImportGroupOrder } from './import-group-order';
55

6-
/**
7-
* Detects potentially dangerous regex patterns that could cause catastrophic backtracking (ReDoS).
8-
* Checks for nested quantifiers like (a+)+, (a*)+, etc.
9-
*/
10-
function isPotentiallyDangerousRegex(pattern: string): boolean {
11-
// Detect nested quantifiers: (pattern)+ or (pattern)* where pattern contains + or *
12-
// This is a simple heuristic, not comprehensive
13-
const nestedQuantifierPattern = /\([^)]*[+*][^)]*\)[+*]/;
14-
return nestedQuantifierPattern.test(pattern);
15-
}
16-
176
/**
187
* Import group that processes all imports that match a certain regex (the lib name).
8+
*
9+
* NOTE: User-provided regex patterns are trusted without validation.
10+
* This is a core configuration value - users are responsible for providing valid patterns.
1911
*/
2012
export class RegexImportGroup implements ImportGroup {
2113
public readonly imports: Import[] = [];
2214
private readonly compiledRegex: RegExp;
2315

2416
public get sortedImports(): Import[] {
25-
// IMPORTANT: Copy array before sorting to avoid mutating the original
26-
const sorted = [...this.imports].sort((i1, i2) =>
17+
const sorted = this.imports.sort((i1, i2) =>
2718
importSort(i1, i2, this.order),
2819
);
2920
return [
@@ -51,23 +42,7 @@ export class RegexImportGroup implements ImportGroup {
5142
regexString = regexString.endsWith('/')
5243
? regexString.substring(0, regexString.length - 1)
5344
: regexString;
54-
55-
// Check for potentially dangerous regex patterns (ReDoS protection)
56-
// Note: We just log to debug console, which is visible in Extension Development Host
57-
if (isPotentiallyDangerousRegex(regexString)) {
58-
// eslint-disable-next-line no-console
59-
console.warn(`[RegexImportGroup] Warning: Potentially dangerous regex pattern detected: ${this.regex}`);
60-
}
61-
62-
// Try to compile regex, use fallback if invalid
63-
try {
64-
this.compiledRegex = new RegExp(regexString);
65-
} catch {
66-
// Invalid regex - use a pattern that never matches
67-
// eslint-disable-next-line no-console
68-
console.error(`[RegexImportGroup] Invalid regex pattern: ${this.regex}`);
69-
this.compiledRegex = /(?!)/; // Never matches
70-
}
45+
this.compiledRegex = new RegExp(regexString);
7146
}
7247

7348
public reset(): void {

src/imports/import-grouping/remain-import-group.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export class RemainImportGroup implements ImportGroup {
1010
public readonly imports: Import[] = [];
1111

1212
public get sortedImports(): Import[] {
13-
// IMPORTANT: Copy array before sorting to avoid mutating the original
14-
const sorted = [...this.imports].sort((i1, i2) =>
13+
const sorted = this.imports.sort((i1, i2) =>
1514
importSort(i1, i2, this.order),
1615
);
1716
return [

0 commit comments

Comments
 (0)