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";
2122import * as Hex from "effect/encoding/Hex" ;
2223import * as Layer from "effect/Layer" ;
2324import * as Option from "effect/Option" ;
25+ import * as Predicate from "effect/Predicate" ;
2426import * as Result from "effect/Result" ;
2527import * as Schema from "effect/Schema" ;
2628import * as SqlClient from "effect/sql/SqlClient" ;
@@ -42,6 +44,21 @@ const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
4244const 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+ / C E R T | S E L F _ S I G N E D | U N A B L E _ T O _ ( G E T | V E R I F Y ) / . 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. */
4663export 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 } ) ,
0 commit comments