Files
core/test/mesh/teapot.test.ts
T
chrnx dd0ea91a1b 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>
2026-07-15 14:47:35 -05:00

105 lines
4.6 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { EventEmitter } from 'events';
import { buildManifest, createMeshControl } from '../../src/mesh/teapot';
import { encode, decode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
const V = MESH_PROTOCOL_VERSION;
import { Unauthorized } from '../../src/signals';
const fakeWs = () => {
const ws: any = new EventEmitter();
ws.sent = [];
ws.send = (s: string) => ws.sent.push(decode(s));
ws.close = vi.fn();
return ws;
};
describe('buildManifest', () => {
it('maps providers to app scope and steps to request scope', () => {
const m = buildManifest({ providers: ['config'], steps: ['auth'], routes: [{ method: 'GET', pattern: '/u/:id' }] });
expect(m.scopes).toContainEqual({ token: 'config', scope: 'app' });
expect(m.scopes).toContainEqual({ token: 'auth', scope: 'request' });
expect(m.routes).toEqual([{ method: 'GET', pattern: '/u/:id' }]);
});
});
describe('createMeshControl', () => {
const manifest = buildManifest({ providers: ['config'], steps: ['auth'], routes: [{ method: 'GET', pattern: '/u/:id' }] });
const make = (over: Partial<Parameters<typeof createMeshControl>[0]> = {}) => createMeshControl({
secret: 'good', manifest,
resolveScope: async (name) => ({ resolved: name }),
resolveRoute: async () => ({ status: 200, headers: {}, body: 'route-body' }),
...over,
});
it('exposes the reserved control path', () => {
expect(make().path).toBe('/__mesh__/control');
});
it('rejects a bad secret and closes', () => {
const ws = fakeWs(); make().handle(ws);
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, stamped with the protocol version', () => {
const ws = fakeWs(); make().handle(ws);
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 () => {
const ws = fakeWs(); make().handle(ws);
ws.emit('message', encode({ type: 'rpc-req', id: '1', kind: 'scope', name: 'config', ctx: {} as any }));
await new Promise((r) => setTimeout(r, 5));
expect(ws.sent).toEqual([]);
});
it('resolves an exported scope after handshake', async () => {
const ws = fakeWs(); make().handle(ws);
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' } });
});
it('rejects an unexported scope', async () => {
const ws = fakeWs(); make().handle(ws);
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');
expect(res.ok).toBe(false);
expect(res.error.status).toBe(403);
});
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', 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');
expect(res).toMatchObject({ id: '9', ok: false, error: { message: 'nope', status: 401 } });
});
});