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
+37
-3
@@ -1,7 +1,9 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { connectLink } from '../../src/mesh/link';
|
||||
import { encode, decode } from '../../src/mesh/protocol';
|
||||
import { encode, decode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
|
||||
|
||||
const V = MESH_PROTOCOL_VERSION;
|
||||
|
||||
const env = { method: 'GET', params: {}, query: {}, body: undefined, headers: {} };
|
||||
|
||||
@@ -13,7 +15,7 @@ function startTeapot(secret = 'good') {
|
||||
ws.on('message', (d) => {
|
||||
const f = decode(d.toString());
|
||||
if (!authed) {
|
||||
if (f.type === 'hello' && f.secret === secret) { authed = true; ws.send(encode({ type: 'manifest', scopes: [{ token: 'auth', scope: 'request' }], routes: [] })); }
|
||||
if (f.type === 'hello' && f.secret === secret) { authed = true; ws.send(encode({ type: 'manifest', v: V, scopes: [{ token: 'auth', scope: 'request' }], routes: [] })); }
|
||||
else ws.close(1008);
|
||||
return;
|
||||
}
|
||||
@@ -33,7 +35,7 @@ function startSilentTeapot(secret = 'good') {
|
||||
wss.on('connection', (ws) => {
|
||||
ws.on('message', (d) => {
|
||||
const f = decode(d.toString());
|
||||
if (f.type === 'hello' && f.secret === secret) ws.send(encode({ type: 'manifest', scopes: [], routes: [] }));
|
||||
if (f.type === 'hello' && f.secret === secret) ws.send(encode({ type: 'manifest', v: V, scopes: [], routes: [] }));
|
||||
// ignore rpc-req entirely
|
||||
});
|
||||
});
|
||||
@@ -41,7 +43,39 @@ function startSilentTeapot(secret = 'good') {
|
||||
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
|
||||
}
|
||||
|
||||
// teapot running a future protocol: handshakes, but answers with a skewed manifest
|
||||
function startSkewedTeapot() {
|
||||
const wss = new WebSocketServer({ port: 0 });
|
||||
wss.on('connection', (ws) => {
|
||||
ws.on('message', () => ws.send(encode({ type: 'manifest', v: V + 1, scopes: [], routes: [] } as any)));
|
||||
});
|
||||
const port = (wss.address() as any).port;
|
||||
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
|
||||
}
|
||||
|
||||
describe('connectLink', () => {
|
||||
it('sends its protocol version in the hello', async () => {
|
||||
const seen: any[] = [];
|
||||
const wss = new WebSocketServer({ port: 0 });
|
||||
wss.on('connection', (ws) =>
|
||||
ws.on('message', (d) => {
|
||||
seen.push(decode(d.toString()));
|
||||
ws.send(encode({ type: 'manifest', v: V, scopes: [], routes: [] }));
|
||||
}),
|
||||
);
|
||||
const url = `ws://127.0.0.1:${(wss.address() as any).port}/__mesh__/control`;
|
||||
const link = await connectLink({ url, secret: 'good' });
|
||||
expect(seen[0]).toMatchObject({ type: 'hello', v: V });
|
||||
link.close();
|
||||
wss.close();
|
||||
});
|
||||
|
||||
it('refuses a teapot on a different protocol version', async () => {
|
||||
const t = startSkewedTeapot();
|
||||
await expect(connectLink({ url: t.url, secret: 'good', timeoutMs: 500 })).rejects.toThrow(/version/i);
|
||||
t.close();
|
||||
});
|
||||
|
||||
it('handshakes and exposes the manifest', async () => {
|
||||
const t = startTeapot();
|
||||
const link = await connectLink({ url: t.url, secret: 'good' });
|
||||
|
||||
Reference in new issue
Block a user