Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(events): withhold group join requests from chat-restricted keys
Yudhi Armyndhariscf348ac4a71e
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 5b390c2c1..5dccba882 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -7145,7 +7145,7 @@ A subscribe request whose `events` array contains no recognized name (after filt### Wildcards and scoping-- **Chat-restricted keys receive only events for `allowedChats`.** Message and presence events require `data.chatId`; group events require `data.groupId`. Persisted phone/LID aliases are matched at delivery time, including mappings learned after subscribing. An event without a resolvable chat is omitted. Account events, calls, statuses, and QR codes are omitted even with `events: ["*"]`; a request containing only account events answers `FORBIDDEN_EVENTS`. These filters apply across Redis replicas. A changed chat authorization disconnects the socket on its next subscribe.+- **Chat-restricted keys receive only events for `allowedChats`.** Message and presence events require `data.chatId`; group events require `data.groupId`. Persisted phone/LID aliases are matched at delivery time, including mappings learned after subscribing. An event without a resolvable chat is omitted. Account events, calls, statuses, QR codes, and `group.join_request` (it names people outside the allowed chats, as the membership-request routes do) are omitted even with `events: ["*"]`; a request containing only account events answers `FORBIDDEN_EVENTS`. These filters apply across Redis replicas. A changed chat authorization disconnects the socket on its next subscribe.- **`sessionId: "*"`** subscribes to every session; **`events: ["*"]`** subscribes to every subscribable event. They combine (e.g. `"*"` + `["*"]` = every event of every session).- The API key is **re-validated on every `subscribe`** (not just at connect), so a key revoked or expired mid-connection is caught — the server replies `UNAUTHORIZED` and disconnects.- **Per-key session scope is enforced** against the fresh key: a key restricted via `allowedSessions` may NOT subscribe to `"*"` and may NOT subscribe to a session outside its allowlist — either is rejected with `FORBIDDEN_SESSION`. An unrestricted key (no `allowedSessions`) may subscribe to anything, including `"*"`.diff --git a/src/modules/events/events.gateway.chat-scope.spec.ts b/src/modules/events/events.gateway.chat-scope.spec.tsindex cd90b1856..5ef840db1 100644--- a/src/modules/events/events.gateway.chat-scope.spec.ts+++ b/src/modules/events/events.gateway.chat-scope.spec.ts@@ -123,6 +123,23 @@ describe('chat-scoped WebSocket rooms', () => {expect(events(unrestricted)).toHaveLength(1);});+ it('keeps group join requests away from a restricted key, since they name people outside its chats', async () => {+ const group = '[email protected]';+ const restricted = await connect('restricted', [group]);+ const unrestricted = await connect('unrestricted', null);+ gateway.emitGroupJoinRequest('s1', { groupId: group, participantIds: ['[email protected]'] });+ gateway.emitGroupUpdate('s1', { groupId: group, subject: 'Renamed' });+ await flush();+ // The REST membership-request routes refuse these keys for the same reason: a requester is, by+ // definition, not in the allowed chats, and listing the queue names them.+ expect(events(restricted).map(frame => frame.payload.event)).toEqual(['group.update']);+ expect(events(unrestricted).map(frame => frame.payload.event)).toEqual(['group.join_request', 'group.update']);+ expect(await subscribe(restricted, ['group.join_request'])).toMatchObject({+ type: 'error',+ code: 'FORBIDDEN_EVENTS',+ });+ });+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 d472026a4..37e4bf712 100644--- a/src/modules/events/events.gateway.ts+++ b/src/modules/events/events.gateway.ts@@ -63,6 +63,9 @@ import type {import { SUBSCRIBABLE_EVENTS, buildRoomName } from './dto/ws-messages.dto';import type { DeliveryStatus } from '../../engine/interfaces/whatsapp-engine.interface';+// The events a chat-restricted key may receive, each delivered only for a chat inside its allowlist.+// group.join_request is not one of them: it names the people asking to join, who are outside the+// allowed chats by definition, and the REST membership-request routes refuse these keys for that reason.const CHAT_EVENTS = new Set(['message.received','message.sent',@@ -73,7 +76,6 @@ const CHAT_EVENTS = new Set(['group.join','group.leave','group.update',- 'group.join_request','presence.update','event.response',]);