Outage2 Jul 2019

Cloudflare's Regex Outage

A single firewall rule with a runaway regular expression maxed out every CPU on Cloudflare's network within seconds.

Severity
Front-page news
Difficulty
beginner
Companies
Cloudflare
Tech
Regular expressions, WAF, PCRE, Lua, CDN
Root cause
config pushed globally, latent bug

The story

Quick (1 min)

On 2 July 2019, websites behind Cloudflare started showing 502 errors worldwide for about 27 minutes. Cloudflare lost around 80% of its traffic.

Cloudflare's web firewall checks incoming requests against rules that look for attacks. An engineer deployed a new rule containing a regular expression (a text-matching pattern) that, on certain input, made the computer try an enormous number of combinations. That's called catastrophic backtracking.

Firewall rule changes went to every server in the world within seconds, so every CPU handling web traffic hit 100% at the same time. A safety limit that would have stopped this had been removed by accident weeks earlier.

The lesson: even small config changes need staged rollouts, and anything that runs user input needs a CPU limit.

Keep reading to the end to earn XP.Read. XP earned.

Engineer (2 min)

The change. At 13:42 UTC, a minor update to the WAF's cross-site scripting (XSS) detection rules was deployed automatically after passing CI. It was in simulate mode, meaning it logs but doesn't block, but simulated rules still execute on real traffic.

