Same-origin writes

How the guard blocks cross-site request forgery for Cookie-based writes.

Browsers attach Cookies to requests that other sites start. For the methods in writeMethods (POST, PUT, PATCH and DELETE by default), the guard therefore asks for proof that the request came from your own pages.

The check

  1. If the request has Sec-Fetch-Site, only same-origin and none pass. same-site is rejected, so a sibling subdomain cannot write.
  2. Without Sec-Fetch-Site, the Origin header must equal the request's own origin: protocol, host and port. The guard reads X-Forwarded-Host and X-Forwarded-Proto, so a trusted reverse proxy must overwrite them.
  3. With neither header, the request is rejected.

A failure answers 403. The check runs after verifyUser on protected routes and on its own on public routes.

Visitor routes are stricter

Routes with access: 'visitor' accept only Sec-Fetch-Site: same-origin, for every method including GET, and do not fall back to Origin. See Sites without users.

Bearer clients

When verifyUser returned { viaBearer: true } for a request whose Authorization header holds a Bearer token, the check is skipped. Another site cannot make the browser send your token, and mobile apps or CLI tools do not send Origin at all.

Server-to-server POSTs

Webhooks and other server-to-server calls send neither header and cannot use a Bearer token you verify, so a wrapped route rejects them. Serve them from a folder outside routePatterns; see Webhooks.

GET must not change state

Methods outside writeMethods are never checked. Keep GET and HEAD free of side effects, or add the method to writeMethods.

With nuxt-csurf

The origin check and a CSRF token protect against the same attack in different ways. You can use both; see CSRF tokens with nuxt-csurf.