Skip to content

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: operator for 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=detect
yaml
# applications[].policy.csrf
csrf:
  mode: detect

Let 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=/pay
yaml
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 answers

Test 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-Token

Find 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 ​

SettingWhat it doesDefaultAllowed valuesApplies in
CSRF protection (policy.csrf.enabled)Refuses cross-site state-changing requests.truetrue, falseall
Protection mode (mode)detect reports; block refuses.empty (same as the application)detect, block, emptydetect wins
Checked methods (methods)Methods that change state.POST, PUT, PATCH, DELETEmethods; GET, HEAD, OPTIONS are never checkedall
Trusted origins (trusted_origins)Other sites allowed to send forms.noneorigins, up to 64all
Paths not checked (skip)Paths other sites call by design.noneexact or prefix ending in *, up to 64all
Pages that get tokens (tokens.pages)HTML pages whose POST forms get a __waap_csrf field.nonepathsblock mode
POSTs that need a token (tokens.require)Paths whose form POSTs must carry a valid token.nonepathsblock mode
Token lifetime (tokens.ttl)How long a page's tokens are accepted.2h5m to 24htokens on

CORS ​

SettingWhat it doesDefaultAllowed valuesApplies in
CORS enforcement (policy.cors.enabled)Enforces the rules and corrects upstream answers.truetrue, falseall
Protection mode (mode)detect reports; block refuses and answers preflights.empty (same as the application)detect, block, emptydetect wins
Requests not allowed (on_disallowed)Refuse them, or strip the CORS headers from the answer.blockblock, stripblock mode
Report unsafe upstream CORS (assess)Reports answers that reflect any origin with credentials or allow null, sampled.truetrue, falsepaths without a rule

CORS rule ​

SettingWhat it doesDefaultAllowed valuesApplies in
nameIdentifies the rule.nonetextall
pathsPaths the rule covers.none (required)exact or prefix ending in *, up to 64all
originsAllowed origins.none (required)exact origins, or https://*.example, up to 64all
methodsAllowed methods.nonemethodsall
headersAllowed request headers.noneheader names, up to 64all
exposeHeaders the page may read.noneheader names, up to 64all
credentialsAllow credentials.falsetrue, falseall
max_ageHow long browsers cache the preflight.10m0 to 24hall
preflightWhether the product answers preflights or the upstream does.answeranswer, passall

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 ​

LimitValue
CORS rules64
Origins, paths, headers per list64
Metricwaap_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_site or csrf.origin_mismatch shows 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.