Security modules

Headers, CSP and request limits with nuxt-security

nuxt-securityheadersrequestSizeLimiterallowedMethodsRestricter
A baseline nuxt-security setup next to the guard, tuned for a BFF.

The guard does not set security headers or a Content Security Policy. nuxt-security does, with strict defaults. This setup keeps those defaults for pages and adjusts the API.

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-security', '@kurashina-design/nuxt-bff-guard'],
  bffGuard: {
    verifyUser: './server/auth/verify-user.ts',
  },
  security: {
    // Pages: strict CSP with a nonce, HSTS and the rest of the defaults.
    nonce: true,
    headers: {
      contentSecurityPolicy: {
        'script-src': ["'self'", "'nonce-{{nonce}}'", "'strict-dynamic'"],
        'connect-src': ["'self'"], // the browser talks only to your BFF
      },
    },
    requestSizeLimiter: {
      maxRequestSizeInBytes: 200_000,
      maxUploadFileRequestInBytes: 8_000_000,
    },
  },
  routeRules: {
    // Allow only the methods the API uses.
    '/api/**': {
      security: { allowedMethodsRestricter: { methods: ['GET', 'POST', 'PATCH', 'DELETE'] } },
    },
    // A larger limit for the one upload route.
    '/api/files': {
      security: { requestSizeLimiter: { maxUploadFileRequestInBytes: 50_000_000 } },
    },
  },
})

What each layer answers

StatusComes fromBefore the guard?
413requestSizeLimiteryes
405allowedMethodsRestricteryes
429rateLimiteryes
400xssValidatoryes
401 / 403 / 503Nuxt BFF Guard–

connect-src 'self' fits the BFF idea: the browser only calls your own server, and upstream APIs are called from handlers.