Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
docs(security): describe rate limits as per route and client IP
Yudhi Armyndharisf64167bc6144
diff --git a/.env.example b/.env.exampleindex e61cdcb2f..419dbcf75 100644--- a/.env.example+++ b/.env.example@@ -621,7 +621,7 @@ WEBHOOK_SSRF_PROTECT=true# =============================================================================# RATE LIMITING (all TTLs are in MILLISECONDS)# =============================================================================-# The "medium" tier is the one enforced on the API; short/long are optional extra tiers.+# All three tiers are enforced, each counted per route handler and client IP; unset values use the defaults.# RATE_LIMIT_MEDIUM_TTL=60000 # window in ms (default 60000 = 60s)# RATE_LIMIT_MEDIUM_LIMIT=100 # max requests per window# RATE_LIMIT_SHORT_TTL=1000 # 1s burst window (default)diff --git a/docs/04-security-design.md b/docs/04-security-design.mdindex 7b565d074..73380752a 100644--- a/docs/04-security-design.md+++ b/docs/04-security-design.md@@ -251,7 +251,7 @@ flowchart LRRL -->|Under Limit| APP[Application]RL -->|Over Limit| ERR[429 Too Many Requests]- subgraph Limits["Global windows (per client IP)"]+ subgraph Limits["Windows (per route, per client IP)"]T1[short: 10 / 1s]T2[medium: 100 / 60s]T3[long: 1000 / 1h]@@ -260,7 +260,7 @@ flowchart LR### Windows-All limits are **global and per client IP** (resolved through `TRUSTED_PROXIES`), applied by a global `ThrottlerGuard`. There is **no per-endpoint limit table** — these three windows apply to every non-exempt route, and exceeding any one returns `429 Too Many Requests`:+Each window is counted **per route handler per client IP** (the IP resolved through `TRUSTED_PROXIES`) by a global `ThrottlerGuard`, so a client gets the full budget on every endpoint; for an aggregate per-client cap, add a limiter at your reverse proxy. There is **no per-endpoint limit table**: these three windows apply to every non-exempt route, and exceeding any one returns `429 Too Many Requests`:| Window | Default limit | Window length | Env overrides || -------- | ------------- | ------------- | --------------------------------------------------- |diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.mdindex 6fb2c8655..1e76d610b 100644--- a/docs/10-devops-infrastructure.md+++ b/docs/10-devops-infrastructure.md@@ -492,7 +492,7 @@ WEBHOOK_DISPATCH_MAX_QUEUED=1000# ===========================================# RATE LIMITING# ===========================================-# Three global per-IP windows (short/medium/long); defaults shown+# Three windows (short/medium/long), each counted per route and client IP; defaults shownRATE_LIMIT_MEDIUM_TTL=60000RATE_LIMIT_MEDIUM_LIMIT=100```diff --git a/docs/12-troubleshooting-faq.md b/docs/12-troubleshooting-faq.mdindex 2e2354b91..198b4422f 100644--- a/docs/12-troubleshooting-faq.md+++ b/docs/12-troubleshooting-faq.md@@ -677,7 +677,7 @@ curl -H "X-API-Key: $API_KEY" \curl -H "X-API-Key: $API_KEY" \http://localhost:2785/api/infra/status-# Rate limiting is global (throttler, env-configured) — there is no per-session rate-limit endpoint+# Rate limiting is env-configured, per route and client IP; there is no per-session rate-limit endpoint```**Common Causes:**diff --git a/src/common/security/proxy-aware-throttler.guard.spec.ts b/src/common/security/proxy-aware-throttler.guard.spec.tsindex e642a4bf7..fba45099c 100644--- a/src/common/security/proxy-aware-throttler.guard.spec.ts+++ b/src/common/security/proxy-aware-throttler.guard.spec.ts@@ -1,5 +1,6 @@import type { ExecutionContext } from '@nestjs/common';-import type { Reflector } from '@nestjs/core';+import { Reflector } from '@nestjs/core';+import { ThrottlerException, ThrottlerStorageService } from '@nestjs/throttler';import { ProxyAwareThrottlerGuard } from './proxy-aware-throttler.guard';/**@@ -276,3 +277,41 @@ describe('ProxyAwareThrottlerGuard.throwThrottlingException', () => {expect(threw).toBe(true);});});++/**+ * Pins the bucket scope the docs describe: every window is counted per route handler per client IP+ * (the library's generateKey includes the controller and handler names). docs/04 section 4.6,+ * docs/10 and docs/12 say so; a generateKey override that changes the scope must update them too.+ */+describe('ProxyAwareThrottlerGuard bucket scope', () => {+ class SessionsController {}+ const list = function list(): void {};+ const get = function get(): void {};+ let storage: ThrottlerStorageService;++ afterEach(() => storage.onApplicationShutdown());++ it('counts each route handler separately for one client IP', async () => {+ storage = new ThrottlerStorageService();+ const guard = new ProxyAwareThrottlerGuard(+ { throttlers: [{ name: 'short', ttl: 1000, limit: 10 }] },+ storage,+ new Reflector(),+ );+ await guard.onModuleInit();+ delete process.env.TRUSTED_PROXIES;+ const req = { ip: '203.0.113.9', socket: { remoteAddress: '203.0.113.9' }, headers: {} };+ const contextFor = (handler: () => void): ExecutionContext =>+ ({+ getHandler: () => handler,+ getClass: () => SessionsController,+ switchToHttp: () => ({ getRequest: () => req, getResponse: () => ({ header: () => undefined }) }),+ }) as unknown as ExecutionContext;+ for (let i = 0; i < 10; i++) {+ await expect(guard.canActivate(contextFor(list))).resolves.toBe(true);+ await expect(guard.canActivate(contextFor(get))).resolves.toBe(true);+ }+ await expect(guard.canActivate(contextFor(list))).rejects.toThrow(ThrottlerException);+ await expect(guard.canActivate(contextFor(get))).rejects.toThrow(ThrottlerException);+ });+});