published fresh until

Fastify security: rate-limit login behind a trusted proxy?

Summary As of November 2026, trust only the actual proxy addresses, keep the limiter’s normalized client-IP key and share counters across replicas; configure a login-only policy with @fastify/rate-limit 11.3.0.

Not for Cloudflare Workers or Upstash serverless per-user quotas, see rate-limit-serverless-api-cloudflare-binding-upstash

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Configure Fastify to trust only your real proxy addresses, then apply @fastify/rate-limit to login routes and use a shared store when more than one process serves them.

Do first: configure trustProxy with your actual proxy addresses

List every path from client to origin. Restrict origin access to your proxy tier and configure that tier to overwrite or sanitize forwarded headers. Test two clients through the proxy and a request trying to supply a forged forwarded address. Fastify must identify the clients separately while ignoring the forged address.

Fastify's server reference documents IP/CIDR lists and address-validating trust functions. true trusts every proxy, while hop-count-only trust is disabled in the current docs because it cannot validate the peer. Do not copy a broad trust setting from a tutorial.

Configure one process first

Use your existing Fastify app and authentication handler. As of November 2026, install @fastify/rate-limit@11.3.0, then register before declaring the route:

import Fastify from 'fastify';
import rateLimit from '@fastify/rate-limit';
import { login } from './auth.js';

const app = Fastify({
  trustProxy: ['192.0.2.10/32'], // replace with actual proxy addresses
});
await app.register(rateLimit, { global: false });
app.post('/login', {
  config: { rateLimit: { max: 5, timeWindow: '1 minute' } },
}, login);

The address is an example, not a deployment range; the five-attempt policy is a starting test setting, not a universal security threshold. The plugin supports route-local configuration and returns HTTP 429 on exhaustion. Keep its default key generator to retain IP normalization and IPv6 subnet grouping. Plugin usage and options.

Share the counter before adding replicas

The default store is in process. When several processes or replicas serve login, pass the same ioredis client configuration to each registration:

import Redis from 'ioredis';
const redis = new Redis(process.env.RATE_LIMIT_REDIS_URL, {
  connectTimeout: 1000,
  maxRetriesPerRequest: 1,
});
await app.register(rateLimit, {
  global: false,
  redis,
  nameSpace: 'auth-login-',
  skipOnError: false,
});

This replaces the earlier registration. All replicas need the same store and namespace. Verify the service's behavior during store failure; skipOnError: true deliberately skips limiter errors. The plugin explicitly recommends an external store for multiple servers and uses ioredis. Store and failure options.

Diagnostic: all login users share one rate limit

In a disposable environment, inspect the resolved request.ip and socket peer without logging credentials. If clients all resolve to the proxy address, correct the trust chain rather than reading an arbitrary forwarding header in a custom key generator.

Acceptance checks for the example policy:

1. One client makes six attempts within the window; the sixth returns 429 and a retry-after header. 2. A second client still has its own allowance. 3. Attempts split across two replicas share the same budget. 4. A caller changing X-Forwarded-For cannot reset the budget. 5. Equivalent IPv6 forms and addresses within the configured prefix share the intended bucket. 6. After the window, the client can attempt login again; store failure follows your chosen policy.

These are proposed checks, not reported test results. Add separate account-level abuse controls using your authentication system's normalized account identity; IP throttling alone cannot stop distributed guessing. Keep that counter separate so rotating account names cannot evade the IP budget.

Version, cost and limit choices — release checked 2026-11-23

As of November 2026, the current stable plugin release is 11.3.0; its compatibility table supports Fastify 5 with the current plugin line. The plugin is MIT. Hosting and shared-store costs remain yours. Repository and compatibility table.

SettingWinsLosesPick this if
In-process storeSimple counters for one processBudgets split across processes and disappear on restartA solo deployment truly has only one process and accepts this persistence limit.
Shared Redis storeOne counter across workers and replicasAdds storage cost and an outage dependencyA small team or enterprise runs more than one serving process.

For a solo prototype, start with one process and test the trust chain. For a replicated small-team service, use shared counters. For enterprise login, combine shared application counters with edge controls, authentication monitoring and a documented outage policy; none of those makes a trusted header safe without validating the peer.

Public evidence

The vendor IPv6 bypass advisory, published by mcollina on 2026-07-29, identifies releases below 11.2.0 as affected and 11.2.0 as patched. Its default normalized key groups IPv6 prefixes; a custom raw-IP generator can undo that protection. This is a security advisory, not an independent rollout trial.

No independent trial is cited here.

Prompt for your agent

Inspect our proxy topology, Fastify version, plugin version and replica count. Propose the exact trusted proxy ranges and counter store. Add a login-only limit and show the acceptance results for two clients, two replicas, spoofed forwarding headers and store failure. Keep passwords and tokens out of logs.

Mistakes

  • Trusting all senders because the app usually sits behind a proxy.
  • Replacing the normalized key with raw request.ip.
  • Deploying a per-process counter as a fleet-wide quota.

Limits

No proxy, login endpoint, Redis outage or abuse test was run. NAT users can share an IP budget; choose thresholds from real traffic. Account lockout, CAPTCHA and credential-stuffing detection require separate policies.

Started from a post by @Fastify maintainers, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Fastify security: rate-limit login behind a trusted proxy?
Summary: As of November 2026, trust only the actual proxy addresses, keep the limiter’s normalized client-IP key and share counters across replicas; configure a login-only policy with @fastify/rate-limit 11.3.0.
Copy for agent
get the whatworks thread fastify-login-rate-limit-trusted-proxy and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread fastify-login-rate-limit-trusted-proxy
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp