boundInbound(socket, meshBounds) runs before ensureBooted() and before the hello is checked, and meshBounds allowed 4,096 queued frames of up to 7 MB each. During a slow boot nothing reads that queue, so an unauthenticated peer could make the process buffer tens of GB. The control channel's bounds are now phased (controlBounds() replaces controlLimits()): - until the hello is accepted: frames of at most HELLO_FRAME_BYTES (64 KiB) and the default 16-frame queue, so about 1 MiB per connection; - after: the frame bound from #120 and the queue bound from #148. boundInbound takes the second set as an optional `widened` argument and keeps the switch on the bounded socket under a Symbol.for key. The teapot calls widenInbound() when the handshake succeeds. The teapot's own parse check follows the same phases, so a standalone createMeshControl also refuses a pre-auth frame over hello size. This is an auth-phased bound rather than a total-bytes cap: the only unauthenticated frame with a purpose is the hello, and phasing keeps the trusted path free of a second accounting rule. No new option. On Node the control path's native maxPayload stays at the authed bound, because ws cannot change it per phase. A pre-auth peer can still make ws assemble one frame of up to that size before boundInbound closes the connection with 1009. That frame is transient and never queued. Tests: during a delayed boot, a 1 MB pre-auth frame closes 1009 at once and a flood closes 1013 within 17 frames; after the hello, large frames and a 64-ping burst are accepted. On Node, Deno and Bun, an unauthenticated 1 MB frame over a real socket closes 1009. The #148 64-RPC test stays green. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
101 lines
3.6 KiB
TypeScript
101 lines
3.6 KiB
TypeScript
// 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();
|
||
});
|
||
});
|