Track 08 · Lesson 17 of 20Attacks and defenses

Track 08 · Attacks and defenses

Rate limiting

Lesson 17 of 20· 6 min

Rate limiting allows each client only so many requests per window, like five login attempts a minute. The key that defines “each client” is everything: if the attacker can change it, the limit vanishes.

Forty password guesses

Change the attacker and the limiter’s key

The attacker
The limiter counts per

✕ reached the password check · ✓ refused with 429 · – account locked

Guesses that reached the password check

0

Keys in the limiter’s memory

0

Choosing the key#

  • The trusted client IP, from the hop you control (the last lesson), stops one machine.
  • Per account, on top of per IP, stops many machines guessing one password. Lock or slow down after a few failures, tell the owner, and offer a reset rather than a permanent lockout.
  • Never a value the client typed, like the first X-Forwarded-For entry.

Limiters have memory too#

An in-memory limiter keeps a counter per key. If expired keys are never swept, an attacker who invents a new key per request fills your server’s memory. Sweep every window, cap the number of keys, and use a shared store like Redis when you run more than one instance.

Not just logins#

Anything expensive for you and cheap for the caller deserves a limit: password reset emails, one-time-code checks, link previews, search, exports and AI calls.

In a code review

MediumApp

Rate limiter keyed on a spoofable value

Fix: Key on the trusted client IP or user ID; sweep every window; cap the map; prefer a shared store (Redis) when running several instances.

0 of 2 checked

Check yourself

Twenty real IPs each make two password guesses against one account. Which limit catches it?