Skip to Content
ZarexDocs

Filters

Write your own conditions to allow or deny a request based on its IP, country, path or headers.

Filters cover what the other rules do not: restrict an admin area to certain addresses, deny a country, block a specific HTTP client… Each condition compares a request field to a value.

The problem

Some rules are specific to your business, and no generic protection can guess them: your back office must only be reachable from the office, your store only ships to certain countries, a hosting network only sends attacks, an old version of your app must no longer call the API.

What they let you do

  • restrict an area, such as the admin, to known addresses;
  • limit or deny countries or networks (ASN);
  • block a specific client, header or parameter;
  • work as an allowlist: deny everything except what you allow.

When to use them

When you know precisely who should, or should not, access a route.

Configuration

import { requestFilter } from "@zarex/express"; // or your framework's package

rules: [
  requestFilter({
    conditions: [
      { field: "path", operator: "STARTS_WITH", value: "/admin", action: "DENY" },
    ],
  }),
],

Fields

FieldContent
ipThe visitor's IP address. With IN / NOT_IN, CIDR ranges are accepted ("10.0.0.0/8").
ip.countryThe IP address's country, ISO code ("BJ", "SN"…). "ZZ" = unknown.
ip.asnThe IP address's network ("AS15169"). "AS0" = unknown.
pathThe request path.
methodThe HTTP method (GET, POST…).
hostThe requested domain name.
headers.<name>A header, for example headers.user-agent.
query.<name>A URL parameter.
cookies.<name>A cookie. Requires the inspectCookies: true option.

Operators

OperatorMeaning
EQ · NEequal · not equal
IN · NOT_INin the values list · not in the list
CONTAINS · STARTS_WITHcontains · starts with
LIKEpattern with * (any sequence) and ? (one character), for example "/admin/*"
REGEXregular expression
LEN_EQ · LEN_GT · LEN_LTlength equal to · greater than · less than

Comparisons are case-insensitive, REGEX included. ip.country and ip.asn only accept EQ, NE, IN and NOT_IN.

  • REGEX follows RE2 syntax: matching time is guaranteed linear, with no risk of a crafted expression hanging the engine. Lookahead/lookbehind assertions ((?=…), (?!…)) and backreferences (\1) are not available: the SDK rejects the rule at startup.
  • LEN_EQ, LEN_GT, LEN_LT count characters, not bytes. value is a number, for example "2000".
  • Missing field: it equals "". { field: "headers.x-api-version", operator: "EQ", value: "" } therefore matches a request without that header.
  • Repeated parameter (?debug=0&debug=1): all its values are examined. EQ, IN, CONTAINS… succeed if one value matches; NE and NOT_IN require that none matches.
  • The Authorization, Proxy-Authorization, Set-Cookie and X-Zarex-Key headers are never sent to Zarex: a filter on one of them is rejected at startup, since it could never match anything.

The path is normalized before comparison

An attacker who wants to reach /admin despite a filter tries other spellings of the same path. Zarex brings them all back to the same form before comparing. With { field: "path", operator: "STARTS_WITH", value: "/admin", action: "DENY" }, all these requests are denied:

/admin · /admin/users · /ADMIN · /%61dmin (encoded letter) · //admin · /x/../admin · /admin;jsessionid=1 · /admin.

STARTS_WITH on a path compares whole segments: /admin matches /admin and /admin/…, but not /administrator-public.

Options

OptionValuesDefaultRole
conditionsfrom 1 to 32 conditionsrequiredEach condition: field, operator, value (256 characters at most) or values (64 at most), action, and not: true to invert it.
action (in a condition)"DENY" · "ALLOW""DENY"What the condition does when it is true.
logic"OR" · "AND""OR"How conditions combine (see below).
default"ALLOW" · "DENY""ALLOW"What happens to a request no condition matches.
oversize"deny" · "inspect" · "allow""deny"What to do with a path or header over 8 KB, which could not be read in full.
mode"LIVE" · "DRY_RUN" · "OFF""LIVE"Deny, only observe, or disable.

logic: order matters

  • "OR" (default): conditions are read in order, and the first true one decides, with its action. The following ones are not read. So put your exceptions (ALLOW) before denials (DENY), as in the "admin" example below.
  • "AND": the rule only applies if all conditions are true; the action of the last one then applies. Give them all the same action.
// Deny payments coming from abroad: all three conditions must be true.
requestFilter({
  logic: "AND",
  conditions: [
    { field: "method", operator: "EQ", value: "POST" },
    { field: "path", operator: "STARTS_WITH", value: "/api/payment" },
    { field: "ip.country", operator: "NOT_IN", values: ["BJ"] },
  ],
}),

Verified: a POST /api/payment/momo from France is denied; the same from Benin, or a GET from France, goes through.

default: "DENY": deny everything, except…

With default: "DENY", a request no condition matches is denied. Then list only action: "ALLOW" conditions: it is an allowlist.

mode: observe before blocking

In DRY_RUN, nothing is blocked: the decision log shows what would have been denied. Essential before a default: "DENY" or a country filter: you see how many real visitors would be affected.

Examples

Restrict the admin area to your office:

requestFilter({
  conditions: [
    { field: "ip", operator: "IN", values: ["203.0.113.0/24"], action: "ALLOW" },
    { field: "path", operator: "STARTS_WITH", value: "/admin", action: "DENY" },
  ],
}),

Only accept certain countries:

requestFilter({
  conditions: [
    { field: "ip.country", operator: "IN", values: ["BJ", "TG", "CI", "SN"], action: "ALLOW" },
  ],
  default: "DENY",
}),

Filtering by country: "unknown" visitors

The country comes from Zarex's IP address database. An address it cannot place equals "ZZ". With default: "DENY", these visitors are denied, as is a customer traveling abroad or behind a foreign VPN. Try DRY_RUN first, and add "ZZ" to the list if you would rather let them through.

ALLOW does not exempt from other rules

An ALLOW condition matches the request for this filter only: the Shield, rate limiting and the other rules still apply.

What the decision contains

The rule's result, in decision.results (ruleType: "REQUEST_FILTER"), contains in reason:

FieldMeaning
matchedtrue: a condition matched the request.
expressionThe number of that condition, starting from 0.
field, operatorThe field and operator of the condition that denied.
action"ALLOW" when an ALLOW condition matched the request.
default"DENY": denied because no condition matched it.

Examples, with the "admin" filter above. A visitor on /admin/users:

{ "matched": true, "expression": 1, "field": "path", "operator": "STARTS_WITH" }

A request coming from France, with the "certain countries" filter:

{ "matched": false, "default": "DENY" }

On this page