Skip to content

Country blocking on Cloudflare done right: challenge vs block

Geo rules cut attack noise fast, but Block and Managed Challenge are different tools for different reasons. How to write country rules that match your actual goal — and what geolocation can and cannot promise.

Country rules are the cheapest noise filter Cloudflare offers: one WAF custom rule can drop the bulk of scanner and brute-force traffic that your business will never sell to. The decision that matters is the action. Managed Challenge filters out automation while letting a human traveler or expat through; Block refuses everyone, full stop. Pick Block only when the requirement is “must not serve this region” — a requirement that comes from your compliance or legal people, not from this article — and pick Managed Challenge for everything that is really about reducing junk traffic.

What’s happening

At the edge, Cloudflare resolves the visitor’s IP to a country before any of your rules run, and exposes it as ip.src.country (a two-letter ISO code, plus special codes like T1 for Tor exit nodes). The request then flows through the security layers in order — DDoS mitigation, IP Access rules, WAF custom rules, rate limiting, managed rulesets — with bot products (Bot Fight Mode on Free, Super Bot Fight Mode on Pro and Business) judging alongside. You can express a country rule in two places: IP Access rules take country entries and act zone-wide, while WAF custom rules (five on Free, twenty on Pro, a hundred on Business) can combine country with paths, methods, and exceptions. Custom rules are the better default because geography is rarely your whole story — “challenge logins from everywhere we do not operate” is one expression there.

The honesty clause: IP geolocation is a best-effort database lookup. VPNs and proxies relocate anyone in thirty seconds, databases lag real-world IP moves, and mobile carriers sometimes egress a neighbouring country. Geo rules reduce volume; they do not establish where a person legally is, and nothing here is legal advice about where you may or must not do business.

What to check first

  • Why you are blocking. “Cut the scanner noise” and “we are required not to serve region X” lead to different actions and different scopes. Write the reason into the rule description.
  • Where your real users are. Security > Analytics broken down by country for the last month — diaspora customers, staff on trips, and partners on foreign VPNs all live in that data.
  • Where your machines come from. Uptime monitors, payment webhooks, and link-preview bots often call from the US and EU; a broad challenge can catch them, and machines do not solve challenges.
  • Whether the concern is actually data residency. Where data is stored is a hosting-architecture question; a visitor-origin rule does not address it.

How to verify

Confirm what country Cloudflare assigns to a client before trusting rules built on it. Enable the IP geolocation header and read it at the origin, or just reproduce from a foreign vantage point:

# From a VPS or VPN exit in the target country
curl -s -D - -o /tmp/geo-body.html "https://www.example.com/en/"

# Managed Challenge: HTTP 403 + cf-mitigated: challenge
# Block:             HTTP 403, block page, no cf-mitigated header
grep -oE "Cloudflare Ray ID: [a-f0-9]+" /tmp/geo-body.html

Then read the verdicts by geography:

Security > Events
  Filter: Country equals BR (repeat per country in the rule)
  Read:   Action taken (Block vs Managed Challenge vs Challenge Solved),
          Service = Custom rules, your rule name

Challenge Solved events are the interesting ones: every solve is a human your rule almost turned away. Finally, test from a clean IP in a country you did not list to prove the rule is not over-matching.

The fix

  1. Write the goal down first. Fraud and noise reduction means Managed Challenge; a serve/do-not-serve obligation from compliance means Block, because a challenge can be solved and solving it grants access.

  2. Create the rule in WAF custom rules. Security > WAF > Custom rules > Create rule:

    (ip.src.country in {"KP" "IR"} )

    Action Block for the obligation case. Keep the list short and sourced from whoever owns the obligation.

  3. Use Managed Challenge, scoped, for the noise case. Challenge only what attackers want and customers rarely need cross-border:

    (ip.src.country in {"BR" "VN" "PK"} and http.request.uri.path in {"/wp-login.php" "/checkout"} and not cf.client.bot)

    not cf.client.bot spares verified crawlers; Managed Challenge (not JS or Interactive) lets Cloudflare pick the least intrusive test. Action mechanics are in WAF custom rules: block, challenge, log, skip.

  4. Decide about T1 deliberately. Tor is a country code in the rule builder. Challenge it rather than block it unless you have a stated reason; some legitimate privacy-conscious users live there.

  5. Keep API paths out of challenge rules. Machines cannot solve a challenge, so an API consumer in a challenged country is simply down. Use keys, allowlists, or a Block with a documented exception list for API routes.

  6. Review solves weekly for the first month. A high solve rate in one country means real humans — narrow the paths or drop that country from the list.

When this fix is wrong

Country rules are the wrong tool against a targeted attacker, who will be on a clean US VPN before lunch. They are also wrong when the volume you want to stop is a distributed L7 flood — that is what the DDoS layer is for, as covered in DDoS protection: automatic vs tuned — and wrong as a compliance instrument on their own, because IP geography is evidence, not proof, of where a visitor is.

Common mistakes

  • Blocking a country your own uptime monitoring or a partner’s webhook calls from, then debugging “random” failures for a week.
  • Using Interactive Challenge for the worst user experience available when Managed Challenge exists.
  • Blocking zone-wide when the actual risk lives on two paths.
  • Treating the absence of traffic from a blocked country as proof the block “works” — the attackers moved to a VPN; the customers did not.
  • Building the list from a blog post instead of from your own Security > Events and analytics data.

Prevention

Re-justify the list quarterly: pull event counts per listed country, check challenge solve rates, and delete entries nobody can explain. Keep the block list (obligations) and the challenge list (noise) as two separate rules so nobody “tidies” a legal requirement into a UX decision. Field reference for geo expressions is under developers.cloudflare.com/waf/, and header behaviour under developers.cloudflare.com/fundamentals/.

Tuning edge security so it blocks attacks without blocking customers is ongoing work — Stack Harbor watches those events around the clock under 24/7 Monitoring & Response; book a monitoring and response review.