feat(mesh): carry the caller's identity across the wire, and bound what an unauthenticated peer can do
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 18s
CI / build (pull_request) Successful in 24s
CI / runtimes (edge) (pull_request) Successful in 11s
CI / runtimes (deno) (pull_request) Successful in 18s

The observability release gave every request an id and lost it at the process
boundary — in the one place a distributed trace is the entire point. The RPC
envelope now carries requestId, traceId and url, and the teapot adopts them
instead of opening its own investigation.

Both fields are optional on the wire, so the protocol version does not move.
That decision needed a rule rather than a judgement call, so the rule is now
written beside the constant: bump when an old peer would misparse or misbehave
silently, not when work happened. Bumping per change would make the number
mean the opposite of what it is for.

Three things found while in here:

- mesh:rpc:error emitted the per-link counter where every other emitter puts a
  name, so the two sides of one failure could not be matched up.
- The teapot had no handshake deadline, so an unauthenticated peer could hold
  a socket by saying nothing, and no frame size cap, so it could make the
  process JSON.parse up to whatever the socket layer allowed — 100 MiB with
  the ws defaults, before authenticating.
- The remote-route envelope was cast from the internal request object, putting
  ip and protocol on the wire as fields the protocol never declared.

The cleartext warning is a warning and not a refusal on purpose: a private
network already doing mutual TLS is legitimate, and green-tea cannot tell it
apart from an exposed one.

4 of the 8 new tests verified to fail against the old code; the rest are the
negative cases, which should pass both before and after. The Deno test drives
the teapot's frame loop with a raw client, because server-side close codes are
where these runtimes have already differed once.

431 vitest / 12 Deno / 7 Bun / edge / JSR dry run.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-18 11:15:54 -05:00
1 parent 1ccbf6cadf
commit f2cb411eac
8 files changed
+472 -10

No files matched your search

+39 -1
View File
@@ -19,6 +19,28 @@ import {
/** WebSocket path where a teapot exposes its mesh control channel. */
export const MESH_CONTROL_PATH = '/__mesh__/control';
/**
* How long an unauthenticated peer may hold the control channel open before it is hung up on.
*
* The teacup has always bounded its side of the handshake; the teapot had no equivalent, so a peer
* could connect and simply never send `hello`, holding a socket indefinitely without proving
* anything. Generous on purpose — this is a deadline against silence, not a latency budget.
*/
const HANDSHAKE_TIMEOUT_MS = 10_000;
/**
* 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 above the 1 MB default body limit an RPC can legitimately carry, with room for the JSON
* overhead of the envelope around it.
*/
const MAX_FRAME_CHARS = 4_000_000;
/** Assemble a {@link Manifest} from exported provider/step tokens and buffered routes. */
export function buildManifest(args: {
providers: string[]; // exported provider tokens (app-scope)
@@ -151,19 +173,34 @@ async function serveFrames(
exportedRoutes: Set<string>,
): Promise<void> {
let authed = false;
// Armed before the loop, and cleared by the handshake. An unauthenticated peer that simply
// says nothing is indistinguishable from a slow one until this fires.
const handshakeTimer = setTimeout(() => {
if (!authed) socket.close(1008, 'mesh handshake timeout');
}, HANDSHAKE_TIMEOUT_MS);
handshakeTimer.unref?.();
try {
for await (const data of socket.inbound) {
const raw = String(data);
// Checked before `decode`, because decoding is the expensive part being defended.
if (raw.length > MAX_FRAME_CHARS) {
socket.close(1009, 'mesh frame too large');
break;
}
let frame: Frame;
try {
frame = decode(String(data));
frame = decode(raw);
} catch {
continue; // undecodable frames are ignored, as before
}
if (!authed) {
authed = handleHandshake(socket, frame, deps);
if (authed) clearTimeout(handshakeTimer);
continue;
}
@@ -183,6 +220,7 @@ async function serveFrames(
} catch {
/* socket failed; the disconnect emit below is the same signal a clean close gives */
} finally {
clearTimeout(handshakeTimer);
deps.bus?.emit('mesh:disconnect', { name: 'teapot' });
}
}