Lesson 11 of 20 · 15m
Regexes That Don't Take Down Production
re is a backtracking engine — nested quantifiers on hostile input are exponential, and a single WAF regex knocked Cloudflare offline in 2019.
On July 2, 2019, Cloudflare deployed one WAF rule containing a .*.*.*= regex and knocked a slice of the internet offline for ~27 minutes. The pattern's nested quantifiers backtracked exponentially on real traffic, ate every CPU on their edge network, and the fix was a global WAF rollback. The wrong mental model: "regex is just pattern matching." Python's re is a backtracking engine — it tries paths and rewinds — which means some patterns have runtime that explodes with input length on inputs that don't match.
The mechanism
re.fullmatch(r"(x+)+$", "x" * 30 + "!") tries every partition of thirty x's — on the order of 2^29 ways to split them — before concluding it cannot match. Nested quantifiers ((a+)+, (\w+)*), overlapping alternation ((a|a)*), and quantified groups sharing prefixes are the shapes that go exponential. Matching usually looks instant because typical input either matches fast or fails fast. The bomb is the near-miss: attacker-controlled strings engineered to almost match.
That is the attack. ReDoS needs no access beyond an input field. A regex validating usernames, a log parser, an upload filter — (.+)+@ on an email field — takes a crafted string and pins a core. Cloudflare's own postmortem attributes the outage to exactly this: one regex, uncontrolled input, no bound.
Defenses that work
- Rewrite the shape.
[^/]+beats.+— a negated class can't overlap the next group, so there is nothing to re-partition. Anchor withfullmatchor\A...\Zso the engine does not hunt. - Atomic groups and possessive quantifiers —
(?>x+),x++— added torein Python 3.11. They commit to a match without saving backtrack states:(?>(x+))+can't repartition because the inner quantifier refuses to give characters back. - The
regexmodule (PyPI) acceptstimeout=:regex.fullmatch(pat, s, timeout=0.1)raisesTimeoutErrorinstead of pinning the CPU. - Linear-time engines. Google's RE2 is a DFA — backtracking doesn't exist in the model, so ReDoS is unexpressible, at the cost of no backreferences or lookarounds. Python bindings (
re2) exist for the hostile-input cases. - Bound the input. A length cap before matching is a one-line defense that always works.
The other failure: wrong tool
Regex can't parse nested syntax — HTML, JSON, email headers — because regular languages can't count depth. html.parser or lxml for markup, email for headers, json for JSON. The "don't parse HTML with regex" joke has teeth: it produces wrong answers and patterns full of the nested-quantifier shapes above.
The habits
re.compile once at module scope — the internal cache is only 512 entries. re.VERBOSE for anything longer than ~20 characters. Negated classes over dots. fullmatch over match + $. A regex-module timeout or RE2 anywhere the input is stranger-controlled. Cloudflare was not a fluke. Stack Overflow took a 34-minute outage on July 20, 2016 when a post containing a pathological whitespace run drove a post-processing regex into backtracking, and the CVE database carries hundreds of ReDoS entries — validator.js, underscore, moment patterns, countless URL/email validators. The class is ordinary enough that GitHub's CodeQL ships dedicated ReDoS queries.
Flags worth naming because they change semantics, not style: re.IGNORECASE, re.MULTILINE (^/$ per line), re.DOTALL (. matches newline — the flag that turns .+ into a cross-line backtracking farm). Every flag is a change to what the pattern can match; reach for them deliberately.
Interpolating user text into a pattern is the regex-side injection: re.escape(name) quotes metacharacters so C++ or file.txt search literally instead of redefining the pattern. Without it, a . in the input is a wildcard the attacker supplied.
A regex with nested quantifiers on unbounded input is a CPU-exhaustion endpoint — Cloudflare demonstrated it at planetary scale in 2019.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Free-Threaded Python, Measured: Threads Finally Scale, and Your Unlocked Counter Finally Breaks
Python 3.14 made the no-GIL build officially supported, and 3.15 kept going. I ran the same CPU-bound job on standard and free-threaded builds of both: threads went from slower-than-serial to 1.9x faster, and a counter that never lost an update under the GIL lost 70% of them without it.
What is Python? Enterprise, Data Science, and the Backend of the Web
Python runs Instagram, Dropbox, Netflix, Spotify, and Reddit. It trains the models that power modern AI. It processes petabytes of data in enterprise pipelines. And I ignored it for years because I thought it was just for academics. Here is what changed my mind.
What Python 3.13 Shipped
The first release of Python 3.13 is now here as a Beta Release. This article is a rundown of all of the important changes and features that are being released in Python 3.13, along with what they mean for Python Developers. Please remember that this can change between
What Python 3.11 Actually Changed
The Python Programming Language releases a new version every year, with a feature-locked beta release in the first half of the year and the final release toward the end of the year. The feature set for Python 3.11 has just been finalized, with a beta version available for testing.