Skip to content

Commit 656f5f2

Browse files
waleedlatif1claude
andcommitted
fix(security): validate regex syntax before safety check
Move new RegExp() before safe() so invalid patterns get a proper syntax error instead of a misleading "catastrophic backtracking" message. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3c88579 commit 656f5f2

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

apps/sim/lib/guardrails/validate_regex.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@ export interface ValidationResult {
1111
}
1212

1313
export function validateRegex(inputStr: string, pattern: string): ValidationResult {
14+
let regex: RegExp
1415
try {
15-
if (!safe(pattern)) {
16-
return {
17-
passed: false,
18-
error: 'Regex pattern rejected: potentially unsafe (catastrophic backtracking)',
19-
}
20-
}
16+
regex = new RegExp(pattern)
17+
} catch (error: any) {
18+
return { passed: false, error: `Invalid regex pattern: ${error.message}` }
19+
}
2120

22-
if (inputStr.length > MAX_INPUT_LENGTH) {
23-
return {
24-
passed: false,
25-
error: `Input exceeds maximum length of ${MAX_INPUT_LENGTH} characters`,
26-
}
21+
if (!safe(pattern)) {
22+
return {
23+
passed: false,
24+
error: 'Regex pattern rejected: potentially unsafe (catastrophic backtracking)',
2725
}
26+
}
2827

29-
const regex = new RegExp(pattern)
30-
const match = regex.test(inputStr)
31-
32-
if (match) {
33-
return { passed: true }
28+
if (inputStr.length > MAX_INPUT_LENGTH) {
29+
return {
30+
passed: false,
31+
error: `Input exceeds maximum length of ${MAX_INPUT_LENGTH} characters`,
3432
}
35-
return { passed: false, error: 'Input does not match regex pattern' }
36-
} catch (error: any) {
37-
return { passed: false, error: `Invalid regex pattern: ${error.message}` }
3833
}
34+
35+
const match = regex.test(inputStr)
36+
if (match) {
37+
return { passed: true }
38+
}
39+
return { passed: false, error: 'Input does not match regex pattern' }
3940
}

0 commit comments

Comments
 (0)