Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(events): refuse an oversized subscribe before building its rooms
Yudhi Armyndharisb6e43fd6e8a6
diff --git a/src/modules/events/events.gateway.chat-scope.spec.ts b/src/modules/events/events.gateway.chat-scope.spec.tsindex 5ef840db1..4d98afe3c 100644--- a/src/modules/events/events.gateway.chat-scope.spec.ts+++ b/src/modules/events/events.gateway.chat-scope.spec.ts@@ -140,6 +140,35 @@ describe('chat-scoped WebSocket rooms', () => {});});+ it('refuses a subscribe that can never fit before it builds a single room name', async () => {+ const chats = Array.from({ length: 450 }, (_, index) => `62811${String(index).padStart(7, '0')}@c.us`);+ const restricted = await connect('restricted', chats);+ // The room set is only consulted once the names exist, so a read after the refusal means the+ // refused frame was still paid for in full (tens of milliseconds at a few thousand allowlist entries).+ let reads = 0;+ const joined = restricted.rooms;+ Object.defineProperty(restricted, 'rooms', {+ get: () => {+ reads += 1;+ return joined;+ },+ });+ const chatEvents = [+ 'message.received',+ 'message.sent',+ 'message.ack',+ 'message.revoked',+ 'message.reaction',+ 'message.edited',+ 'group.join',+ 'group.leave',+ 'group.update',+ 'presence.update',+ ];+ expect(await subscribe(restricted, chatEvents)).toMatchObject({ type: 'error', code: 'TOO_MANY_SUBSCRIPTIONS' });+ expect(reads).toBe(0);+ });+it('performs no chat lookups for a local stream without restricted subscribers', async () => {const previous = process.env.REDIS_ENABLED;process.env.REDIS_ENABLED = 'false';diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.tsindex 37e4bf712..24af80db7 100644--- a/src/modules/events/events.gateway.ts+++ b/src/modules/events/events.gateway.ts@@ -135,6 +135,7 @@ const MAX_SUBSCRIBE_SESSION_ID_LENGTH = 128;// A request id only correlates a reply with its request, so only a short string is ever echoed back.const MAX_REQUEST_ID_LENGTH = 128;const MAX_ROOMS_PER_SOCKET = 4096;+const TOO_MANY_SUBSCRIPTIONS_MESSAGE = `A connection may hold at most ${MAX_ROOMS_PER_SOCKET} subscriptions; unsubscribe first`;/** Why an API key's live WebSocket sockets are being torn down — drives the client-facing message. */export type ApiKeyEvictionReason = 'revoked' | 'deleted' | 'authorization_changed' | 'expired';@@ -653,6 +654,11 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGatereturn this.createError('FORBIDDEN_EVENTS', 'API key may subscribe only to chat events', requestId);}const chats = chatRestricted ? [...this.chatScope.scopeFor(subscriberKey)!.allowed] : undefined;+ // A socket holds every room it asks for, so events x chats rooms that exceed the cap can never fit.+ // Refused here, before the names are built: a large allowlist made each refused frame cost real time.+ if (chats && permittedEvents.length * chats.length > MAX_ROOMS_PER_SOCKET) {+ return this.createError('TOO_MANY_SUBSCRIPTIONS', TOO_MANY_SUBSCRIPTIONS_MESSAGE, requestId);+ }const rooms = permittedEvents.flatMap(event =>chats ? chats.map(chatId => chatRoom(sessionId, event, chatId)) : [buildRoomName(sessionId, event)],);@@ -663,11 +669,7 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGateconst held = [...client.rooms].filter(room => room.startsWith('session:')).length;const newRooms = rooms.filter(room => !client.rooms.has(room)).length;if (held + newRooms > MAX_ROOMS_PER_SOCKET) {- return this.createError(- 'TOO_MANY_SUBSCRIPTIONS',- `A connection may hold at most ${MAX_ROOMS_PER_SOCKET} subscriptions; unsubscribe first`,- requestId,- );+ return this.createError('TOO_MANY_SUBSCRIPTIONS', TOO_MANY_SUBSCRIPTIONS_MESSAGE, requestId);}// Join rooms for each session/event combination