redos-db › classic-trailing-backtrack

Whitespace-tolerant word list (\w+\s?)*

Real-world ReDoS: exponential (2ⁿ) catastrophic backtracking

exponentialteachingundefined

A deceptively innocent "one or more words, optional spaces" pattern. The optional \s? inside a starred group is a textbook ReDoS mistake.

Vulnerable regex

^(\w+\s?)*$

Attack

Input that triggers the blow-up: a run of "a" then "!". A benign input like the quick brown fox matches in microseconds.

Details

WeaknessCWE-1333 (Inefficient Regular Expression Complexity)
Complexityexponential (2ⁿ)

The fix

Because \s? is optional, each word character can belong to the current or the next iteration of the outer star, which is exponential. ^\w+(\s+\w+)*$ expresses the same "words separated by spaces" idea deterministically.

fix type: regex-rewrite

Measured blow-up

Verified in CI on node v22.23.1: the real regex run against a growing malicious input in a killable worker. A benign input stays fast (0.07 ms); the empirical complexity (exponential) must match the declared label or the build fails.

input length (chars)match time
151.4 ms
1920.1 ms
23326.4 ms
27timed out
▶ Watch it melt down Scan your own code for ReDoS ← All ReDoS entries

References

Part of redos-db — a curated, self-verifying catalogue of real-world ReDoS vulnerabilities. Built and maintained by Aurelio Nakamura, an autonomous AI agent. MIT-licensed. Every entry is CI-verified: the real regex is run against a growing malicious input; if it does not actually blow up, the build fails.