Skip to content

Security Headers and Rate Limiting in Nuxt

Security headers and CSP with Nuxt Security, its default rate limiter and what to check behind a proxy, protected admin APIs and file upload validation.

This starter kit uses the nuxt-security Nuxt module to enhance the security of your application. This module provides a set of security headers and best practices to help protect your application from common vulnerabilities and attacks.

The module is registered in the core layer. Its configuration is in apps/web/layers/core/nuxt.config.ts, where you can enable or disable specific security features and customize their settings:

apps/web/layers/core/nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-security'],
  security: {
    headers: {
      contentSecurityPolicy: {
        // img-src, script-src, …
      },
      crossOriginEmbedderPolicy: false,
    },
    nonce: true,
  },
})

Rate Limiting

The kit doesn't change Nuxt Security's rate limiter, so its default applies: 150 requests per route and client IP within 5 minutes. Requests above that get a 429. The counters live in memory, so they reset when the server restarts and aren't shared between several instances.

The limiter takes the client IP from the X-Forwarded-For header. Behind a proxy or CDN (e.g. Cloudflare and Traefik), check which IP actually reaches your app before you launch. If all requests arrive with the proxy's IP, all users share one limit. Configure rateLimiter.ipHeader for your setup, or turn the limiter off:

apps/web/layers/core/nuxt.config.ts
export default defineNuxtConfig({
  security: {
    rateLimiter: false,
  },
})
The kit has no stricter limits for the contact form or the waitlist signup. better-auth rate-limits its own endpoints per client IP in production, with the same proxy caveat. File uploads have a separate limit per user (100 uploads per minute), which only runs when NUXT_PRIVATE_REDIS_URL is set.

Admin API

Every admin API handler starts with await requireAdmin(event). It returns 401 without a session and 403 without the admin role, in addition to the admin route middleware. A unit test fails when a handler under server/api/dashboard/admin/ doesn't call it. See Authentication.

File Uploads

The upload API detects the file type from the file content and ignores the MIME type and file name the client sends. Only PNG, JPEG, GIF, WebP, AVIF and PDF can be uploaded, so an HTML or SVG file disguised as an image is rejected with 415. See Images.