Skip to content

Lesson 11 of 14 · 20m

Regexes and ReDoS: The Synchronous DoS You Can't Abort

Backtracking engines, catastrophic patterns, the Cloudflare outage — and why no AbortSignal will save you mid-match.

JavaScript's regex engine backtracks. It's a recursive matcher, not a finite automaton — and that means some patterns on some inputs don't just fail slowly, they explore an exponential search tree on your single request-handling thread. /^(a+)+$/ against 'a'.repeat(30) + '!' doesn't return false — it returns nothing, for longer than your pod's health check will wait.

Why backtracking explodes

When the engine hits a dead end, it rewinds and tries a different split of the same work. Nested quantifiers — (a+)+, (a|aa)+, (\w+\s?)+ — give it exponentially many ways to partition the input, and a string that almost matches forces it to try all of them. 'aaaa…!' fails only after the engine explores every possible grouping of the as.

This isn't a theoretical class of bug. On July 2, 2019, Cloudflare took its global network down for ~27 minutes when a WAF rule containing a regex with a .*(?:.*=.*) shape backtracked catastrophically on real traffic — their public postmortem walks through the CPU exhaustion. In the npm ecosystem, semver (the version parser npm itself uses) shipped ReDoS-vulnerable range parsing as CVE-2022-25883, and minimatch had CVE-2022-3517. These are dependencies in your tree, called on input that may trace back to a request.

The patterns to look for

  • Nested quantifiers — (x+)+, (x*)*, (x+)*
  • Quantified alternation with overlapping prefixes — (a|aa)+, (\d|\w)+ where branches can consume the same characters
  • Adjacent .* / .+ — .*foo.* backtracks quadratically on long misses
  • .*= inside a quantified group — the Cloudflare shape

A linear-time check beats a clever pattern. Cap length first (if (input.length > 200) reject), pre-filter with includes or startsWith before handing the engine anything ambiguous, and anchor patterns (^…$) so the engine can't scan. Test adversarially: ('a'.repeat(50) + '!') is the input that separates a safe pattern from a production incident.

The dependency angle is just as important as your own patterns. You do not have to write the vulnerable regex — a transitive dep can do it for you, which is exactly what the semver and minimatch CVEs were: packages called on version ranges and glob patterns that sometimes trace back to a package.json field, a request header, or a URL path segment. When a lint rule or a postinstall audit flags ReDoS in a dep, the fix is usually a version bump — but knowing where in your call chain the pattern sees attacker input is what tells you whether the bump is urgent or cosmetic.

You cannot abort it

There is no timeout for a regex — AbortSignal can't help, because the match is synchronous code on the event loop. The only escapes are: don't run the pattern on hostile input, move it to a worker_threads worker you can terminate(), or swap the engine — the re2 npm package wraps Google's linear-time RE2 library, which can't backtrack (it also rejects some JS regex syntax, which is the price of the guarantee).

replace() has its own trap

The replacement string in String.prototype.replace interprets $ patterns: $& is the match, $' is the text after it, $` the text before. Pass user input as the replacement and userInput containing $& injects the matched text into the output — a subtle injection primitive in templating and redaction code. Escape $ as $$, or pass a function:

s.replace(/secret/g, () => userInput);   // function return is literal — no $ magic

Try this

Benchmark /^(\w+\s?)+$/ against ('a '.repeat(25) + '!') — count how long it takes, then double the input length and count again. Now fix it with a length cap plus includes, and re-run. Then grep your codebase for .* inside quantified groups in any regex reachable from request input.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.