Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): answer an empty WebSocket frame with INVALID_MESSAGE

Yudhi Armyndharisce8c9a5b63ac
diff --git a/src/modules/events/events.gateway.spec.ts b/src/modules/events/events.gateway.spec.ts
index a3327f787..7537d083e 100644
--- a/src/modules/events/events.gateway.spec.ts
+++ b/src/modules/events/events.gateway.spec.ts
@@ -316,6 +316,16 @@ describe('EventsGateway connection auth + subscribe re-validation', () => {
const unknown = await gateway.handleMessage(asSocket(sock), { type: 'nonsense' } as unknown as WSClientMessage);
expect((unknown as WSErrorResponse).code).toBe('INVALID_MESSAGE');
expect(sock.emit).toHaveBeenCalledWith('message', unknown);
+
+ // A 'message' emitted with no payload, or with null, is answered like any unknown frame rather
+ // than throwing inside the handler.
+ for (const nil of [undefined, null]) {
+ sock.emit.mockClear();
+ const invalid = await gateway.handleMessage(asSocket(sock), nil);
+ expect((invalid as WSErrorResponse).code).toBe('INVALID_MESSAGE');
+ expect(sock.emit).toHaveBeenCalledTimes(1);
+ expect(sock.emit).toHaveBeenCalledWith('message', invalid);
+ }
});
it('forbids a session-scoped key from subscribing to the * wildcard', async () => {
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index 37efe4f0a..d1f244214 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -454,7 +454,9 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
}
@SubscribeMessage('message')
- async handleMessage(@ConnectedSocket() client: Socket, @MessageBody() message: WSClientMessage) {
+ // A client may emit 'message' with no payload or with null, so the body is typed as possibly nil and
+ // every read is guarded: such a frame answers INVALID_MESSAGE instead of throwing in the handler.
+ async handleMessage(@ConnectedSocket() client: Socket, @MessageBody() message: WSClientMessage | null | undefined) {
// Per-key token bucket on every inbound frame. Keyed by the validated key id; a socket
// whose handshake validation is still in flight has no key yet and is metered by IP.
// Over-budget frames get an error frame back and are NOT dispatched to a handler — in
@@ -463,7 +465,7 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
(client.data as { apiKey?: Pick<ApiKey, 'id'> } | undefined)?.apiKey?.id ??
limiterKeyForIp(this.resolveClientIp(client));
if (!this.frameLimiter.allow(frameSubject)) {
- const requestId = (message as { requestId?: string } | undefined)?.requestId;
+ const requestId = (message as { requestId?: string } | null | undefined)?.requestId;
this.noteRateLimitViolation('frame', {
apiKeyId: (client.data as { apiKey?: Pick<ApiKey, 'id'> } | undefined)?.apiKey?.id,
ipAddress: this.resolveClientIp(client),
@@ -471,7 +473,7 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
return this.reply(client, this.createError('RATE_LIMITED', 'Frame rate limit exceeded, slow down', requestId));
}
- switch (message.type) {
+ switch (message?.type) {
case 'subscribe':
return this.reply(client, await this.handleSubscribe(client, message));
case 'unsubscribe':
@@ -481,7 +483,11 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
default:
return this.reply(
client,
- this.createError('INVALID_MESSAGE', `Unknown message type`, (message as { requestId?: string }).requestId),
+ this.createError(
+ 'INVALID_MESSAGE',
+ `Unknown message type`,
+ (message as { requestId?: string } | null | undefined)?.requestId,
+ ),
);
}
}