Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nuxt-csurf

Morgbn270kMIT1.6.5TypeScript support: included

Nuxt Cross-Site Request Forgery (CSRF) Prevention

nuxt, csrf, module

readme

nuxt-oa-social-card

npm version npm downloads License Nuxt

Nuxt Csurf

Cross-Site Request Forgery (CSRF) prevention. \ Create a middleware for CSRF token creation and validation.

✅ Supports Node.js server & serverless environments \ ✅ Supports both universal and client-side rendering (ssr: true|false) \ ✅ Per-route configuration \ ✅ TypeScript \ ❌ Don't support static hosting and nitro prerender due to certain limitations *

Installation

npx nuxi@latest module add csurf

Global configuration

// nuxt.config.js
export default defineNuxtConfig({
  modules: ['nuxt-csurf'],
  csurf: { // optional
    https: false, // default true if in production
    cookieKey: '', // "__Host-csrf" if https is true otherwise just "csrf"
    cookie: { // CookieSerializeOptions from unjs/cookie-es
      path: '/',
      httpOnly: true,
      sameSite: 'strict'
    },
    methodsToProtect: ['POST', 'PUT', 'PATCH'], // the request methods we want CSRF protection for
    encryptSecret: /** a 32 bits secret */, // for stateless server (like serverless runtime), random bytes by default
    encryptAlgorithm: 'aes-256-cbc', // by default 'aes-256-cbc' (node), 'AES-CBC' (serverless)
    addCsrfTokenToEventCtx: true, // default false, to run useCsrfFetch on server set it to true
    headerName: 'csrf-token' // the header where the csrf token is stored
  } 
})

Per route configuration

To enable per-route configuration, use the routeRules like following:

export default defineNuxtConfig({
  routeRules: {
    '/api/nocsrf': {
      csurf: false
    },
    '/api/test': {
      csurf: {
        methodsToProtect: ['POST'] // protect POST request only
      }
    }
  }
})

useCsrfFetch

This composable provides a convenient wrapper around useFetch. It automatically adds the CSRF token in headers.

const { data, pending, error, refresh } = useCsrfFetch('/api/login', { query: param1: 'value1' })

$csrfFetch

This helper provides a convenient wrapper around $fetch. It automatically adds the CSRF token in headers.

const { $csrfFetch } = useNuxtApp()
const { data } = await $csrfFetch('/api/login', { method: 'POST', body: …, headers: … })

useCsrf

Use this composable if you need to access to the CSRF token value.

const { csrf } = useCsrf()
console.log(csrf) // something like: mo4+MrFaeXP7fhAie0o2qw==:tLUaqtHW6evx/coGQVAhtGAR+v6cxgFtrqmkOsuAMag8PHRnMwpbGGUO0TPJjL+4

Try production on localhost (yarn preview):

NITRO_CSURF_HTTPS=false
NITRO_CSURF_COOKIE_KEY=csrf

Limitations

The CSRF Token value is stored in the DOM as described in Owasp's CSRF cheatsheet. So the DOM has to be generated for each new page request, which is not the case with a static site (or prerendered routes). See error #42

Credits

changelog

1.6.5 (2024-10-29)

Bug Fixes

  • :bug: use Fetch API Headers (5d8cbcd)

1.6.4 (2024-10-29)

Bug Fixes

  • :art: better custom useFetch (60115ef), closes #49
  • :art: store encryptSecret in runtimeConfig (9c901c5), closes #41
  • :label: useCsrfFetch type (219b729)

1.6.3 (2024-10-01)

Bug Fixes

  • augment @nuxt/schema rather than nuxt/schema (91b7164)

1.6.2 (2024-08-20)

Bug Fixes

1.6.1 (2024-07-17)

Bug Fixes

  • :bug: defaults merge array (0f756fc), closes #37

1.6.0 (2024-07-17)

Bug Fixes

  • :bug: support uppercase methods (1c3073b), closes #39

Features

  • :sparkles: option to configure header name (2e093cc), closes #38

1.5.2 (2024-04-21)

Bug Fixes

  • :bug: import missing useLazyCsrfFetch (3343a9d), closes #33
  • opt in to import.meta.* properties (377ce75)

1.5.1 (2024-03-20)

Bug Fixes

  • :truck: mv utils/ in runtime (e728ff2), closes #30

1.5.0 (2024-03-20)

Features

1.4.2 (2024-02-22)

Bug Fixes

  • :label: declare plugin type (87ad5fa)

1.4.1 (2024-02-21)

Bug Fixes

  • :adhesive_bandage: string-width error (dcad5e8)
  • :label: add types to $csrfFetch (b3ffc49), closes #19

1.4.0 (2023-12-15)

Bug Fixes

  • :rotating_light: extends ./.nuxt/tsconfig.json (4b18bda)

Features

  • :sparkles: add addCsrfTokenToEventCtx option (e4408fc), closes #20 #22

1.3.2 (2023-11-24)

Bug Fixes

  • :art: store token in meta tag (7d770bb)

1.3.1 (2023-09-11)

Bug Fixes

1.3.0 (2023-09-09)

Bug Fixes

  • :bug: importEncryptSecret on runtime (65c22c0)
  • :hammer: add playground scripts (5a26b63)
  • :rotating_light: change eslint config (0c19252)

Features

  • :sparkles: add useLazyCsrfFetch (b06beac)
  • :sparkles: support non ssr build (7624ea1), closes #12 #9 #8
  • :sparkles: support serverless (4628f29)

1.2.0 (2023-03-07)

Features

  • :arrow_up: upgrade nuxt (acc9921), closes #5

1.1.0 (2023-02-11)

Features

  • :sparkles: add a plugin that provide a $fetch wrapper with csrf automatically added (bc302ab), closes #4

1.0.1 (2023-01-27)

Bug Fixes

  • :art: copy useFetch signature (beeb821), closes #3

1.0.0 (2023-01-19)

Features

  • :sparkles: composables & server middleware (d7eec56)
  • :white_check_mark: playground tests (27d2725)