refactor(mesh): import node:crypto explicitly in the teapot
safeEqual guards the mesh shared secret and reached for Buffer + timingSafeEqual through a bare 'crypto' specifier, leaving resolution to Deno/Bun node-compat heuristics. Name the builtin explicitly — the authentication path should not depend on which compat layer happens to answer. Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 2 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 file changed
+57
-33
+57
-33
@@ -1,5 +1,9 @@
|
|||||||
import crypto from 'crypto';
|
// node: prefix, not a bare specifier: this is the mesh auth path, and on Deno/Bun a bare
|
||||||
|
// 'crypto' leans on node-compat resolution heuristics. Buffer + timingSafeEqual resolve
|
||||||
|
// under node-compat on both; edge/workerd is out of scope (see the cross-runtime spec).
|
||||||
|
import crypto from 'node:crypto';
|
||||||
import type { Bus } from '../bus';
|
import type { Bus } from '../bus';
|
||||||
|
import type { WsSocket } from '../http/ws-core';
|
||||||
import type { ResponseShape } from '../pipeline';
|
import type { ResponseShape } from '../pipeline';
|
||||||
import { isHttpError } from '../signals';
|
import { isHttpError } from '../signals';
|
||||||
import {
|
import {
|
||||||
@@ -62,15 +66,15 @@ function notExported(name: string): Error {
|
|||||||
* The version is checked *before* the secret: a skewed peer is not an auth failure, and telling it
|
* The version is checked *before* the secret: a skewed peer is not an auth failure, and telling it
|
||||||
* "wrong secret" would send its operator hunting the wrong bug.
|
* "wrong secret" would send its operator hunting the wrong bug.
|
||||||
*/
|
*/
|
||||||
function handleHandshake(ws: any, frame: Frame, deps: MeshControlDeps): boolean {
|
function handleHandshake(socket: WsSocket, frame: Frame, deps: MeshControlDeps): boolean {
|
||||||
if (frame.type !== 'hello') {
|
if (frame.type !== 'hello') {
|
||||||
ws.close(1008);
|
socket.close(1008);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frame.v !== MESH_PROTOCOL_VERSION) {
|
if (frame.v !== MESH_PROTOCOL_VERSION) {
|
||||||
ws.close(
|
socket.close(
|
||||||
1008,
|
1008,
|
||||||
`mesh protocol version mismatch: peer speaks v${frame.v}, this teapot speaks v${MESH_PROTOCOL_VERSION}`,
|
`mesh protocol version mismatch: peer speaks v${frame.v}, this teapot speaks v${MESH_PROTOCOL_VERSION}`,
|
||||||
);
|
);
|
||||||
@@ -79,12 +83,12 @@ function handleHandshake(ws: any, frame: Frame, deps: MeshControlDeps): boolean
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!safeEqual(frame.secret, deps.secret)) {
|
if (!safeEqual(frame.secret, deps.secret)) {
|
||||||
ws.close(1008);
|
socket.close(1008);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.send(
|
socket.send(
|
||||||
encode({
|
encode({
|
||||||
type: 'manifest',
|
type: 'manifest',
|
||||||
v: MESH_PROTOCOL_VERSION,
|
v: MESH_PROTOCOL_VERSION,
|
||||||
@@ -119,7 +123,7 @@ function resolveRpc(
|
|||||||
|
|
||||||
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
|
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
|
||||||
async function handleRpc(
|
async function handleRpc(
|
||||||
ws: any,
|
socket: WsSocket,
|
||||||
frame: RpcReqFrame,
|
frame: RpcReqFrame,
|
||||||
deps: MeshControlDeps,
|
deps: MeshControlDeps,
|
||||||
exportedScopes: Set<string>,
|
exportedScopes: Set<string>,
|
||||||
@@ -130,44 +134,64 @@ async function handleRpc(
|
|||||||
try {
|
try {
|
||||||
const result = await resolveRpc(frame, deps, exportedScopes, exportedRoutes);
|
const result = await resolveRpc(frame, deps, exportedScopes, exportedRoutes);
|
||||||
|
|
||||||
ws.send(encode({ type: 'rpc-res', id, ok: true, result }));
|
socket.send(encode({ type: 'rpc-res', id, ok: true, result }));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
||||||
|
|
||||||
deps.bus?.emit('mesh:rpc:error', { name, error: err });
|
deps.bus?.emit('mesh:rpc:error', { name, error: err });
|
||||||
ws.send(encode({ type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } }));
|
socket.send(encode({ type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build a WebSocket handler that authenticates peers and serves scope/route RPCs from the manifest. */
|
/** Drive one control connection to completion: handshake, then serve RPCs until the socket ends. */
|
||||||
export function createMeshControl(deps: MeshControlDeps): { path: string; handle: (ws: any) => void } {
|
async function serveFrames(
|
||||||
|
socket: WsSocket,
|
||||||
|
deps: MeshControlDeps,
|
||||||
|
exportedScopes: Set<string>,
|
||||||
|
exportedRoutes: Set<string>,
|
||||||
|
): Promise<void> {
|
||||||
|
let authed = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
for await (const data of socket.inbound) {
|
||||||
|
let frame: Frame;
|
||||||
|
|
||||||
|
try {
|
||||||
|
frame = decode(String(data));
|
||||||
|
} catch {
|
||||||
|
continue; // undecodable frames are ignored, as before
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!authed) {
|
||||||
|
authed = handleHandshake(socket, frame, deps);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (frame.type !== 'rpc-req') continue;
|
||||||
|
|
||||||
|
// deliberately not awaited: RPCs overlap, exactly as they did under the `ws.on('message')`
|
||||||
|
// listener. The id-keyed `pending` map on the peer exists to allow in-flight concurrency —
|
||||||
|
// awaiting here would silently serialize every mesh call.
|
||||||
|
void handleRpc(socket, frame, deps, exportedScopes, exportedRoutes);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* socket failed; the disconnect emit below is the same signal a clean close gives */
|
||||||
|
} finally {
|
||||||
|
deps.bus?.emit('mesh:disconnect', { name: 'teapot' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build a control handler that authenticates peers and serves scope/route RPCs from the manifest. */
|
||||||
|
export function createMeshControl(deps: MeshControlDeps): { path: string; handle: (socket: WsSocket) => void } {
|
||||||
const exportedScopes = new Set(deps.manifest.scopes.map((scope) => scope.token));
|
const exportedScopes = new Set(deps.manifest.scopes.map((scope) => scope.token));
|
||||||
const exportedRoutes = new Set(deps.manifest.routes.map((route) => `${route.method} ${route.pattern}`));
|
const exportedRoutes = new Set(deps.manifest.routes.map((route) => `${route.method} ${route.pattern}`));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
path: MESH_CONTROL_PATH,
|
path: MESH_CONTROL_PATH,
|
||||||
handle(ws: any) {
|
handle(socket: WsSocket) {
|
||||||
let authed = false;
|
// `channel` is fan-out and subscribes when the async iterator is created, which
|
||||||
ws.on('message', async (data: unknown) => {
|
// `for await` does synchronously — so the loop is live before any frame can arrive.
|
||||||
let frame: Frame;
|
void serveFrames(socket, deps, exportedScopes, exportedRoutes);
|
||||||
|
|
||||||
try {
|
|
||||||
frame = decode(String(data));
|
|
||||||
} catch {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!authed) {
|
|
||||||
authed = handleHandshake(ws, frame, deps);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (frame.type !== 'rpc-req') return;
|
|
||||||
|
|
||||||
await handleRpc(ws, frame, deps, exportedScopes, exportedRoutes);
|
|
||||||
});
|
|
||||||
ws.on('close', () => deps.bus?.emit('mesh:disconnect', { name: 'teapot' }));
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user