forked from Split-Naira/SplitNaira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate-limit.ts
More file actions
85 lines (77 loc) · 2.8 KB
/
Copy pathrate-limit.ts
File metadata and controls
85 lines (77 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Rate limiting middleware (#290 — DDoS protection).
*
* Layered strategy:
* 1. `globalLimiter` — hard ceiling on all inbound traffic. Applied first
* at the app level so it catches every route including unknown paths.
* 2. `readLimiter` — general read endpoints.
* 3. `writeLimiter` — state-changing / mutation endpoints.
* 4. `adminLimiter` — admin-only surfaces.
* 5. `authLimiter` — strict per-IP limit for wallet / auth flows to
* prevent credential-stuffing and replay-attack bursts.
*
* All limits are configurable via environment variables so they can be
* tightened in production without a code deploy.
*/
import rateLimit from "express-rate-limit";
import type { Request, Response } from "express";
function rateLimitHandler(req: Request, res: Response) {
return res.status(429).json({
error: "rate_limited",
code: "RATE_LIMITED",
message: "Too many requests. Please try again later.",
retryAfter: res.getHeader("Retry-After"),
requestId: res.locals.requestId as string | undefined,
});
}
const WINDOW_MS = Number(process.env.RATE_LIMIT_WINDOW_MS ?? 15 * 60 * 1000);
/**
* Global safety-net limiter — applied to EVERY route.
* Protects the process from catastrophic volumetric floods.
* Default: 500 req / 15 min per IP.
*/
export const globalLimiter = rateLimit({
windowMs: WINDOW_MS,
limit: Number(process.env.RATE_LIMIT_GLOBAL_MAX ?? 500),
standardHeaders: true,
legacyHeaders: false,
handler: rateLimitHandler,
// Skip well-known health check calls from internal monitors
skip: (req) => req.path === "/health" && req.method === "GET",
});
/** General read endpoints — 100 requests per 15 minutes. */
export const readLimiter = rateLimit({
windowMs: WINDOW_MS,
limit: Number(process.env.RATE_LIMIT_MAX ?? 100),
standardHeaders: true,
legacyHeaders: false,
handler: rateLimitHandler,
});
/** Write / mutation endpoints — 30 requests per 15 minutes. */
export const writeLimiter = rateLimit({
windowMs: WINDOW_MS,
limit: Number(process.env.RATE_LIMIT_WRITE_MAX ?? 30),
standardHeaders: true,
legacyHeaders: false,
handler: rateLimitHandler,
});
/** Admin endpoints — 20 requests per 15 minutes. */
export const adminLimiter = rateLimit({
windowMs: WINDOW_MS,
limit: Number(process.env.RATE_LIMIT_ADMIN_MAX ?? 20),
standardHeaders: true,
legacyHeaders: false,
handler: rateLimitHandler,
});
/**
* Auth / wallet endpoints — strict burst cap to prevent credential stuffing
* and replay-attack floods.
* Default: 10 requests per 5 minutes per IP.
*/
export const authLimiter = rateLimit({
windowMs: Number(process.env.RATE_LIMIT_AUTH_WINDOW_MS ?? 5 * 60 * 1000),
limit: Number(process.env.RATE_LIMIT_AUTH_MAX ?? 10),
standardHeaders: true,
legacyHeaders: false,
handler: rateLimitHandler,
});