Lesson 9 of 13 · 25m
Catastrophic Backtracking and ReDoS
The pattern shapes that turn a regex into a denial-of-service, the real outages they caused, and the fixes in order of preference.
On July 2, 2019, Cloudflare deployed a new WAF rule and CPU usage on every edge machine serving HTTP went to 100%. Global traffic dropped for about 27 minutes. The culprit was a regex containing .*(?:.*=.*). Three years earlier — July 20, 2016 — Stack Overflow was down for 34 minutes because a whitespace-trimming regex, ^[\s\u200c]+|[\s\u200c]+$, met a post containing roughly 20,000 consecutive whitespace characters. Neither pattern looks dangerous. That's the point.
Why it blows up
Take ^(a+)+$ against aaaaaaaaaaaaaaaaaaaaaa!. The $ can never match, so the engine proves failure by trying every way to split the run of as between the inner + and the outer +. There are 2^(n−1) splits; each extra character doubles the work:
const evil = /^(a+)+$/;
for (const n of [16, 18, 20, 22, 24]) {
const input = "a".repeat(n) + "!";
const t0 = performance.now();
evil.test(input);
console.log(n, (performance.now() - t0).toFixed(1) + " ms");
}
// every +2 characters is roughly 4x slower; ~30 chars is seconds, ~40 is "page the on-call"The three shapes that cause it
- Nested quantifiers:
(a+)+,(\w+\s?)*,(\d+)*$. Exponential. - Overlapping alternation under a quantifier:
(\w|\d)+,(a|ab)*c,(\s|\S)*. Exponential. - Adjacent quantifiers over the same characters:
.*.*=.*,\s*\s+$,\d+\d+x. Polynomial — quadratic, cubic, worse with each extra term.
Stack Overflow was shape 3 combined with an unanchored search: for each of ~20,000 starting positions inside the whitespace run, \s+$ scanned to the end and failed on the next character. That's n²/2 — about 200 million steps, for one string.
It keeps happening in real dependencies
ua-parser-js, CVE-2021-27292: user-agent parsing regexes that hung the process on crafted UA strings.nth-check, CVE-2021-3803: a CSS:nth-childformula parser with quadratic backtracking, reachable through css-select.marked,moment,validator.js, andunderscorehave all shipped ReDoS advisories. Any library that compiles a regex from your input — or runs its own regexes on your input — inherits this.
Fixes, in order of preference
- Remove the ambiguity.
^(a+)+$becomes^a+$;(\w+\s?)*becomes\w+(?:\s\w+)*. When only one path can match each character there's nothing to backtrack through. - Bound the input. Reject anything over a sane length before the regex sees it. A 4 KB cap turns "exponential" into "annoying."
- Atomic groups and possessive quantifiers where supported:
(?>\w+),\w++in PCRE2, Java, Python 3.11+. They delete the bookmarks the engine would have retried. - A linear-time engine for untrusted input. RE2 (
re2on npm,google-re2on PyPI), Goregexp, Rustregex. Cloudflare's post-mortem committed to exactly this. - Timeouts as a backstop. .NET accepts
new Regex(pattern, options, TimeSpan.FromMilliseconds(100)). Node and Python have no per-regex timeout — isolate risky matching in a worker you can kill. Python's third-partyregexmodule takestimeout=.
Stack Overflow's actual fix was the best kind: they replaced the regex with a plain trim function. str.trimEnd() cannot backtrack.
Find them before attackers do
eslint-plugin-regexpwith theregexp/no-super-linear-backtrackingrule flags the shapes in review.- CodeQL ships
js/redosandjs/polynomial-redosqueries — run them in CI. recheckanalyzes a pattern and generates the attack string for it.- Fuzz with near-misses: a long run of the repeated character plus one that forces failure.
Field notes
- The attack input is always a near-miss. Matching inputs are fast; almost-matching inputs are where the time goes.
- A user-supplied regex — search filters, "custom alert rules" — is untrusted code. Run it on RE2 or not at all.
- Compile once.
Pattern.compileinside a Java loop andString.matches(which compiles on every call) are classic hot spots. Python caches recent patterns; hoist anyway. - Put a failing-input timing test next to every regex that guards an endpoint — assert the worst near-miss in your corpus finishes in milliseconds.
Keep reading
Related articles from the SudoSecurity blog.
Java Pulled Primitive Patterns After Five Previews. JDK 28 Won't Compile Them.
Primitive types in patterns, instanceof, and switch previewed in every JDK from 23 to 27. On November 9 the Amber team withdrew the feature, and it won't be in JDK 28 even with --enable-preview. Here's why Valhalla changed the plan, what replaces it, and how to rewrite code that used it.
.NET 11 Is Out. Union Types Got the Applause, but unsafe Is What Changed.
.NET 11 shipped on November 10, 2026, and C# 15 finally has union types, the feature people have asked for since pattern matching arrived.
PHP 8.6 Finally Turns On the Session Settings You Were Told to Set in 2006
Every PHP hardening guide for two decades has told you to flip the same three session settings. PHP 8.6, due November 19, 2026, flips them for you: strict mode on, HttpOnly on, SameSite set to Lax.
Rust 1.100: The Never Type Is Finally Stable, and Cargo Learned to Wait
Rust 1.100 stabilizes the never type after ten years and five failed attempts, along with the core Allocator API and a DropGuard helper. The change most teams will notice is in Cargo: a one-line config that makes it skip any crate version published in the last few days.