feat(ws)!: bound inbound WebSocket message size and queue
Inbound WebSocket traffic was unbounded per connection: Node used ws's 100 MiB default payload, the other runtimes had no cap of their own, and the queue behind `inbound` was an unbounded channel on every runtime, so one client could exhaust memory with large messages or a slow consumer. Add `limits.maxWsMessageBytes` (default 1 MiB, close 1009) and `limits.maxWsQueue` (default 16 unread messages, close 1013), as recorded in D1. Both are server-wide and validated by createApp, which throws a TypeError for anything but a positive integer. Enforcement is shared in `boundInbound`, which subscribes immediately and replaces the subscribe-before-boot trick in app.upgrade. Node and Bun also get the native pre-buffer cap; Bun drops the connection without a close frame, so its peer sees 1006, which is documented and tested. Deno and workerd expose no such option and check the size after receipt. BREAKING CHANGE: messages over 1 MiB now close the connection with 1009, and a connection with 16 unread inbound messages is closed with 1013 on the next one, including a handler that never reads `inbound`. Closes #107 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
13 files changed
+762
-34
No files matched your search
@@ -29,6 +29,15 @@ class Rt {
|
||||
})();
|
||||
}
|
||||
|
||||
// Never reads `inbound`, so every frame the client sends stays queued (#107).
|
||||
@Ws('/stall')
|
||||
stall() {
|
||||
return (async function* () {
|
||||
await new Promise<never>(() => {});
|
||||
yield '';
|
||||
})();
|
||||
}
|
||||
|
||||
@Ws('/room/:name')
|
||||
room(@ctx() { inbound }: any) {
|
||||
return (async function* () {
|
||||
@@ -105,3 +114,70 @@ Deno.test('serveDeno: a malformed upgrade path closes 1008 and the server keeps
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
/** Opens `path`, sends `frames` once open, and resolves with every reply and the close. */
|
||||
function exchange(port: number, path: string, frames: string[], until = 2000) {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}${path}`);
|
||||
const replies: string[] = [];
|
||||
return new Promise<{ replies: string[]; close?: { code: number; reason: string } }>((resolve) => {
|
||||
const timer = setTimeout(() => {
|
||||
ws.close();
|
||||
resolve({ replies });
|
||||
}, until);
|
||||
ws.onopen = () => frames.forEach((f) => ws.send(f));
|
||||
ws.onmessage = (e) => replies.push(String(e.data));
|
||||
ws.onclose = (e) => {
|
||||
clearTimeout(timer);
|
||||
resolve({ replies, close: { code: e.code, reason: e.reason } });
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// #107: on Deno the runtime receives the whole message before user code sees it, so the size
|
||||
// bound is checked after receipt — the close code is the same as on Node and Bun.
|
||||
Deno.test('serveDeno: ws message size and inbound queue limits', async () => {
|
||||
const app = createApp({ modules: [M], limits: { maxWsMessageBytes: 1024, maxWsQueue: 4 } });
|
||||
let port = 0;
|
||||
const server = await serveDeno(app, {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
},
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
|
||||
try {
|
||||
const atLimit = await exchange(port, '/rt/echo', ['a'.repeat(1024)], 300);
|
||||
assertEquals(atLimit.replies, [`echo:${'a'.repeat(1024)}`]);
|
||||
|
||||
const over = await exchange(port, '/rt/echo', ['a'.repeat(1025)]);
|
||||
assertEquals(over.close, { code: 1009, reason: 'message too large' });
|
||||
|
||||
const full = await exchange(port, '/rt/stall', ['1', '2', '3', '4', '5']);
|
||||
assertEquals(full.close, { code: 1013, reason: 'inbound queue full' });
|
||||
} finally {
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('serveDeno: ws message size default is 1 MiB', async () => {
|
||||
const app = createApp({ modules: [M] });
|
||||
let port = 0;
|
||||
const server = await serveDeno(app, {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
},
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
|
||||
try {
|
||||
const atLimit = await exchange(port, '/rt/echo', ['a'.repeat(1_048_576)], 500);
|
||||
assertEquals(atLimit.replies.length, 1);
|
||||
|
||||
const over = await exchange(port, '/rt/echo', ['a'.repeat(1_048_577)]);
|
||||
assertEquals(over.close?.code, 1009);
|
||||
} finally {
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user