Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): count only subscription rooms toward the socket cap

Yudhi Armyndharis33786fdfad8c
diff --git a/src/modules/events/events.gateway.spec.ts b/src/modules/events/events.gateway.spec.ts
index ccad33fef..a3327f787 100644
--- a/src/modules/events/events.gateway.spec.ts
+++ b/src/modules/events/events.gateway.spec.ts
@@ -65,7 +65,8 @@ describe('EventsGateway connection auth + subscribe re-validation', () => {
disconnect: jest.fn(),
join: jest.fn(),
leave: jest.fn(),
- rooms: new Set<string>(),
+ // Socket.IO puts every socket in a room named after its own id.
+ rooms: new Set<string>(['sock-1']),
});
// Subscription rooms joined by the socket; the QR-denied role room is not a subscription.
const sessionRoomJoins = (s: MockSocket): string[] =>
@@ -267,6 +268,18 @@ describe('EventsGateway connection auth + subscribe re-validation', () => {
expect(again.type).toBe('subscribed');
});
+ it('counts only subscription rooms toward the cap, not the own-id or role rooms', async () => {
+ authService.validateApiKey.mockResolvedValue({ name: 'k', allowedSessions: null });
+ const sock = makeSocket({ apiKey: 'good' });
+ await gateway.handleConnection(asSocket(sock));
+ sock.rooms.add(QR_DENIED_ROOM);
+ for (let i = 0; i < 4095; i++) sock.rooms.add(buildRoomName(`s${i}`, '*'));
+
+ const res = (await gateway.handleMessage(asSocket(sock), subscribeMsg('sess-1', ['*']))) as WSSubscribedResponse;
+ expect(res.type).toBe('subscribed');
+ expect(sessionRoomJoins(sock)).toEqual([buildRoomName('sess-1', '*')]);
+ });
+
it('pushes a command reply on the message event, not only through the ack callback', async () => {
// The Socket.IO adapter delivers a handler's return value through the ack callback and nothing
// else, so a client that emits without one (the dashboard, and the documented example client)
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index 2345e0242..37efe4f0a 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -92,7 +92,7 @@ const QR_ALLOWED_ROLES: ReadonlySet<string> = new Set([ApiKeyRole.OPERATOR, ApiK
/**
* Subscription rooms live until the socket disconnects, so their names and count are bounded: a session
* id is a uuid (any id the engines accept is isSafeSessionName), and one socket holds at most this many
- * rooms, far above every event of every session a client would follow.
+ * subscription rooms, far above every event of every session a client would follow.
*/
const MAX_SUBSCRIBE_SESSION_ID_LENGTH = 128;
const MAX_ROOMS_PER_SOCKET = 4096;
@@ -571,8 +571,10 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
);
}
+ // Only subscription rooms count: the socket also sits in its own id room and may hold a role room.
+ const held = [...client.rooms].filter(room => room.startsWith('session:')).length;
const newRooms = validEvents.filter(event => !client.rooms.has(buildRoomName(sessionId, event))).length;
- if (client.rooms.size + newRooms > MAX_ROOMS_PER_SOCKET) {
+ 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`,