Bot and firewall rules
Bot and firewall rules let you decide what happens to the requests you describe before your application sees them: block a scanner, ask a suspicious visitor to prove they are a real browser, slow down a script hammering your login page, or simply see how often something happens. This page explains how to write a rule, what each part does, how challenges work and what you can change about them.
What rules do
A rule matches requests by what they are, such as their path, a header or the client address, and then blocks, challenges, slows down, answers, redirects, logs or tags them. Nothing is on by default: until you add a rule, every request goes to your application exactly as before.
Rules run inside your application, alongside web protection, after the blocked clients list and the rate limit. A CDN or firewall you already run in front keeps doing its job; these rules add a view of the request your application actually receives, after your proxies have handled it. They work in applications built on Django, Flask, FastAPI and other Starlette applications, and aiohttp, including an application mounted under a path such as /shop.
You keep rules for your whole account on the Filters page, under Bot and firewall rules, and rules for one machine in its Configure window. A change reaches a running program within about 30 seconds, without a restart. The one exception is the same as for web protection: a program started while every taint source and every web protection setting was off needs one restart the first time.
Write a rule
A rule has a name, a state, an expression saying which requests it matches, an action, and optionally a limit on how often it may match before it acts. Choose Add rule, give it a name, and build the expression.
The builder lets you pick a field, an operator and a value for each condition, and combine conditions with Add condition and Add group. Edit expression shows the same rule as text, which you can type directly; Use the builder goes back. The two stay in step: whatever you change in one is what the other shows. An expression the builder cannot draw, such as a group inside one of those groups, or two functions on one field, is kept as text only, and the window says so.
Every expression is checked as you type, and a mistake is pointed out where it is, with what to change. A few examples:
| Expression | Matches |
|---|---|
http.path starts_with "/wp-admin" |
requests for a path that starts /wp-admin |
http.method eq "POST" and http.path eq "/login" |
sign-in attempts |
ip.src in {203.0.113.0/24 198.51.100.7} |
one range and one address |
lower(http.user_agent) contains "curl" |
a User-Agent containing curl, in any case |
http.path starts_with "/checkout" and not client.challenge_passed |
checkout requests from visitors without a pass |
A new rule starts in Log only, so you can see what it would do before it does it. See Order and Log only.
Fields
Each condition reads one field of the request. Field names are not case-sensitive.
| Field | Type | What it holds |
|---|---|---|
ip.src |
address | The client address, as Who sent the request decides it |
http.method |
text | GET, POST and so on |
http.host |
text | The Host header |
http.path |
text | The path, without the query |
http.query |
text | The query string, without the ? |
http.query_arg["name"] |
text | One query parameter, by name |
http.header["name"] |
text | One request header, by name; the name is not case-sensitive |
http.cookie["name"] |
text | One cookie, by name |
http.user_agent |
text | The User-Agent header |
http.referer |
text | The Referer header |
http.version |
text | HTTP/1.0, HTTP/1.1, HTTP/2 or HTTP/3 |
client.challenge_passed |
yes or no | The client holds a valid challenge pass |
A field the request does not carry, such as a missing header, is empty text. Parameter and cookie names keep their case: http.cookie["Session"] and http.cookie["session"] are two cookies.
Operators and functions
The operators a condition can use depend on the type of its field. Text is written in double quotes; inside it, write \" for a quote and \\ for a backslash. Comparisons are exact and case-sensitive.
| Type | Operators |
|---|---|
| text | eq, ne, contains, starts_with, ends_with, matches, in {"a" "b"}, not in {...} |
| address | eq, ne with one address; in {...} and not in {...} with addresses and ranges such as 10.0.0.0/8 |
| yes or no | on its own (client.challenge_passed), or eq true / eq false |
| number | eq, ne, lt, le, gt, ge, in {...} |
Join conditions with and and or, put not in front of a condition or a group in parentheses, and use parentheses to group. Four functions change a value before it is compared: lower() and upper() change its case, url_decode() decodes %XX escapes, and len() gives its length in characters. They can be combined, as in lower(url_decode(http.path)).
matches searches the text for a regular expression anywhere in it, such as http.user_agent matches "^python-requests/". Start the pattern with (?i) to ignore case. Because the pattern sits inside quotes, a backslash in it is written twice: "\\d+" is the pattern \d+.
Actions
The action is what the rule does with a request it matches. Five of them stop the request before your application sees it:
| Action | What it does | Options |
|---|---|---|
| Block | Refuses the request | Status 400 to 499 (403), a body up to 8 KB, its content type |
| Challenge | Asks the visitor to pass a challenge first | The kind, or your challenge setting |
| Tarpit | Holds the request for some seconds, then refuses it with 429 | 1 to 30 seconds (10) |
| Respond | Answers with your own status and body | Status 200 to 599 (200), a body up to 64 KB, its content type |
| Redirect | Sends the visitor to another address | The address or a path on your site; status 301, 302, 303, 307 or 308 (302) |
Three let the request through:
| Action | What it does |
|---|---|
| Log | Reports the match as an event and changes nothing |
| Skip | Stops the rules after this one; or, with Every later check, also skips web protection's own checks for this request |
| Set header | Adds a request header your application can read, with a value you type or the value of a field. A header the client sent under the same name is replaced, so your code can trust what it reads. It works with Django, Flask and Starlette applications; aiohttp does not let a request's headers change, so there it does nothing |
Every account can switch a rule with one of the first five actions On. Until you are sure of a new rule, keep it in Log only: it reports what it would have done and lets the request through.
A held tarpit request keeps the worker that serves it busy for the whole delay, which matters most under WSGI servers with a fixed number of workers. Each process holds at most 8 at once (tarpit.max_concurrent in the API); past that, a matching request is refused straight away.
Limit how often
With Limit how often, a rule acts only once a client has sent more than a set number of matching requests within a set number of seconds, such as 10 requests in 60 seconds. Below that number the rule matches but does nothing, and reports nothing.
Each client is counted by its address, and each application process keeps its own count, as with the rate limit. If your server runs four processes, a client can reach up to four times the number before every process acts.
A challenge pass resets the client's counts on every challenge rule, so a visitor who passes gets the full number again before being asked once more. A challenge rule without a limit leaves visitors who hold a pass alone.
Counting by address is only as strong as the address. If nothing sits in front of your application, set Who sent the request to The connection address (client = ip), so a client cannot pick a fresh address for every request by sending its own X-Forwarded-For. See Who sent the request.
Order and Log only
A machine runs your account's rules first, in the order you set, and then its own. Every rule that matches reports what it did, and the first one whose action stops the request decides the answer: the rules after it do not run. A Skip rule ends the list where it stands.
A rule is On, Log only or Off. In Log only it reports what it would have done, as <rule name>: would block for example, and lets the request through. Leave a new rule in Log only for a day or two, read its events, and switch it on once it matches only what you meant.
Challenges
A challenge asks a visitor to show they are a real browser, or a person, before the request goes on. A visitor who passes gets a pass and is not asked again until it runs out. Three kinds:
- JavaScript check: the browser solves a short puzzle on its own, in a second or two, with nothing to click. Visitors need JavaScript turned on.
- Interactive (captcha): a check from your captcha provider, using your own keys.
- Managed (the default): es-python picks the kind for you. It currently uses the JavaScript check.
In the challenge settings for your account or for one machine, Challenge sets the kind a Challenge rule uses when the rule does not pick one, and Puzzle strength (12 to 24, 18 by default) sets how much work the JavaScript check asks of each browser. These settings are not part of Machines map policies or templates.
A browser gets a page, with the status 429 Too Many Requests, and is returned to where it was going once it passes, with its query string; a form it was posting comes back as a plain page load. A client that does not ask for HTML, such as an API client, gets a short JSON answer instead, with the puzzle, the verify address and what is needed to solve it, so a well-behaved script can solve it and retry. For your own scripts, it is simpler to put a Skip rule first that matches them, for example by a header only they send.
Challenges, like rate limits, recognise a visitor by the address from Who sent the request. If nothing sits in front of your application, set it to The connection address, so a client cannot choose its own address with X-Forwarded-For.
Each application process keeps its own record of the answers it has accepted, so an answer works only once. Each also keeps its own Limit how often counts, the ones a pass resets, as with rate limits. The pass itself is accepted everywhere your account's rules run.
Answers are sent to /__guard/verify, below the path your application is mounted at, so an application mounted at /shop receives them at /shop/__guard/verify. That path is answered only while a challenge rule is on; otherwise it reaches your application like any other.
The challenge page
The challenge page carries your look and your words, in English or Hebrew. You set it under The page visitors see, in the challenge settings for your account or for one machine:
- Title and message: the heading (up to 120 characters) and the text under it (up to 500).
- Logo address: an
https://address of your logo. - Color: the page's accent color, as
#rrggbb. - Language: English, or Hebrew, which also lays the page out right to left.
- Use my own page HTML (up to 32 KB): replaces the page with yours. It must contain
{{challenge}}exactly once, where the check appears, and no<script>. Style it with a<style>block: inlinestyle=attributes are blocked by the page's security policy, and images load only when a logo address is set, fromhttps://addresses. Set the page's language and direction yourself.
A visitor who passes receives a cookie named __guard_pass, sent only to your site and never readable by page scripts. It lasts 24 hours by default, or anything from 1 to 720 hours with A pass lasts (hours), and it belongs to the visitor's address: a visitor whose address changes is asked again. It is there for security, but you may want to list it in your own cookie notice.
Captcha providers
For interactive challenges, use your own account with one of three providers: Cloudflare Turnstile, Google reCAPTCHA v2 (the "I'm not a robot" checkbox) or hCaptcha. Create a site in the provider's dashboard, add the domain your visitors use, and copy its site key and secret key into the challenge settings.
The secret key is never shown again once saved; the settings say whether one is stored, and you can replace or remove it. Your visitors' answers are checked with the provider directly from your machine, so the machine needs to be able to open HTTPS connections to it.
If the provider cannot be reached, gives no usable answer or turns down your keys, visitors get the built-in JavaScript check instead, so your rules keep protecting the site and nobody is locked out. You are told: a challenge provider unreachable or guard error event says what happened. If the provider refuses a visitor's answer, the visitor is sent back to the captcha.
Events
Rules and challenges report what they do as events on the es-python list, with the source http. A rule match or a challenge is reported at most once a minute for each rule and client, so a busy rule does not flood the list.
| Event | Severity | Means |
|---|---|---|
guard rule |
Info | A rule matched. The detail names the rule and its action, or would and the action in Log only |
challenge issued |
Info | A visitor was asked to pass a challenge, and which kind |
challenge passed |
Info | A visitor passed and got a pass |
challenge failed |
Low | An answer was not accepted: a wrong answer, too late, sent twice, made for another visitor, or turned down by the provider |
challenge provider unreachable |
Low | Your captcha provider could not be reached, so the JavaScript check was used |
guard error |
Low | Part of your rules or challenge settings could not be used as written; the detail says which. That part is skipped and your application keeps working |
Like every event, these work with your rules for events: an event rule set to Raise on guard rule turns a matching Log, Skip or Set header rule that is On into a 403 for that client, for the rest of that minute.
Limits
Rules have a few limits, so that every request stays fast to check:
- 200 rules for your account and 50 for each machine; a name is up to 100 characters.
- An expression is up to 4 KB, with up to 64 conditions and up to 1,024 values in one
{...}set. - A
matchespattern is up to 256 bytes. Back-references, look-arounds and nested repeats such as(a+)+are refused, and so is(?m), because they can make matching slow or mean different things in different tools. A pattern that is still too costly to check is refused with "This pattern is too complex. Split it into several conditions." - The bodies of your block and respond rules add up to at most 1 MB across your account, and your patterns to at most 2 MB once prepared for checking.
- A rate limit is 1 to 1,000,000 requests in 1 to 86,400 seconds.