Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(security): mask IPv6 client addresses to subnet prefix in rate limiter
Saksham5b24f58e2726
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5361d7ee9..c3324a9c6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The rate limiter masks IPv6 client addresses to their /64 subnet prefix (or the configured `ipv6SubnetPrefix`), so a client cannot evade per-client rate limits by rotating addresses within its IPv6 allocation ([#1686](https://github.com/rmyndharis/OpenWA/issues/1686)).- whatsapp-web.js chat history resolves each message's sender through `getContact()`, as the live `message` handler already does, so a group participant outside the account's contacts gets a sender label in history too. Thanks @TanmayChachra.- A WebSocket client that emits without an ack callback now receives command replies at all. The gateway answered by returning a frame, which Socket.IO delivers through the ack callback and nowhere else, so a client written to the documented `message` event saw no subscribe confirmation, no pong, and none of the refusals, including the session-scope denial. Replies now go out on `message` as well as through the ack.- A Baileys message the account sent from its phone is no longer lost when the message store cannot be read. The repeat-delivery check threw on a locked database or an unparseable row and the message was dropped with it, and WhatsApp does not re-deliver one it has already acked; the check now fails open, so at worst such a message is reported twice rather than never.diff --git a/src/common/security/proxy-aware-throttler.guard.spec.ts b/src/common/security/proxy-aware-throttler.guard.spec.tsindex e9a0a6bb9..e642a4bf7 100644--- a/src/common/security/proxy-aware-throttler.guard.spec.ts+++ b/src/common/security/proxy-aware-throttler.guard.spec.ts@@ -22,8 +22,9 @@ describe('ProxyAwareThrottlerGuard.getTracker', () => {});afterEach(() => warn.mockRestore());- // getTracker uses only process.env + the pure resolveClientIp (no `this`), so we can- // invoke it on a prototype instance without the throttler's storage/reflector deps.+ // getTracker uses process.env, resolveClientIp, and this.ipv6SubnetPrefix (which defaults to+ // 64 when undefined), so we can invoke it on a prototype instance without the throttler's+ // storage/reflector deps.const guard = Object.create(ProxyAwareThrottlerGuard.prototype) as ProxyAwareThrottlerGuard;const track = (req: unknown): Promise<string> =>(guard as unknown as { getTracker(r: unknown): Promise<string> }).getTracker(req);@@ -55,6 +56,52 @@ describe('ProxyAwareThrottlerGuard.getTracker', () => {// peer 203.0.113.9 is NOT a trusted proxy → its XFF is ignored, key on the socket IPexpect(await track(reqFrom('203.0.113.9', '10.0.0.1'))).toBe('203.0.113.9');});++ it('masks directly connected IPv6 clients to a /64 so addresses in the same subnet share a tracker', async () => {+ delete process.env.TRUSTED_PROXIES;+ const a = await track(reqFrom('2001:db8:0:1::1'));+ const b = await track(reqFrom('2001:db8:0:1:dead:beef:1:2'));+ expect(a).toBe('2001:db8:0:1::/64');+ expect(b).toBe('2001:db8:0:1::/64');+ expect(a).toBe(b);+ });++ it('gives directly connected IPv6 clients in different /64 subnets independent buckets', async () => {+ delete process.env.TRUSTED_PROXIES;+ const a = await track(reqFrom('2001:db8:0:1::1'));+ const b = await track(reqFrom('2001:db8:0:2::1'));+ expect(a).toBe('2001:db8:0:1::/64');+ expect(b).toBe('2001:db8:0:2::/64');+ expect(a).not.toBe(b);+ });++ it('masks forwarded IPv6 clients behind a trusted proxy to a /64', async () => {+ process.env.TRUSTED_PROXIES = '172.18.0.0/16';+ const a = await track(reqFrom('172.18.0.5', '2001:db8:0:1::1'));+ const b = await track(reqFrom('172.18.0.5', '2001:db8:0:1::2'));+ const c = await track(reqFrom('172.18.0.5', '2001:db8:0:2::1'));+ expect(a).toBe('2001:db8:0:1::/64');+ expect(b).toBe('2001:db8:0:1::/64');+ expect(a).toBe(b);+ expect(c).toBe('2001:db8:0:2::/64');+ expect(a).not.toBe(c);+ });++ it('leaves IPv4, IPv4-mapped, and loopback addresses unchanged', async () => {+ delete process.env.TRUSTED_PROXIES;+ expect(await track(reqFrom('203.0.113.9'))).toBe('203.0.113.9');+ expect(await track(reqFrom('::ffff:203.0.113.9'))).toBe('203.0.113.9');+ expect(await track(reqFrom('::1'))).toBe('::1');+ });++ it('honors a custom ipv6SubnetPrefix configured on the guard', async () => {+ delete process.env.TRUSTED_PROXIES;+ const customGuard = Object.create(ProxyAwareThrottlerGuard.prototype) as ProxyAwareThrottlerGuard;+ Object.assign(customGuard, { ipv6SubnetPrefix: 48 });+ const trackCustom = (req: unknown): Promise<string> =>+ (customGuard as unknown as { getTracker(r: unknown): Promise<string> }).getTracker(req);+ expect(await trackCustom(reqFrom('2001:db8:0:1::1'))).toBe('2001:db8::/48');+ });});/**diff --git a/src/common/security/proxy-aware-throttler.guard.ts b/src/common/security/proxy-aware-throttler.guard.tsindex 166f6c76c..bb3d740b4 100644--- a/src/common/security/proxy-aware-throttler.guard.ts+++ b/src/common/security/proxy-aware-throttler.guard.ts@@ -1,5 +1,5 @@import { ExecutionContext, Injectable } from '@nestjs/common';-import { ThrottlerGuard, ThrottlerLimitDetail } from '@nestjs/throttler';+import { normalizeIp, ThrottlerGuard, ThrottlerLimitDetail } from '@nestjs/throttler';import { resolveClientIp, RequestLike } from '../utils/ip';import { createLogger } from '../services/logger.service';@@ -25,6 +25,11 @@ const LIBRARY_DEFAULT_SKIP_KEY = 'THROTTLER:SKIPdefault';* This reuses the same trusted-proxy-aware resolution as ApiKeyGuard: with no* TRUSTED_PROXIES configured it falls back to the socket IP (no behavior change and no* XFF-spoofing risk); with trusted proxies it keys on the real forwarded client IP.+ *+ * The resolved address is normalized through `@nestjs/throttler`'s `normalizeIp`:+ * IPv6 addresses are masked to `ipv6SubnetPrefix` (default /64) so that an IPv6 client+ * cannot evade rate limits by rotating addresses within its subnet, while IPv4,+ * IPv4-mapped, and loopback addresses remain unchanged.*/@Injectable()export class ProxyAwareThrottlerGuard extends ThrottlerGuard {@@ -57,7 +62,8 @@ export class ProxyAwareThrottlerGuard extends ThrottlerGuard {);}}- return Promise.resolve(resolveClientIp(req as unknown as RequestLike, trustedProxies));+ const clientIp = resolveClientIp(req as unknown as RequestLike, trustedProxies);+ return Promise.resolve(normalizeIp(clientIp, this.ipv6SubnetPrefix));}/**