Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 36 ยท Security: WAF and DDoS

Cloudflare Rate Limiting Rules: Stop Brute Force and Abuse

Configure Cloudflare rate limiting to stop login brute force, scraping and API abuse: match expression, characteristics, period, threshold, action and mitigation timeout.

Some abuse looks perfectly legitimate request by request. One login attempt is normal; four hundred per minute from one address is a password-guessing attack. One page view is normal; ten thousand is a scraper. A rate limiting rule counts matching requests over a window of time and reacts when a threshold is crossed. You find it in Security > WAF > Rate limiting rules.

The ingredients of a rule

Every rule is built from the same few settings:

Setting Meaning Example
Matching expression Which requests count path equals /login and method is POST
Characteristics What counts as "the same client" IP address (default), or IP plus a header such as an API key, or a cookie
Period The time window 10 seconds, 1 minute, 10 minutes... (choices depend on plan)
Requests threshold How many requests are allowed in the period 5
Action What to do when the threshold is passed Block, Managed Challenge, JS Challenge, Log
Mitigation timeout How long the action lasts after being triggered 10 minutes
Response For Block: status code and body (JSON for APIs) 429 with a JSON message

The simplest useful rule: "more than 5 POST /login requests per minute from one IP are blocked for 10 minutes".

flowchart LR
  R["Request"] --> M{"Matches expression?"}
  M -- "no" --> P["Pass"]
  M -- "yes" --> C["Increment counter for this client in the window"]
  C --> T{"Above threshold?"}
  T -- "no" --> P
  T -- "yes" --> A["Action for the mitigation timeout"]

Only matching requests are counted, and each client has its own counter.

Choosing characteristics

By default a client is its IP address. That breaks down in two ways:

  • Many users behind one IP (an office, a university, a mobile carrier NAT) share a counter, so one noisy user can block everyone.
  • Attackers rotating through many IPs each stay below the limit.

Higher plans let you count by other characteristics such as an API key header, a session cookie, a JWT claim or a header value, or to combine them with the IP. For an API, counting per API key is far fairer than counting per IP. For a login form, counting by username (when you can read it from the request) stops one account being guessed from a botnet.

Counting methods

Cloudflare counts by fixed periods on the Free plan and offers sliding windows and counting based on responses (for example count only requests that got a 401) on higher plans. A fixed window resets completely at each boundary, so a client can send the maximum just before and just after a boundary and double the effective rate. A sliding window looks back from the current moment and avoids the burst.

JS Stand-in: fixed versus sliding window

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The fixed window lets all ten requests through in about five seconds, while the sliding window blocks the second half of the burst. That difference matters when you set thresholds for expensive endpoints.

A complete rule-based limiter

The next stand-in implements the whole pipeline: match, count per characteristic, compare with the threshold and apply a mitigation timeout.

JS Stand-in: rate limiting rule with mitigation timeout

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The attacker is blocked on the sixth login in a minute and stays blocked until the timeout ends. The normal visitor is never affected, because each client is counted separately.

Design guidelines

  1. Measure before you limit. Look at Security Analytics for realistic request rates per path and choose a threshold with comfortable headroom above normal peaks.
  2. Limit expensive and sensitive paths hardest: login, password reset, signup, search, checkout, any endpoint that sends email or SMS.
  3. Prefer challenges for human-facing pages, blocks for APIs. A Managed Challenge lets a real user continue; an API client cannot solve it, so return 429 with a Retry-After header where possible.
  4. Use a sensible mitigation timeout. Too short and the attack resumes at once; too long and a false positive locks out a customer for hours.
  5. Start with Log. Count what would have been limited, then enforce.
  6. Layer with other tools. Rate limits slow brute force, but a strong password policy, lockouts in your app and multi-factor authentication stop it.
  7. Keep your application's own limits. Cloudflare protects the edge; your code should still defend itself if the proxy is bypassed.

Common mistakes

  • Limiting the whole site at one tiny number, so ordinary browsing triggers it (a single page can fire dozens of asset requests).
  • Forgetting to exclude trusted services, such as payment webhooks and uptime checks.
  • Counting by IP for an API used behind a corporate proxy.
  • Using a long period with a high threshold and expecting it to stop bursts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the 'characteristics' setting define?

    Characteristics decide how requests are grouped into counters.
  2. 2. Why can a fixed window allow a burst larger than the limit?

    A sliding window avoids this boundary effect.
  3. 3. For a public JSON API, a good limit action is:

    API clients cannot solve challenges; a clear 429 tells them to slow down.
  4. 4. What is the mitigation timeout?

    After the trigger, the client keeps getting the action for the timeout.

Summary

  • A rate limiting rule has a matching expression, characteristics, period, threshold, action and mitigation timeout.
  • Count by IP by default, by API key or cookie when your plan allows.
  • Sliding windows resist boundary bursts; fixed windows are simpler.
  • Measure normal traffic first, start in Log mode, and layer rate limits with strong authentication.

Next: DDoS protection and bot management