remix/middleware/csrf
CSRF protection middleware for Remix. It provides synchronizer-token validation backed by session storage, plus origin checks for unsafe requests.
Features
- Session-Backed Tokens - Creates and persists CSRF tokens in the request session
- Flexible Token Extraction - Reads tokens from headers, form fields, or a custom resolver
- Origin Validation - Validates
Origin/Refererfor unsafe methods with customizable policies - Configurable Enforcement - Control safe methods, token keys, and failure responses
Installation
npm i remixUsage
This middleware requires session-middleware to run before it.
import { createCookie } from 'remix/cookie'
import { createRouter } from 'remix/router'
import { createCookieSessionStorage } from 'remix/session-storage/cookie'
import { session } from 'remix/middleware/session'
import { csrf, getCsrfToken } from 'remix/middleware/csrf'
let sessionCookie = createCookie('__session', { secrets: ['secret1'] })
let sessionStorage = createCookieSessionStorage()
let router = createRouter({
middleware: [session(sessionCookie, sessionStorage), csrf()],
})
router.get('/form', (context) => {
let token = getCsrfToken(context)
return new Response(`
<form method="post" action="/submit">
<input type="hidden" name="_csrf" value="${token}" />
<button type="submit">Submit</button>
</form>
`)
})Token Sources
By default, csrf() checks token values in this order:
- Request headers:
X-Csrf-Token,X-Xsrf-Token,Csrf-Token - Form field:
_csrf(requiresformData()middleware to parse request bodies)
Query parameters are not read by default. Keep tokens in headers or form fields to avoid exposing them in logs, browser history, and copied URLs.
You can replace the default lookup using value(context). Applications that need query parameter tokens can opt in explicitly:
csrf({
value(context) {
return context.url.searchParams.get('_csrf')
},
})This resolver replaces both header and form field lookup; it does not add another fallback.
Origin Validation
For requests whose original method is unsafe (POST, PUT, PATCH, DELETE), the middleware validates the token and request origin. Routing middleware may change context.method, but method overrides do not change whether csrf() requires validation.
- Default: same-origin validation when
OriginorRefereris present - Custom: provide
originas string, regex, array, or function - Missing origin behavior: controlled by
allowMissingOrigin(defaulttrue)
Caveats
- The synchronizer token is the primary defense in
csrf().OriginandRefererchecks are an additional signal, not the only protection. - By default, unsafe requests with a valid token still pass when
OriginandRefererare both missing. SetallowMissingOrigin: falseif your deployment wants to require provenance headers on unsafe requests. - If you want to reject more unsafe requests before token validation, especially when browser provenance headers are available, layer
cop-middlewarein front ofcsrf().
Why This Exists
Modern browsers now provide stronger cross-origin signals like Sec-Fetch-Site, and explicit SameSite=Lax cookies already block many CSRF attacks. We have considered the lighter, tokenless model used by Go's CrossOriginProtection, and we think it is a good fit when a deployment can make all of the guarantees that model depends on.
Remix cannot assume those guarantees for every app. csrf() still exists as the conservative option for apps that want synchronizer tokens in addition to origin checks, especially for session-backed HTML form workflows and mixed deployment environments.
If your deployment can guarantee the prerequisites for the tokenless model, this middleware is optional. In that case, cop-middleware may be a better fit.
Related Packages
cop-middleware- Middleware for tokenless cross-origin protection using browser provenance headersfetch-router- Router for the web Fetch APIsession-middleware- Session middleware required bycsrf()form-data-middleware- Needed for form body token extraction
License
See LICENSE