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:
6 files changed
+67
-32
No files matched your search
+8
-7
@@ -234,13 +234,14 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
above 1 MiB was capped at 1 MiB on the channel. Either way the teapot closed the whole channel
|
above 1 MiB was capped at 1 MiB on the channel. Either way the teapot closed the whole channel
|
||||||
with `1009`, and every request through that link failed with `503` until it reconnected.
|
with `1009`, and every request through that link failed with `503` until it reconnected.
|
||||||
|
|
||||||
The control channel is now bounded by the teapot's `limits.maxBodyBytes`: 4,000,000 bytes at the
|
The control channel is now bounded by the teapot's `limits.maxBodyBytes`: six times the body
|
||||||
1 MB default, the figure its frame parser already used, and four times the body limit when that
|
limit, the most JSON escaping can grow a body (a control character becomes `\u0001`), plus 1 MiB
|
||||||
is raised. `limits.maxWsMessageBytes` still governs every user `@Ws` route, unchanged. On Bun,
|
for headers and the rest of the envelope. That is 7,048,576 bytes at the 1 MB default, up from
|
||||||
which takes one native payload limit for all sockets, a teapot raises that limit to the control
|
the frame parser's former fixed 4,000,000. `limits.maxWsMessageBytes` still governs every user
|
||||||
channel's bound, so an oversized frame on a user route is refused by the framework instead: still
|
`@Ws` route, unchanged. On Bun, which takes one native payload limit for all sockets, a teapot
|
||||||
`1009`, on every Bun version, where Bun before 1.4 refused natively by dropping the connection
|
raises that limit to the control channel's bound, so an oversized frame on a user route is
|
||||||
(`1006`).
|
refused by the framework instead: still `1009`, on every Bun version, where Bun before 1.4
|
||||||
|
refused natively by dropping the connection (`1006`).
|
||||||
|
|
||||||
- **More than 16 mesh RPCs in flight on one link no longer drop the control channel** (#148). The
|
- **More than 16 mesh RPCs in flight on one link no longer drop the control channel** (#148). The
|
||||||
16-frame inbound queue from #107 (`limits.maxWsQueue`) also applied to the control channel. On
|
16-frame inbound queue from #107 (`limits.maxWsQueue`) also applied to the control channel. On
|
||||||
|
|||||||
+20
-23
@@ -41,34 +41,31 @@ export const MESH_CONTROL_DISABLED_REASON =
|
|||||||
*/
|
*/
|
||||||
const HANDSHAKE_TIMEOUT_MS = 10_000;
|
const HANDSHAKE_TIMEOUT_MS = 10_000;
|
||||||
|
|
||||||
/**
|
/** `limits.maxBodyBytes`'s default, which a teapot without one is sized for. */
|
||||||
* Largest control frame the teapot will parse, in characters.
|
|
||||||
*
|
|
||||||
* `decode` runs `JSON.parse` on peer-controlled input, and until the handshake succeeds that peer
|
|
||||||
* is *unauthenticated* — so without a cap, anyone who can reach the port can make the process parse
|
|
||||||
* as much as its WebSocket layer will accept (100 MiB under the `ws` package's defaults, and no
|
|
||||||
* documented ceiling on the platform sockets Deno and Bun provide).
|
|
||||||
*
|
|
||||||
* Sized for the 1 MB default body limit an RPC can legitimately carry, with room for the JSON
|
|
||||||
* overhead of the envelope around it. That overhead is not a constant: a JSON body re-encodes to
|
|
||||||
* about its own size, but a text body doubles wherever it holds a `"` or a backslash, since the
|
|
||||||
* envelope escapes it as a JSON string. {@link controlFrameLimit} scales this figure with a raised
|
|
||||||
* limit.
|
|
||||||
*/
|
|
||||||
const MAX_FRAME_CHARS = 4_000_000;
|
|
||||||
|
|
||||||
/** The body limit {@link MAX_FRAME_CHARS} is sized for: `limits.maxBodyBytes`'s default. */
|
|
||||||
const DEFAULT_MAX_BODY_BYTES = 1_000_000;
|
const DEFAULT_MAX_BODY_BYTES = 1_000_000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Largest control frame a teapot accepts when the largest body it may be sent is `maxBodyBytes`
|
* The most a body byte can grow inside an `rpc-req` envelope. The envelope is JSON, and a text
|
||||||
* (#120): {@link MAX_FRAME_CHARS}, scaled by the same ratio for a body limit raised above the
|
* body travels as a JSON string, so a control character (`\u0001`) becomes the six characters of its
|
||||||
* default. Never below it, because the frame also carries headers, params and query, which do not
|
* escape. Nothing a body can hold grows more: a `"` or backslash doubles, a JSON number such as
|
||||||
* shrink with a lowered body limit. Applies to the control channel only: user WebSocket routes keep
|
* `1e20` re-encodes to about five times its length, a multipart `Buffer` to at most four, and
|
||||||
* `limits.maxWsMessageBytes`.
|
* non-ASCII text is not escaped at all.
|
||||||
|
*/
|
||||||
|
const WORST_ESCAPE = 6;
|
||||||
|
|
||||||
|
/** Room in a frame for everything but the body: headers, params, query, url and the envelope. */
|
||||||
|
const ENVELOPE_HEADROOM = 1_048_576;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Largest control frame a teapot accepts, in characters, when the largest body it may be sent is
|
||||||
|
* `maxBodyBytes` (#120): the body at its worst escaping plus {@link ENVELOPE_HEADROOM}, so any body
|
||||||
|
* within the limit fits. 7,048,576 at the 1 MB default.
|
||||||
|
*
|
||||||
|
* `decode` runs `JSON.parse` on peer-controlled input, so the parse is capped by this figure.
|
||||||
|
* Applies to the control channel only: user WebSocket routes keep `limits.maxWsMessageBytes`.
|
||||||
*/
|
*/
|
||||||
export function controlFrameLimit(maxBodyBytes = DEFAULT_MAX_BODY_BYTES): number {
|
export function controlFrameLimit(maxBodyBytes = DEFAULT_MAX_BODY_BYTES): number {
|
||||||
return Math.max(MAX_FRAME_CHARS, Math.ceil((MAX_FRAME_CHARS / DEFAULT_MAX_BODY_BYTES) * maxBodyBytes));
|
return WORST_ESCAPE * maxBodyBytes + ENVELOPE_HEADROOM;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Assemble a {@link Manifest} from exported step tokens and buffered routes. */
|
/** Assemble a {@link Manifest} from exported step tokens and buffered routes. */
|
||||||
|
|||||||
@@ -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 () => {
|
test('mesh on Bun: a body over maxBodyBytes is 413, and the channel stays open (T2)', async () => {
|
||||||
const { post, close, teacup } = await pair();
|
const { post, close, teacup } = await pair();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -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 () => {
|
Deno.test('mesh on Deno: a body over maxBodyBytes is 413, and the channel stays open (T2)', async () => {
|
||||||
const { post, close } = await pair();
|
const { post, close } = await pair();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
|
|||||||
import { WebSocketServer } from 'ws';
|
import { WebSocketServer } from 'ws';
|
||||||
import { connectLink } from '../../src/mesh/link';
|
import { connectLink } from '../../src/mesh/link';
|
||||||
import { envelopeFrom } from '../../src/mesh/teacup';
|
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 { encode, decode, MESH_PROTOCOL_VERSION, type RequestEnvelope } from '../../src/mesh/protocol';
|
||||||
import { Bus } from '../../src/bus';
|
import { Bus } from '../../src/bus';
|
||||||
import { createApp, Step, Route, Get, Module, needs } from '../../src/index';
|
import { createApp, Step, Route, Get, Module, needs } from '../../src/index';
|
||||||
@@ -139,7 +139,7 @@ describe('teapot hardening', () => {
|
|||||||
const fake = fakeSocket();
|
const fake = fakeSocket();
|
||||||
const running = control().handle(fake.socket as never);
|
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;
|
await running;
|
||||||
|
|
||||||
expect(fake.closes[0]?.code).toBe(1009);
|
expect(fake.closes[0]?.code).toBe(1009);
|
||||||
|
|||||||
@@ -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 () => {
|
it('a body over maxBodyBytes is refused 413 by the teacup, and the channel stays open (T2)', async () => {
|
||||||
const { post, close } = await pair();
|
const { post, close } = await pair();
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in new issue
Block a user