# Firewall

> Block, challenge, redirect or rate-limit requests before they reach your app, with custom rules, IP blocks, attack mode and always-on DDoS mitigations. Changes apply in about a second, without a redeploy.

- Category: Security and team
- URL: https://www.forkrail.com/docs/firewall

## How it works

Every project has a firewall in front of all its deployments and domains, and the install has one in front of every project. It runs in the routers before anything else: before deployment protection, redirects, files and functions.

1. **System mitigations**, always on, refuse floods from a single address.
2. The **install’s firewall**, which owners manage, runs for every project.
3. **Attack mode**, when it’s on, challenges every visitor.
4. The project’s **IP blocks**, then its **custom rules**, in order.

Every change is a draft until you publish it. Publishing reaches every router within about a second, every version is kept, and any version can be made live again. The project’s **Firewall** page has tabs for Overview, Traffic, Blocked, DDoS and Rule Settings.

## Custom rules

A rule matches requests by conditions, then takes an action. From the CLI, a condition is `[not] TYPE[:KEY] OP VALUE`; join them with `--and` (all must match) or `--or` (either group).

```bash
forkrail firewall rule add my-api --name "Block .env probes" --if 'path suf .env' --then deny --for 1h
forkrail firewall rule add my-api --name "Admin from outside the US" \
  --if 'path pre /admin' --and 'not country eq US' --then deny
forkrail firewall rule add my-api --name "Old page" --if 'path eq /old' --then redirect --location /new --permanent
forkrail firewall rule add my-api --name "Scrapers" --if 'ua sub python-requests' --or 'asn inc 14061,16276' --then log
forkrail firewall rule add my-app --name "Login page" --if 'path eq /login' --then challenge
forkrail firewall diff my-api       # what publishing would change
forkrail firewall publish my-api    # or: discard, versions, rollback VERSION
```

### What a rule can match

| Type | Matches |
| --- | --- |
| `path`, `raw_path`, `target_path` | The decoded path, the path as sent, or the file a request is served from. |
| `method`, `host`, `protocol`, `scheme` | The request line and host. |
| `header:NAME`, `query:NAME`, `cookie:NAME` | A header, query parameter or cookie by name. |
| `user_agent` (or `ua`) | The client’s user agent. |
| `ip_address` (or `ip`) | An address or CIDR range. |
| `geo_country` (`country`), `geo_continent`, `geo_country_region`, `geo_city` | Where the client is. |
| `geo_as_number` (`asn`) | The client’s network, such as a hosting provider. |
| `ja4_digest` (`ja4`) | The TLS client’s JA4 fingerprint: browsers, script libraries and scanners each have their own, whatever user agent they claim. |
| `environment` (`env`) | `production` or `preview`. |
| `region` | The router’s AWS region. |

Operators: `eq`, `neq`, `pre` (starts with), `suf` (ends with), `sub` (contains), `re` (regex), `inc` and `ninc` (is or isn’t one of up to 75 values), `ex` and `nex` (exists or not), `gt`, `gte`, `lt`, `lte`, and `list` (in a named IP list). Every comparison ignores case.

### Actions

