|
| 1 | +import { Request, Response, NextFunction } from 'express' |
| 2 | +import { redisConnection } from '../cache/redis.js' |
| 3 | + |
| 4 | +export interface RateLimitOptions { |
| 5 | + /** Redis key namespace, e.g. 'ratelimit:login' */ |
| 6 | + namespace: string |
| 7 | + /** Max requests allowed in the window */ |
| 8 | + max: number |
| 9 | + /** Window in seconds */ |
| 10 | + windowSec: number |
| 11 | + /** Function to extract tenant identifier from request */ |
| 12 | + getTenantId?: (req: Request) => string | undefined |
| 13 | + /** Function to extract IP address from request */ |
| 14 | + getIp?: (req: Request) => string | undefined |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Express middleware for tenant/IP rate limiting using Redis counters. |
| 19 | + * |
| 20 | + * - Supports independent limits for tenant and IP. |
| 21 | + * - Returns 429 with standard Retry-After header if exceeded. |
| 22 | + * |
| 23 | + * Usage: |
| 24 | + * app.post('/api/login', rateLimit({ ...options }), handler) |
| 25 | + */ |
| 26 | +export function rateLimit(options: RateLimitOptions) { |
| 27 | + const { |
| 28 | + namespace, |
| 29 | + max, |
| 30 | + windowSec, |
| 31 | + getTenantId = (req) => req.headers['x-api-key'] as string | undefined, |
| 32 | + getIp = (req) => req.ip, |
| 33 | + } = options |
| 34 | + |
| 35 | + return async (req: Request, res: Response, next: NextFunction) => { |
| 36 | + const redis = redisConnection.getClient() |
| 37 | + const now = Math.floor(Date.now() / 1000) |
| 38 | + const windowStart = now - (now % windowSec) |
| 39 | + |
| 40 | + const tenantId = getTenantId(req) |
| 41 | + const ip = getIp(req) |
| 42 | + |
| 43 | + // Compose Redis keys |
| 44 | + const keys: { key: string; label: string }[] = [] |
| 45 | + if (tenantId) keys.push({ key: `${namespace}:tenant:${tenantId}:${windowStart}`, label: 'tenant' }) |
| 46 | + if (ip) keys.push({ key: `${namespace}:ip:${ip}:${windowStart}`, label: 'ip' }) |
| 47 | + |
| 48 | + let exceeded = false |
| 49 | + let retryAfter = windowSec |
| 50 | + |
| 51 | + for (const { key, label } of keys) { |
| 52 | + const count = await redis.incr(key) |
| 53 | + if (count === 1) { |
| 54 | + await redis.expire(key, windowSec) |
| 55 | + } |
| 56 | + if (count > max) { |
| 57 | + exceeded = true |
| 58 | + // Calculate seconds until window resets |
| 59 | + const ttl = await redis.ttl(key) |
| 60 | + retryAfter = Math.min(retryAfter, ttl > 0 ? ttl : windowSec) |
| 61 | + res.setHeader('X-RateLimit-Limit', max) |
| 62 | + res.setHeader('X-RateLimit-Remaining', Math.max(0, max - count)) |
| 63 | + res.setHeader('X-RateLimit-Reset', now + retryAfter) |
| 64 | + res.setHeader('Retry-After', retryAfter) |
| 65 | + res.status(429).json({ |
| 66 | + error: 'RateLimitExceeded', |
| 67 | + message: `Too many requests for this ${label}. Try again later.`, |
| 68 | + retryAfter, |
| 69 | + }) |
| 70 | + return |
| 71 | + } else { |
| 72 | + res.setHeader('X-RateLimit-Limit', max) |
| 73 | + res.setHeader('X-RateLimit-Remaining', Math.max(0, max - count)) |
| 74 | + res.setHeader('X-RateLimit-Reset', now + windowSec) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + next() |
| 79 | + } |
| 80 | +} |
0 commit comments