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

+28 -8
View File
@@ -1,7 +1,9 @@
import { describe, it, expect, vi } from 'vitest';
import { EventEmitter } from 'events';
import { buildManifest, createMeshControl } from '../../src/mesh/teapot';
import { encode, decode } from '../../src/mesh/protocol';
import { encode, decode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
const V = MESH_PROTOCOL_VERSION;
import { Unauthorized } from '../../src/signals';
const fakeWs = () => {
@@ -36,15 +38,33 @@ describe('createMeshControl', () => {
it('rejects a bad secret and closes', () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'hello', secret: 'wrong' }));
ws.emit('message', encode({ type: 'hello', v: V, secret: 'wrong' }));
expect(ws.close).toHaveBeenCalledWith(1008);
expect(ws.sent).toEqual([]);
});
it('answers a good hello with the manifest', () => {
it('answers a good hello with the manifest, stamped with the protocol version', () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'hello', secret: 'good' }));
expect(ws.sent[0]).toMatchObject({ type: 'manifest', scopes: manifest.scopes });
ws.emit('message', encode({ type: 'hello', v: V, secret: 'good' }));
expect(ws.sent[0]).toMatchObject({ type: 'manifest', v: V, scopes: manifest.scopes });
});
it('rejects a version-skewed peer before checking its secret, naming both versions', () => {
const ws = fakeWs(); make().handle(ws);
// correct secret, wrong protocol: must still be refused — and not as an auth failure
ws.emit('message', encode({ type: 'hello', v: V + 1, secret: 'good' } as any));
expect(ws.sent).toEqual([]);
const [code, reason] = ws.close.mock.calls[0];
expect(code).toBe(1008);
expect(reason).toContain(String(V));
expect(reason).toContain(String(V + 1));
});
it('refuses a version-skewed peer even when its secret is also wrong', () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'hello', v: V + 1, secret: 'wrong' } as any));
expect(ws.sent).toEqual([]);
expect(ws.close).toHaveBeenCalled();
});
it('does NOT process rpc before handshake', async () => {
@@ -56,7 +76,7 @@ describe('createMeshControl', () => {
it('resolves an exported scope after handshake', async () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'hello', secret: 'good' }));
ws.emit('message', encode({ type: 'hello', v: V, secret: 'good' }));
ws.emit('message', encode({ type: 'rpc-req', id: '7', kind: 'scope', name: 'config', ctx: {} as any }));
await new Promise((r) => setTimeout(r, 5));
expect(ws.sent.find((f: any) => f.type === 'rpc-res')).toEqual({ type: 'rpc-res', id: '7', ok: true, result: { resolved: 'config' } });
@@ -64,7 +84,7 @@ describe('createMeshControl', () => {
it('rejects an unexported scope', async () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'hello', secret: 'good' }));
ws.emit('message', encode({ type: 'hello', v: V, secret: 'good' }));
ws.emit('message', encode({ type: 'rpc-req', id: '8', kind: 'scope', name: 'secretsvc', ctx: {} as any }));
await new Promise((r) => setTimeout(r, 5));
const res: any = ws.sent.find((f: any) => f.type === 'rpc-res');
@@ -75,7 +95,7 @@ describe('createMeshControl', () => {
it('propagates a thrown HttpError status in the error frame', async () => {
const ws = fakeWs();
make({ resolveScope: async () => { throw new Unauthorized('nope'); } }).handle(ws);
ws.emit('message', encode({ type: 'hello', secret: 'good' }));
ws.emit('message', encode({ type: 'hello', v: V, secret: 'good' }));
ws.emit('message', encode({ type: 'rpc-req', id: '9', kind: 'scope', name: 'config', ctx: {} as any }));
await new Promise((r) => setTimeout(r, 5));
const res: any = ws.sent.find((f: any) => f.type === 'rpc-res');