fix(mesh): validate the manifest's route entries, not just its steps

decode is the mesh's trust boundary, and the steps array gained a per-element
check on this branch while the routes array beside it kept only its
Array.isArray. A peer sending `{ method: 5, pattern: {} }` got past decode and
died in compilePattern as a boot TypeError — it failed closed, so nothing was
exposed, but the error blamed green-tea for what was a malformed frame from
the other side. Both fields reach compilePattern, so both are checked.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-12 15:40:26 -05:00
1 parent e69f604053
commit 8ef7c3ee14
2 files changed
+19

No files matched your search

+7
View File
@@ -88,6 +88,9 @@ export type Frame =
const isString = (value: unknown): value is string => typeof value === 'string';
const isNumber = (value: unknown): boolean => typeof value === 'number';
const isObject = (value: unknown): boolean => typeof value === 'object' && value !== null;
/** A manifest route entry: both fields reach `compilePattern`, so both must really be strings. */
const isRouteEntry = (value: unknown): boolean =>
isObject(value) && isString((value as RouteEntry).method) && isString((value as RouteEntry).pattern);
/** Rejects a frame whose fields don't match its `type` tag. `bad` always throws. */
type ShapeCheck = (frame: Record<string, unknown>, bad: (why: string) => never) => void;
@@ -108,6 +111,10 @@ const SHAPE: Record<Frame['type'], ShapeCheck> = {
// Array.isArray above already guarantees this, but TS does not carry that narrowing through a
// `never`-typed callback parameter — the cast is safe, not a bypass.
if (!(frame.steps as unknown[]).every(isString)) bad('every step must be a string');
// Checked for the same reason steps are: past `decode` a route entry goes straight to
// `compilePattern`, where a non-string `pattern` dies as a boot `TypeError` about a peer's
// frame — a failure that reads like a green-tea bug rather than a malformed manifest.
if (!(frame.routes as unknown[]).every(isRouteEntry)) bad('every route must have a string method and pattern');
},
'rpc-req': (frame, bad) => {
if (!isString(frame.id)) bad('id must be a string');
+12
View File
@@ -38,6 +38,18 @@ describe('protocol codec', () => {
);
});
it('rejects a manifest whose routes array contains a malformed entry', () => {
// Not a hole that was open — `compilePattern` would have thrown downstream — but it threw a
// `TypeError` at boot instead of naming the peer's frame as the malformed thing it is.
const routes = [{ method: 5, pattern: {} }];
expect(() => decode(JSON.stringify({ type: 'manifest', v: V, steps: [], routes }))).toThrow(
/every route must have a string method and pattern/,
);
expect(() => decode(JSON.stringify({ type: 'manifest', v: V, steps: [], routes: ['GET /x'] }))).toThrow(
/every route must have a string method and pattern/,
);
});
it('throws on malformed json', () => {
expect(() => decode('{not json')).toThrow();
});