Catastrophic Backtracking
debt(d8/e3/b3/t8)
Closest to 'silent in production until users hit it' (d9, scored d8). No detection_hints provided; specialist ReDoS scanners (vuln-regex-detector, regex101) exist but are rarely run in standard PHP CI pipelines. Default linters and PHPStan won't flag (a+)+ patterns. Usually only discovered when an attacker or pathological input locks a worker.
Closest to 'simple parameterised fix' (e3). Per quick_fix, the remediation is rewriting the pattern with possessive quantifiers, atomic groups, or negated character classes — a localized regex change, not architectural. Touches the single regex literal but requires careful re-testing.
Closest to 'localised tax' (b3). The vulnerable regex itself is usually a single pattern in a validator; its presence doesn't shape the architecture, though if the validator is reused widely it leans toward b5. applies_to is web/cli but the choice is per-pattern, not system-wide.
Closest to 'serious trap' (t7, scored t8). Per misconception, simple-looking patterns like the canonical email regex are catastrophically vulnerable — the 'obvious' regex written by a competent dev is frequently the wrong one. PHP's backtrack_limit gives false reassurance (still burns CPU). Contradicts intuition that pattern complexity correlates with risk.
Also Known As
TL;DR
Explanation
Regex engines work by trying every possible path through the pattern. Catastrophic backtracking occurs when a pattern contains nested quantifiers over overlapping character classes — the classic form is (a+)+ or (a|aa)+. On input like 'aaaaaab', the engine explores exponentially many combinations of how to divide the 'a' characters among the nested groups before concluding there is no match. Each additional character doubles the work. In PHP, a catastrophic pattern on attacker-controlled input can consume 100% CPU for minutes, effectively performing a denial of service with a single HTTP request. Production PHP applications have been taken down by ReDoS on email validation patterns.
Watch Out
Common Misconception
Why It Matters
Common Mistakes
- Using (a+)+ or (\w+\s?)+ style nested quantifiers on user input — these are the canonical catastrophic backtracking patterns.
- Copying email or URL validation patterns from Stack Overflow without testing them against pathological inputs.
- Not setting PCRE backtrack limits — PHP's pcre.backtrack_limit defaults to 1,000,000 which may not be sufficient to prevent DoS.
- Assuming PHP's PCRE will timeout automatically — the backtrack limit throws a PREG_BACKTRACK_LIMIT_ERROR but does not stop CPU consumption until the limit is hit.
Avoid When
- Never use nested quantifiers on patterns that can match the same characters multiple ways — (a+)+ and (a|aa)+ are classic ReDoS triggers.
- Avoid complex regex for validating user-supplied strings on a web endpoint without a timeout or length cap on the input.
- Do not use a custom regex for email validation in PHP — use filter_var($email, FILTER_VALIDATE_EMAIL) which delegates to a C implementation with no backtracking risk.
When To Use
- You're debugging a web form that suddenly hangs on certain user inputs — check regex patterns for nested quantifiers or alternation over overlapping character classes.
- You're reviewing email, URL, or phone validation regex in user-facing code — these are high-risk targets for ReDoS attacks and should be tested with long malformed input.
- A regex takes unpredictable time and you need to explain why to developers — identifying catastrophic backtracking pinpoints the root cause and justifies switching to a non-backtracking engine or simpler pattern.
- You're performing a security audit of input validation and need to spot denial-of-service vulnerabilities — catastrophic backtracking patterns are a common weakness in legacy validation code.
Code Examples
// Catastrophic — nested quantifiers on user input
function validateEmail(string $email): bool {
return (bool) preg_match(
'/^([a-z0-9]+)([._-]?[a-z0-9]+)*@([a-z0-9]+)([._-]?[a-z0-9]+)*\.[a-z]{2,}$/i',
$email
);
// 'aaaaaaaaaaaaaaaaaa@' causes exponential backtracking
}
// Safe — use filter_var for email (C implementation, no backtracking)
function validateEmail(string $email): bool {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
// Or set limits and check return value
ini_set('pcre.backtrack_limit', '100000');
$result = preg_match($pattern, $input);
if ($result === false) {
throw new RuntimeException('Regex failed: ' . preg_last_error_msg());
}