Lesson 17 of 20 · 15m
Inputs that burn: ReDoS, bombs and rate-limit economics
The regex that took down Cloudflare, decompression bombs, hash flooding and rate limits that make attacks cost more than they're worth.
Denial of service isn't only a network problem. Most application-level DoS lives in your parsing layer: input that's cheap to send and expensive to process. The budget question is per-endpoint: how many CPU-seconds does one request buy the attacker? The wrong mental model is "DoS is ops' job — rate-limit at the edge." The edge can count requests; it can't see that one request is carrying a bomb. (Slowloris and friends — connections that trickle headers forever — really are ops' job; this lesson is the application half.)
ReDoS: the regex that ate the site
Backtracking regexes go exponential on adversarial input. ^(a+)+$ on "aaaa…aaaa!" explores every way to partition the as between the nested quantifiers before it can fail — each backtrack multiplies the remaining work. The killer shapes are nested quantifiers ((x+)+, (x|y)* over overlapping alternatives) glued to input that can almost-but-not-quite match.
This has taken down real infrastructure. Cloudflare's July 2, 2019 outage — 27 minutes of global CPU exhaustion — was a WAF regex shipped in a routine rules push; their post-mortem names it plainly. Stack Overflow's July 2016 outage was a trim regex (^[\s\u200c]+|[\s\u200c]+$) running on a malformed post body — about 34 minutes down.
Defenses, in order of strength:
- Use a linear-time engine where you can. Go's
regexpand Rust'sregexare RE2-class automata — backtracking is impossible by construction, andre2bindings exist for Python and Node (Node's built-inRegExphas no timeout — RE2 or wrapper libraries fill that gap). - Set a match timeout where the engine supports it — .NET's
Regex.MatchTimeoutexists precisely for this, and its infinite default is a footgun. - Prefer boring patterns: anchored, flat, no nested quantifiers over alternations. Test with adversarial strings (
"a" * 40 + "!") in CI; tools likeregexploitstatically find the exponential ones.
The rest of the bomb inventory
- Decompression bombs. A kilobyte-scale gzip or PNG that expands to gigabytes in memory — the folklore 42.zip nests to ~4.5 petabytes. Cap decompressed size and pixel dimensions before allocating; image decoders need the same bound, because a small header can claim a canvas that expands to gigabytes.
- Algorithmic attacks. HashDoS (n.runs, 2011): keys crafted to collide flood hash tables — the reason Python randomized string hashing in 3.3, and why
PYTHONHASHSEED=0in production is a small self-own. JSON depth limits exist for the same reason; cap nesting. - Resource amplification.
LIMITevery query, paginate every list, timeout every external call. An unboundedSELECTbehind a public parameter is a self-DoS. GraphQL endpoints need depth and complexity budgets for the same reason (graphql-depth-limitexists because naive queries nest). - Multipart and header shapes. Cap file counts in multipart bodies and header counts/sizes (nginx does this by default); bound streaming endpoints per-connection.
Caps belong at every layer, not just the proxy: client_max_body_size in nginx, express.json({ limit: '100kb' }), route guards in FastAPI — a 50 MB JSON body burns RAM and CPU before your handler ever runs. Timeouts on outbound calls matter as much as inbound caps: Python's requests defaults to no timeout, so a hung dependency holds your worker indefinitely; timeout= is never optional.
Rate limits as economics
The point of a limiter isn't "stop abuse" — it's making the attacker's cost exceed yours. Token bucket per account and per IP — limit_req in nginx, express-rate-limit, Redis-backed counters — with distinct, tighter limits on the expensive routes: login (bcrypt is slow on purpose), reset, search, export. Per-route limits also isolate blast radius — a hammered search endpoint shouldn't starve login. Return 429 with Retry-After — the ugly alternative is a worker pool held hostage.
And remember X-Forwarded-For is attacker text: derive client IP from your proxy's trusted hop, not the raw header, or your limiter throttles a fiction.
Practice
Timeouts and size caps at every layer; linear-time or timeout-bounded regex; bounded queries and bounded decompression; tight limits on expensive routes; adversarial-input tests in the suite.
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
RSA Signatures: The Version in Most Tutorials Is Forgeable
Signing is encryption with the keys swapped, and that symmetry is exactly what makes the naive implementation breakable. Here are two forgeries executed against textbook RSA signatures, and what PSS does that hashing alone cannot.
Ask Java for AES and You Get ECB. Nobody Warns You.
Cipher.getInstance("AES") compiles, runs, encrypts, decrypts, and hands you the weakest mode in the box. Here is the OpenJDK source that makes that decision, and the line where ECB gets chosen for you.