Rule expressions

The language a custom firewall rule uses to decide which requests it applies to.

A custom rule decides which requests it applies to with an expression. Build it with the rule builder, or open a rule and choose Edit expression to type it.

The shape of an expression

(condition) and (condition) and (condition)
(condition) or (condition)
  • Each condition is one test. Parentheses may be left out for a single condition.
  • Conditions are joined by and (all must match) or or (any may match).
  • A rule uses one joiner. a and b or c is refused. Split it into two rules.
  • At most 10 conditions and 4000 characters.
  • Keywords and operators are not case sensitive. Whitespace and line breaks are free.

A condition

field operator value
(ip.country in {"RU" "BY"})
(http.request.uri.path starts_with "/admin")
(not http.user_agent contains "Mozilla")
(client.bot)

Values

Kind Written as Example
Text in double quotes; escape a quote or backslash with \ "/wp-login.php"
List in braces, separated by spaces {"RU" "KP"}
Boolean true or false, only for client.bot client.bot == false

Lists hold up to 1000 values. A text value is up to 256 characters and a regular expression up to 512.

Fields

Field Matches
ip.country the visitor’s two-letter country code, in capitals
ip.src the visitor’s IP address (IPv4 or IPv6)
http.request.uri.path the path, without the query string, decoded
http.request.uri.query the query string, decoded
http.user_agent the User-Agent header
http.request.method the HTTP method in capitals
http.host the host name the visitor asked for
http.referer the Referer header
http.request.headers["name"] any request header, by name
client.bot true when the user agent looks like an automated client

Note The headers cookie, authorization, proxy-authorization and set-cookie cannot be used, because they carry credentials.

Paths and query strings are decoded twice and compared in lower case, so %2e%2e/ and %252e%252e/ both match ../.

Operators

Operator Meaning
eq, == equals
ne, != does not equal
contains contains the text
not contains does not contain the text
starts_with begins with
ends_with ends with
matches matches a regular expression
in, not in is, or is not, one of a list

not can go in front of the whole condition or between the field and the operator; these are the same:

(not http.request.uri.path contains "/ok")
(http.request.uri.path not contains "/ok")

Regular expressions

matches takes an RE2 pattern. It runs in linear time, so no pattern can slow the edge down. Backreferences and look-around are not available.

(http.request.uri.path matches "^/(wp-admin|wp-login)")
(http.user_agent matches "python-requests|go-http-client|scrapy")

IP addresses and ranges

ip.src accepts single addresses and CIDR ranges in the same list, IPv4 and IPv6.

(ip.src in {203.0.113.7 198.51.100.0/24 2001:db8::/32})

Headers

Compare a header with the empty text to test whether it is present:

(http.request.headers["x-api-key"] ne "")    the header is present
(http.request.headers["x-api-key"] eq "")    the header is missing

Examples

Block two countries:

(ip.country in {"RU" "BY"})

Allow only these countries:

(not ip.country in {"AU" "NZ" "US"})

Block an admin path for bots only:

(http.request.uri.path starts_with "/admin") and (client.bot)