55 EnvironmentHttpApi ,
66 EnvironmentId ,
77 type ExecutionEnvironmentCapabilities ,
8+ type ExecutionEnvironmentDescriptor ,
89 MCP_PEER_LINK_SOFTWARE_ID ,
910 type PeerLink ,
1011 type PeerLinkCreateInput ,
@@ -22,6 +23,7 @@ import * as Base64Url from "effect/encoding/Base64Url";
2223import * as Hex from "effect/encoding/Hex" ;
2324import * as Layer from "effect/Layer" ;
2425import * as Option from "effect/Option" ;
26+ import * as Predicate from "effect/Predicate" ;
2527import * as Redacted from "effect/Redacted" ;
2628import * as Result from "effect/Result" ;
2729import * as Schema from "effect/Schema" ;
@@ -50,6 +52,23 @@ const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
5052const probeTimeout = ( origin : string ) =>
5153 isLocalLoopbackHost ( new URL ( origin ) . hostname ) ? Duration . seconds ( 3 ) : Duration . seconds ( 10 ) ;
5254
55+ /** Node's TLS verification code in a failed request's cause chain, if any. */
56+ export const certificateErrorCode = ( error : unknown ) => {
57+ let current = error ;
58+ for ( let depth = 0 ; depth < 6 && Predicate . isObject ( current ) ; depth ++ ) {
59+ if (
60+ Predicate . hasProperty ( current , "code" ) &&
61+ typeof current . code === "string" &&
62+ / 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 ) | I N V A L I D _ ( C A | P U R P O S E ) | P A T H _ L E N G T H _ E X C E E D E D | H O S T N A M E _ M I S M A T C H / . test (
63+ current . code ,
64+ )
65+ )
66+ return current . code ;
67+ current = Predicate . hasProperty ( current , "cause" ) ? current . cause : undefined ;
68+ }
69+ return undefined ;
70+ } ;
71+
5372/** A usable link: where to reach the peer and the token to send there. */
5473export interface ResolvedPeerLink {
5574 readonly link : PeerLink ;
@@ -197,14 +216,38 @@ const make = Effect.gen(function* () {
197216 Effect . provideService ( HttpClient . HttpClient , peerHttpClient ) ,
198217 ) ;
199218
200- /** The environment answering at `origin`, or nothing when it does not answer as T3 Code. */
219+ /**
220+ * The environment answering at `origin`, or nothing when it does not answer
221+ * as T3 Code. A certificate this machine rejects fails instead, since
222+ * the peer may well be there.
223+ */
201224 const describe = ( origin : string ) =>
202225 clientFor ( origin ) . pipe (
203226 Effect . flatMap ( ( client ) => client . metadata . descriptor ( ) ) ,
204227 Effect . timeout ( probeTimeout ( origin ) ) ,
205- Effect . option ,
228+ Effect . asSome ,
229+ Effect . catch ( ( error ) => {
230+ const code = certificateErrorCode ( error ) ;
231+ return code === undefined
232+ ? Effect . succeed ( Option . none < ExecutionEnvironmentDescriptor > ( ) )
233+ : Effect . fail (
234+ new PeerLinkError ( {
235+ reason : "unreachable" ,
236+ message : `${ origin } presented a certificate this machine rejected (${ code } ).` ,
237+ } ) ,
238+ ) ;
239+ } ) ,
206240 ) ;
207241
242+ /** Whether `url` answers as `environmentId`, counting an untrusted certificate as no. */
243+ const answersAs = Effect . fn ( "PeerLinks.answersAs" ) ( function * (
244+ url : string ,
245+ environmentId : EnvironmentId ,
246+ ) {
247+ const found = yield * describe ( url ) . pipe ( Effect . orElseSucceed ( ( ) => Option . none ( ) ) ) ;
248+ return Option . filter ( found , ( peer ) => peer . environmentId === environmentId ) ;
249+ } ) ;
250+
208251 const toLink = Effect . fn ( "PeerLinks.toLink" ) ( function * ( row : PeerLinkRow ) {
209252 return {
210253 environmentId : EnvironmentId . make ( row . environment_id ) ,
@@ -511,11 +554,9 @@ const make = Effect.gen(function* () {
511554 if ( DateTime . isLessThanOrEqualTo ( link . expiresAt , now ) ) return { ...link , status : "expired" } ;
512555 let label = link . label ;
513556 const answered = yield * Effect . findFirst ( link . urls , ( url ) =>
514- describe ( url ) . pipe (
557+ answersAs ( url , link . environmentId ) . pipe (
515558 Effect . map ( ( found ) => {
516- if ( Option . isNone ( found ) || found . value . environmentId !== link . environmentId ) {
517- return false ;
518- }
559+ if ( Option . isNone ( found ) ) return false ;
519560 label = found . value . label ;
520561 return true ;
521562 } ) ,
@@ -584,9 +625,9 @@ const make = Effect.gen(function* () {
584625 let fenced = true ;
585626 let label = link . label ;
586627 const url = yield * Effect . findFirst ( link . urls , ( candidate ) =>
587- describe ( candidate ) . pipe (
628+ answersAs ( candidate , environmentId ) . pipe (
588629 Effect . map ( ( found ) => {
589- if ( Option . isNone ( found ) || found . value . environmentId !== environmentId ) return false ;
630+ if ( Option . isNone ( found ) ) return false ;
590631 fenced = linkLimitsHold ( found . value ) ;
591632 label = found . value . label ;
592633 return true ;
0 commit comments