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 { WsSocket } from '../http/ws-core';
|
||||
import type { ResponseShape } from '../pipeline';
|
||||
import { isHttpError } from '../signals';
|
||||
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
|
||||
* "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') {
|
||||
ws.close(1008);
|
||||
socket.close(1008);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (frame.v !== MESH_PROTOCOL_VERSION) {
|
||||
ws.close(
|
||||
socket.close(
|
||||
1008,
|
||||
`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)) {
|
||||
ws.close(1008);
|
||||
socket.close(1008);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
ws.send(
|
||||
socket.send(
|
||||
encode({
|
||||
type: 'manifest',
|
||||
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. */
|
||||
async function handleRpc(
|
||||
ws: any,
|
||||
socket: WsSocket,
|
||||
frame: RpcReqFrame,
|
||||
deps: MeshControlDeps,
|
||||
exportedScopes: Set<string>,
|
||||
@@ -130,44 +134,64 @@ async function handleRpc(
|
||||
try {
|
||||
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) {
|
||||
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
||||
|
||||
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. */
|
||||
export function createMeshControl(deps: MeshControlDeps): { path: string; handle: (ws: any) => void } {
|
||||
/** Drive one control connection to completion: handshake, then serve RPCs until the socket ends. */
|
||||
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 exportedRoutes = new Set(deps.manifest.routes.map((route) => `${route.method} ${route.pattern}`));
|
||||
|
||||
return {
|
||||
path: MESH_CONTROL_PATH,
|
||||
handle(ws: any) {
|
||||
let authed = false;
|
||||
ws.on('message', async (data: unknown) => {
|
||||
let frame: Frame;
|
||||
|
||||
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' }));
|
||||
handle(socket: WsSocket) {
|
||||
// `channel` is fan-out and subscribes when the async iterator is created, which
|
||||
// `for await` does synchronously — so the loop is live before any frame can arrive.
|
||||
void serveFrames(socket, deps, exportedScopes, exportedRoutes);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user