jt
Back to the blog

Turning off Vercel Bot Protection for some environments

Vercel's Bot Protection is all or nothing per project, and the firewall can't target custom environments. One bypass rule on the right hostnames gets you per-environment bot protection anyway.

  • vercel
  • security
  • firewall

On a project at work I wanted Vercel's Bot Protection on for some environments and off for others. That sounds like a checkbox. It isn't, and it took a bit of digging to find a clean way to do it.

Turning off Vercel Bot Protection for some environments: one bypass rule, on the right hostnames.

Two walls

Bot Protection covers the whole project. It's a managed ruleset in the Vercel Firewall. You switch it on, pick an action (log or challenge), and it applies to every request the project receives. There's no field for "only production" or "not on staging".

The firewall doesn't know about custom environments. Custom rules do have an environment condition, so my first idea was a rule that matches the environments I wanted to skip. But the condition only lists the standard environments. The custom environments I had created weren't offered, so I couldn't target them.

So I couldn't scope the managed ruleset, and I couldn't name the environments in a rule.

The way around: bypass by hostname

What I could match on was the hostname. Every environment I cared about had its own domain, so the host told me which environment a request was going to.

The firewall also has a bypass action, and it does what the name says: a request that matches a bypass rule skips the rules after it, including the managed rulesets. Bot Protection is a managed ruleset.

Put those two together:

  1. Leave Bot Protection on for the project.
  2. Add one custom rule, action bypass, condition "host is one of" the domains of the environments that shouldn't be challenged.
  3. Make it the last custom rule.

Requests to those hosts go through every rule above the bypass, then skip Bot Protection. Every other host still gets challenged.

Rule order is priority. Custom rules run first, then the bypass rule on the listed hosts, then Bot Protection. Listed hosts skip Bot Protection, every other host still gets challenged.

In the Vercel dashboard, it takes a minute:

  1. Open the project, go to Firewall, and make sure Bot Protection is on.
  2. Add a custom rule. Condition: Hostname is any of staging.example.com, qa.example.com. Action: Bypass.
  3. Drag the rule to the bottom of the custom rules list and publish.

Why it has to be last

Rule order is evaluation order. A bypass stops evaluation for the request, so any rule below it never runs for those hosts. Put it at the top and you haven't only turned off Bot Protection, you've turned off your own deny and rate-limit rules too.

I checked this before trusting it, in two steps on a test project:

  • Control. Bot Protection on, plus a deny rule on a test path, no bypass. The test host got the challenge page, and the test path was denied.
  • With the bypass. Same setup plus the bypass rule as the last rule. The test host loaded without a challenge, and the test path was still denied.

So the rules above the bypass keep working, and only what comes after it is skipped.

Things to keep in mind

  • Only the hosts you list are affected. Auto-generated deployment URLs don't match unless you add them, so they stay behind Bot Protection. That's usually what you want.
  • A bypassed host has no bot protection at all. Use this for hosts that don't need it, not for hosts you'd simply like to be less annoying.
  • It's a list, so keep it up to date. When an environment gets a new domain, the rule has to change too, or that domain starts getting challenged.

If you need Bot Protection on some environments of a Vercel project and off on others, this is the way I'd do it: one bypass rule on the right hostnames, at the bottom of the list.