The regex. The rule contained (?:(?:\"|'|\]|\}|\\|\d|(?:nan|infinity|true|false|null|undefined|symbol|math)|\|-|+)+[)];?((?:\s|-|~|!|{}||||+).(?:.=.))). The .(?:.=.)` part makes a backtracking engine try a huge number of ways to split the input when a match fails. The WAF ran on Lua using PCRE, a backtracking engine with no protection against runaway expressions.

Global in seconds. WAF rules ship through Quicksilver, Cloudflare's distributed key-value store, which reaches every machine worldwide with a p99 of 2.29 seconds. The rule-change procedure explicitly allowed global pushes, unlike software releases, which go through the DOG (employees only) → PIG (some free traffic) → Canary → global stages.

Why it wasn't caught:

  • A CPU protection for regexes had been removed by mistake during a WAF refactor weeks earlier.
  • The test suite checked that rules blocked or allowed the right requests, but not CPU usage.

Response. Alerts fired within 3 minutes, and at first some suspected an attack. At 14:00 the WAF was identified, and at 14:02 a "global terminate" was proposed. But Cloudflare's own Access product was down, so engineers couldn't easily reach internal tools, and some had lost credentials to an inactivity timeout. The WAF was globally disabled at 14:07, and traffic recovered by 14:09.

Interview takeaway: fast global config is a superpower and a single point of failure. Pair it with staged rollouts, resource limits and a break-glass path.

Keep reading to the end to earn XP.Read. XP earned.

Deep dive (3 min)

Timeline (UTC, from Cloudflare's postmortem)

  • 13:31: a pull request with the rule change is merged after approval.
  • 13:37: TeamCity builds the rules and runs tests. They pass, and logs later showed no increase in test run time.
  • 13:42: automatic deploy via Quicksilver; the rule is live everywhere within seconds. CPUs serving HTTP/HTTPS spike to nearly 100%.
  • 13:45: first PagerDuty page from a synthetic WAF test, followed by end-to-end test failures, a global traffic drop alert and widespread 502s. Leadership hears traffic is down 80%. A P0 is declared.
  • 14:00: WAF identified through live CPU data, strace and error logs. An attack is ruled out.
  • 14:02: global WAF termination proposed. Access is down, so engineers use a rarely used bypass to reach internal systems.
  • 14:07: global WAF terminate executed. 14:09: traffic and CPU back to normal worldwide.
  • 14:52: after negative and positive tests in one city (with paying customers' traffic removed), the WAF is re-enabled globally.

Why the 502s

Front-line web servers still had CPU, but couldn't reach the processes that serve HTTP/HTTPS, which were saturated by the regex. So they returned 502 Bad Gateway.

Everything "went right"

"Everything that occurred up to the point the rules were deployed was done 'correctly': a pull request was raised, it was approved, CI/CD built the code and tested it, a change request was submitted with an SOP detailing rollout and rollback, and the rollout was executed."

The process itself allowed a non-emergency rule to go global with no staging. About 476 WAF change requests had been handled in the previous 60 days.

All the contributing factors Cloudflare listed

  • A regex that could backtrack enormously.
  • A CPU protection removed by mistake during a refactor.
  • A regex engine with no complexity guarantees.
  • Tests that couldn't identify excessive CPU use.
  • An SOP allowing a global rollout without staging.
  • A rollback plan needing two complete WAF builds.
  • A slow-to-fire global traffic drop alert, and a status page that wasn't updated quickly.
  • Hard access to internal systems, a poorly trained bypass, and timed-out credentials.
  • The customer Dashboard and API also sat behind the failing edge.

Fixes

  • Re-introduce the CPU protection (done) and manually inspect all 3,868 WAF rules (done).
  • Add performance profiling for every rule to the test suite.
  • Switch to re2 or the Rust regex engine, which have run-time guarantees.
  • Stage rule rollouts like other software, keeping an emergency global path for active attacks.
  • Be able to move the Dashboard and API off Cloudflare's own edge, and automate status page updates.

Keep reading to the end to earn XP.Read. XP earned.

Failure replay

Step by step, what broke and what it hit next.

  1. 13:31–13:37
    WAF rule changehealthy

    A new XSS rule is reviewed, merged and passes CI. The tests check matches, not CPU cost.

    Affects: Quicksilver

  2. 13:42
    Quicksilverfailing

    The rule ships to every machine worldwide in seconds. The rules SOP allows global pushes with no staging.

    Affects: Edge servers

  3. 13:42
    Edge serversdown

    The regex backtracks catastrophically on real traffic. Every HTTP core hits nearly 100% CPU, because the protection had been removed weeks earlier.

    Affects: Visitors

  4. 13:45
    Visitorsdown

    Sites behind Cloudflare return 502 errors. About 80% of traffic is lost.

  5. 14:00
    SRE teamdegraded

    Live CPU data and strace point to the WAF. An attack is ruled out.

    Affects: Internal tools

  6. 14:02
    Internal toolsdown

    Cloudflare's own Access login is down too. Engineers need a rarely used bypass to reach the kill switch.

  7. 14:07
    Edge serversrecovering

    A global WAF terminate runs. CPU and traffic are back to normal by 14:09.

    Affects: Visitors

  8. 14:52
    WAF rule changehealthy

    After testing in one city, the fixed WAF is re-enabled globally.

Impact

Who was affected
Visitors to sites using Cloudflare's proxy, CDN and WAF worldwide, plus customers trying to use the Cloudflare Dashboard and API.
How long
27 minutes of global outage (13:42 to 14:09 UTC); the WAF stayed disabled until 14:52.
Cost
not disclosed

Lessons

  • Stage config and rule changes like code; global-in-seconds needs a canary step.
  • Use regex engines with linear-time guarantees (re2, Rust regex) for anything that runs on untrusted input.
  • Test performance, not just correctness; a CPU budget test would have caught this.
  • Keep a well-practised break-glass path to internal tools that doesn't depend on your own product.

Quiz

  1. What caused CPU exhaustion across Cloudflare's network?

    Check answer

    A WAF rule with a regular expression that backtracked enormously. A single XSS-detection rule contained a regex that caused catastrophic backtracking on the PCRE-based Lua WAF.

  2. Why did the rule affect every server at once?

    Check answer

    WAF rules ship globally through Quicksilver in seconds, and the SOP allowed global pushes without staging. Quicksilver distributes changes worldwide with a p99 of about 2.3 seconds, and rule changes skipped the DOG, PIG and Canary stages used for software.

  3. The rule was in "simulate" mode. Why did it still cause harm?

    Check answer

    Simulated rules still execute on real traffic; they just don't block. Simulate mode logs results without blocking, but the regex still has to run against every request.

  4. Which engines did Cloudflare plan to switch to?

    Check answer

    re2 or the Rust regex engine, which have run-time guarantees. re2 and Rust regex guarantee linear-time matching, so they can't backtrack catastrophically.

Interview prep

Try answering out loud before you open the model answer.

  1. debugging

    What is catastrophic backtracking in regular expressions, and how would you protect a system that runs user-supplied input through regexes?

    Show model answer

    Backtracking engines like PCRE try alternative ways to match when part of a pattern fails. Patterns with nested or overlapping quantifiers, such as .*(?:.*=.*), can make the number of attempts grow very fast with input length, so one request can burn a CPU core. Protections: use engines with linear-time guarantees (RE2, Rust regex), which don't support backtracking features; enforce match time or step limits; review patterns for nested quantifiers; and add performance tests that run rules against adversarial inputs with a CPU budget. Cloudflare had a CPU protection but it was removed during a refactor, so also guard against regressions in safety features.

    A strong answer hits:

    • How backtracking explodes with overlapping quantifiers
    • Linear-time engines (RE2, Rust regex)
    • Timeouts or step limits per match
    • Performance tests with adversarial input
  2. system design

    You run a global config distribution system that pushes changes to every server in seconds. How do you keep that speed without risking global outages?

    Show model answer

    Separate emergency speed from normal changes. Default changes go through stages: internal-only, then a small slice of traffic, then canaries, then global, with automatic checks on CPU, error rates and traffic between stages. Keep a fast path for real emergencies, gated by explicit approval. Make every change instantly reversible (version and roll back in seconds), and make components fail safe. If a rule blows its CPU budget, disable that rule, not the server. Cloudflare changed its SOP to stage WAF rules while keeping emergency global deploys.

    A strong answer hits:

    • Staged rollout by default with automated health gates
    • Separate, approved emergency fast path
    • Instant rollback and versioned config
    • Per-rule resource isolation
  3. behavioural

    During an outage you can't log in to your own internal tools because they depend on the broken system. How should teams prepare for that?

    Show model answer

    Identify circular dependencies ahead of time: authentication, dashboards, CI and paging that run on your own product. Keep a break-glass path that bypasses them, document it, and practise it in drills; Cloudflare's bypass was rarely used and slowed the response. Review security policies like inactivity-based credential expiry so on-call staff keep access to emergency tools. Put the global kill switch somewhere reachable without the main stack.

    A strong answer hits:

    • Map circular dependencies on your own systems
    • Documented, drilled break-glass access
    • Check credential policies for on-call access

Go deeper