Cookie Settings

Choose which cookies to allow.

Full list in our Privacy Policy.

Best Cloudflare WAF Rules: 8 Custom Rules and the Plan Each One Needs

Four WAF custom rules in order, each with its expression and action, the verified bots Skip rule first

The best Cloudflare WAF rules, the custom rules of Cloudflare’s web application firewall, match on signals Cloudflare computes for each request, such as a leaked password, a verified bot or the WAF attack score, and on request fields like the port or user agent. Your plan decides which you can deploy: most of Cloudflare’s own example rules need Enterprise. Below are eight rules that hold up, five of them on every plan, each with its expression, action, false positives and plan. Ask Lelops, the chat in Lelops Advise, writes each rule on your zone once you approve the call it shows, and reads the security events that show what the rule matched.

Which of the Best Cloudflare WAF Rules Work on Your Plan?

A rule works on your plan when every part of it does: each field, the matches operator for regular expressions, which needs Business or above, and the action, since Log is Enterprise only. We checked the 56 example custom rules on Cloudflare’s 26 use case and detection example pages against the plan each field needs in Cloudflare’s fields reference and detection pages. 17 run on every plan; 34 need Enterprise, a paid add-on such as Bot Management, or early access. Free also caps a zone at five custom rules, which the five rules below fill.

Best Cloudflare WAF Rules That Run on Every Plan

Verified bots leave the rule list at the Skip rule, placed first

  1. Let verified bots skip the rules below (every plan). Expression (cf.client.bot), action Skip with All remaining custom rules, placed first (Cloudflare’s guide). The cf.client.bot field states no plan; its Bot Management twin, cf.bot_management.verified_bot, needs Enterprise. What it lets through by mistake: no rule below it applies to verified bots, so a rule meant for crawlers goes above it (why order decides).
  2. Challenge requests that carry a leaked password (every plan). Expression (cf.waf.credential_check.password_leaked), action Managed Challenge; on Pro and above, (cf.waf.credential_check.username_and_password_leaked) matches a leaked username and password pair (Cloudflare’s example). The detection is on by default on Free; elsewhere, turn on Leaked credential detection under Security > Settings. On Free, Pro and Business, Cloudflare only looks for passwords where common apps and login forms send them, such as WordPress or a generic login form; pointing it at your own login fields needs Enterprise (leaked credentials), so a custom login may never match. What it catches by mistake: a real user who reuses a leaked password, a case Cloudflare names; the challenge lets that person through.
  3. Challenge login posts with no user agent (every plan). Expression (http.request.uri.path eq "/login" and http.request.method eq "POST" and len(http.user_agent) eq 0) with your login path, action Managed Challenge, from the same Cloudflare guide. What it catches by mistake: your own clients that send no User-Agent header, which RFC 9110 allows: a client SHOULD send one, not MUST. On Pro and above, the Cloudflare Managed Ruleset has an empty user agent rule, off by default, that covers the zone without a custom rule.
  4. Block ports other than 80 and 443 (every plan). Cloudflare proxies 13 HTTP and HTTPS ports by default, 8080 and 8443 among them. Expression (http.host eq "www.example.com" and not cf.edge.server_port in {80 443}), action Block (Cloudflare’s example). What it catches by mistake: a service you publish on another of those ports; add it to the set.
  5. Block Worker subrequests from other zones (every plan). The cf.worker.upstream_zone field holds the zone whose Cloudflare Worker sent the request, and is empty for direct visitors. Expression (not cf.worker.upstream_zone in {"" "your-zone.com"}), action Block. Don’t match on the CF-Worker header: Cloudflare adds it after WAF rules run. What it catches by mistake: Workers on your other zones; add those zones to the set.

Best Cloudflare WAF Rules for Business and Enterprise Plans

WAF attack score from 1 to 99: Block covers 1 to 20, Likely attack 21 to 50

  1. Block requests the WAF attack score marks as attacks (Business and Enterprise). The attack score rates each request from 1, almost certainly malicious, to 99, likely clean. Enterprise: (cf.waf.score le 20); Business gets only the class field, (cf.waf.score.class eq "attack"), which covers 1 to 20. Action Block; Free and Pro have no attack score field. What it catches by mistake: Cloudflare warns that the Likely attack class, 21 to 50, can include legitimate requests, so a higher threshold needs a second condition, such as a path.
  2. Block low bot scores outside your API (Enterprise with Bot Management). Expression (cf.bot_management.score lt 30 and not cf.bot_management.verified_bot and not starts_with(http.request.uri.path, "/api")), action Block (Cloudflare’s example). What it catches by mistake: your scripts and integrations outside /api, and mobile app traffic, to which Cloudflare says Bot Management can be more sensitive. Lower plans get Bot Fight Mode or Super Bot Fight Mode, with no bot score for rules.
  3. Block uploads that contain malware (Enterprise with the malicious uploads add-on). Expression (cf.waf.content_scan.has_malicious_obj and http.request.uri.path eq "/upload.php"), action Block (Cloudflare’s example). What it misses: the scanner reads the first 50 MB of a body and reports password-protected files as not scanned; cf.waf.content_scan.truncated and cf.waf.content_scan.has_encrypted_obj catch those in a second rule.

How to Check a Cloudflare WAF Rule Before It Blocks

