fix(mesh): cap what an unauthenticated control peer can send on Node (#120)
Before a peer's hello is accepted, `boundInbound` refuses any frame over 64 KiB, but only once it has the whole frame. On Node, the control channel's `WebSocketServer` takes the authenticated bound as its `maxPayload` (about 7 MB at the defaults, so an RPC body fits), and `maxPayload` is fixed per server. So `ws` assembled a frame up to that size for a peer that had not yet authenticated. Found in the second pre-freeze review. The existing pre-auth tests use a fake socket and never reached `ws`'s own limit. The Node adapter now counts the raw socket bytes from the upgrade until the hello is accepted (`boundInbound` gains an `onWiden` hook for that moment). Past a hello's worth (64 KiB + 1 KiB), it sends close 1009, as every runtime does for an oversized frame, pauses the socket so `ws` reads nothing more, and ends it. The counter is installed in the `handleUpgrade` callback, after `ws` attaches its own `data` listener, so no chunk is lost. Bun and Deno have no per-socket limit for this. The CHANGELOG #120 entry now says so, and says to limit frame size at the proxy if untrusted peers can reach a Bun or Deno teapot's control path. Test: a raw client announces a 5 MB frame before its hello and sends 200 KiB of it. The teapot answers with a 1009 close frame and closes the connection. Without the fix it waits for the rest of the frame. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+111
-4
No files matched your search
@@ -98,3 +98,57 @@ describe('mesh control channel before authentication', () => {
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
|
||||
// The Node adapter's own half: `ws` assembles a frame up to its `maxPayload` before `boundInbound`
|
||||
// sees it, and the control channel's must fit an authenticated RPC (~7 MB). A raw client shows
|
||||
// what `ws` does with a frame it has only started to receive.
|
||||
describe('mesh control channel before authentication, on Node', () => {
|
||||
@Module({ mountpoint: '/api', steps: [Cfg] })
|
||||
class NodeTeapot {}
|
||||
|
||||
/** Upgrades a raw TCP socket to the control path and resolves once the `101` is in. */
|
||||
async function rawControl(port: number) {
|
||||
const { connect } = await import('node:net');
|
||||
const sock = connect(port, '127.0.0.1');
|
||||
let closed = false;
|
||||
const received: Buffer[] = [];
|
||||
sock.on('close', () => (closed = true));
|
||||
sock.on('error', () => {});
|
||||
sock.write(
|
||||
`GET ${MESH_CONTROL_PATH} HTTP/1.1\r\nHost: 127.0.0.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n` +
|
||||
'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n',
|
||||
);
|
||||
await new Promise<void>((resolve) => sock.once('data', () => resolve()));
|
||||
sock.on('data', (chunk: Buffer) => received.push(chunk));
|
||||
sock.on('end', () => sock.end());
|
||||
return { sock, closed: () => closed, received: () => Buffer.concat(received) };
|
||||
}
|
||||
|
||||
/** The start of a masked text frame announcing `length` payload bytes (64-bit length form). */
|
||||
const frameHeader = (length: number) => {
|
||||
const header = Buffer.alloc(14);
|
||||
header[0] = 0x81; // FIN, text
|
||||
header[1] = 0x80 | 127; // masked, 64-bit length follows
|
||||
header.writeBigUInt64BE(BigInt(length), 2);
|
||||
return header; // mask key: the last four bytes, zero
|
||||
};
|
||||
|
||||
it('closes 1009 a peer that sends more than a hello before its hello, instead of assembling the frame', async () => {
|
||||
const app = createApp({ modules: [NodeTeapot], experimental: true, mesh: { secret: 's3cr3t' } });
|
||||
const server = await app.listen(0);
|
||||
const { sock, closed, received } = await rawControl((server.address() as { port: number }).port);
|
||||
try {
|
||||
// 5 MB announced, under the authenticated bound; 200 KiB of it sent, then nothing.
|
||||
sock.write(frameHeader(5_000_000));
|
||||
sock.write(Buffer.alloc(200 * 1024, 0x78));
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
expect(closed()).toBe(true);
|
||||
const close = received();
|
||||
expect(close[0]).toBe(0x88); // FIN, close
|
||||
expect(close.readUInt16BE(2)).toBe(1009);
|
||||
} finally {
|
||||
sock.destroy();
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user