After a Cloudflare rate limiting rule keyed on IP went live on a mobile app's login API, thousands of users on one carrier got blocked; why, and how do you fix it?
answer
- many phones, one public address
- one counter per IP per data center
- count failures, not all logins
- a cookie tells NAT users apart
- challenges need a browser
basics
~20 sCarrier-grade NAT puts thousands of phones behind one public IP, so an IP-keyed counter adds all their logins together and can block them all. Count only failed logins, key on IP with NAT support or an identifier, and block with a JSON 429.
solid answer
~50 sThe carrier's subscribers share a few public IPv4 addresses through carrier-grade NAT, and a rule keyed on **IP** keeps one counter per address per data center. Normal logins from thousands of phones cross the threshold together, and the action then hits every matching request from that address in that data center for the full duration. Confirm it in Security Events: a few addresses from one carrier's ASN with many user agents and accounts. Then fix the key: count only `401` and `403` answers with a counting expression; switch to **IP with NAT support** (`cf.unique_visitor_id`), which needs the app's HTTP client to keep the `_cfuvid` cookie; on Enterprise with Advanced Rate Limiting, key on a header or cookie the app sends, with a per-IP ceiling behind it. Also replace Managed Challenge with Block and a JSON `429`, because a native app cannot solve a challenge page.
go deeper
Know that many mobile users share one public IP through carrier NAT, so an IP-keyed limit treats them as one client.
Explain how the counter key, the threshold and the duration combine to block a whole address, and what IP with NAT support and a counting expression change.
Walk through confirming the NAT case in Security Events, choosing characteristics by plan, and swapping a challenge for a JSON block on a native API.
Weigh per-visitor, per-account and per-IP layers against plan cost and false-positive risk, and set who owns thresholds after the incident.
## What happened Mobile carriers commonly put many subscribers behind **carrier-grade NAT**: thousands of phones leave the carrier's network through a small pool of public IPv4 addresses. A Cloudflare rate limiting rule keyed on **IP** keeps one counter per source address in each data center. To the rule, every phone behind one carrier address is a single client. Their ordinary logins add up, the counter crosses the threshold, and the action then applies to **every** matching request from that address in that data center for the whole duration. A rule sized for one attacker can block a city's worth of real users. The same thing happens on office networks, university networks and privacy proxies. The rule is working as configured; the key is wrong for the traffic. ## Confirming it 1. Open **Security Events** in the zone's security analytics, filter on the rate limiting rule, and look at the top sources. A handful of IP addresses from one carrier's ASN, each carrying many different user agents and accounts, points to NAT rather than to an attack. 2. Check the timing: blocks that peak at the busy hour of the app's normal use, instead of at an attack's start and stop, are legitimate load. 3. Compare successful and failed logins from those addresses at the origin. Shared addresses with a normal success rate are users. ## Fixing the key | Change | Plan | What it fixes | What to watch | |---|---|---|---| | Count only failures | Business and up | Successful logins stop moving the counter | Needs an endpoint that returns 401 or 403 on failure | | **IP with NAT support** (`cf.unique_visitor_id`) | Business and up | Separate counters per visitor behind one address | Relies on the `_cfuvid` cookie | | Key on a header or cookie the app sends | Enterprise with Advanced Rate Limiting | Counters per device or session | Values are client-supplied; pair with an IP ceiling | | Per-account limit in a Worker | Wherever Workers run | Counters per account or user named in the request | Runs after the Worker starts | **IP with NAT support** cannot be combined with **IP** in the same rule. It relies on the `_cfuvid` cookie, which Cloudflare sets only for zones whose rules use the characteristic, among other privacy-preserving techniques for telling visitors apart. A client that does not keep and resend cookies is not told apart: all such clients behind one address share **one** counter, which can recreate the false positive. Make sure the app's HTTP client keeps cookies, and for security-critical paths combine the characteristic with **Path** or a header, as Cloudflare advises. ## Fixing the action The rule may also be using the wrong action for a mobile API. **Managed Challenge** and the other challenge actions answer with an HTML challenge page meant for a browser to render and solve. A native app calling a JSON endpoint cannot do that, so users see a generic error and cannot pass. For API paths, use **Block** with a custom response: status `429`, content type `application/json`, and a body the app can show, such as "too many attempts, try again in a few minutes". Keep challenges for paths that browsers load. ## Re-sizing the threshold - On Enterprise, use **Request rate analysis** in Security Analytics to see request rates per client and move the threshold until normal peaks pass and attack traffic does not, then deploy with the **Log** action before enforcing. - On other plans, start loose and tighten from the rule's events. - Remember that counters are per data center: a carrier's users are concentrated in the locations nearest them, which is exactly where the shared counter fills fastest. ## A layered result 1. A per-visitor rule (IP with NAT support plus the login path) counting only `401` and `403` answers, with Block and a JSON 429. 2. A high per-IP ceiling on the login path, well above any carrier address's honest peak, so a single address flooding attempts is still stopped. 3. A per-account limit in code where the account is only known after parsing the body, so one account cannot be attacked from many addresses. ## What not to do - Raising the threshold for everyone lets a real attacker through at the same time as the carrier's users. - Switching the key to country or ASN makes the problem worse: a whole carrier then shares one counter. - Lengthening the duration only keeps blocked users out for longer. - Allow-listing the carrier's addresses removes protection from exactly the addresses an attacker on that carrier would use.
- Why can IP with NAT support still block a whole carrier address?It tells visitors apart with the `_cfuvid` cookie Cloudflare sets for the rule. Clients that clear cookies, browse privately or never store cookies are not told apart, and all of them behind one address share a single counter. If the app's HTTP client drops cookies, the rule behaves much like plain IP keying. Ensure the client keeps cookies, and combine the characteristic with Path or a header.
- How do you tell a NAT false positive from a real credential-stuffing run in Cloudflare's analytics?In Security Events, filter on the rule and read the top sources. NAT shows a few addresses from one carrier ASN carrying many user agents and accounts, peaking at the app's normal busy hours, with a normal login success rate at the origin. Stuffing shows a near-zero success rate, uniform clients and sudden start and stop times, often spread over many addresses.
It is like a doorman who lets each street address in five times an hour and then turns away a whole apartment tower because every resident shares one street number. The fix is to ask for the flat number, not to fire the doorman.
- Each phone has its own public IP, so an IP key counts one user per counter.
- Managed Challenge is the safest action for a native app's JSON login API.
- Raising the mitigation timeout reduces how many carrier users are blocked.
- IP with NAT support can be combined with IP in the same rule.
- IP with NAT support tells apart clients that never store cookies.