Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): refuse an unsubscribe frame without a sessionId

Yudhi Armyndharis6a2c60bb5fbd
diff --git a/src/modules/events/events.gateway.spec.ts b/src/modules/events/events.gateway.spec.ts
index 568be6409..c5a16b817 100644
--- a/src/modules/events/events.gateway.spec.ts
+++ b/src/modules/events/events.gateway.spec.ts
@@ -365,6 +365,24 @@ describe('EventsGateway connection auth + subscribe re-validation', () => {
expect(sessionRoomJoins(sock)).toEqual([]);
});
+ it('answers an unsubscribe without a sessionId with INVALID_SESSION and leaves the rooms alone', async () => {
+ authService.validateApiKey.mockResolvedValue({ name: 'k', allowedSessions: null });
+ const sock = makeSocket({ apiKey: 'good' });
+ await gateway.handleConnection(asSocket(sock));
+ sock.rooms.add(buildRoomName('sess-1', 'message.received'));
+
+ for (const sessionId of [undefined, null, 5]) {
+ const res = (await gateway.handleMessage(asSocket(sock), {
+ type: 'unsubscribe',
+ sessionId,
+ requestId: 'r1',
+ } as unknown as WSClientMessage)) as WSErrorResponse;
+ expect(res.code).toBe('INVALID_SESSION');
+ expect(res.requestId).toBe('r1');
+ }
+ expect(sock.leave).not.toHaveBeenCalledWith(buildRoomName('sess-1', 'message.received'));
+ });
+
it('forbids a session-scoped key from subscribing to the * wildcard', async () => {
authService.validateApiKey.mockResolvedValue({ name: 'k', allowedSessions: ['sess-1'] });
const sock = makeSocket({ apiKey: 'good' });
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index b2e1090cd..65f10a3f0 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -637,8 +637,13 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
}
}
- private handleUnsubscribe(client: Socket, message: WSUnsubscribeRequest): WSUnsubscribedResponse {
+ private handleUnsubscribe(client: Socket, message: WSUnsubscribeRequest): WSUnsubscribedResponse | WSErrorResponse {
const { sessionId, requestId } = message;
+ // Same check as subscribe: a missing sessionId matched no room, left every subscription in place,
+ // and was still answered 'unsubscribed'.
+ if (!sessionId || typeof sessionId !== 'string') {
+ return this.createError('INVALID_SESSION', 'sessionId is required', requestId);
+ }
// Leave all rooms for this session
const clientRooms = Array.from(client.rooms);