@@ -92,7 +92,7 @@ it.layer(NodeServices.layer)("DpopReplayStore.layer", (it) => {
9292 } ) . pipe ( Effect . provide ( makeDpopReplayStoreLayer ( ) ) ) ,
9393 ) ;
9494
95- it . effect ( "creates a durable legacy marker during the compatibility window " , ( ) =>
95+ it . effect ( "does not create legacy markers when claiming a new proof " , ( ) =>
9696 Effect . gen ( function * ( ) {
9797 const replayStore = yield * DpopReplayStore . DpopReplayStore ;
9898 const crypto = yield * Crypto . Crypto ;
@@ -108,8 +108,7 @@ it.layer(NodeServices.layer)("DpopReplayStore.layer", (it) => {
108108 yield * replayStore . claim ( { thumbprint, jti } ) ;
109109
110110 const legacyMarker = path . join ( config . secretsDir , `dpop-proof-${ replayKey } .bin` ) ;
111- const marker = yield * fileSystem . readFile ( legacyMarker ) ;
112- assert . equal ( marker . byteLength , 0 ) ;
111+ assert . isFalse ( yield * fileSystem . exists ( legacyMarker ) ) ;
113112 } ) . pipe ( Effect . provide ( makeDpopReplayStoreLayer ( ) ) ) ,
114113 ) ;
115114
@@ -170,7 +169,7 @@ it.layer(NodeServices.layer)("DpopReplayStore.layer", (it) => {
170169 } ) . pipe ( Effect . provide ( makeSecondMarkerOpenFailureStoreLayer ( ) ) ) ,
171170 ) ;
172171
173- it . effect ( "honors legacy markers until their replay window expires " , ( ) =>
172+ it . effect ( "retains and rejects legacy markers indefinitely, including after restart " , ( ) =>
174173 Effect . gen ( function * ( ) {
175174 const replayStore = yield * DpopReplayStore . DpopReplayStore ;
176175 const crypto = yield * Crypto . Crypto ;
@@ -191,11 +190,112 @@ it.layer(NodeServices.layer)("DpopReplayStore.layer", (it) => {
191190 assert . equal ( protectedReplay . source , "legacy" ) ;
192191 }
193192
194- yield * TestClock . adjust ( Duration . minutes ( 6 ) ) ;
193+ yield * TestClock . adjust ( Duration . hours ( 24 ) ) ;
195194 yield * replayStore . prune ( ) ;
196- const remainingSecrets = yield * fileSystem . readDirectory ( config . secretsDir ) ;
197- assert . notInclude ( remainingSecrets , path . basename ( legacyMarker ) ) ;
198- yield * replayStore . claim ( { thumbprint, jti } ) ;
195+ assert . isTrue ( yield * fileSystem . exists ( legacyMarker ) ) ;
196+ const laterReplay = yield * Effect . flip ( replayStore . claim ( { thumbprint, jti } ) ) ;
197+ assert . equal ( laterReplay . _tag , "DpopReplayAlreadyClaimedError" ) ;
198+
199+ const restartedStore = yield * Effect . scoped ( DpopReplayStore . make ) ;
200+ const restartedReplay = yield * Effect . flip ( restartedStore . claim ( { thumbprint, jti } ) ) ;
201+ assert . equal ( restartedReplay . _tag , "DpopReplayAlreadyClaimedError" ) ;
202+ } ) . pipe ( Effect . provide ( makeDpopReplayStoreTestClockLayer ( ) ) ) ,
203+ ) ;
204+
205+ it . effect ( "rejects a legacy marker written just before the former startup deadline" , ( ) =>
206+ Effect . gen ( function * ( ) {
207+ const replayStore = yield * DpopReplayStore . DpopReplayStore ;
208+ const crypto = yield * Crypto . Crypto ;
209+ const fileSystem = yield * FileSystem . FileSystem ;
210+ const path = yield * Path . Path ;
211+ const config = yield * ServerConfig . ServerConfig ;
212+ const input = { thumbprint : "legacy-thumbprint" , jti : "late-legacy-jti" } ;
213+ const replayKey = yield * crypto
214+ . digest ( "SHA-256" , new TextEncoder ( ) . encode ( `${ input . thumbprint } :${ input . jti } ` ) )
215+ . pipe ( Effect . map ( Encoding . encodeBase64Url ) ) ;
216+
217+ yield * TestClock . adjust ( Duration . seconds ( 359 ) ) ;
218+ yield * fileSystem . writeFile (
219+ path . join ( config . secretsDir , `dpop-proof-${ replayKey } .bin` ) ,
220+ new Uint8Array ( ) ,
221+ ) ;
222+ yield * TestClock . adjust ( Duration . seconds ( 2 ) ) ;
223+ const error = yield * Effect . flip ( replayStore . claim ( input ) ) ;
224+ assert . equal ( error . _tag , "DpopReplayAlreadyClaimedError" ) ;
225+ if ( error . _tag === "DpopReplayAlreadyClaimedError" ) {
226+ assert . equal ( error . source , "legacy" ) ;
227+ }
199228 } ) . pipe ( Effect . provide ( makeDpopReplayStoreTestClockLayer ( ) ) ) ,
200229 ) ;
230+
231+ it . effect ( "does not prune a fresh legacy marker written long after startup" , ( ) =>
232+ Effect . gen ( function * ( ) {
233+ const replayStore = yield * DpopReplayStore . DpopReplayStore ;
234+ const crypto = yield * Crypto . Crypto ;
235+ const fileSystem = yield * FileSystem . FileSystem ;
236+ const path = yield * Path . Path ;
237+ const config = yield * ServerConfig . ServerConfig ;
238+ const input = { thumbprint : "legacy-thumbprint" , jti : "fresh-legacy-jti" } ;
239+ const replayKey = yield * crypto
240+ . digest ( "SHA-256" , new TextEncoder ( ) . encode ( `${ input . thumbprint } :${ input . jti } ` ) )
241+ . pipe ( Effect . map ( Encoding . encodeBase64Url ) ) ;
242+ const legacyMarker = path . join ( config . secretsDir , `dpop-proof-${ replayKey } .bin` ) ;
243+
244+ yield * TestClock . adjust ( Duration . hours ( 1 ) ) ;
245+ yield * fileSystem . writeFile ( legacyMarker , new Uint8Array ( [ 42 ] ) ) ;
246+ yield * replayStore . prune ( ) ;
247+ assert . deepEqual ( yield * fileSystem . readFile ( legacyMarker ) , new Uint8Array ( [ 42 ] ) ) ;
248+ const error = yield * Effect . flip ( replayStore . claim ( input ) ) ;
249+ assert . equal ( error . _tag , "DpopReplayAlreadyClaimedError" ) ;
250+ } ) . pipe ( Effect . provide ( makeDpopReplayStoreTestClockLayer ( ) ) ) ,
251+ ) ;
252+
253+ it . effect ( "allows only one concurrent claim across independent stores sharing secrets" , ( ) =>
254+ Effect . gen ( function * ( ) {
255+ const firstStore = yield * DpopReplayStore . DpopReplayStore ;
256+ const secondStore = yield * Effect . scoped ( DpopReplayStore . make ) ;
257+ const input = { thumbprint : "shared-thumbprint" , jti : "concurrent-jti" } ;
258+ const outcomes = yield * Effect . all (
259+ [ firstStore , secondStore ] . map ( ( store ) =>
260+ store . claim ( input ) . pipe (
261+ Effect . match ( {
262+ onSuccess : ( ) => "claimed" ,
263+ onFailure : ( error ) => error . _tag ,
264+ } ) ,
265+ ) ,
266+ ) ,
267+ { concurrency : "unbounded" } ,
268+ ) ;
269+ assert . sameMembers ( outcomes , [ "claimed" , "DpopReplayAlreadyClaimedError" ] ) ;
270+ } ) . pipe ( Effect . provide ( makeDpopReplayStoreLayer ( ) ) ) ,
271+ ) ;
272+
273+ it . effect ( "fails closed when reading a legacy marker returns an unexpected stat error" , ( ) =>
274+ Effect . gen ( function * ( ) {
275+ const fileSystem = yield * FileSystem . FileSystem ;
276+ const store = yield * Effect . scoped (
277+ DpopReplayStore . make . pipe (
278+ Effect . provideService ( FileSystem . FileSystem , {
279+ ...fileSystem ,
280+ stat : ( path ) =>
281+ String ( path ) . includes ( "dpop-proof-" )
282+ ? Effect . fail (
283+ PlatformError . systemError ( {
284+ _tag : "PermissionDenied" ,
285+ module : "FileSystem" ,
286+ method : "stat" ,
287+ pathOrDescriptor : String ( path ) ,
288+ description : "Injected legacy marker stat failure." ,
289+ } ) ,
290+ )
291+ : fileSystem . stat ( path ) ,
292+ } ) ,
293+ ) ,
294+ ) ;
295+ const error = yield * Effect . flip (
296+ store . claim ( { thumbprint : "thumbprint" , jti : "stat-failure-jti" } ) ,
297+ ) ;
298+ assert . equal ( error . _tag , "DpopReplayStoreClaimError" ) ;
299+ } ) . pipe ( Effect . provide ( makeDpopReplayStoreLayer ( ) ) ) ,
300+ ) ;
201301} ) ;
0 commit comments