| Action | What the visitor gets |
| --- | --- |
| `deny` | 403, with `x-forkrail-mitigated: deny`. |
| `challenge` | A page that runs a moment of JavaScript, then reloads with a pass good for an hour. For browsers only: API clients and `curl` stay at the page. |
| `redirect` | 307, or 301 with `--permanent`, to `--location`. |
| `rate_limit` | See [rate limits](#rate-limits). |
| `bypass` | Skips the remaining rules. |
| `log` | Nothing: the request is recorded and goes on. |

A deny or challenge can also hold the client’s address for 1 minute to 1 hour (`--for 15m`), so every request it sends gets the same answer until then.

## Rate limits

A rate limit allows a number of requests per window for each client, and answers the rest with 429 (`retry-after` included), 403 or just a log line.

```bash
# 10 login attempts a minute per IP; over that, denied for 15 minutes
forkrail firewall rule add my-api --name "Login" --if 'path eq /api/login' \
  --then rate_limit --requests 10 --window 60 --over deny --for 15m

# 1,000 requests a minute per API key, with bursts
forkrail firewall rule add my-api --name "API keys" --if 'path pre /api' \
  --then rate_limit --requests 1000 --window 60 --key header:x-api-key --algo token_bucket
```

- `--window`: 10 to 3,600 seconds (default 60). `--requests`: 1 to 10,000,000.
- `--key`: what a client is: `ip` (an IPv6 address counts by its /64), `ja4`, `header:NAME`, `user_agent`, `asn`, `cookie:NAME`, `query:NAME`, `path`, `host` or `country`. Several keys count each combination apart.
- `--algo`: `fixed_window` (windows start on the clock) or `token_bucket` (bursts up to the limit, then refills).
- Counts are each router’s own, as Vercel’s are each region’s.

### Rate limits from your own code

Your app can ask the firewall about its own keys, such as a user id. `checkRateLimit` from `@vercel/firewall` works unchanged: make a rule with a `rate_limit_api_id` condition, and call it with the request.

```bash
forkrail firewall rule add my-api --name "Updates per user" --if 'rate_limit_api_id eq update-object' \
  --then rate_limit --requests 20 --window 60
```

```ts
import { checkRateLimit } from '@vercel/firewall'

export async function POST(request: Request) {
  const { rateLimited } = await checkRateLimit('update-object', { request, rateLimitKey: userId })
  if (rateLimited) return new Response('Too many requests', { status: 429 })
  // ...
}
```

## IP blocks

IP blocks run before the custom rules: an address or range, on one host, every subdomain (`*.example.com`) or every host of the project, with `deny`, `log` or `bypass`.

```bash
forkrail firewall ip add my-api 192.0.2.0/24 --host api.example.com --note "abuse report 42"
forkrail firewall show my-api    # rules, IP blocks and their ids
```

## Attack mode

Under attack, turn on attack mode from the Firewall page’s button or the CLI. It challenges every visitor to the project for 1, 6 or 24 hours, and applies at once.

```bash
forkrail firewall attack-mode my-app --for 6h    # --off to stop
```

Cron jobs, verified crawlers (Google, Bing, Apple, DuckDuckGo and OpenAI, checked by their published addresses), Stripe’s and GitHub’s webhooks, and your system bypasses pass without a challenge.

## DDoS mitigations

Always on, in front of every project, at no cost:

- An address may hold at most 256 connections open.
- An address that sends more than 5,000 requests in 10 seconds is blocked for 10 minutes. Its new connections are closed before the TLS handshake.

A **system bypass** leaves an address you trust alone, such as a partner’s servers (up to 100). Owners can change the limits for the whole install, or pause the mitigations for 24 hours.

```bash
forkrail firewall system-bypass add my-api 203.0.113.0/24 --note "partner's servers"
forkrail firewall limits --connections 1024 --requests 20000 --block 5m   # owners; --reset for the defaults
forkrail firewall mitigations my-api
```

## Test, import and limits

Test what the firewall would do with a request before you publish:

```bash
forkrail firewall test my-api https://api.example.com/admin --ip 192.0.2.7 --country DE
forkrail firewall import my-api vercel-firewall.json    # a configuration exported from Vercel
```

| What | forkrail |
| --- | --- |
| Rules per configuration | 1,000, of which 100 can be rate limits |
| Values in one `inc` condition | 75 |
| System bypasses | 100 |
| Time to apply a change | About a second, on every router |
| Price | Included. Rate limiting, challenges and attack mode cost nothing extra. |

Anyone in the team can look and test. Members and owners change a project’s firewall; owners change the install’s (`--install`). Each request’s log line records what the firewall did and which rule decided.
