Status: Accepted
Date: -
Author: Mehdi Maleki
Authentication endpoints (/sign-in, /sign-up) are highly vulnerable to brute-force and credential stuffing attacks. We need rate limiting that works across a distributed, serverless environment.
We will use Upstash Redis with the @upstash/ratelimit library, applying a fixed-window limit (e.g., 5 requests per minute per IP) to authentication Server Actions.
- In-Memory Rate Limiting (e.g.,
express-rate-limitormemory-cache): Rejected. Serverless functions spin up multiple independent instances. In-memory state is not shared across instances, rendering it useless for distributed rate limiting. - Self-Hosted Redis: Rejected. Requires provisioning, securing, and maintaining a dedicated server or managed database, which is overkill for a solo project.
- Distributed State: Upstash provides a centralized, globally distributed Redis instance that all serverless functions can access synchronously.
- Security: Effectively mitigates brute-force attacks at the application layer before they hit the database.
- Added Latency: Every authentication request now requires a network hop to Upstash Redis to check the limit.
- IP Spoofing: Relying on
x-forwarded-forcan be spoofed if the app is accessed directly, bypassing the proxy. - Mitigation: We ensure the app is strictly deployed behind Vercel's proxy to trust the
x-forwarded-forheader. The ~10ms latency added by Upstash is an acceptable trade-off for security.