@@ -176,6 +176,7 @@ import * as VcsProcess from "./vcs/VcsProcess.ts";
176176import * as GitWorkflowService from "./git/GitWorkflowService.ts" ;
177177import * as ReviewService from "./review/ReviewService.ts" ;
178178import * as SourceControlRepositoryService from "./sourceControl/SourceControlRepositoryService.ts" ;
179+ import { REPLAY_MARKER_MAX_AGE } from "./auth/replayMarkers.ts" ;
179180import * as ServerSecretStore from "./auth/ServerSecretStore.ts" ;
180181import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts" ;
181182import * as PairingGrantStore from "./auth/PairingGrantStore.ts" ;
@@ -2643,6 +2644,40 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
26432644 } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
26442645 ) ;
26452646
2647+ it . effect ( "rejects a DPoP replay by time alone once its marker can be pruned" , ( ) =>
2648+ Effect . gen ( function * ( ) {
2649+ yield * buildAppUnderTest ( ) ;
2650+
2651+ const ownerCookie = yield * getAuthenticatedSessionCookieHeader ( ) ;
2652+ const credentialResponse = yield * HttpClient . post ( "/api/auth/pairing-token" , {
2653+ headers : { cookie : ownerCookie } ,
2654+ body : yield * HttpBody . json ( { } ) ,
2655+ } ) ;
2656+ const credential = ( yield * credentialResponse . json ) as { readonly credential : string } ;
2657+ const tokenUrl = yield * getHttpServerUrl ( "/oauth/token" ) ;
2658+ const acceptedAt = yield * DateTime . now ;
2659+ // The longest-lived proof: `iat` at the 5 s future skew the verifier allows.
2660+ const dpop = makeDpopProof ( {
2661+ method : "POST" ,
2662+ url : tokenUrl ,
2663+ iat : Math . floor ( acceptedAt . epochMilliseconds / 1_000 ) + 5 ,
2664+ } ) ;
2665+ const exchange = exchangeAccessToken ( credential . credential , {
2666+ headers : { dpop : dpop . proof } ,
2667+ scope : "orchestration:read orchestration:operate terminal:operate review:write" ,
2668+ } ) ;
2669+
2670+ assert . equal ( ( yield * exchange ) . response . status , 200 ) ;
2671+ // While the proof is fresh, only the replay marker rejects it.
2672+ assert . equal ( ( yield * exchange ) . body . dpopFailureReason , "replay" ) ;
2673+ // Once the marker can be pruned, the time check rejects the proof by itself.
2674+ yield * TestClock . setTime (
2675+ acceptedAt . epochMilliseconds + Duration . toMillis ( REPLAY_MARKER_MAX_AGE ) ,
2676+ ) ;
2677+ assert . equal ( ( yield * exchange ) . body . dpopFailureReason , "time_window" ) ;
2678+ } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
2679+ ) ;
2680+
26462681 it . effect ( "ignores forwarded host headers when validating token exchange DPoP URLs" , ( ) =>
26472682 Effect . gen ( function * ( ) {
26482683 yield * buildAppUnderTest ( ) ;
@@ -3710,6 +3745,82 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
37103745 } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
37113746 ) ;
37123747
3748+ it . effect ( "rejects cloud replays by time alone once their markers can be pruned" , ( ) =>
3749+ Effect . gen ( function * ( ) {
3750+ yield * buildAppUnderTest ( ) ;
3751+
3752+ const cloudKeyPair = NodeCrypto . generateKeyPairSync ( "ed25519" , {
3753+ privateKeyEncoding : { format : "pem" , type : "pkcs8" } ,
3754+ publicKeyEncoding : { format : "pem" , type : "spki" } ,
3755+ } ) ;
3756+ const ownerCookie = yield * getAuthenticatedSessionCookieHeader ( ) ;
3757+ const relayConfigResponse = yield * fetchEffect (
3758+ yield * getHttpServerUrl ( "/api/connect/relay-config" ) ,
3759+ {
3760+ method : "POST" ,
3761+ headers : { cookie : ownerCookie , "content-type" : "application/json" } ,
3762+ body : jsonRequestBody ( {
3763+ relayUrl : "https://relay.example.test" ,
3764+ cloudUserId : "user_123" ,
3765+ environmentCredential : "t3env_test_credential" ,
3766+ cloudMintPublicKey : cloudKeyPair . publicKey ,
3767+ endpointRuntime : null ,
3768+ } ) ,
3769+ } ,
3770+ ) ;
3771+ assert . equal ( relayConfigResponse . status , 200 ) ;
3772+
3773+ const acceptedAt = yield * DateTime . now ;
3774+ // The longest-lived proofs: `iat` at the 60 s future skew the handlers
3775+ // allow, and the 5 minute maximum lifetime.
3776+ const issuedAt = DateTime . add ( acceptedAt , { minutes : 1 } ) ;
3777+ const proofTimes = {
3778+ issuedAt : DateTime . formatIso ( issuedAt ) ,
3779+ expiresAt : DateTime . formatIso ( DateTime . add ( issuedAt , { minutes : 5 } ) ) ,
3780+ } ;
3781+ const requests = [
3782+ [
3783+ "/api/t3-connect/health" ,
3784+ makeCloudEnvironmentHealthRequest ( {
3785+ privateKey : cloudKeyPair . privateKey ,
3786+ environmentId : testEnvironmentDescriptor . environmentId ,
3787+ nonce : "cloud-health-nonce-pruned" ,
3788+ ...proofTimes ,
3789+ } ) ,
3790+ ] ,
3791+ [
3792+ "/api/t3-connect/mint-credential" ,
3793+ makeCloudMintCredentialRequest ( {
3794+ privateKey : cloudKeyPair . privateKey ,
3795+ environmentId : testEnvironmentDescriptor . environmentId ,
3796+ clientProofKeyThumbprint : "client-proof-key-thumbprint" ,
3797+ nonce : "cloud-mint-nonce-pruned" ,
3798+ ...proofTimes ,
3799+ } ) ,
3800+ ] ,
3801+ ] as const ;
3802+ const postAll = Effect . forEach ( requests , ( [ pathname , request ] ) =>
3803+ Effect . gen ( function * ( ) {
3804+ const response = yield * fetchEffect ( yield * getHttpServerUrl ( pathname ) , {
3805+ method : "POST" ,
3806+ headers : { "content-type" : "application/json" } ,
3807+ body : jsonRequestBody ( request ) ,
3808+ } ) ;
3809+ return response . status ;
3810+ } ) ,
3811+ ) ;
3812+
3813+ assert . deepStrictEqual ( yield * postAll , [ 200 , 200 ] ) ;
3814+ // While the proofs are fresh, only the replay markers reject them (409).
3815+ assert . deepStrictEqual ( yield * postAll , [ 409 , 409 ] ) ;
3816+ // Once the markers can be pruned, the time checks reject the proofs by themselves (401).
3817+ yield * TestClock . setTime (
3818+ acceptedAt . epochMilliseconds + Duration . toMillis ( REPLAY_MARKER_MAX_AGE ) ,
3819+ ) ;
3820+ assert . deepStrictEqual ( yield * postAll , [ 401 , 401 ] ) ;
3821+ } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
3822+ ) ;
3823+
37133824 it . effect (
37143825 "validates cloud proofs against the configured relay issuer, not the transport URL" ,
37153826 ( ) =>
0 commit comments