Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): cap the size of a client frame

Yudhi Armyndharisccf33dd2ca61
diff --git a/src/modules/events/events.gateway.ts b/src/modules/events/events.gateway.ts
index c482ebb30..434d914f0 100644
--- a/src/modules/events/events.gateway.ts
+++ b/src/modules/events/events.gateway.ts
@@ -156,11 +156,20 @@ const EVICTION_MESSAGES: Record<ApiKeyEvictionReason, string> = {
expired: 'API key has expired',
};
+/**
+ * Largest client frame the socket accepts, in bytes. A client only sends subscribe, unsubscribe and ping
+ * frames, and the longest legal one (the longest session and request id, every event) is under 1 KB.
+ * Every frame is decoded and walked before it is answered, so the Socket.IO default of 1 MB is far more
+ * than any client needs.
+ */
+export const WS_MAX_FRAME_BYTES = 8 * 1024;
+
@WebSocketGateway({
cors: {
origin: resolveWsCorsOrigin(),
},
namespace: '/events',
+ maxHttpBufferSize: WS_MAX_FRAME_BYTES,
})
export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, OnModuleDestroy {
@WebSocketServer()
diff --git a/src/modules/events/redis-io.adapter.spec.ts b/src/modules/events/redis-io.adapter.spec.ts
index 9431c8cc9..169be78d7 100644
--- a/src/modules/events/redis-io.adapter.spec.ts
+++ b/src/modules/events/redis-io.adapter.spec.ts
@@ -142,6 +142,20 @@ describe('RedisIoAdapter', () => {
}
});
+ // Gateway options such as the client frame bound reach the Socket.IO server through this call.
+ it.each(['true', undefined])('hands the server options to the base server untouched (%s)', enabled => {
+ if (enabled === undefined) delete process.env.REDIS_ENABLED;
+ else process.env.REDIS_ENABLED = enabled;
+ const spy = withBaseServer(fakeServer().server);
+ try {
+ const options = { maxHttpBufferSize: 8192 };
+ new RedisIoAdapter({} as never).createIOServer(0, options as never);
+ expect(spy).toHaveBeenCalledWith(0, options);
+ } finally {
+ spy.mockRestore();
+ }
+ });
+
it('attaches the Redis adapter with a pub/sub pair when Redis is enabled', () => {
process.env.REDIS_ENABLED = 'true';
const { server, adapterFn } = fakeServer();
diff --git a/src/modules/events/ws-frame-limit.spec.ts b/src/modules/events/ws-frame-limit.spec.ts
new file mode 100644
index 000000000..05e165f49
--- /dev/null
+++ b/src/modules/events/ws-frame-limit.spec.ts
@@ -0,0 +1,125 @@
+import type { Server as HttpServer } from 'node:http';
+import type { AddressInfo } from 'node:net';
+import { ConfigService } from '@nestjs/config';
+import type { INestApplication } from '@nestjs/common';
+import { Test } from '@nestjs/testing';
+import WebSocket from 'ws';
+// The gateway first: auth.service imports it back, and loading that side first leaves the gateway's
+// constructor metadata with an undefined AuthService.
+import { EventsGateway, WS_MAX_FRAME_BYTES } from './events.gateway';
+import { AuditService } from '../audit/audit.service';
+import { AuthService } from '../auth/auth.service';
+import { ChatScopeService } from '../auth/chat-scope.service';
+import { SUBSCRIBABLE_EVENTS } from './dto/ws-messages.dto';
+import { RedisIoAdapter } from './redis-io.adapter';
+
+/**
+ * What matters is what a real socket does with a frame, so this boots the gateway behind the adapter the
+ * application installs and talks raw Engine.IO to it. The size bound is a Socket.IO server option set on
+ * the gateway, so only a booted server shows that it reaches the server and closes the connection.
+ */
+describe('/events client frame size', () => {
+ const key = { id: 'k1', name: 'viewer', role: 'viewer', isActive: true, allowedSessions: null, allowedChats: null };
+ let app: INestApplication;
+ let port: number;
+ let peers: WebSocket[];
+
+ beforeEach(async () => {
+ delete process.env.REDIS_ENABLED;
+ jest.spyOn(console, 'log').mockImplementation(() => undefined);
+ jest.spyOn(console, 'warn').mockImplementation(() => undefined);
+ peers = [];
+ const moduleRef = await Test.createTestingModule({
+ providers: [
+ EventsGateway,
+ ChatScopeService,
+ { provide: AuthService, useValue: { validateApiKey: () => Promise.resolve(key) } },
+ { provide: AuditService, useValue: { logWarn: () => Promise.resolve() } },
+ { provide: ConfigService, useValue: { get: (_key: string, fallback?: unknown) => fallback } },
+ ],
+ }).compile();
+ app = moduleRef.createNestApplication();
+ app.useWebSocketAdapter(new RedisIoAdapter(app));
+ await app.listen(0, '127.0.0.1');
+ port = ((app.getHttpServer() as HttpServer).address() as AddressInfo).port;
+ });
+
+ afterEach(async () => {
+ for (const ws of peers) ws.terminate();
+ await app.close();
+ jest.restoreAllMocks();
+ });
+
+ /** An authenticated /events peer: resolves once the namespace has accepted it. */
+ const connect = async (): Promise<{ ws: WebSocket; frames: string[]; closed: Promise<number> }> => {
+ const ws = new WebSocket(`ws://127.0.0.1:${port}/socket.io/?EIO=4&transport=websocket`);
+ peers.push(ws);
+ const frames: string[] = [];
+ const closed = new Promise<number>(resolve => ws.on('close', code => resolve(code)));
+ const joined = new Promise<void>(resolve => {
+ ws.on('message', (data: Buffer) => {
+ const frame = data.toString();
+ frames.push(frame);
+ if (frame === '2') ws.send('3');
+ if (frame.startsWith('0')) ws.send('40/events,{"apiKey":"k"}');
+ if (frame.startsWith('40/events,')) resolve();
+ });
+ });
+ await joined;
+ return { ws, frames, closed };
+ };
+
+ const subscribeFrame = (subscribe: Record<string, unknown>): string =>
+ `42/events,${JSON.stringify(['message', { type: 'subscribe', ...subscribe }])}`;
+
+ const waitFor = async (predicate: () => boolean): Promise<boolean> => {
+ for (let waited = 0; waited < 2000 && !predicate(); waited += 20) await new Promise(r => setTimeout(r, 20));
+ return predicate();
+ };
+
+ it('answers a subscribe', async () => {
+ const { ws, frames } = await connect();
+
+ ws.send(subscribeFrame({ sessionId: '*', events: ['session.status'] }));
+
+ expect(await waitFor(() => frames.some(frame => frame.includes('"type":"subscribed"')))).toBe(true);
+ });
+
+ // The bound has to leave the longest frame a client can legitimately send untouched: the longest session id
+ // and request id the gateway accepts, and every event it can subscribe to.
+ it('answers the longest legal subscribe, which is a small fraction of the bound', async () => {
+ const { ws, frames } = await connect();
+ const frame = subscribeFrame({
+ sessionId: 'a'.repeat(128),
+ requestId: 'r'.repeat(128),
+ events: [...SUBSCRIBABLE_EVENTS, '*'],
+ });
+ expect(Buffer.byteLength(frame)).toBeLessThan(WS_MAX_FRAME_BYTES / 4);
+
+ ws.send(frame);
+
+ expect(await waitFor(() => frames.some(f => f.includes('"type":"subscribed"')))).toBe(true);
+ });
+
+ // A polling client batches the frames it has queued into one POST, and splits the batch to the limit the server
+ // advertises in its first answer. Without it a client with many subscriptions to send would exceed the bound.
+ it('advertises the bound to polling clients and refuses a request body over it', async () => {
+ const base = `http://127.0.0.1:${port}/socket.io/?EIO=4&transport=polling`;
+ const open = await (await fetch(base)).text();
+ const handshake = JSON.parse(open.slice(1)) as { sid: string; maxPayload: number };
+ expect(handshake.maxPayload).toBe(WS_MAX_FRAME_BYTES);
+
+ const post = (body: string): Promise<Response> => fetch(`${base}&sid=${handshake.sid}`, { method: 'POST', body });
+ expect((await post('40/events,{"apiKey":"k"}')).status).toBe(200);
+ expect((await post(`42/events,["message",{"x":"${'a'.repeat(WS_MAX_FRAME_BYTES)}"}]`)).status).toBe(413);
+ });
+
+ it('closes a connection that sends a frame over the bound, without answering it', async () => {
+ const { ws, frames, closed } = await connect();
+
+ ws.send(subscribeFrame({ sessionId: '*', events: Array.from({ length: 20_000 }, (_, i) => `e${i}`) }));
+
+ expect(await closed).toBe(1009);
+ expect(frames.some(frame => frame.includes('"type":"subscribed"') || frame.includes('"type":"error"'))).toBe(false);
+ });
+});