Skip to content

Commit d0d37fc

Browse files
committed
fix(server): linking names an untrusted peer certificate
Linking to an https peer whose certificate this machine does not trust reported that nothing there answered as T3 Code. The descriptor probe dropped the TLS error with Effect.option, so the real cause was lost. The probe now fails with the TLS code, and status and token checks still treat that address as not answering.
1 parent da04ebc commit d0d37fc

4 files changed

Lines changed: 130 additions & 9 deletions

File tree

‎apps/server/src/peer/PeerLinks.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off - The HTTPS test server needs certificate bytes.
2+
import * as NodeFS from "node:fs";
3+
// @effect-diagnostics-next-line nodeBuiltinImport:off - A peer with a certificate this machine does not trust.
4+
import * as NodeHttps from "node:https";
15
import { NodeHttpServer } from "@effect/platform-node";
26
import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts";
37
import { expect, it } from "@effect/vitest";
@@ -526,3 +530,40 @@ it.effect("linking a picked machine that answers nowhere names it and links noth
526530
}),
527531
).pipe(Effect.provide(NodeHttpServer.layerTest)),
528532
);
533+
534+
it.effect("names an untrusted certificate instead of calling the peer something else", () =>
535+
Effect.scoped(
536+
Effect.gen(function* () {
537+
const server = yield* Layer.build(
538+
Layer.fresh(
539+
NodeHttpServer.layer(
540+
() =>
541+
NodeHttps.createServer({
542+
key: NodeFS.readFileSync(new URL("./fixtures/localhost-key.pem", import.meta.url)),
543+
cert: NodeFS.readFileSync(
544+
new URL("./fixtures/localhost-cert.pem", import.meta.url),
545+
),
546+
}),
547+
{ port: 0, host: "127.0.0.1" },
548+
),
549+
),
550+
);
551+
const url = yield* Effect.gen(function* () {
552+
yield* HttpServer.serve(Effect.succeed(HttpServerResponse.empty({ status: 404 }))).pipe(
553+
Layer.build,
554+
);
555+
const address = (yield* HttpServer.HttpServer).address;
556+
if (address._tag === "UnixPathAddress") return yield* Effect.die("expected a TCP address");
557+
return `https://127.0.0.1:${address.port}`;
558+
}).pipe(Effect.provideContext(server));
559+
const a = yield* makeA(laptop);
560+
const failed = yield* a.links
561+
.link({ url, pairingCode: "unused", access: "auto" })
562+
.pipe(Effect.flip);
563+
expect(failed.reason).toBe("unreachable");
564+
expect(failed.message).toContain("certificate");
565+
expect(failed.message).toContain("DEPTH_ZERO_SELF_SIGNED_CERT");
566+
expect(yield* a.links.list).toEqual([]);
567+
}),
568+
),
569+
);

‎apps/server/src/peer/PeerLinks.ts‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
EnvironmentHttpApi,
66
EnvironmentId,
77
type ExecutionEnvironmentCapabilities,
8+
type ExecutionEnvironmentDescriptor,
89
MCP_PEER_LINK_SOFTWARE_ID,
910
type PeerLink,
1011
type PeerLinkCreateInput,
@@ -21,6 +22,7 @@ import * as Base64Url from "effect/encoding/Base64Url";
2122
import * as Hex from "effect/encoding/Hex";
2223
import * as Layer from "effect/Layer";
2324
import * as Option from "effect/Option";
25+
import * as Predicate from "effect/Predicate";
2426
import * as Result from "effect/Result";
2527
import * as Schema from "effect/Schema";
2628
import * as SqlClient from "effect/sql/SqlClient";
@@ -42,6 +44,21 @@ const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
4244
const probeTimeout = (origin: string) =>
4345
isLocalLoopbackHost(new URL(origin).hostname) ? Duration.seconds(3) : Duration.seconds(10);
4446

