Appearance
CSRF and CORS
Symptoms of cross-site request forgery (CSRF) checks, form tokens and cross-origin resource sharing (CORS) rules.
Form posts are refused with csrf.cross_site or csrf.origin_mismatch
- Cause: the form or script is on another site (a payment provider returning the customer, a partner page, or a sibling subdomain: same-site is not same-origin). A proxy in front rewrites
Host, so theOriginno longer matches it. Or the page was opened from a sandboxed frame or a local file (Origin: null, which is never trusted). - Check: the event's snippet (
OriginandSec-Fetch-Siteas the browser sent them);waap cors test -app NAME -path PATH -origin ORIGIN -site cross-site. - Fix: add the origin to
trusted_origins, or allow it with a CORS rule for the path. Skip paths that other sites call by design (webhooks). Add an exception (csrf.*) for one client.
POSTs are refused with csrf.token.missing or csrf.token.invalid
- Cause: the page with the form is not under
tokens.pages, is not HTML, has the form after the firstmax_bodybytes of the response scope, or its form posts to another host (no token is added). The form is built by a script (no token). The page was open longer thantokens.ttl. The browser lost the__waap_cscookie. The token was signed with a key rotated out (more than two rotations ago). Or the application is in detect mode: tokens are added in block mode only, sotokens.requirerefuses nothing in detect mode but reports. - Check: the token page's trace (
body: CSRF token added to N forms,set-cookie __waap_cs); the event's message (missing, expired, does not verify, or key no longer kept);waap csrf keys list -app NAME. - Fix: list the page under
tokens.pages; raiseresponse.max_body; raisetokens.ttl. For forms built by a script, rely on the fetch metadata checks and leave the path out oftokens.require.
No token is added to forms
- Cause: CSRF is off, or the application is in detect mode. There is no CSRF key yet: the active node makes one within about 10 seconds of listing token pages, and inspection loads it within 2 more. The method is not
post, or the action points to another host. The answer is not 2xx HTML. - Check: the trace's
csrfstep on the answer;waap csrf keys list -app NAME.
Another site's script cannot call the API (cors.origin, cors.method, cors.header)
- Cause: no CORS rule allows the origin (the scheme, host and port must match exactly:
http://andhttps://differ, and*.example.commatches one level only), the method, or a requested header. - Check:
waap cors test -app NAME -method OPTIONS -path PATH -origin ORIGIN -preflight PUT -headers X-Token. The event names what was not allowed. - Fix: add it to the rule (
waap cors list set -app NAME -list rules -f rules.yaml).
The browser reports a CORS error although the upstream sends CORS headers
- Cause: on a rule's path, the rule is the authority. Headers the rule does not set are removed, and others are set from it. With
on_disallowed=strip, refused requests lose their CORS headers. A cached preflight answer (max_age) from before a rule change can also be the cause. - Check: the request's trace (the
corsstep shows the edits: set by the CORS rule, or removed);waap cors test. - Fix: put the upstream's intended origins, methods and headers into the rule, or use
preflight: passto let the upstream answer preflight requests.
A token page answers 503 through Envoy (mismatch_between_content_length_and_the_length_of_the_mutated_body)
- Cause: an inspection service older than this release changes the body without removing
Content-Length. Envoy (1.39) keeps the header when the body mode is switched per answer, and refuses the changed body. - Check: the access log's
detailsfield. - Fix: update the inspection service. The current version removes the header when it changes a body, and the answer is sent chunked.