How to Fix Missing Rate Limiting in Express
Learn how to prevent and fix Missing Rate Limiting vulnerabilities in Express applications. Step-by-step guide with code examples, security checklists, and best practices.
What Is Missing Rate Limiting?
Missing Rate Limiting is a vulnerability where an application does not restrict the number or frequency of requests a user or client can make to a particular endpoint or resource. Without rate limits, there is no mechanism to prevent abuse of API endpoints, authentication forms, or resource-intensive operations.
This vulnerability is particularly relevant for: login and authentication endpoints (allowing brute force attacks); password reset and OTP verification endpoints (allowing enumeration and bypasses); API endpoints that return sensitive data (allowing mass data harvesting); resource-intensive operations like file processing or report generation (allowing resource exhaustion); and endpoints that send emails or SMS messages (allowing spam or cost amplification).
In serverless and edge environments (Vercel, Cloudflare Workers), traditional rate limiting using in-memory counters does not work because each request may be handled by a different instance. Applications in these environments need distributed rate limiting using external stores like Redis, Upstash, or purpose-built services.
Why It Matters
Without rate limiting, attackers can automate attacks at scale. Credential stuffing attacks can try thousands of username/password combinations per second. API abuse can extract large volumes of data or incur significant compute costs. Brute force attacks on OTP codes or short tokens become feasible. Denial of service attacks can overwhelm backend resources. For SaaS applications, missing rate limits can lead to unexpected infrastructure costs as attackers consume compute, bandwidth, and third-party API quotas. Rate limiting is also a requirement for compliance with many security standards.
How to Fix It in Express
Implement rate limiting on all externally accessible endpoints, with stricter limits on authentication and sensitive operations. Use a distributed rate limiting solution (Upstash, Redis) for serverless deployments. Apply different rate limit tiers based on authentication status and user role. Implement exponential backoff for failed authentication attempts. Use CAPTCHA as a secondary defense for endpoints under heavy abuse. Return appropriate HTTP 429 (Too Many Requests) responses with Retry-After headers. Monitor rate limit hits to detect attack patterns. Consider using an API gateway (Kong, AWS API Gateway) that provides built-in rate limiting. Implement per-user, per-IP, and global rate limits as separate layers.
Express-Specific Advice
- Use `helmet` middleware for setting security headers (CSP, HSTS, X-Frame-Options, etc.) with sensible defaults.
- Use `express-rate-limit` for rate limiting. Apply stricter limits to authentication endpoints and API routes.
- Always use parameterized queries with your database driver. Never concatenate user input into SQL strings.
- Validate request bodies using `express-validator`, Zod, or Joi middleware. Reject requests that do not match expected schemas.
Code Examples
// DANGEROUS -- no rate limiting
app.post("/auth/login", async (req, res) => {
const user = await authenticate(req.body.email, req.body.password);
if (!user) return res.status(401).json({ error: "Invalid" });
res.json({ token: createToken(user) });
});import rateLimit from "express-rate-limit";
const loginLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // 5 attempts per window
message: { error: "Too many login attempts. Try again in 15 minutes." },
standardHeaders: true,
legacyHeaders: false,
});
app.post("/auth/login", loginLimiter, async (req, res) => {
const user = await authenticate(req.body.email, req.body.password);
if (!user) return res.status(401).json({ error: "Invalid" });
res.json({ token: createToken(user) });
});Express Security Checklist for Missing Rate Limiting
Express Security Best Practices
Use `helmet` middleware for setting security headers (CSP, HSTS, X-Frame-Options, etc.) with sensible defaults.
Use `express-rate-limit` for rate limiting. Apply stricter limits to authentication endpoints and API routes.
Always use parameterized queries with your database driver. Never concatenate user input into SQL strings.
Validate request bodies using `express-validator`, Zod, or Joi middleware. Reject requests that do not match expected schemas.
Use `cors` middleware with explicit origin allowlists. Never use `cors({ origin: '*' })` in production.
Disable the `X-Powered-By` header with `app.disable('x-powered-by')` or by using helmet.
Use `multer` or `busboy` for file uploads with strict file type and size limits. Store files outside the web root.
Implement proper error handling middleware that does not leak stack traces or internal details in production.
Scan Your Express App with SafeVibe
Stop guessing if your Express app is vulnerable to Missing Rate Limiting. Run an automated penetration test in minutes and get actionable results.
Start Free Scan