47+
/** Node's TLS verification code in a failed request's cause chain, if any. */
48+
const certificateErrorCode = (error: unknown) => {
49+
let current = error;
50+
for (let depth = 0; depth < 6 && Predicate.isObject(current); depth++) {
51+
if (
52+
Predicate.hasProperty(current, "code") &&
53+
typeof current.code === "string" &&
54+
/CERT|SELF_SIGNED|UNABLE_TO_(GET|VERIFY)/.test(current.code)
55+
)
56+
return current.code;
57+
current = Predicate.hasProperty(current, "cause") ? current.cause : undefined;
58+
}
59+
return undefined;
60+
};
61+
4562
/** A usable link: where to reach the peer and the token to send there. */
4663
export interface ResolvedPeerLink {
4764
readonly link: PeerLink;
@@ -188,12 +205,34 @@ const make = Effect.gen(function* () {
188205
Effect.provideService(HttpClient.HttpClient, peerHttpClient),
189206
);
190207

191-
/** The environment answering at `origin`, or nothing when it does not answer as T3 Code. */
208+
/**
209+
* The environment answering at `origin`, or nothing when it does not answer
210+
* as T3 Code. A certificate this machine does not trust fails instead, since
211+
* the peer may well be there.
212+
*/
192213
const describe = (origin: string) =>
193214
clientFor(origin).pipe(
194215
Effect.flatMap((client) => client.metadata.descriptor()),
195216
Effect.timeout(probeTimeout(origin)),
196-
Effect.option,
217+
Effect.asSome,
218+
Effect.catch((error) => {
219+
const code = certificateErrorCode(error);
220+
return code === undefined
221+
? Effect.succeed(Option.none<ExecutionEnvironmentDescriptor>())
222+
: Effect.fail(
223+
new PeerLinkError({
224+
reason: "unreachable",
225+
message: `${origin} presented a certificate this machine does not trust (${code}).`,
226+
}),
227+
);
228+
}),
229+
);
230+
231+
/** Whether `url` answers as `environmentId`, counting an untrusted certificate as no. */
232+
const answersAs = (url: string, environmentId: EnvironmentId) =>
233+
describe(url).pipe(
234+
Effect.orElseSucceed(() => Option.none()),
235+
Effect.map((found) => Option.filter(found, (peer) => peer.environmentId === environmentId)),
197236
);
198237

199238
const toLink = Effect.fn("PeerLinks.toLink")(function* (row: PeerLinkRow) {
@@ -489,11 +528,7 @@ const make = Effect.gen(function* () {
489528
DateTime.isLessThanOrEqualTo(link.expiresAt, now)
490529
? Effect.succeed<PeerLinkStatus>("expired")
491530
: Effect.findFirst(link.urls, (url) =>
492-
describe(url).pipe(
493-
Effect.map(
494-
(found) => Option.isSome(found) && found.value.environmentId === link.environmentId,
495-
),
496-
),
531+
answersAs(url, link.environmentId).pipe(Effect.map(Option.isSome)),
497532
).pipe(
498533
Effect.map((answered): PeerLinkStatus =>
499534
Option.isSome(answered) ? "reachable" : "unreachable",
@@ -560,9 +595,9 @@ const make = Effect.gen(function* () {
560595
// reused LAN address or a moved hostname never receives it.
561596
let fenced = true;
562597
const url = yield* Effect.findFirst(link.urls, (candidate) =>
563-
describe(candidate).pipe(
598+
answersAs(candidate, environmentId).pipe(
564599
Effect.map((found) => {
565-
if (Option.isNone(found) || found.value.environmentId !== environmentId) return false;
600+
if (Option.isNone(found)) return false;
566601
fenced = linkLimitsHold(found.value);
567602
return true;
568603
}),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICyTCCAbGgAwIBAgIJAOpcH/7sbrKbMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV
3+
BAMMCWxvY2FsaG9zdDAeFw0yNjEwMjgxMzIwMDJaFw0zNjEwMjUxMzIwMDJaMBQx
4+
EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
5+
ggEBANHdJw88xLujStCPbzRi9ESeQ68eLHbOYCWEgViNAmzU4HvMINRxLtIFcIbK
6+
E8AZ+zRauNSJTbQdvBLxYmbzmQZb9tzQZ28VqlkaXDRvExLhbJe9csxbRExcwEbN
7+
tBXhyEztlSgJSGP+a22GYsGBljCIaOC6HtpbJo1/GBwhKlJfucGuwr2aT/njUaby
8+
BNFaam1M44jCIqmQ1WifxMZxDzlegjKbBeis6q/2aA+EygQ4OxdjaADFj6+ZQsxm
9+
MoEbNKni4mERPKfTM9irh8UoZDCTFHLHqTUQkzIr6dTO10ljZOLCxCmmKiyeUXH6
10+
Q+IFJYxXk3k5OZ+3qo1/RqOQelcCAwEAAaMeMBwwGgYDVR0RBBMwEYIJbG9jYWxo
11+
b3N0hwR/AAABMA0GCSqGSIb3DQEBCwUAA4IBAQCRpA0t5fnHYhOvm7b1McZV10le
12+
WXaJIw17ksV0BfJyZhWm2ru8udEJ6qtzSnxrHhBBHUXUJVPSJfbgV+UFcpTPnjGZ
13+
RZzYr7Oq7K/2VyRUCRMQRv4ONo3wbh91DoN6llFiX6VYpa1kuVRscs12jPXomZ9W
14+
Ca1jVTqr9Ty8izHCqVhNgb8xcvEONLCj8Ikeeej4cVl7gW6538UJj0LpQGv2suUR
15+
0dAFo0tSE7KqcsPPsfdOiWWSWhy8r7unCdU/ySmIDK2/tOsQ56vkAl/0zbNVTxck
16+
gSZOEpfqpWCDxg0txC08dQ7+Gh6MdomPXTb/Zxw9pHU3EjV14tQtcDkFa5VS
17+
-----END CERTIFICATE-----
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDR3ScPPMS7o0rQ
3+
j280YvREnkOvHix2zmAlhIFYjQJs1OB7zCDUcS7SBXCGyhPAGfs0WrjUiU20HbwS
4+
8WJm85kGW/bc0GdvFapZGlw0bxMS4WyXvXLMW0RMXMBGzbQV4chM7ZUoCUhj/mtt
5+
hmLBgZYwiGjguh7aWyaNfxgcISpSX7nBrsK9mk/541Gm8gTRWmptTOOIwiKpkNVo
6+
n8TGcQ85XoIymwXorOqv9mgPhMoEODsXY2gAxY+vmULMZjKBGzSp4uJhETyn0zPY
7+
q4fFKGQwkxRyx6k1EJMyK+nUztdJY2TiwsQppiosnlFx+kPiBSWMV5N5OTmft6qN
8+
f0ajkHpXAgMBAAECggEBAMrmKBdhR0uqinCt3HQnn8afxQyP/ak3ylk7jNqSKmAS
9+
SJ8oMi1y6DOBstN+mC3IcMijVcOWpBuaC/W8jsi+h5pQW5INo5MQvUu8Yp0HLse4
10+
Toc+ULknHBdALV3HD2+AhWlq4iR9JRIYLsELhntT0GNH3y42hBHUwqdW03aO0h4k
11+
UHPDtXBA0qWEwLUxtDt6dMWblaBc679FVpQflmj1zjVvcir8hgU5jE8B52zXo0AW
12+
2QtrKZe0ND1TwCm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