Files
core/test/mesh/control-preauth.test.ts
T
chrnx 411c7ef3f5
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s
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>
2026-10-24 14:36:15 -05:00

155 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Before a peer's `hello` is accepted, the mesh control channel takes only hello-sized frames and
// a small queue. The large bounds that carry RPC bodies (#120, #148) apply after authentication
// only, so an unauthenticated peer cannot make the process buffer `queue × frame` bytes — which
// at 4,096 × 7 MB was tens of GB, reachable while `app.upgrade` waits for a slow boot.
import { describe, it, expect } from 'vitest';
import { createApp, Module, Provider, Step } from '../../src/index';
import { channel } from '../../src/channel';
import { MESH_CONTROL_PATH } from '../../src/mesh/teapot';
import { encode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
import type { WsSocket, WsRequest } from '../../src/http/ws-core';
@Provider({ provides: 'slow' })
class Slow {
async provide() {
await new Promise((resolve) => setTimeout(resolve, 300));
return { slow: true };
}
}
@Step({ provides: 'cfg', needs: [], export: true })
class Cfg {
run() {
return { cfg: 1 };
}
}
@Module({ mountpoint: '/api', providers: [Slow], steps: [Cfg] })
class SlowTeapot {}
function fakeSocket() {
const inbound = channel<unknown>();
const ac = new AbortController();
const sent: string[] = [];
const closes: number[] = [];
const socket: WsSocket = {
inbound,
abort: ac.signal,
get isOpen() {
return !ac.signal.aborted;
},
send: (d) => sent.push(d),
close: (code) => {
if (ac.signal.aborted) return;
closes.push(code ?? 1005);
ac.abort();
inbound.close();
},
terminate: () => ac.abort(),
};
return { socket, inbound, sent, closes };
}
const req: WsRequest = { url: MESH_CONTROL_PATH, headers: {}, protocol: 'http', ip: '' };
const teapot = () => createApp({ modules: [SlowTeapot], experimental: true, mesh: { secret: 's3cr3t' } });
describe('mesh control channel before authentication', () => {
it('closes 1009 on a frame over hello size while the boot is still running', async () => {
const app = teapot();
const { socket, inbound, closes } = fakeSocket();
const run = app.upgrade(req, socket);
inbound.push('x'.repeat(1_000_000));
expect(closes).toEqual([1009]); // at once: the boot has not finished, nothing was buffered
await run;
await app.close();
});
it('closes 1013 when an unauthenticated peer floods frames during the boot', async () => {
const app = teapot();
const { socket, inbound, closes } = fakeSocket();
const run = app.upgrade(req, socket);
let accepted = 0;
for (let i = 0; i < 64 && !closes.length; i++, accepted++) inbound.push('x'.repeat(1024));
expect(closes).toEqual([1013]);
expect(accepted).toBeLessThanOrEqual(17); // the user default, not the authenticated 4,096
await run;
await app.close();
});
it('lifts the bounds once the hello is accepted', async () => {
const app = teapot();
const { socket, inbound, sent, closes } = fakeSocket();
const run = app.upgrade(req, socket);
inbound.push(encode({ type: 'hello', v: MESH_PROTOCOL_VERSION, secret: 's3cr3t' }));
await new Promise((resolve) => setTimeout(resolve, 400)); // boot, then the handshake
expect(JSON.parse(sent[0]).type).toBe('manifest');
// A frame far over hello size is accepted now (undecodable, so ignored), and a burst of 64
// fits the authenticated queue.
inbound.push('x'.repeat(1_000_000));
for (let i = 0; i < 64; i++) inbound.push(encode({ type: 'ping' }));
await new Promise((resolve) => setTimeout(resolve, 20));
expect(closes).toEqual([]);
expect(sent.filter((s) => s.includes('pong'))).toHaveLength(64);
socket.close(1000);
await run;
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();
}
});
});