Security modules
CSRF tokens with nuxt-csurf
nuxt-csurfuseCsrfFetchrouteRules
Add a synchronizer token on top of the origin check, without breaking Bearer clients.
The guard blocks cross-site writes by checking Sec-Fetch-Site and Origin. nuxt-csurf (also available as security.csrf in nuxt-security) adds a second, independent defense: a secret Cookie plus a token header. Using both is fine when policy asks for a CSRF token.
nuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-csurf', '@kurashina-design/nuxt-bff-guard'],
bffGuard: {
verifyUser: './server/auth/verify-user.ts',
},
csurf: {
// DELETE is not protected by default. Match the guard's writeMethods.
methodsToProtect: ['POST', 'PUT', 'PATCH', 'DELETE'],
},
routeRules: {
// Clients that authenticate with Bearer tokens have no CSRF Cookie.
'/api/mobile/**': { csurf: false },
// Webhooks come from other servers.
'/api/webhooks/**': { csurf: false },
},
})
nuxt-csurf 1.6.6 declares the csurf route rule for an older Nitro type, so on Nuxt 4.7 nuxt typecheck and your editor report it as an unknown property. The rule still works: nuxt-csurf reads it at runtime.
Send writes from the browser with the token:
pages/orders/[id].vue
<script setup lang="ts">
const route = useRoute()
const { $csrfFetch } = useNuxtApp()
async function cancel() {
await $csrfFetch(`/api/orders/${route.params.id}`, { method: 'DELETE' })
}
</script>
How the two checks differ
| Guard origin check | nuxt-csurf | |
|---|---|---|
| Needs client changes | no | $csrfFetch or useCsrfFetch |
Old browsers without Sec-Fetch-Site | uses Origin | works |
| Bearer clients | skipped after verification | must be excluded per route |
| Runs | inside the wrapped handler | as middleware, before the guard |
Because nuxt-csurf runs first, a request without its token gets nuxt-csurf's 403 before verifyUser is called.