Avoid regex denial of service (ReDoS) in your web app
Short answer: Some regular expressions take a fraction of a second on normal input but can run for minutes on a specially crafted string, freezing a web server or blocking a Node.js app for every user. This is regular expression denial of service (ReDoS). It comes from patterns with nested or overlapping repetition, such as (a+)+, combined with a backtracking regex engine. Simplify risky patterns, limit input length before matching, use a linear-time engine such as RE2 for untrusted input, and test patterns with tools built to spot the problem.
How a regex can hang your app
Most programming languages use backtracking regex engines. When a match fails partway, the engine goes back and tries other ways of matching the earlier parts. For most patterns that's quick. For some, the number of ways to try grows explosively with input length.
OWASP's ReDoS page explains it with the pattern ^(a+)+$. On a string of "a" characters followed by one character that doesn't fit, the engine tries every way of dividing the "a"s between the inner and outer repetition before giving up, and each extra character roughly doubles the work. OWASP calls a pattern that can get stuck like this an "evil regex", and describes the typical ingredients: a group with repetition, containing something that itself repeats or alternates between overlapping options.
An attacker, or an unlucky user, only needs to send the right string to a form field, URL parameter or API that runs the pattern.
Why it's especially bad on Node.js
Node.js runs your JavaScript on a single event loop. The Node.js guide "Don't block the Event Loop" names vulnerable regular expressions as one of the most common ways to block it disastrously. While one request is stuck in a slow regex, the whole process can't serve anyone else. Other platforms with thread pools suffer too, as stuck threads pile up.
Where risky patterns hide
- Email address validation with elaborate home-grown patterns.
- URL and domain checks with nested groups for subdomains and paths.
- Search and filter features that build regexes from user input.
- Log parsing and routing rules in middleware.
- Copied snippets from forums that "validate everything".
- Libraries you depend on, which have had ReDoS issues in the past.
Spotting a dangerous pattern
Warning signs:
- Nested quantifiers:
(x+)+,(x*)*,(x+)*. - Repeated groups with overlapping alternatives:
(a|aa)+,(\w|\d)+. - Repeated groups followed by something that might not match, forcing the engine to try every split.
Not every such pattern is exploitable, but each deserves a closer look. Static analysis tools for regexes and security linters can flag likely problems; Node's guide mentions tools such as safe-regex while warning that no tool catches every vulnerable pattern.
Fixes that work
Rewrite the pattern. Remove nesting where the outer repetition adds nothing: (a+)+ matches the same as a+. Make alternatives mutually exclusive. Anchor patterns and be specific about what each part matches.
Limit input length first. Reject inputs longer than you need before running any pattern. A name field rarely needs thousands of characters; an email address has practical limits. Length limits reduce the worst case dramatically.
Use a linear-time engine for untrusted input. Engines such as Google's RE2 guarantee matching time proportional to input length by not backtracking. The trade-off is that some features, like backreferences and some lookarounds, aren't available; RE2's syntax documentation lists which constructs it doesn't support. Bindings exist for many languages, including Node.js, and the Node guide suggests a different engine as one approach.
Avoid regex where a parser fits. For URLs, use your language's URL parser. For emails, use a simple sanity check and confirm the address by sending a verification email, rather than a complex pattern.
Never build regexes from raw user input. If users search with text, escape it or use plain substring matching.
Set timeouts where the platform allows. Some languages and libraries let you set a matching timeout. It's a backstop, not a cure.
Responding to a report
If someone reports a ReDoS issue in your app, treat it like any other security bug. Thank them, reproduce it on staging rather than production, and fix it quickly, because the exploit is easy to repeat once known. Check your logs for unusually long requests to the same endpoint, which may show whether it has already been used. After the fix, look for the same pattern elsewhere in your codebase: copied validation code tends to live in several places at once.
Keep validation simple
Many risky patterns exist because validation tries to do too much. A short, readable rule plus a confirmation step, such as a verification email or a lookup against real data, is usually more accurate and far safer than one enormous expression nobody on the team can read.
Testing
- Add unit tests that run each important pattern against long, near-matching inputs and fail if they take too long.
- Run a security scanner over your dependencies to catch known ReDoS issues in libraries.
- Review patterns in pull requests with the warning signs above in mind.
A worked example
Consider a hypothetical Kilkenny events platform whose sign-up API used a copied email-validation regex with nested groups. A security researcher reported that a long crafted string sent to the sign-up endpoint froze the Node.js server. The developer added a length limit on the field, replaced the regex with a simple check for one "@" and a dot in the domain, confirmed addresses by email as before, and ran the remaining patterns in the codebase through a regex safety checker. Two more risky patterns in a search filter were rewritten.
FAQ
Is this only a problem for JavaScript?
No. Any language with a backtracking engine can be affected, including Python, Java, PHP, Ruby and .NET.
Does a web application firewall protect me?
It may block some obvious attempts, but the fix belongs in your code.
How long should my input limits be?
As long as real data needs, plus a margin. Decide per field.
Can I just catch the error?
There's usually no error to catch; the regex simply runs for a very long time. Prevention and timeouts are what help.
If you'd like your app's validation and search patterns checked for this kind of risk, tell us about your stack.