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

+14 -3
View File
@@ -1,6 +1,6 @@
import type { Bus } from '../bus';
import { HttpError } from '../signals';
import { encode, decode, type Manifest, type RequestEnvelope, type Frame } from './protocol';
import { encode, decode, MESH_PROTOCOL_VERSION, type Manifest, type RequestEnvelope, type Frame } from './protocol';
/** Client handle to a remote mesh server: its manifest, an RPC caller, and a close(). */
export interface Link {
@@ -105,7 +105,7 @@ export function connectLink(args: { url: string; secret: string; timeoutMs?: num
reject(new Error(`mesh connect timeout: ${args.url}`));
}, timeoutMs);
ws.on('open', () => ws.send(encode({ type: 'hello', secret: args.secret })));
ws.on('open', () => ws.send(encode({ type: 'hello', v: MESH_PROTOCOL_VERSION, secret: args.secret })));
ws.on('error', (error: unknown) => reject(error));
ws.on('close', () => {
clearTimeout(connectTimer);
@@ -124,8 +124,19 @@ export function connectLink(args: { url: string; secret: string; timeoutMs?: num
}
if (frame.type === 'manifest') {
manifest = { scopes: frame.scopes, routes: frame.routes };
clearTimeout(connectTimer);
if (frame.v !== MESH_PROTOCOL_VERSION) {
ws.close();
reject(
new Error(
`mesh protocol version mismatch at ${args.url}: teapot speaks v${frame.v}, this teacup speaks v${MESH_PROTOCOL_VERSION}`,
),
);
return;
}
manifest = { scopes: frame.scopes, routes: frame.routes };
args.bus?.emit('mesh:connect', { name: args.url });
resolve(buildLink(ws, pending, manifest, timeoutMs));
return;