Appearance
CSRF and CORS Since v0.1
Two protections handle requests that come from another site's page. CSRF protection refuses state-changing requests (POST, PUT, PATCH, DELETE) that a browser sends from another site. API clients and servers send no browser headers, so they pass. CORS enforcement applies the rules you write for cross-origin requests and corrects the upstream's CORS answers.
Before you begin
- Role:
operatorfor the application. - Know the partner or payment origins that legitimately post forms, and the webhook paths other sites call by design.
- For CORS rules, know which origins, methods and headers each API path should allow.
Turn on CSRF protection
The first change turns it on. Start in detect and watch the events csrf.cross_site and csrf.origin_mismatch.
text
1. Open Applications > All applications > the application > Response checks > CSRF and CORS.
2. On the CSRF tab, set Protection mode to detect.
3. Select Preview changes, then Apply.sh
waap csrf set -app shop -m "csrf" mode=detectyaml
# applications[].policy.csrf
csrf:
mode: detectLet a partner or provider send forms
Add its origin to Trusted origins. Origins a CORS rule allows for a path are trusted there too. Webhooks and other paths other sites call by design go under Paths not checked.
text
1. On the CSRF tab, add the origin under Trusted origins and the paths under Paths not checked.
2. Select Preview changes, then Apply.sh
waap csrf set -app shop -m "partner" trusted_origins=https://pay.example-psp.com skip=/webhooks/*yaml
csrf:
trusted_origins: ["https://pay.example-psp.com"]
skip: ["/webhooks/*"]Use form tokens
For old browsers, or a strict requirement, list the pages whose forms get a token and the paths whose POSTs must carry one. Tokens are added in block mode only and are bound to a __waap_cs cookie the application never sees. Keys rotate weekly on their own: see the Token keys tab (waap csrf keys list -app shop, waap csrf keys rotate -app shop).
text
1. On the CSRF tab, set Pages that get tokens and POSTs that need a token.
2. Select Preview changes, then Apply.sh
waap csrf set -app shop -m "tokens" tokens.pages=/checkout tokens.require=/payyaml
csrf:
tokens: {pages: ["/checkout"], require: ["/pay"], ttl: 2h}Add a CORS rule
The first rule whose paths match decides. Requests from other origins are refused, or with Requests not allowed set to strip they pass without CORS answer headers.
text
1. Open the CORS rules tab and select the add control.
2. Enter paths, origins, methods, request headers, exposed headers, credentials, preflight cache and who answers preflights.
3. Select Preview changes, then Apply.sh
waap cors list set -app shop -list rules -f rules.yaml -m "cors"yaml
# policy.cors.rules
- name: partner-api
paths: ["/api/*"]
origins: ["https://app.partner.example", "https://*.partner.example"]
methods: [GET, POST, PUT]
headers: [X-Token]
credentials: false
preflight: answer # or pass: the upstream answersTest a request before you enforce
The Test a request tab takes a method, path, Origin, Sec-Fetch-Site and an optional preflight method and headers. It shows what each check decides and what the application does, without sending anything.
sh
waap cors test -app shop -path /api/items -method OPTIONS -origin https://app.partner.example -preflight PUT -headers X-TokenFind unsafe CORS in the application
The Unsafe upstream CORS tab lists paths whose answers reflect any origin with credentials, or allow null. waap headers posture shows the same. Add a rule for them.
Settings reference
CSRF
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
CSRF protection (policy.csrf.enabled) | Refuses cross-site state-changing requests. | true | true, false | all |
Protection mode (mode) | detect reports; block refuses. | empty (same as the application) | detect, block, empty | detect wins |
Checked methods (methods) | Methods that change state. | POST, PUT, PATCH, DELETE | methods; GET, HEAD, OPTIONS are never checked | all |
Trusted origins (trusted_origins) | Other sites allowed to send forms. | none | origins, up to 64 | all |
Paths not checked (skip) | Paths other sites call by design. | none | exact or prefix ending in *, up to 64 | all |
Pages that get tokens (tokens.pages) | HTML pages whose POST forms get a __waap_csrf field. | none | paths | block mode |
POSTs that need a token (tokens.require) | Paths whose form POSTs must carry a valid token. | none | paths | block mode |
Token lifetime (tokens.ttl) | How long a page's tokens are accepted. | 2h | 5m to 24h | tokens on |
CORS
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
CORS enforcement (policy.cors.enabled) | Enforces the rules and corrects upstream answers. | true | true, false | all |
Protection mode (mode) | detect reports; block refuses and answers preflights. | empty (same as the application) | detect, block, empty | detect wins |
Requests not allowed (on_disallowed) | Refuse them, or strip the CORS headers from the answer. | block | block, strip | block mode |
Report unsafe upstream CORS (assess) | Reports answers that reflect any origin with credentials or allow null, sampled. | true | true, false | paths without a rule |
CORS rule
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
name | Identifies the rule. | none | text | all |
paths | Paths the rule covers. | none (required) | exact or prefix ending in *, up to 64 | all |
origins | Allowed origins. | none (required) | exact origins, or https://*.example, up to 64 | all |
methods | Allowed methods. | none | methods | all |
headers | Allowed request headers. | none | header names, up to 64 | all |
expose | Headers the page may read. | none | header names, up to 64 | all |
credentials | Allow credentials. | false | true, false | all |
max_age | How long browsers cache the preflight. | 10m | 0 to 24h | all |
preflight | Whether the product answers preflights or the upstream does. | answer | answer, pass | all |
Rule IDs are csrf.cross_site, csrf.origin_mismatch, csrf.token.missing, csrf.token.invalid, cors.origin, cors.method, cors.header and cors.backend.reflect. Exceptions can ignore csrf.* or cors.* for a path or client.
Limits
| Limit | Value |
|---|---|
| CORS rules | 64 |
| Origins, paths, headers per list | 64 |
| Metric | waap_crossorigin_refusals_total{app,check} |
Troubleshooting
A partner's form post is refused
- Cause: its origin is not trusted, and the request carries cross-site browser headers.
- Check: the event
csrf.cross_siteorcsrf.origin_mismatchshows the origin. - Fix: add the origin under Trusted origins, or the path under Paths not checked.
A script from another site gets no CORS answer
- Cause: no rule matches the path, or the origin is not in the rule.
- Check: Test a request on the CORS test tab.
- Fix: add or widen a rule.