Rate limiting
Rate limiting caps how many requests a client may send to a server, or to one operation on it, in a period of time. A request over the cap is refused or delayed, and an HTTP server usually answers it with the status code 429 Too Many Requests, defined in section 4 of RFC 6585.1 The client is identified by a key, most often its IP address, or a user account when the request is signed in.1
Mechanism
A rate limiter keeps a counter for each key. Below the limit, a request goes through and the counter goes up; above it, the request is refused. The refusal can carry a Retry-After header giving the number of seconds the client should wait, so Retry-After: 3600 asks for one hour.1
The Upstash library for TypeScript implements three counting methods.2
| Method | How it counts | Weakness stated in the documentation |
|---|---|---|
| Fixed window | Time is cut into windows of fixed length, such as 10 seconds. Each request adds one to the counter of the current window, and requests above the limit are refused until the next window begins at a fixed boundary. | A burst at the end of one window and another at the start of the next can both pass. Waiting clients all retry when a window opens. |
| Sliding window | The count of the previous window is weighted by the share of it that falls inside the last full period, then added to the count of the current window. | An approximation that assumes the previous window had an even flow of requests. Needs more storage and computation. |
| Token bucket | A bucket holds a maximum number of tokens and refills at a set rate. Each request removes one token; with no token left, the request is refused. | Costly to compute. |
With a sliding window of 10 requests a minute, a client that sent 4 requests in the previous minute and 5 so far in the current one is counted at 00:01:15 as 4 * (60 - 15) / 60 + 5 = 8, so its next request is allowed.2 A token bucket whose size is larger than its refill rate lets a client that has been idle send a short burst. Supabase Auth uses a token bucket for its limits by IP address; most of its buckets hold 30 requests, so an idle client can send 30 at once, but a steady rate above the refill rate is refused with a 429.3
Attacks it slows
An operation that clients can repeat without any cap has the weakness known as Improper Control of Interaction Frequency.4 A limit slows any attack that depends on volume. A Brute-force attack attack needs many attempts on one account; NIST SP 800-63B allows at most 100 consecutive failed attempts with one authenticator.5 Credential stuffing and API scraping need many requests across many accounts or records.
Requests that cost money are a further target, a form of Denial of wallet. In one example scenario, a script calls a password-reset route tens of thousands of times; each call makes the server send a text message that the provider bills at $0.05, and the company loses thousands of dollars within minutes.6
A pause inside the code is no limit. A sign-in handler that sleeps before checking the password still accepts any number of parallel connections, so an attacker sends many attempts at once and the attack takes no longer.7
Choosing the key
The IP address is the usual key, and express-rate-limit uses it unless given another key function.18 As the only or main defence, IP blocking is weak. Credential stuffing toolkits such as Sentry MBA spread their requests over proxy networks with many addresses, so each address sends little, which can defeat IP blocklists and rate limits alike.9
A limit on an account or on one operation still applies when the attacker changes address. Checking a one-time password and requesting password recovery are operations to limit per client.6 Blocking a recognised attacker for a time can also lock out a valid user, if the attacker sends requests in that user's name.10 Two measures make this harder: a bot detection challenge before the attempt, and a wait after each failure that grows as the account nears its limit, for example from 30 seconds up to an hour.5
express-rate-limit counts an IPv6 client by a prefix of its address, 56 bits in the example configuration, set with the ipv6Subnet option.8 When a server-side framework or a proxy calls an API for its users, every request arrives from the server's own address; Supabase Auth accepts the user's address in an Sb-Forwarded-For header sent with a secret API key, once IP address forwarding is turned on for the project.3
What a limit does not count
A request limit counts each request once, however much work it asks for. In another example scenario, a GraphQL endpoint with a request limit receives one request holding hundreds of image uploads, each of which starts memory-heavy processing, and the server runs out of memory. The number of operations in one request and the number of records per page need caps of their own for that reason.6 Counters kept at the network edge are approximate: Cloudflare states that its rules are not designed to let an exact number of requests reach the origin, because counters are kept per data centre and can take a few seconds to update.11
Setting a limit
Express
The express-rate-limit middleware answers with status 429 by default. Its built-in store keeps counts in memory; to share counts across several servers, it takes an external store such as Redis or Memcached.8
import { rateLimit } from 'express-rate-limit'
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // requests per IP per window
standardHeaders: 'draft-8',
legacyHeaders: false,
ipv6Subnet: 56,
})
app.use(limiter)Next.js and serverless functions
The Upstash Ratelimit library keeps its counters in an Upstash Redis database. The identifier passed to limit() can be a user ID, an API key or an IP address.12
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "10 s"),
});
// inside the route handler
const { success } = await ratelimit.limit(userId);
if (!success) {
// refuse the request, for example with status 429
}Supabase Auth
Supabase Auth limits its own sign-in endpoints, set under Authentication > Rate Limits. The defaults include 30 sign-up and sign-in requests per 5 minutes per IP address and 2 emails an hour through the built-in email provider.3
Cloudflare
A Cloudflare rate limiting rule matches requests by an expression, such as a path, and sets the counting period, the requests allowed in it, and how long further requests are blocked. On the Free plan a zone has one rule, counted per IP over 10 seconds with a 10-second block.11
Prompt for a coding agent
Find every route in this project that signs a user in or up, resets a password, sends or checks a one-time code, sends email or text messages, or calls a paid API. Where no rate limit runs before the work starts, add one, keyed on the user ID when signed in and on the IP address otherwise, with a shared store if the app runs on several processes or as serverless functions. Refuse with status 429 and a Retry-After header. List each route you changed, the limit you chose, and any route you were unsure about.
In SecFlash
SecFlash reports a sign-in, sign-up, password-reset or one-time-code route with no rate limit in the code as SF-SEC-727 on the Security page. API calls that are not rate limited are reported as SF-EXP-208 on the Exposure page, and a Cloudflare zone with no rate limiting rule as SF-CLD-307 on the Cloud page.
See also
- Brute-force attack
- Credential stuffing
- API scraping
- Denial of wallet
- Denial-of-service attack
- Authentication
- Multi-factor authentication
- Defence in depth
- Category: Security concepts
References
- a b c d MDN contributors (22 June 2026). "429 Too Many Requests". MDN Web Docs. Mozilla.
- a b "Ratelimit TypeScript SDK Algorithms". Upstash Docs. Upstash.
- a b c "Rate limits". Supabase Docs. Supabase.
- back "CWE-799: Improper Control of Interaction Frequency". Common Weakness Enumeration. MITRE.
- a b "NIST SP 800-63B-4, Digital Identity Guidelines: Authentication and Authenticator Management". NIST.
- a b c OWASP (2023). "API4:2023 Unrestricted Resource Consumption". OWASP API Security Top 10.
- back "CWE-307: Improper Restriction of Excessive Authentication Attempts". Common Weakness Enumeration. MITRE.
- a b c "express-rate-limit". GitHub.
- back "Credential Stuffing Prevention Cheat Sheet". OWASP Cheat Sheet Series.
- back "CWE-770: Allocation of Resources Without Limits or Throttling". Common Weakness Enumeration 5.0. MITRE.
- a b "Rate limiting rules". Cloudflare WAF docs. Cloudflare.
- back "Ratelimit: Getting started". Upstash Docs. Upstash.
External links
- express-rate-limit documentation, express-rate-limit
- RFC 6585, section 4: 429 Too Many Requests, RFC Editor