Skip to content

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 the Origin no 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 (Origin and Sec-Fetch-Site as 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 first max_body bytes 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 than tokens.ttl. The browser lost the __waap_cs cookie. 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, so tokens.require refuses 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; raise response.max_body; raise tokens.ttl. For forms built by a script, rely on the fetch metadata checks and leave the path out of tokens.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 csrf step 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:// and https:// differ, and *.example.com matches 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 cors step 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: pass to 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 details field.
  • Fix: update the inspection service. The current version removes the header when it changes a body, and the answer is sent chunked.