A Cloudflare WAF custom rule is the unit you reach for when a Managed Rule does not match what you actually want to block. This article is for operators who already have a Cloudflare zone in front of an origin and need to write a rule that matches an attack pattern, picks the right action, and gets deployed without locking out real users. We will cover the expression language, the five actions, how the rule list short-circuits, and the staged rollout that has saved us from every “we blocked our own checkout” call.
How to verify
List the custom rules already attached to a zone:
curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \
-H "Authorization: Bearer $CF_API_TOKEN" \
| jq '.result[] | select(.phase=="http_request_firewall_custom")'
Then read the rules inside that ruleset:
curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
-H "Authorization: Bearer $CF_API_TOKEN" \
| jq '.result.rules[] | {description, expression, action}'
In the dashboard, the same view is Security → WAF → Custom rules. A rule in Log mode shows up in Security → Events with action: log, which is how you confirm a new rule is seeing the traffic you expect before you flip it to Block.
What’s happening
Cloudflare evaluates an inbound HTTP request through a fixed sequence of phases: DDoS protection, then Managed Rules, then your custom rules, then rate limiting, then transforms, then origin selection. Inside the custom rules phase, rules execute top-to-bottom and the first one that matches with a terminal action (Block, Challenge, JS Challenge, Managed Challenge) wins — the request stops being evaluated and the chosen action is applied. Log and Skip are non-terminal: Log records the match in Security Events and continues; Skip jumps over later WAF features for that specific request.
This is why ordering matters. A broad block at position 1 will swallow requests that a more specific allow-or-log rule at position 5 was supposed to catch. The model is: put narrow exceptions first (Skip rules for known good traffic, allowlists for your own webhooks), then specific blocks, then broad catch-alls last.
The procedure
-
Write the expression in the rule builder, not in your head. Open Security → WAF → Custom rules → Create rule. Use the visual builder to pick fields and operators; the engine generates the expression below. Typical fields:
ip.src,ip.src.country,http.host,http.request.uri.path,http.request.method,http.user_agent,cf.client.bot,cf.threat_score,http.request.headers["x-forwarded-for"][0]. Combine withand/orand parentheses. -
Pick the action with intent. Five choices, each with a specific use:
- Block — terminal, returns a 1020 error page (or your custom block page). Use when you are certain the match is malicious.
- Managed Challenge — terminal, Cloudflare picks the right challenge for the visitor (often invisible to humans, blocking to bots). This is the default for “probably bad but I want a human to be able to retry”.
- JS Challenge — terminal, runs a JavaScript verification. Lower confidence than Managed Challenge; mostly useful when you know the client supports JS.
- Log — non-terminal, emits an event in Security → Events with the rule ID. Use during the dry-run phase of any new rule.
- Skip — non-terminal with a choice of what to skip: all subsequent custom rules, specific phases (Rate Limiting, Managed Rules, Zone Lockdown), or all WAF features. Use for known-good traffic that would otherwise trip a downstream Block.
-
Order the rule. Drag it into position in the dashboard list, or set
positionin the API payload. The conventional layout we use is:Skiprules at the top (allowlist), then narrowBlockrules, thenManaged Challengerules, then broad catch-alls. -
Roll out in three stages. Create the rule with action
Log. Wait at least one business day. Check Security → Events filtered by your rule ID — confirm the volume looks right and that no traffic you care about is matching. Then flip the action toManaged Challenge. Watch the event stream for solve rates. Only after that, if appropriate, flip toBlock. -
Export and version the rule. API rules are the source of truth. Pull the ruleset JSON into a file, commit it, and apply changes via
PUT /rulesets/$RULESET_IDso the rule list is reviewable like any other piece of infrastructure.
Example expression for a credential-stuffing pattern — POST to login from a known-bad ASN, no Referer:
(http.request.method eq "POST"
and http.request.uri.path eq "/wp-login.php"
and ip.src.asnum in {12345 67890 13335}
and not any(http.request.headers["referer"][*] != ""))
Wrap it with action Managed Challenge first, watch the Security Events, then promote to Block once you have evidence the false-positive rate is acceptable.
Common pitfalls
- A Block rule at the top of the list will fire before any Skip rule below it — Skip only skips rules after itself, not rules above.
cf.client.botis true for verified good bots (Googlebot, Bingbot) — blocking oncf.client.bot eq truewill deindex you in a day.- Custom rules run after Managed Rules; if a Managed Rule already blocked the request, your custom Log rule will never see it.
- The 1020 block page is bare; if you want a friendly explanation, configure a custom error page under Rules → Custom Error Pages.
- Skip rules with a specific phase target only skip that phase — a Skip to “all WAF managed rules” still lets your custom Block rules fire.
In the engagements we run on Cloudflare, the WAF rule list is treated as code: every rule has a description that explains the matched threat, a Log-first rollout, and a periodic review when the threat surface shifts. That discipline is part of what we operate for clients under Managed Operations.