fix(mesh): size control frames for the worst escaping a body can get (#120)

A 999,000-byte text/plain body of U+0001, within maxBodyBytes, still
failed 503: the envelope carries a text body as a JSON string, each
control character escapes to six characters, and the ~6 MB frame was
over the 4,000,000 the channel was sized to.

controlFrameLimit() is now 6 x maxBodyBytes plus 1 MiB for headers,
params, query and the envelope: 7,048,576 at the 1 MB default. Six is
the worst growth of any body byte; quotes and backslashes double, JSON
numbers like 1e20 grow about fivefold, multipart Buffers at most
fourfold, and non-ASCII text is not escaped. The fixed MAX_FRAME_CHARS
goes away with it.

Test (T1c) on Node, Deno and Bun: the control-character body now
answers 200 and the channel stays open.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 00:49:34 -05:00
1 parent 3455b421a4
commit de798771e4
6 files changed
+67 -32

No files matched your search

+12
View File
@@ -98,6 +98,18 @@ test('mesh on Bun: a text body that JSON escaping doubles still reaches the teap
}
});
test('mesh on Bun: a text body of control characters, escaped 6x, still reaches the teapot (T1c)', async () => {
const { post, close } = await pair();
try {
const res = await post('\u0001'.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 {
+12
View File
@@ -105,6 +105,18 @@ Deno.test('mesh on Deno: a text body that JSON escaping doubles still reaches th
}
});
Deno.test('mesh on Deno: a text body of control characters, escaped 6x, still reaches the teapot (T1c)', async () => {
const { post, close } = await pair();
try {
const res = await post('\u0001'.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 {
+2 -2
View File
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
import { WebSocketServer } from 'ws';
import { connectLink } from '../../src/mesh/link';
import { envelopeFrom } from '../../src/mesh/teacup';
import { createMeshControl } from '../../src/mesh/teapot';
import { controlFrameLimit, createMeshControl } from '../../src/mesh/teapot';
import { encode, decode, MESH_PROTOCOL_VERSION, type RequestEnvelope } from '../../src/mesh/protocol';
import { Bus } from '../../src/bus';
import { createApp, Step, Route, Get, Module, needs } from '../../src/index';
@@ -139,7 +139,7 @@ describe('teapot hardening', () => {
const fake = fakeSocket();
const running = control().handle(fake.socket as never);
fake.feed(`{"type":"hello","v":${V},"secret":"${'a'.repeat(4_000_001)}"}`);
fake.feed(`{"type":"hello","v":${V},"secret":"${'a'.repeat(controlFrameLimit() + 1)}"}`);
await running;
expect(fake.closes[0]?.code).toBe(1009);
+13
View File
@@ -91,6 +91,19 @@ describe('mesh RPC body near maxBodyBytes (#120)', () => {
}
});
it('a text body of control characters, escaped 6x, still reaches the teapot (T1c)', async () => {
const { post, close } = await pair();
try {
// `\u0001` re-encodes as the six characters `\u0001`: the worst escaping a body can get.
const res = await post('\u0001'.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 {