Skip to content

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 with fullmatch or \A...\Z so the engine does not hunt.
  • Atomic groups and possessive quantifiers — (?>x+), x++ — added to re in 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 regex module (PyPI) accepts timeout=: regex.fullmatch(pat, s, timeout=0.1) raises TimeoutError instead 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.