Catastrophic Backtracking (ReDoS): Find and Fix Slow Regex
Why some regular expressions hang on short inputs, the three pattern shapes behind regex denial of service, and how to rewrite them so matching stays fast.
What catastrophic backtracking is
The regex engines in JavaScript, Java, Python, PHP (PCRE), .NET and Ruby are backtracking engines. When part of a pattern fails, they step back and try another way to match the earlier parts. Usually there are only a few alternatives to try, but some patterns allow an exponential number of ways to split the same text.
On a string that almost matches, the engine must try all of them before it can report failure. Each extra character can double the work, so a 30-character input can keep a CPU busy for minutes. Because many servers validate input with regexes, an attacker can send such a string to tie up the server: this is regular expression denial of service, or ReDoS.
Pattern: ^(a+)+$
Input: aaaaaaaaaaaaaaaaaaaaaaaaaaaa!
(a+)+ can split 28 a's in 2^27 ways, and the ! makes every one of them fail.Shape 1: nested quantifiers
A repeated group that contains its own unbounded repetition, such as (a+)+, (\w+\s?)* or (.*)*, is the classic culprit. The inner and outer loops can divide the same characters between them in many ways.
Fix it by removing one level of repetition, or by giving every iteration a required delimiter that the inner part cannot match. In (?:\.[a-z]+)*, each repetition must start with a dot that [a-z]+ never consumes, so there is only one way to split the text.
Slow: ^([a-zA-Z0-9]+\.?)+@example\.com$
Fast: ^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@example\.com$Shape 2: overlapping alternatives inside a loop
(\w|\d)+ looks harmless, but every digit matches both alternatives, so a run of n digits can be matched in 2^n ways. The same happens with (a|ab)* or (\s|[ \t])*.
Make the alternatives mutually exclusive, or merge them into a single character class: (\w|\d)+ is just \w+.
Slow: ^(\w|\d)+$
Fast: ^\w+$Shape 3: adjacent loops over the same characters
Two quantifiers in a row that can match the same characters, such as \d+\d+ or .*.*=, let the engine move the boundary between them to every position. This is polynomial rather than exponential, but on long lines it still turns a fast check into a slow one, and it gets worse with three or more loops.
Merge them into one quantifier, or make the second loop match something the first cannot.
Slow: ^\s*\s+$ Fast: ^\s+$
Slow: ^.*.*=.*$ Fast: ^[^=]*=.*$How to test a pattern for ReDoS
Paste the pattern into the regex tester. It analyzes the pattern as you type and shows a Slow-regex risk badge for nested quantifiers, overlapping alternatives in loops and adjacent overlapping loops, with the exact part highlighted and a suggested fix. Matching runs in a separate worker that stops after two seconds, so pasting an attack string such as 30 a's followed by ! can't freeze the page. Chrome sometimes finishes such inputs quickly because V8 can fall back to a linear-time engine, so trust the analysis rather than the timing: Java, Python, PHP and .NET servers backtrack all the way.
Static checks can't prove a pattern is safe, so also limit input length before matching, set timeouts where your platform supports them, and consider a non-backtracking engine such as RE2 (Go, and libraries for other languages) for untrusted input.
Reference: OWASP: Regular expression Denial of Service (ReDoS).