A Cloudflare WAF custom rule meant to block two scraper ASNs on /search and /api/search blocked every visitor to /api/search; why, and how do you fix it?
answer
- and versus or
- which operator binds first
- parentheses or one set
- case-sensitive exact matches
basics
~10 sIn Cloudflare's Rules language and binds tighter than or, so ASN and path1 or path2 meant (ASN and path1) or path2, matching everyone on /api/search. Group the paths in parentheses or an inline set.
solid answer
~40 sThe expression was `ip.src.asnum in {64496 64500} and http.request.uri.path eq "/search" or http.request.uri.path eq "/api/search"`. The Rules language evaluates `not`, then `and`, then `xor`, then `or`. That made it `(ASN and /search) or /api/search`, and the second branch has no network condition. The fix is `ip.src.asnum in {64496 64500} and http.request.uri.path in {"/search" "/api/search"}`, or explicit parentheses around the two path tests. Then I check what the corrected rule still misses. `eq` is exact and case-sensitive, so `/API/search` or `/search/` slips past if the origin serves those too. URL normalization decodes percent-encoded letters and merges slashes, but it does not lowercase. If those variants matter, compare `lower(http.request.uri.path)`. I would confirm the fix in Security Events before trusting it again.
code
plaintext · 5 lines# Wrong: means (ASN and /search) or /api/search
ip.src.asnum in {64496 64500} and http.request.uri.path eq "/search" or http.request.uri.path eq "/api/search"
# Right: the network condition applies to both paths
ip.src.asnum in {64496 64500} and http.request.uri.path in {"/search" "/api/search"}go deeper
Recall that and binds tighter than or in Cloudflare expressions and that string comparisons there are case-sensitive.
Explain how the incident expression was parsed and the two fixes, explicit parentheses or an inline set of paths.
Show the whole diagnosis: reading Security Events to see the spread of matched networks, fixing precedence, then checking case and trailing-slash variants against what the origin serves.
Push for review rules that catch this class of bug: mandatory parentheses for mixed operators, rule names stating intent, and a trial before Block.
## What happened The operations team wanted a scraper's two networks blocked on two search endpoints and wrote: ```plaintext ip.src.asnum in {64496 64500} and http.request.uri.path eq "/search" or http.request.uri.path eq "/api/search" ``` Read left to right, it looks like "these networks, on either path". The **Rules language** does not read it that way. Logical operators have a fixed **order of precedence**: | Precedence | English | C-like | |---|---|---| | 1 | `not` | `!` | | 2 | `and` | `&&` | | 3 | `xor` | `^^` | | 4 | `or` | `\|\|` | Because `and` is evaluated before `or`, the expression means `(ASN and /search) or /api/search`. The right-hand branch has no network condition at all, so **every** request for `/api/search` matched and was blocked, from every visitor. ## Fix 1: say the grouping Either write the parentheses: ```plaintext ip.src.asnum in {64496 64500} and (http.request.uri.path eq "/search" or http.request.uri.path eq "/api/search") ``` or, simpler, put the paths in an **inline set**, since `in` works on strings as well as numbers and addresses: ```plaintext ip.src.asnum in {64496 64500} and http.request.uri.path in {"/search" "/api/search"} ``` Nested parentheses and function calls need the **Expression Editor**; the Expression Builder supports neither. An expression this shape is easy to flatten by accident when rebuilding it there. ## Fix 2: match every spelling the origin serves With precedence fixed, the rule can still be **too narrow**. String comparisons are **case-sensitive**, and `eq` compares the whole value: - `/Search` or `/API/search` does not equal `/search`. If the origin routes paths case-insensitively, the scraper still gets results. - `/search/` with a trailing slash is a different string. - **URL normalization**, when it is on, helps only partly. It decodes percent-encoded unreserved characters (`/%73earch` becomes `/search`), merges repeated slashes and, in Cloudflare mode, turns backslashes into slashes. It never lowercases a path or removes a trailing slash. Options, by plan: 1. `lower(http.request.uri.path) eq "/search"`, on any plan, catches every letter case; `lower()` converts ASCII uppercase only. 2. `wildcard` is case-insensitive, but it matches the whole value against a pattern, so `"/search*"` also matches `/searches`. 3. `matches` with a regular expression is exact and flexible, but needs Business or Enterprise. Match only the variants your origin actually answers. A rule that tries to anticipate every string grows unreadable. ## Checking the fix before it bites again - In **Security Events**, filter by the rule and look at the top paths and ASNs it matched. During the incident, `/api/search` from many unrelated networks gave the bug away. - Redeploy the corrected rule with **Managed Challenge** first, or **Log** on Enterprise, and switch back to Block once the matches are only the two networks. - Validate syntax with the dashboard's automatic checks or the API's dry run, keeping in mind that a valid expression can still be the wrong one. ## Why the rule deployed without complaint Nothing in the platform could have caught this bug at save time, and it is worth saying why in a review: - The expression is **syntactically valid**. Precedence resolves the mix of `and` and `or`, so no error is raised. - The dashboard's automatic validation and the API's `dry_run=true` check syntax, action parameters, permissions and quotas; they never compare an expression with its author's intent. - The rule worked for the original path: scraper requests to `/search` were blocked, so a quick spot check of that endpoint looked fine. Only evidence from real traffic shows the problem: a trial with a softer action, or Security Events after deployment. That is why a mixed-operator rule deserves parentheses even when precedence would give the right answer, and why the review question is "which requests does this match?", not "does this save?". ## Habits for precise expressions - Always parenthesise a mix of `and` and `or`, even where precedence would happen to give the right answer. - Prefer one inline set to several `or`-joined equality tests. - Keep the *who* condition (network, address, country) outside the group of *what* conditions (paths, methods), so it applies to all of them. - Name the rule after its intent so a reviewer can compare the expression with what it claims to do.
- Why does URL normalization not solve the case problem?Normalization standardises encoding, not letter case. It percent-decodes unreserved characters, uppercases the remaining percent-encodings, removes dot segments and merges repeated slashes; in Cloudflare mode it also turns backslashes into slashes. A request for `/Search` stays `/Search`. If the origin treats it as `/search`, the rule needs `lower(http.request.uri.path)` or a case-insensitive operator.
- How would Security Events have shown the bug quickly?Filtered to the rule, Top events by source would list matches from a wide spread of ASNs and countries on `/api/search`, instead of only the two scraper networks. A rule meant for two networks that matches hundreds of them is matching on the path alone, which points straight at the missing grouping.
- The Rules language evaluates and and or strictly from left to right.
- The ip.src.asnum condition at the start of an expression applies to every later clause.
- URL normalization lowercases the path, so eq "/search" also catches /Search.
- wildcard "/search*" matches only the /search endpoint, in any case.
- An expression that saves without an error must match what was intended.