The Greedy Regex

Medium

· Backend

Incident

SEV-1

BUG-WAF-711

Priority: Critical · reported by edge team

Time to fix: 20 min

Failing checks on the shipped build

  • the first assignment wins

    expected {"key":"score","value":"5"} but got {"key":"score=5 action","value":"block"}

  • value keeps everything after the first equals

    expected {"key":"expr","value":"a==b"} but got {"key":"expr=a=","value":"b other"}

  • tokens with invalid keys are skipped

    expected {"key":"mode","value":"strict"} but got {"key":"9bad=1 mode","value":"strict"}

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Cloudflare · 2019

Inspired by Cloudflare's 2019 global outage — a single greedy regex in the WAF pinned every CPU on the edge. Beyond the meltdown, greedy patterns are just wrong-answer machines.

ruleParser.js extracts the FIRST key=value token from a firewall rule line. The shipped pattern grabs the wrong things.

Code map

// Extracts the first key=value assignment from a rule line.
exports.extractAssignment = function (line) {
  var m = line.match(/(.*)=(.*)/);
  if (!m) return null;
  return { key: m[1], value: m[2] };
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Greedy Regex — JavaScript Bug Hunt

Inspired by Cloudflare's 2019 global outage — a single greedy regex in the WAF pinned every CPU on the edge.

  • Language: JavaScript
  • Layer: Backend
  • Difficulty: Medium
  • Concepts: Regex, Parsing
  • Modelled on: Cloudflare · 2019
  • Visible tests: the first assignment wins; value keeps everything after the first equals; tokens with invalid keys are skipped
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-1 (priority: Critical)
  • Ticket: BUG-WAF-711
  • Reported by: edge team
  • Time to fix: 20 minutes, the target for a medium incident

Failing checks on the shipped build

  • the first assignment wins — expected {"key":"score","value":"5"} but got {"key":"score=5 action","value":"block"}
  • value keeps everything after the first equals — expected {"key":"expr","value":"a==b"} but got {"key":"expr=a=","value":"b other"}
  • tokens with invalid keys are skipped — expected {"key":"mode","value":"strict"} but got {"key":"9bad=1 mode","value":"strict"}

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Inspired by Cloudflare's 2019 global outage — a single greedy regex in the WAF pinned every CPU on the edge. Beyond the meltdown, greedy patterns are just wrong-answer machines.

ruleParser.js extracts the FIRST key=value token from a firewall rule line. The shipped pattern grabs the wrong things.

Bug report

BUG-WAF-711 · Priority: Critical · Reported by: edge team

extractAssignment(line) spec:

  • scan whitespace-separated tokens; the first token containing "=" wins
  • key = the part before the FIRST "=", value = everything after it
  • keys are lowercase letters/underscores only, else keep scanning
  • no such token -> null

Observed: on "score=5 action=block", the greedy pattern returns key "score=5 action" value "block".

Logs

[waf] parse("score=5 action=block") -> { key: "score=5 action", value: "block" }

The code as shipped

src/waf/ruleParser.js (editable)

// Extracts the first key=value assignment from a rule line.
exports.extractAssignment = function (line) {
  var m = line.match(/(.*)=(.*)/);
  if (!m) return null;
  return { key: m[1], value: m[2] };
};

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.