fix(mesh): bound the control channel by maxBodyBytes, not the 1 MiB ws cap (CARD-24, #120)

The 1 MiB inbound cap from #107 applied to every WebSocket, the mesh
control channel included. An RPC carries a whole request body, so a body
inside maxBodyBytes could still overflow the cap once wrapped in an
rpc-req frame, and the teapot then closed the whole channel with 1009:
every request through that link failed 503 until it reconnected.

The test asked for by the card (T1, a 1 MB JSON body at defaults) passes
on Node, Deno and Bun, because a JSON body re-encodes to about its own
size. Two other cases fail on all three before this change:
- T1b: a 999 KB text body of quotes doubles under JSON escaping;
- T4: maxBodyBytes raised to 4 MB, a 3.9 MB JSON body.

The control channel's bound is now derived from the teapot's
limits.maxBodyBytes via controlFrameLimit(): MAX_FRAME_CHARS (4,000,000)
at the 1 MB default, scaled by the same ratio above it, never below
maxWsMessageBytes. No new option. User @Ws routes keep
maxWsMessageBytes: Node gets a second WebSocketServer for the control
path, app.upgrade picks the bound from the path before subscribing, and
Bun's single native maxPayloadLength is raised on a teapot only, leaving
app.upgrade to close user routes 1009 itself.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 01:16:43 -05:00
1 parent bfa4c1464a
commit 713122d53a
10 files changed
+536 -18

No files matched your search

+6
View File
@@ -132,6 +132,12 @@ export interface WsRouteDef {
/** Hook for a mesh gateway to take over WebSocket upgrades on a fixed path. */
export interface MeshControl {
path: string;
/**
* Inbound bound for this channel's socket, in bytes, used in place of
* `limits.maxWsMessageBytes` when larger (#120): an RPC carries a whole request body, which
* `maxBodyBytes` bounds, not the WebSocket message limit.
*/
maxFrameBytes?: number;
/**
* Serves one control connection over the neutral socket — the same capability every other ws
* consumer takes, so Node (via `nodeSocket`) and Deno/Bun (via `app.upgrade`) both feed it.
+12
View File
@@ -41,6 +41,18 @@ export function wsMessageBytes(limits: Pick<RequestLimits, 'maxWsMessageBytes'>
return limits?.maxWsMessageBytes ?? DEFAULT_MAX_WS_MESSAGE_BYTES;
}
/**
* The limits the mesh control channel's socket is bounded by (#120): `limits` with
* `maxWsMessageBytes` raised to the channel's own frame bound when that is larger. A user route
* never goes through this, so it keeps `maxWsMessageBytes` as configured.
*/
export function controlLimits(
limits: Pick<RequestLimits, 'maxWsMessageBytes' | 'maxWsQueue'> | undefined,
maxFrameBytes: number | undefined,
): Pick<RequestLimits, 'maxWsQueue'> & { maxWsMessageBytes: number } {
return { ...limits, maxWsMessageBytes: Math.max(wsMessageBytes(limits), maxFrameBytes ?? 0) };
}
/** Throws when a WebSocket limit is present but not a positive safe integer (#107). */
export function validateWsLimits(limits: RequestLimits | undefined): void {
for (const key of ['maxWsMessageBytes', 'maxWsQueue'] as const) {
+20 -4
View File
@@ -6,6 +6,7 @@ import {
matchWsRoute,
trackUntil,
boundInbound,
controlLimits,
wsMessageBytes,
type WsSocket,
type WsRequest,
@@ -50,13 +51,18 @@ function handleMeshUpgrade(
return true;
}
// An RPC carries a whole request body, so the channel is bounded by the teapot's body limit
// rather than `maxWsMessageBytes` (#120). `wss` is built with that bound as `maxPayload`, and
// `boundInbound` repeats it on the decoded text, as it does for every route.
const bounds = controlLimits(limits, meshControl.maxFrameBytes);
// nodeSocket, not the raw ws: the control channel is the last consumer that spoke the
// `ws` EventEmitter API directly. It now takes the same neutral socket every route does.
// Tracked like any other long-lived socket: a connected teacup has no reason to hang up, so
// an untracked control channel leaves the teapot's server.close() waiting on it forever.
// serveFrames swallows its own failures, so the floating promise cannot reject.
wss.handleUpgrade(req, socket, head, (ws: any) => {
const neutral = boundInbound(nodeSocket(ws), limits);
const neutral = boundInbound(nodeSocket(ws), bounds);
void trackUntil(neutral, meshControl.handle(neutral), streams);
});
@@ -128,6 +134,14 @@ export function attachWs(
// fragmented or not, before it is assembled, and closes 1009 itself. `boundInbound` repeats the
// check on the decoded text, which is the only half Deno and the edge have.
const wss = WebSocketServer ? new WebSocketServer({ noServer: true, maxPayload: wsMessageBytes(limits) }) : null;
// The control channel's own server, so its larger bound (#120) never reaches a user route.
const meshWss =
WebSocketServer && meshControl
? new WebSocketServer({
noServer: true,
maxPayload: controlLimits(limits, meshControl.maxFrameBytes).maxWsMessageBytes,
})
: null;
server.on('upgrade', (req, socket, head) => {
// A peer that resets mid-rejection must not raise an unhandled 'error' on a socket nobody
// else is listening to yet: that is as fatal as a throw.
@@ -136,7 +150,7 @@ export function attachWs(
// Nothing thrown in here may escape. This is a raw EventEmitter listener, so a throw is an
// uncaught exception that takes the whole process down, not a 500 for one client.
try {
routeUpgrade({ wss, wsRoutes, bus, meshControl, streams, limits }, req, socket, head);
routeUpgrade({ wss, meshWss, wsRoutes, bus, meshControl, streams, limits }, req, socket, head);
} catch {
if (socket.writable) rejectUpgrade(socket, 500, 'Internal Server Error');
else socket.destroy();
@@ -147,6 +161,8 @@ export function attachWs(
/** What every upgrade on one server shares. */
interface UpgradeEnv {
wss: any;
/** The mesh control channel's server, bounded by its own frame limit (#120). */
meshWss: any;
wsRoutes: WsRouteDef[];
bus?: Bus;
meshControl?: MeshControl;
@@ -156,7 +172,7 @@ interface UpgradeEnv {
/** Routes one upgrade: validates the path the way HTTP does, then mesh control, then the ws route table. */
function routeUpgrade(env: UpgradeEnv, req: http.IncomingMessage, socket: any, head: Buffer): void {
const { wss, wsRoutes, bus, meshControl, streams, limits } = env;
const { wss, meshWss, wsRoutes, bus, meshControl, streams, limits } = env;
let path: string;
// The same normalization the HTTP path runs (`dispatch` in core.ts). Without it a malformed
@@ -168,7 +184,7 @@ function routeUpgrade(env: UpgradeEnv, req: http.IncomingMessage, socket: any, h
return;
}
if (meshControl && handleMeshUpgrade(wss, meshControl, path, req, socket, head, streams, limits)) return;
if (meshControl && handleMeshUpgrade(meshWss, meshControl, path, req, socket, head, streams, limits)) return;
// The reserved path with no control channel: refused exactly as `app.upgrade` refuses it on Deno,
// Bun and the edge — accept, then close 1008 with the same reason (D17). Never falls through to the