fix(mesh): bound the control channel by maxBodyBytes, not the 1 MiB ws cap (CARD-24, #120)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 21s
CI / runtimes (edge) (pull_request) Successful in 14s

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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnxandClaude Opus 5.5 committed 2026-10-22 23:57:57 -05:00
1 parent e565b1d4c9
commit c40e6ebf1b
10 files changed
+525 -19

No files matched your search

+136
View File
@@ -0,0 +1,136 @@
// #120 (CARD-24, D22) on Bun: an RPC whose body is within `maxBodyBytes` reaches the teapot,
// although the control channel is a WebSocket bounded like every other. Node twin:
// test/mesh/large-body.test.ts, which explains the cases.
//
// Run with: npm run test:bun
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Route, Post, Module, Ws, body, ctx } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
const SECRET = 's3cr3t';
const WRAP = '{"data":""}'.length;
@Route('/big')
class BigCtl {
@Post('/echo', { export: true })
echo(@body() b: { data: string } | string) {
return { length: typeof b === 'string' ? b.length : b.data.length };
}
@Ws('/sock')
sock(@ctx() { inbound }: any) {
return (async function* () {
for await (const msg of inbound) yield `echo:${String(msg).length}`;
})();
}
}
@Module({ mountpoint: '/api', controllers: [BigCtl] })
class TeapotModule {}
@Module({ mountpoint: '/api' })
class TeacupModule {}
async function serve(app: ReturnType<typeof createApp>) {
const server = await serveBun(app, { port: 0 });
return { server, port: server.port };
}
async function pair(maxBodyBytes?: number) {
const limits = maxBodyBytes ? { maxBodyBytes } : undefined;
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET }, limits });
const t = await serve(teapot);
const url = `ws://127.0.0.1:${t.port}/__mesh__/control`;
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url, secret: SECRET }] },
limits,
});
const c = await serve(teacup);
const post = (payload: string, type = 'application/json') =>
fetch(`http://127.0.0.1:${c.port}/api/big/echo`, {
method: 'POST',
headers: { 'content-type': type },
body: payload,
});
const close = async () => {
await teacup.close();
await teapot.close();
c.server.stop(true);
t.server.stop(true);
};
return { post, close, tPort: t.port, teacup };
}
const json = (bytes: number) => JSON.stringify({ data: 'x'.repeat(bytes - WRAP) });
test('mesh on Bun: a 1 MB JSON body reaches the teapot (T1)', async () => {
const { post, close } = await pair();
try {
const res = await post(json(1_000_000));
expect(res.status).toEqual(200);
expect(await res.json()).toEqual({ length: 1_000_000 - WRAP });
expect((await post(json(1_000_000))).status).toEqual(200);
} finally {
await close();
}
});
test('mesh on Bun: a text body that JSON escaping doubles still reaches the teapot (T1b)', async () => {
const { post, close } = await pair();
try {
const res = await post('"'.repeat(999_000), 'text/plain');
expect(res.status).toEqual(200);
expect(await res.json()).toEqual({ length: 999_000 });
expect((await post('ok', 'text/plain')).status).toEqual(200);
} finally {
await close();
}
});
test('mesh on Bun: a body over maxBodyBytes is 413, and the channel stays open (T2)', async () => {
const { post, close, teacup } = await pair();
try {
// Through `app.fetch`, not the socket: a client still writing a 1 MB body when Bun's 413 lands
// can stall or reset (see body-limits.test.ts). That race is the transport's, not the mesh's.
const over = new Request('http://x/api/big/echo', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: json(1_000_001),
});
expect((await teacup.fetch(over)).status).toEqual(413);
expect((await post(json(1_000))).status).toEqual(200);
} finally {
await close();
}
});
// A teapot raises Bun's native `maxPayloadLength` to the control channel's bound, so a user route's
// oversized frame is refused by `app.upgrade` instead, with a real 1009 close frame rather than the
// 1006 drop the native bound gives (see ws.test.ts).
test('mesh on Bun: a user ws route on the same teapot still closes 1009 over 1 MiB (T3)', async () => {
const { close, tPort } = await pair();
try {
const code = await new Promise<number>((resolve) => {
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/sock`);
ws.onopen = () => ws.send('x'.repeat(1_048_577));
ws.onclose = (e) => resolve(e.code);
});
expect(code).toEqual(1009);
} finally {
await close();
}
});
test('mesh on Bun: the control-channel cap follows a raised maxBodyBytes (T4)', async () => {
const { post, close } = await pair(4_000_000);
try {
const res = await post(json(3_900_000));
expect(res.status).toEqual(200);
expect(await res.json()).toEqual({ length: 3_900_000 - WRAP });
} finally {
await close();
}
});
+132
View File
@@ -0,0 +1,132 @@
// #120 (CARD-24, D22) on Deno: an RPC whose body is within `maxBodyBytes` reaches the teapot,
// although the control channel is a WebSocket bounded like every other. Node twin:
// test/mesh/large-body.test.ts, which explains the cases.
//
// Run with: npm run test:deno
import 'npm:reflect-metadata';
import { assertEquals } from 'jsr:@std/assert';
import { createApp, Route, Post, Module, Ws, body, ctx } from '../../src/index.ts';
import { serveDeno } from '../../src/deno.ts';
const SECRET = 's3cr3t';
const WRAP = '{"data":""}'.length;
@Route('/big')
class BigCtl {
@Post('/echo', { export: true })
echo(@body() b: { data: string } | string) {
return { length: typeof b === 'string' ? b.length : b.data.length };
}
@Ws('/sock')
sock(@ctx() { inbound }: any) {
return (async function* () {
for await (const msg of inbound) yield `echo:${String(msg).length}`;
})();
}
}
@Module({ mountpoint: '/api', controllers: [BigCtl] })
class TeapotModule {}
@Module({ mountpoint: '/api' })
class TeacupModule {}
async function serve(app: ReturnType<typeof createApp>) {
let port = 0;
const server = await serveDeno(app, {
port: 0,
onListen: (addr) => {
port = addr.port;
},
});
await new Promise((resolve) => setTimeout(resolve, 50));
return { server, port };
}
async function pair(maxBodyBytes?: number) {
const limits = maxBodyBytes ? { maxBodyBytes } : undefined;
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET }, limits });
const t = await serve(teapot);
const url = `ws://127.0.0.1:${t.port}/__mesh__/control`;
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url, secret: SECRET }] },
limits,
});
const c = await serve(teacup);
const post = (payload: string, type = 'application/json') =>
fetch(`http://127.0.0.1:${c.port}/api/big/echo`, {
method: 'POST',
headers: { 'content-type': type },
body: payload,
});
const close = async () => {
await teacup.close();
await teapot.close();
await c.server.shutdown();
await t.server.shutdown();
};
return { post, close, tPort: t.port };
}
const json = (bytes: number) => JSON.stringify({ data: 'x'.repeat(bytes - WRAP) });
Deno.test('mesh on Deno: a 1 MB JSON body reaches the teapot (T1)', async () => {
const { post, close } = await pair();
try {
const res = await post(json(1_000_000));
assertEquals(res.status, 200);
assertEquals(await res.json(), { length: 1_000_000 - WRAP });
assertEquals((await post(json(1_000_000))).status, 200);
} finally {
await close();
}
});
Deno.test('mesh on Deno: a text body that JSON escaping doubles still reaches the teapot (T1b)', async () => {
const { post, close } = await pair();
try {
const res = await post('"'.repeat(999_000), 'text/plain');
assertEquals(res.status, 200);
assertEquals(await res.json(), { length: 999_000 });
assertEquals((await post('ok', 'text/plain')).status, 200);
} finally {
await close();
}
});
Deno.test('mesh on Deno: a body over maxBodyBytes is 413, and the channel stays open (T2)', async () => {
const { post, close } = await pair();
try {
assertEquals((await post(json(1_000_001))).status, 413);
assertEquals((await post(json(1_000))).status, 200);
} finally {
await close();
}
});
Deno.test('mesh on Deno: a user ws route on the same teapot still closes 1009 over 1 MiB (T3)', async () => {
const { close, tPort } = await pair();
try {
const code = await new Promise<number>((resolve) => {
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/sock`);
ws.onopen = () => ws.send('x'.repeat(1_048_577));
ws.onclose = (e) => resolve(e.code);
});
assertEquals(code, 1009);
} finally {
await close();
}
});
Deno.test('mesh on Deno: the control-channel cap follows a raised maxBodyBytes (T4)', async () => {
const { post, close } = await pair(4_000_000);
try {
const res = await post(json(3_900_000));
assertEquals(res.status, 200);
assertEquals(await res.json(), { length: 3_900_000 - WRAP });
} finally {
await close();
}
});
+117
View File
@@ -0,0 +1,117 @@
// #120 (CARD-24, D22): an RPC whose body is within `maxBodyBytes` must reach the teapot.
//
// The control channel is a WebSocket, and every WebSocket was bounded by
// `limits.maxWsMessageBytes` (1 MiB by default, #107). A body at the HTTP limit (1,000,000 B by
// default) travels inside an `rpc-req` envelope, re-encoded as JSON:
// - a JSON body re-encodes to about its own size, so 1 MB fits under 1 MiB (T1);
// - a text body does not: every `"` or `\` doubles under JSON escaping, so a 999 KB text body
// became a ~2 MB frame and the teapot hung up the whole channel with 1009 (T1b);
// - a raised `maxBodyBytes` was capped at 1 MiB on the channel regardless (T4).
// The Deno and Bun twins live in test/deno and test/bun.
import { describe, it, expect } from 'vitest';
import WebSocket from 'ws';
import { createApp, Route, Post, Module, Ws, body, ctx } from '../../src/index';
const SECRET = 's3cr3t';
const WRAP = '{"data":""}'.length;
@Route('/big')
class BigCtl {
@Post('/echo', { export: true })
echo(@body() b: { data: string } | string) {
return { length: typeof b === 'string' ? b.length : b.data.length };
}
@Ws('/sock')
sock(@ctx() { inbound }: any) {
return (async function* () {
for await (const msg of inbound) yield `echo:${String(msg).length}`;
})();
}
}
@Module({ mountpoint: '/api', controllers: [BigCtl] })
class TeapotModule {}
@Module({ mountpoint: '/api' })
class TeacupModule {}
/** A JSON body of exactly `bytes` bytes: `{"data":"xxx…"}`. */
const json = (bytes: number) => JSON.stringify({ data: 'x'.repeat(bytes - WRAP) });
async function pair(maxBodyBytes?: number) {
const limits = maxBodyBytes ? { maxBodyBytes } : undefined;
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET }, limits });
const tServer = await teapot.listen(0);
const tPort = (tServer.address() as any).port;
const url = `ws://127.0.0.1:${tPort}/__mesh__/control`;
const teacup = createApp({ modules: [TeacupModule], experimental: true, mesh: { teapots: [{ url, secret: SECRET }] }, limits });
const cServer = await teacup.listen(0);
const cPort = (cServer.address() as any).port;
const post = (payload: string, type = 'application/json') =>
fetch(`http://127.0.0.1:${cPort}/api/big/echo`, { method: 'POST', headers: { 'content-type': type }, body: payload });
const close = async () => {
await teacup.close();
await teapot.close();
};
return { post, close, tPort };
}
describe('mesh RPC body near maxBodyBytes (#120)', () => {
it('a 1 MB JSON body reaches the teapot, and the channel stays open (T1)', async () => {
const { post, close } = await pair();
try {
const res = await post(json(1_000_000));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ length: 1_000_000 - WRAP });
expect((await post(json(1_000_000))).status).toBe(200);
} finally {
await close();
}
});
it('a text body that JSON escaping doubles still reaches the teapot (T1b)', async () => {
const { post, close } = await pair();
try {
const res = await post('"'.repeat(999_000), 'text/plain');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ length: 999_000 });
expect((await post('ok', 'text/plain')).status).toBe(200);
} finally {
await close();
}
});
it('a body over maxBodyBytes is refused 413 by the teacup, and the channel stays open (T2)', async () => {
const { post, close } = await pair();
try {
expect((await post(json(1_000_001))).status).toBe(413);
expect((await post(json(1_000))).status).toBe(200);
} finally {
await close();
}
});
it("a user ws route on the same teapot still closes 1009 over maxWsMessageBytes (T3)", async () => {
const { close, tPort } = await pair();
try {
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/sock`);
await new Promise((resolve) => ws.once('open', resolve));
const closed = new Promise<number>((resolve) => ws.once('close', (code) => resolve(code)));
ws.send('x'.repeat(1_048_577));
expect(await closed).toBe(1009);
} finally {
await close();
}
});
it('the control-channel cap follows a raised maxBodyBytes (T4)', async () => {
const { post, close } = await pair(4_000_000);
try {
const res = await post(json(3_900_000));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ length: 3_900_000 - WRAP });
} finally {
await close();
}
});
});