feat(mesh): carry the caller's identity across the wire, and bound what an unauthenticated peer can do
The observability release gave every request an id and lost it at the process boundary — in the one place a distributed trace is the entire point. The RPC envelope now carries requestId, traceId and url, and the teapot adopts them instead of opening its own investigation. Both fields are optional on the wire, so the protocol version does not move. That decision needed a rule rather than a judgement call, so the rule is now written beside the constant: bump when an old peer would misparse or misbehave silently, not when work happened. Bumping per change would make the number mean the opposite of what it is for. Three things found while in here: - mesh:rpc:error emitted the per-link counter where every other emitter puts a name, so the two sides of one failure could not be matched up. - The teapot had no handshake deadline, so an unauthenticated peer could hold a socket by saying nothing, and no frame size cap, so it could make the process JSON.parse up to whatever the socket layer allowed — 100 MiB with the ws defaults, before authenticating. - The remote-route envelope was cast from the internal request object, putting ip and protocol on the wire as fields the protocol never declared. The cleartext warning is a warning and not a refusal on purpose: a private network already doing mutual TLS is legitimate, and green-tea cannot tell it apart from an exposed one. 4 of the 8 new tests verified to fail against the old code; the rest are the negative cases, which should pass both before and after. The Deno test drives the teapot's frame loop with a raw client, because server-side close codes are where these runtimes have already differed once. 431 vitest / 12 Deno / 7 Bun / edge / JSR dry run. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
8 files changed
+472
-10
No files matched your search
+49
-2
@@ -615,6 +615,26 @@ function assertNeedsSatisfiable(routePlans: RoutePlan[], providerNodes: GraphNod
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warn when a teapot's shared secret would cross a network in cleartext.
|
||||
*
|
||||
* The `hello` frame carries the secret verbatim, so `ws://` to anywhere but this machine puts it on
|
||||
* the wire for anyone in the path to read. Loopback is exempt because there is no path.
|
||||
*
|
||||
* A warning rather than a refusal: a private network behind a service mesh that already does mutual
|
||||
* TLS is a legitimate deployment, and green-tea cannot tell it apart from an exposed one.
|
||||
*/
|
||||
function warnIfCleartext(url: string, logger: Logger): void {
|
||||
if (!url.startsWith('ws://')) return;
|
||||
const host = url.slice('ws://'.length).split('/')[0].split(':')[0];
|
||||
if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]') return;
|
||||
|
||||
logger.warn(
|
||||
`mesh: ${url} is not encrypted, and the shared secret is sent in the handshake — ` +
|
||||
'use wss:// unless the link already runs inside an encrypted network.',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-register a link's app-scope bindings after it reconnects, so their next resolve re-runs the RPC.
|
||||
*
|
||||
@@ -642,6 +662,7 @@ async function spliceRemoteScopes(
|
||||
|
||||
try {
|
||||
for (const teapot of mesh.teapots ?? []) {
|
||||
warnIfCleartext(teapot.url, logger);
|
||||
// Deferred so the reconnect callback can name this link's provider tokens, which are only
|
||||
// known once its manifest arrives — the callback is registered before the link can drop.
|
||||
const rebind: Array<() => void> = [];
|
||||
@@ -717,7 +738,19 @@ async function spliceRemoteScopes(
|
||||
method: route.method,
|
||||
pattern: route.pattern,
|
||||
transport: 'buffer',
|
||||
handler: async (req) => route.handler(req as RequestEnvelope),
|
||||
// Built explicitly rather than cast from the handler argument. The cast put the whole
|
||||
// internal request on the wire — `ip` and `protocol` included — and sent fields the
|
||||
// protocol never declared, so a teapot could come to rely on something we never promised.
|
||||
handler: async (req) =>
|
||||
route.handler({
|
||||
method: req.method,
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
headers: req.headers,
|
||||
url: req.url,
|
||||
correlation: { requestId: req.requestId, traceId: req.traceId },
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -867,7 +900,14 @@ function buildMeshControl(
|
||||
if (container.has(provider.name)) Object.assign(seed, await container.resolve(provider.name));
|
||||
|
||||
const steps = orderedSteps.map((step) => ({ name: step.name, origin: step.origin, run: runners.get(step.name)! }));
|
||||
const context = await runSteps(steps, seed, bus);
|
||||
// The caller's identity, adopted rather than replaced — the same rule an incoming
|
||||
// `x-request-id` gets, applied at the process boundary where it matters most. No `route`:
|
||||
// resolving a scope is not a route, and inventing one would put a token in a route label.
|
||||
const context = await runSteps(steps, seed, bus, {
|
||||
requestId: env.correlation?.requestId,
|
||||
traceId: env.correlation?.traceId,
|
||||
method: env.method,
|
||||
});
|
||||
|
||||
return context[name];
|
||||
};
|
||||
@@ -889,6 +929,13 @@ function buildMeshControl(
|
||||
bus,
|
||||
onError,
|
||||
transport: plan.transport,
|
||||
correlation: {
|
||||
requestId: env.correlation?.requestId,
|
||||
traceId: env.correlation?.traceId,
|
||||
route: plan.pattern,
|
||||
method: plan.method,
|
||||
transport: plan.transport,
|
||||
},
|
||||
seed: { ...provided, req: env, params: env.params, query: env.query, body: env.body, headers: env.headers },
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user