What Is ReDoS (Regular Expression Denial of Service)?

Regular Expression Denial of Service (ReDoS) : A denial of service attack that exploits vulnerable regular expressions with catastrophic backtracking behavior. When a crafted input is matched against a vulnerable regex pattern, the regex engine enters exponential or polynomial time complexity, consuming CPU resources and potentially freezing the application for seconds, minutes, or indefinitely.

Why It Matters for AI-Coded Apps

AI code generators frequently produce complex regular expressions for input validation that are vulnerable to ReDoS. LLMs optimize for correctness (matching the right strings) without considering computational complexity. A regex that correctly validates email addresses might also freeze the server when given a specially crafted input.

Real-World Example

An AI generates email validation: /^([a-zA-Z0-9]+\.)*[a-zA-Z0-9]+@([a-zA-Z0-9]+\.)*[a-zA-Z]{2,}$/. The nested quantifiers ()+ create catastrophic backtracking. Input aaaaaaaaaaaaaaaaaaaaaaaaaaa! takes exponential time to reject, freezing the event loop. A safe alternative: use a simple check or a dedicated validation library.

How to Detect and Prevent It

Avoid nested quantifiers in regex (e.g., (a+)+, (a|a)*). Use atomic groups or possessive quantifiers where supported. Set timeout limits on regex execution. Use libraries like re2 (linear-time regex engine) instead of backtracking engines. For common validations (email, URL), use dedicated validation libraries instead of regex.

Frequently Asked Questions

How do I identify vulnerable regex patterns?

Look for nested quantifiers: (a+)+, (a|b)+, (a+b*)+. Use tools like recheck, redos-detector, or safe-regex to analyze your patterns. The general rule: if a regex has a quantifier inside a group that also has a quantifier, it may be vulnerable.

Can ReDoS affect production applications?

Yes. A single ReDoS payload can freeze a Node.js event loop, making the entire application unresponsive. In Python, it blocks the thread handling the request. Real-world incidents have taken down production services for hours with a single malicious input.

What is the safest way to validate user input?

Use established validation libraries (Zod, Joi, validator.js) instead of custom regex. If regex is necessary, use Google’s RE2 engine which guarantees linear-time execution. Set server-side timeouts on all regex operations and validate input length before applying regex.

Scan your app for security issues automatically

Vibe Eval checks for 200+ vulnerabilities in AI-generated code.

Try Vibe Eval

AI Coding Security Insights.
Ship Vibe-Coded Apps Safely.

Effortlessly test and evaluate web application security using Vibe Eval agents.