Security modules

Rate limits with nuxt-security

nuxt-securityrateLimiterrouteRules
Slow down credential stuffing and token guessing before verifyUser runs.

nuxt-security adds its rate limiter as a server middleware. Nitro runs middleware before route handlers, so an over-limit request is answered with 429 before verifyUser makes a single call to your auth provider.

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-security', '@kurashina-design/nuxt-bff-guard'],
  bffGuard: {
    verifyUser: './server/auth/verify-user.ts',
  },
  security: {
    rateLimiter: {
      tokensPerInterval: 150,
      interval: 300_000, // 5 minutes
      headers: true, // x-ratelimit-* response headers
      // Behind a proxy, name the header it sets. Otherwise the client's own
      // X-Forwarded-For decides the bucket.
      ipHeader: 'x-real-ip',
      driver: { name: 'redis', options: { url: process.env.REDIS_URL } },
    },
  },
  routeRules: {
    // Public sign-in routes get a much smaller bucket.
    '/api/session': {
      security: { rateLimiter: { tokensPerInterval: 5, interval: 60_000 } },
    },
    '/api/auth/**': {
      security: { rateLimiter: { tokensPerInterval: 10, interval: 60_000 } },
    },
  },
})

The limiter counts per IP and per matched rule, so every path under one rule shares a bucket. Its default storage is an in-memory LRU cache, which is per process; use a shared driver such as Redis when you run more than one instance.

Why before the guard

  • A flood of invalid tokens costs nothing at your auth provider.
  • Public sign-in routes, which skip verifyUser, are limited too.
  • Your handler's upstream calls are protected from abuse by signed-in users as well.