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:
6 files changed
+243
-38
No files matched your search
@@ -1,16 +1,22 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { encode, decode, type Frame, type RequestEnvelope } from '../../src/mesh/protocol';
|
||||
import { encode, decode, MESH_PROTOCOL_VERSION, type Frame, type RequestEnvelope } from '../../src/mesh/protocol';
|
||||
|
||||
const env: RequestEnvelope = { method: 'GET', params: { id: '1' }, query: {}, body: undefined, headers: {} };
|
||||
const V = MESH_PROTOCOL_VERSION;
|
||||
|
||||
describe('protocol codec', () => {
|
||||
it('round-trips each frame type', () => {
|
||||
const frames: Frame[] = [
|
||||
{ type: 'hello', secret: 's3cr3t' },
|
||||
{ type: 'manifest', scopes: [{ token: 'auth', scope: 'request' }], routes: [{ method: 'GET', pattern: '/u/:id' }] },
|
||||
{ type: 'rpc-req', id: 'c1:1', kind: 'scope', name: 'auth', ctx: env },
|
||||
{ type: 'rpc-res', id: 'c1:1', ok: true, result: { id: 'u1' } },
|
||||
{ type: 'rpc-res', id: 'c1:2', ok: false, error: { message: 'no', status: 401 } },
|
||||
{ type: 'hello', v: V, secret: 's3cr3t' },
|
||||
{
|
||||
type: 'manifest',
|
||||
v: V,
|
||||
scopes: [{ token: 'auth', scope: 'request' }],
|
||||
routes: [{ method: 'GET', pattern: '/u/:id' }],
|
||||
},
|
||||
{ type: 'rpc-req', id: '1', kind: 'scope', name: 'auth', ctx: env },
|
||||
{ type: 'rpc-res', id: '1', ok: true, result: { id: 'u1' } },
|
||||
{ type: 'rpc-res', id: '2', ok: false, error: { message: 'no', status: 401 } },
|
||||
];
|
||||
for (const f of frames) expect(decode(encode(f))).toEqual(f);
|
||||
});
|
||||
@@ -22,4 +28,57 @@ describe('protocol codec', () => {
|
||||
it('throws on unknown frame type', () => {
|
||||
expect(() => decode(JSON.stringify({ type: 'bogus' }))).toThrow(/unknown frame/i);
|
||||
});
|
||||
|
||||
it.each(['constructor', 'toString', '__proto__', 'hasOwnProperty'])(
|
||||
'treats inherited Object property %s as an unknown type, not a validator',
|
||||
(type) => {
|
||||
// `type` is peer-controlled; a bare SHAPE[type] lookup would walk the prototype chain
|
||||
expect(() => decode(JSON.stringify({ type }))).toThrow(/unknown frame/i);
|
||||
},
|
||||
);
|
||||
|
||||
it('throws on a non-string type tag', () => {
|
||||
expect(() => decode(JSON.stringify({ type: 42 }))).toThrow(/unknown frame/i);
|
||||
expect(() => decode(JSON.stringify({}))).toThrow(/unknown frame/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('protocol versioning', () => {
|
||||
it('carries the protocol version on the handshake frames', () => {
|
||||
const hello = decode(encode({ type: 'hello', v: V, secret: 's' }));
|
||||
const manifest = decode(encode({ type: 'manifest', v: V, scopes: [], routes: [] }));
|
||||
|
||||
expect(hello).toMatchObject({ v: V });
|
||||
expect(manifest).toMatchObject({ v: V });
|
||||
});
|
||||
|
||||
it('rejects a handshake frame with no version — an unversioned peer is not decodable', () => {
|
||||
expect(() => decode(JSON.stringify({ type: 'hello', secret: 's' }))).toThrow(/malformed hello/i);
|
||||
expect(() => decode(JSON.stringify({ type: 'manifest', scopes: [], routes: [] }))).toThrow(/malformed manifest/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('protocol field validation', () => {
|
||||
// decode is the trust boundary: past it, teapot.ts feeds frames straight into
|
||||
// resolveScope/resolveRoute. A bad shape must die here, not downstream.
|
||||
it.each([
|
||||
['hello without secret', { type: 'hello', v: V }],
|
||||
['hello with non-string secret', { type: 'hello', v: V, secret: 42 }],
|
||||
['manifest with non-array scopes', { type: 'manifest', v: V, scopes: 'nope', routes: [] }],
|
||||
['rpc-req without id', { type: 'rpc-req', kind: 'scope', name: 'auth', ctx: env }],
|
||||
['rpc-req without ctx', { type: 'rpc-req', id: '1', kind: 'scope', name: 'auth' }],
|
||||
['rpc-req with unknown kind', { type: 'rpc-req', id: '1', kind: 'bogus', name: 'auth', ctx: env }],
|
||||
['rpc-req without name', { type: 'rpc-req', id: '1', kind: 'scope', ctx: env }],
|
||||
['rpc-res without id', { type: 'rpc-res', ok: true, result: 1 }],
|
||||
['rpc-res with non-boolean ok', { type: 'rpc-res', id: '1', ok: 'yes' }],
|
||||
['rpc-res error without an error payload', { type: 'rpc-res', id: '1', ok: false }],
|
||||
])('rejects %s', (_label, frame) => {
|
||||
expect(() => decode(JSON.stringify(frame))).toThrow(/malformed/i);
|
||||
});
|
||||
|
||||
it('accepts a valid rpc-req unchanged', () => {
|
||||
const frame: Frame = { type: 'rpc-req', id: '1', kind: 'route', name: '/u/:id', ctx: env };
|
||||
|
||||
expect(decode(encode(frame))).toEqual(frame);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user