feat(mesh): version the wire protocol and validate frame shapes

Cross-runtime mesh means peers are separate processes on separate deploy
cadences, so the frame union is a contract between green-tea versions rather
than an internal detail. There was no version field, and decode validated only
that `type` was a known tag before casting `parsed as Frame` — every other
field was trusted, so a skewed or malformed peer failed somewhere downstream
instead of at the boundary.

Add MESH_PROTOCOL_VERSION to the hello/manifest frames. Both ends refuse a
mismatch: the teapot checks the version *before* the secret, since a skewed
peer is not an auth failure and "wrong secret" would send its operator hunting
the wrong bug.

Validate fields per frame type via a Record keyed on the type tag, so a new
frame type must add a row. The lookup uses an own-property check: `type` is
peer-controlled and a bare lookup would resolve SHAPE['constructor'] up the
prototype chain to a truthy non-check.

This is a breaking wire change, done now because it is free exactly once —
mesh is alpha behind experimental:true and has no deployed peers. Adding a
version later would break every peer already in the field.

Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 1

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-15 14:47:35 -05:00
1 parent d56eeafcd8
commit dd0ea91a1b
6 files changed
+243 -38

No files matched your search

+42 -8
View File
@@ -2,7 +2,15 @@ import crypto from 'crypto';
import type { Bus } from '../bus';
import type { ResponseShape } from '../pipeline';
import { isHttpError } from '../signals';
import { encode, decode, type Manifest, type RequestEnvelope, type RouteEntry, type Frame } from './protocol';
import {
encode,
decode,
MESH_PROTOCOL_VERSION,
type Manifest,
type RequestEnvelope,
type RouteEntry,
type Frame,
} from './protocol';
/** WebSocket path where a teapot exposes its mesh control channel. */
export const MESH_CONTROL_PATH = '/__mesh__/control';
@@ -49,18 +57,44 @@ function notExported(name: string): Error {
return error;
}
/** Validate an unauthenticated hello frame; reply with the manifest on success or close on mismatch. */
/**
* Validate an unauthenticated hello frame; reply with the manifest on success or close on mismatch.
* The version is checked *before* the secret: a skewed peer is not an auth failure, and telling it
* "wrong secret" would send its operator hunting the wrong bug.
*/
function handleHandshake(ws: any, frame: Frame, deps: MeshControlDeps): boolean {
if (frame.type === 'hello' && safeEqual(frame.secret, deps.secret)) {
ws.send(encode({ type: 'manifest', scopes: deps.manifest.scopes, routes: deps.manifest.routes }));
deps.bus?.emit('mesh:connect', { name: 'teapot' });
if (frame.type !== 'hello') {
ws.close(1008);
return true;
return false;
}
ws.close(1008);
if (frame.v !== MESH_PROTOCOL_VERSION) {
ws.close(
1008,
`mesh protocol version mismatch: peer speaks v${frame.v}, this teapot speaks v${MESH_PROTOCOL_VERSION}`,
);
return false;
return false;
}
if (!safeEqual(frame.secret, deps.secret)) {
ws.close(1008);
return false;
}
ws.send(
encode({
type: 'manifest',
v: MESH_PROTOCOL_VERSION,
scopes: deps.manifest.scopes,
routes: deps.manifest.routes,
}),
);
deps.bus?.emit('mesh:connect', { name: 'teapot' });
return true;
}
/** Resolve an authenticated rpc-req against the exported scopes/routes, enforcing export gating. */