Start each rule with Managed Challenge, read what it matched in Security Events, then switch it to Block: Cloudflare’s advice for plans without Log, the action that records matches without acting. Before deploying, filter Security Analytics, which shows a sample of all visitor traffic on every plan, on the rule’s conditions (filters vary by plan) and read the paths and user agents in its top statistics. It leaves out Worker subrequests, so rule 5 can’t be previewed there.

A challenge needs a client that renders HTML, so a login posting with JavaScript, or an API client, cannot complete it (Cloudflare): there, Managed Challenge blocks, and its false positives reach users. A copied rule such as cf.threat_score gt 10 matches no request: Cloudflare’s field reference says the score is now always 0.

How to Add the Best Cloudflare WAF Rules with Lelops Advise

Ask Lelops shows the call with its expression and action, waits for your OK, saves the change as a version, then lists the last security events

Lelops Advise is the Ask Lelops chat for teams that run Cloudflare without a WAF specialist: it reads your zone’s rules and traffic, writes custom rules on it and saves each change as a version you can restore.

  1. Connect Cloudflare with a token that can edit WAF rules. When you connect, the Resources & permissions screen lists what your API token allows. Lelops needs read access; for Ask Lelops to write rules, the token must also allow editing the zone’s WAF. You can change the token’s permissions or revoke it at any time.
  2. Check the zone first. Ask “Which Cloudflare plan is example.com on, and which custom rules does it have?” Ask Lelops reads the zone’s details, plan included, and lists its custom rules with their expressions and actions, so you see how many custom rules the zone has left on its plan and whether an old rule tests cf.threat_score.
  3. Ask for the rule. Write “Add a custom rule on example.com that challenges POST requests to /login with no user agent.” Before it calls Cloudflare, Ask Lelops shows the call, with the expression and the action, and waits for your OK. It writes custom rules with Block, Interactive Challenge, Non-Interactive Challenge (formerly JS Challenge), Managed Challenge or Skip, and saves each change as a version. For the Skip rule, ask it to skip all remaining custom rules: that setting shows in the call too.
  4. Create the Skip rule before the others. Ask Lelops adds each new rule at the end of the zone’s custom rules, which is the Cloudflare API’s default. Ask for the verified bots Skip rule first; on a zone that already has block or challenge rules, create it in the Cloudflare dashboard, where Place at sets its position.
  5. Read what the rule matched. Ask “Show the last security events on example.com.” Ask Lelops lists the 50 most recent, each with its action, the rule’s ID, the IP address, the path, the user agent and the country. Ask “Which WAF rules triggered most on example.com today?” for the top 10, each with its count. Both reads cover the last 24 hours, or the time range you name. In Lelops Sentry, the Security tab of Monitoring keeps Top 10 WAF rules triggered on screen for any zone and time range.
  6. Switch to Block, or roll back. When the events show the traffic you meant and nothing else, write “Change that rule’s action to Block.” Ask Lelops shows that call too and saves a new version. If a rule stops real users, Versioning > Restore and deploy brings back the whole zone as it was before the change, its custom rules, DNS records and settings included, so later changes are undone too (what a version holds); restoring needs the Administrator role. To undo only the rule, ask Ask Lelops to delete it or change its action back.

Lelops Advise writes the Cloudflare WAF rules your plan can run, shows each call before it runs and keeps every change as a version: lelops.ai/product/advise.

FAQ

What are the best Cloudflare WAF rules on the Free plan?

Five custom rules run on Cloudflare's Free plan and rely on what Cloudflare checks in each request: a Skip rule for verified bots, a Managed Challenge on requests carrying a leaked password, a Managed Challenge on login posts with no user agent, a block on ports other than 80 and 443, and a block on Worker subrequests from other zones. Free allows five custom rules per zone, so these five fill it. The attack score and bot score fields are not available on Free.

Can the WAF attack score be used on Cloudflare's Free or Pro plan?

No. Cloudflare gives Business plans one attack score field, the attack score class, and Enterprise plans the full score from 1 to 99, with separate scores for SQL injection, cross-site scripting and remote code execution. Free and Pro plans have no attack score field to use in custom rules.

Do Cloudflare's example WAF rules work on every plan?

No. We checked the 56 example custom rules on Cloudflare's use case and detection pages: 17 run on every plan, 3 need Pro, 2 need Business, and 34 need Enterprise, a paid add-on such as Bot Management, or early access. A rule works on a plan only when each of its fields, its operator and its action do: the matches operator for regular expressions needs Business or above, and the Log action needs Enterprise.

How do you test a Cloudflare WAF custom rule without the Log action?

The Log action, which records matches without acting on them, is available on Enterprise plans only. On Free, Pro and Business, Cloudflare recommends starting a new rule with Managed Challenge, reviewing the requests it matched in Security Events, and switching it to Block afterwards. Filtering Security Analytics on the rule's conditions before deploying it shows a sample of the requests it would match.

Do Cloudflare WAF custom rules block Googlebot and other good bots?

They can, since a custom rule acts on every request its expression matches. A first rule with the expression cf.client.bot and the Skip action exempts verified bots, such as search engine crawlers, from the custom rules below it, on every plan. Bot Fight Mode on the Free plan cannot be adjusted with custom rules, while Super Bot Fight Mode on paid plans runs after custom rules and accepts Skip exceptions.

Get started

Connect Cloudflare. See where your edge stands today.

Your first configuration health report, with the fixes to make first, in minutes.

  • No credit card required