Prevent ReDoS
Eliminate catastrophic regex backtracking by using atomic groups, possessive quantifiers, or linear-time regex engines — preventing single-user input from consuming CPU for minutes or hours.
Why This Is Best Practice
Adopted by: CWE-1333 (Inefficient Regular Expression Complexity) is a named weakness tracked by NVD with hundreds of CVEs. Node.js, Cloudflare, and Stack Overflow have all experienced ReDoS outages. OWASP Top 10 2021 A06 (Vulnerable and Outdated Components) includes ReDoS in third-party regex libraries. Google's re2 library (used in Go's stdlib regex) is designed to guarantee linear-time matching. GitHub's CodeQL, Semgrep, and Snyk all include ReDoS detection rules.
Impact: Stack Overflow's 2016 outage was caused by a single malicious regex ^\s*(\w+\s*)+$ consuming 100% CPU on a crafted input string — taking down the site for 34 minutes. Cloudflare's 2019 global outage lasted 27 minutes due to a ReDoS in a WAF regex. Node.js has had multiple ReDoS CVEs in the validator, marked, and moment packages. A single well-crafted string can freeze a single-threaded server.
Why best: Input length limits alone are insufficient — catastrophic backtracking is exponential, so even 30-character inputs can cause seconds-long hangs. Rewriting to atomic/possessive patterns or using a linear-time engine eliminates the root cause.
Sources: OWASP ReDoS Cheat Sheet; Stack Overflow outage post-mortem (2016); Cloudflare outage post-mortem (2019); CWE-1333
Steps
Identify catastrophically vulnerable regex patterns — look for:
- Nested quantifiers:
(a+)+, (a*)*, ([ab]+)+
- Alternation with overlapping options:
(a|aa)+
- Multiple optional groups applied to the same characters:
(a+b?)+
# VULNERABLE — exponential backtracking on "aaaaaaaaaaaaaaaax"
import re
pattern = re.compile(r'^(\w+\s*)+$') # nested quantifier
pattern = re.compile(r'^(a+)+$') # classic evil regex
pattern = re.compile(r'^([a-zA-Z]+)*$') # same structure
# Test: time how long this takes (should be microseconds, not seconds)
import time
start = time.time()
re.match(r'^(\w+\s*)+$', 'a' * 30 + '!')
print(f"{time.time() - start:.3f}s") # > 1s = vulnerable
Use atomic groups or possessive quantifiers to prevent backtracking:
# Python 3.11+ supports atomic groups (?>...) and possessive quantifiers
import re
# VULNERABLE
pattern = re.compile(r'^(\w+\s*)+$')
# FIXED — atomic group prevents backtracking into the group
pattern = re.compile(r'^(?>\w+\s*)+$') # Python 3.11+
# Alternative for older Python — use regex module
import regex
pattern = regex.compile(r'^(\w+\s*)++$') # possessive quantifier ++
Use re2 or linear-time engines for user-facing regex:
# Python — google-re2 (linear time guarantee)
pip install google-re2
import re2
pattern = re2.compile(r'^\w+(\.\w+)*$')
match = pattern.match(user_input)
# Note: re2 does not support backreferences or lookaheads
// Node.js — use 're2' package
const RE2 = require('re2');
const pattern = new RE2('^\\w+(\\.\\w+)*$');
pattern.test(userInput); // linear time, no catastrophic backtracking
Set timeouts on regex execution for patterns that cannot be rewritten:
import signal
class RegexTimeout(Exception):
pass
def safe_match(pattern, text, timeout_ms=100):
def handler(signum, frame):
raise RegexTimeout("Regex timed out")
signal.signal(signal.SIGALRM, handler)
signal.setitimer(signal.ITIMER_REAL, timeout_ms / 1000)
try:
result = pattern.match(text)
signal.setitimer(signal.ITIMER_REAL, 0)
return result
except RegexTimeout:
return None
Note: signal.SIGALRM is POSIX-only (not Windows).
Limit input length before applying regex:
MAX_INPUT_LEN = 1000 # adjust per expected input
def validate_input(user_input, pattern):
if len(user_input) > MAX_INPUT_LEN:
raise ValueError("Input too long")
return pattern.match(user_input)
Length limits reduce the window of catastrophic backtracking but don't eliminate it — pair with pattern fixing.
Use static analysis to detect vulnerable patterns at CI time:
# Semgrep ReDoS rules
semgrep --config p/redos .
# vuln-regex-detector (Node.js)
npx vuln-regex-detector check '(a+)+'
# safe-regex (npm)
npx safe-regex '(\w+\s*)+' # exits non-zero if unsafe
Rules
re2 does not support backreferences (\1), lookaheads, or lookbehinds — evaluate compatibility before switching.
- Possessive quantifiers (
++, *+, ?+) in PCRE/Python regex module prevent backtracking into that group — they are safe but also change match semantics.
- Alternation with shared prefixes is safe —
(abc|abd) — alternation with overlapping suffixes is dangerous — (a|aa)+.
- Testing a regex with crafted input in development is not sufficient — use static analysis tools.
Common Mistakes
- Assuming third-party library regexes are safe — npm packages like
validator, path-to-regexp, and moment have had ReDoS CVEs. Check package.json deps with npx auditjs.
- Using complex regex for email validation — the RFC-compliant email regex is catastrophically backtracking. Use a simple
\S+@\S+\.\S+ or a dedicated library.
- Not testing with adversarial input — a regex that works fine on valid inputs may still explode on malformed ones; always test with
'a' * 30 + '!' style inputs.
- Fixing ReDoS with length checks alone — catastrophic backtracking is exponential; even short inputs (20–50 chars) can take seconds.
1---2name: prevent-redos3description: Use when writing or reviewing regular expressions that process user-controlled input — especially patterns with nested quantifiers, alternation with overlapping cases, or patterns applied to long strings.4source: 'OWASP Regular Expression Denial of Service (ReDoS) Defense Cheat Sheet (owasp.org/www-project-cheat-sheets); CWE-1333; OWASP Top 10 2021 A06'5tags: [security, owasp, redos, regex, denial-of-service, input-validation, developer]6metadata:7 source: https://skillmd.com/skills/jeffreytse/prevent-redos8---910# Prevent ReDoS1112Eliminate catastrophic regex backtracking by using atomic groups, possessive quantifiers, or linear-time regex engines — preventing single-user input from consuming CPU for minutes or hours.1314## Why This Is Best Practice1516**Adopted by:** CWE-1333 (Inefficient Regular Expression Complexity) is a named weakness tracked by NVD with hundreds of CVEs. Node.js, Cloudflare, and Stack Overflow have all experienced ReDoS outages. OWASP Top 10 2021 A06 (Vulnerable and Outdated Components) includes ReDoS in third-party regex libraries. Google's `re2` library (used in Go's stdlib regex) is designed to guarantee linear-time matching. GitHub's CodeQL, Semgrep, and Snyk all include ReDoS detection rules.17**Impact:** Stack Overflow's 2016 outage was caused by a single malicious regex `^\s*(\w+\s*)+$` consuming 100% CPU on a crafted input string — taking down the site for 34 minutes. Cloudflare's 2019 global outage lasted 27 minutes due to a ReDoS in a WAF regex. Node.js has had multiple ReDoS CVEs in the `validator`, `marked`, and `moment` packages. A single well-crafted string can freeze a single-threaded server.18**Why best:** Input length limits alone are insufficient — catastrophic backtracking is exponential, so even 30-character inputs can cause seconds-long hangs. Rewriting to atomic/possessive patterns or using a linear-time engine eliminates the root cause.1920Sources: OWASP ReDoS Cheat Sheet; Stack Overflow outage post-mortem (2016); Cloudflare outage post-mortem (2019); CWE-13332122## Steps23241. **Identify catastrophically vulnerable regex patterns** — look for:2526 - Nested quantifiers: `(a+)+`, `(a*)*`, `([ab]+)+`27 - Alternation with overlapping options: `(a|aa)+`28 - Multiple optional groups applied to the same characters: `(a+b?)+`2930 ```python31 # VULNERABLE — exponential backtracking on "aaaaaaaaaaaaaaaax"32 import re33 pattern = re.compile(r'^(\w+\s*)+$') # nested quantifier34 pattern = re.compile(r'^(a+)+$') # classic evil regex35 pattern = re.compile(r'^([a-zA-Z]+)*$') # same structure3637 # Test: time how long this takes (should be microseconds, not seconds)38 import time39 start = time.time()40 re.match(r'^(\w+\s*)+$', 'a' * 30 + '!')41 print(f"{time.time() - start:.3f}s") # > 1s = vulnerable42 ```43442. **Use atomic groups or possessive quantifiers to prevent backtracking**:4546 ```python47 # Python 3.11+ supports atomic groups (?>...) and possessive quantifiers48 import re4950 # VULNERABLE51 pattern = re.compile(r'^(\w+\s*)+$')5253 # FIXED — atomic group prevents backtracking into the group54 pattern = re.compile(r'^(?>\w+\s*)+$') # Python 3.11+5556 # Alternative for older Python — use regex module57 import regex58 pattern = regex.compile(r'^(\w+\s*)++$') # possessive quantifier ++59 ```60613. **Use `re2` or linear-time engines for user-facing regex**:6263 ```python64 # Python — google-re2 (linear time guarantee)65 pip install google-re26667 import re268 pattern = re2.compile(r'^\w+(\.\w+)*$')69 match = pattern.match(user_input)70 # Note: re2 does not support backreferences or lookaheads71 ```7273 ```javascript74 // Node.js — use 're2' package75 const RE2 = require('re2');76 const pattern = new RE2('^\\w+(\\.\\w+)*$');77 pattern.test(userInput); // linear time, no catastrophic backtracking78 ```79804. **Set timeouts on regex execution** for patterns that cannot be rewritten:8182 ```python83 import signal8485 class RegexTimeout(Exception):86 pass8788 def safe_match(pattern, text, timeout_ms=100):89 def handler(signum, frame):90 raise RegexTimeout("Regex timed out")91 signal.signal(signal.SIGALRM, handler)92 signal.setitimer(signal.ITIMER_REAL, timeout_ms / 1000)93 try:94 result = pattern.match(text)95 signal.setitimer(signal.ITIMER_REAL, 0)96 return result97 except RegexTimeout:98 return None99 ```100101 Note: `signal.SIGALRM` is POSIX-only (not Windows).1021035. **Limit input length before applying regex**:104105 ```python106 MAX_INPUT_LEN = 1000 # adjust per expected input107108 def validate_input(user_input, pattern):109 if len(user_input) > MAX_INPUT_LEN:110 raise ValueError("Input too long")111 return pattern.match(user_input)112 ```113114 Length limits reduce the window of catastrophic backtracking but don't eliminate it — pair with pattern fixing.1151166. **Use static analysis to detect vulnerable patterns at CI time**:117118 ```bash119 # Semgrep ReDoS rules120 semgrep --config p/redos .121122 # vuln-regex-detector (Node.js)123 npx vuln-regex-detector check '(a+)+'124125 # safe-regex (npm)126 npx safe-regex '(\w+\s*)+' # exits non-zero if unsafe127 ```128129## Rules130131- `re2` does not support backreferences (`\1`), lookaheads, or lookbehinds — evaluate compatibility before switching.132- Possessive quantifiers (`++`, `*+`, `?+`) in PCRE/Python `regex` module prevent backtracking into that group — they are safe but also change match semantics.133- Alternation with shared prefixes is safe — `(abc|abd)` — alternation with overlapping suffixes is dangerous — `(a|aa)+`.134- Testing a regex with crafted input in development is not sufficient — use static analysis tools.135136## Common Mistakes137138- **Assuming third-party library regexes are safe** — npm packages like `validator`, `path-to-regexp`, and `moment` have had ReDoS CVEs. Check `package.json` deps with `npx auditjs`.139- **Using complex regex for email validation** — the RFC-compliant email regex is catastrophically backtracking. Use a simple `\S+@\S+\.\S+` or a dedicated library.140- **Not testing with adversarial input** — a regex that works fine on valid inputs may still explode on malformed ones; always test with `'a' * 30 + '!'` style inputs.141- **Fixing ReDoS with length checks alone** — catastrophic backtracking is exponential; even short inputs (20–50 chars) can take seconds.