Skip to content

Commit 9eaf1d0

Browse files
committed
fix(server): preserve legacy replay markers across upgrades
1 parent 6aeab92 commit 9eaf1d0

4 files changed

Lines changed: 158 additions & 104 deletions

File tree

‎apps/server/src/auth/DpopReplayStore.test.ts‎

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

‎apps/server/src/auth/DpopReplayStore.ts‎

Lines changed: 30 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,19 @@ import * as ServerConfig from "../config.ts";
1414

1515
const REPLAY_BUCKET_DURATION = Duration.minutes(10);
1616
const REPLAY_BUCKET_MILLISECONDS = Duration.toMillis(REPLAY_BUCKET_DURATION);
17-
const LEGACY_REPLAY_RETENTION = Duration.minutes(6);
1817
const REPLAY_DIRECTORY_NAME = "dpop-replay";
1918
const replayBucketPattern = /^(0|[1-9][0-9]*)$/;
20-
const legacyReplayMarkerPattern = /^dpop-proof-[A-Za-z0-9_-]{43}\.bin$/;
2119

2220
const dpopReplayStoreSetupStage = Schema.Literals(["make-directory", "set-permissions"]);
2321
const dpopReplayStoreClaimStage = Schema.Literals([
22+
"legacy-lookup",
2423
"ensure-bucket",
2524
"sync-directory",
2625
"write-marker",
2726
]);
28-
const dpopReplayStorePruneStage = Schema.Literals([
29-
"read-buckets",
30-
"remove-bucket",
31-
"read-legacy",
32-
"remove-legacy",
33-
]);
27+
const dpopReplayStorePruneStage = Schema.Literals(["read-buckets", "remove-bucket"]);
3428

35-
export class DpopReplayStoreSetupError extends Schema.TaggedErrorClass<DpopReplayStoreSetupError>()(
29+
export class DpopReplayStoreSetupError extends Schema.TaggedError<DpopReplayStoreSetupError>()(
3630
"DpopReplayStoreSetupError",
3731
{
3832
cause: Schema.Defect(),
@@ -45,7 +39,7 @@ export class DpopReplayStoreSetupError extends Schema.TaggedErrorClass<DpopRepla
4539
}
4640
}
4741

48-
export class DpopReplayStoreKeyCalculationError extends Schema.TaggedErrorClass<DpopReplayStoreKeyCalculationError>()(
42+
export class DpopReplayStoreKeyCalculationError extends Schema.TaggedError<DpopReplayStoreKeyCalculationError>()(
4943
"DpopReplayStoreKeyCalculationError",
5044
{ cause: Schema.Defect() },
5145
) {
@@ -54,7 +48,7 @@ export class DpopReplayStoreKeyCalculationError extends Schema.TaggedErrorClass<
5448
}
5549
}
5650

57-
export class DpopReplayStoreClaimError extends Schema.TaggedErrorClass<DpopReplayStoreClaimError>()(
51+
export class DpopReplayStoreClaimError extends Schema.TaggedError<DpopReplayStoreClaimError>()(
5852
"DpopReplayStoreClaimError",
5953
{
6054
cause: Schema.Defect(),
@@ -68,7 +62,7 @@ export class DpopReplayStoreClaimError extends Schema.TaggedErrorClass<DpopRepla
6862
}
6963
}
7064

71-
export class DpopReplayAlreadyClaimedError extends Schema.TaggedErrorClass<DpopReplayAlreadyClaimedError>()(
65+
export class DpopReplayAlreadyClaimedError extends Schema.TaggedError<DpopReplayAlreadyClaimedError>()(
7266
"DpopReplayAlreadyClaimedError",
7367
{
7468
source: Schema.Literals(["bucket", "legacy"]),
@@ -82,7 +76,7 @@ export class DpopReplayAlreadyClaimedError extends Schema.TaggedErrorClass<DpopR
8276
}
8377
export const isDpopReplayAlreadyClaimedError = Schema.is(DpopReplayAlreadyClaimedError);
8478

85-
export class DpopReplayStorePruneError extends Schema.TaggedErrorClass<DpopReplayStorePruneError>()(
79+
export class DpopReplayStorePruneError extends Schema.TaggedError<DpopReplayStorePruneError>()(
8680
"DpopReplayStorePruneError",
8781
{
8882
cause: Schema.Defect(),
@@ -131,9 +125,6 @@ export const make = Effect.gen(function* () {
131125
const path = yield* Path.Path;
132126
const config = yield* ServerConfig.ServerConfig;
133127
const replayDirectory = path.join(config.secretsDir, REPLAY_DIRECTORY_NAME);
134-
const legacyProtectionEndsAt = DateTime.add(yield* DateTime.now, {
135-
milliseconds: Duration.toMillis(LEGACY_REPLAY_RETENTION),
136-
});
137128

138129
yield* fileSystem.makeDirectory(replayDirectory, { recursive: true }).pipe(
139130
Effect.mapError(
@@ -175,78 +166,30 @@ export const make = Effect.gen(function* () {
175166
);
176167
};
177168

178-
const claimLegacyMarker = (replayKey: string) => {
169+
const checkLegacyMarker = (replayKey: string) => {
179170
const markerPath = path.join(config.secretsDir, `dpop-proof-${replayKey}.bin`);
180-
return Effect.gen(function* () {
181-
yield* Effect.scoped(
182-
Effect.gen(function* () {
183-
const marker = yield* fileSystem.open(markerPath, {
184-
flag: "wx",
185-
mode: 0o600,
186-
});
187-
yield* marker.sync;
188-
}),
189-
).pipe(
190-
Effect.mapError((cause) =>
191-
cause.reason._tag === "AlreadyExists"
192-
? new DpopReplayAlreadyClaimedError({
193-
source: "legacy",
194-
markerPath,
195-
})
196-
: new DpopReplayStoreClaimError({
197-
cause,
198-
stage: "write-marker",
199-
resource: markerPath,
200-
}),
201-
),
202-
);
203-
yield* Effect.scoped(
204-
Effect.gen(function* () {
205-
const secretsDirectory = yield* fileSystem.open(config.secretsDir, { flag: "r" });
206-
yield* secretsDirectory.sync;
207-
}),
208-
).pipe(
209-
Effect.mapError(
210-
(cause) =>
211-
new DpopReplayStoreClaimError({
212-
cause,
213-
stage: "sync-directory",
214-
resource: config.secretsDir,
215-
}),
216-
),
217-
);
218-
});
219-
};
220-
221-
const pruneLegacy = Effect.fn("DpopReplayStore.pruneLegacy")(function* () {
222-
const entries = yield* fileSystem.readDirectory(config.secretsDir, { recursive: false }).pipe(
223-
Effect.mapError(
224-
(cause) =>
225-
new DpopReplayStorePruneError({
226-
cause,
227-
stage: "read-legacy",
228-
resource: config.secretsDir,
229-
}),
230-
),
231-
);
232-
yield* Effect.forEach(
233-
entries,
234-
(entry) =>
235-
legacyReplayMarkerPattern.test(entry)
236-
? fileSystem.remove(path.join(config.secretsDir, entry), { force: true }).pipe(
237-
Effect.mapError(
238-
(cause) =>
239-
new DpopReplayStorePruneError({
240-
cause,
241-
stage: "remove-legacy",
242-
resource: path.join(config.secretsDir, entry),
243-
}),
171+
return fileSystem.stat(markerPath).pipe(
172+
Effect.matchEffect({
173+
onFailure: (cause) =>
174+
cause.reason._tag === "NotFound"
175+
? Effect.void
176+
: Effect.fail(
177+
new DpopReplayStoreClaimError({
178+
cause,
179+
stage: "legacy-lookup",
180+
resource: markerPath,
181+
}),
244182
),
245-
)
246-
: Effect.void,
247-
{ concurrency: 1 },
183+
onSuccess: () =>
184+
Effect.fail(
185+
new DpopReplayAlreadyClaimedError({
186+
source: "legacy",
187+
markerPath,
188+
}),
189+
),
190+
}),
248191
);
249-
});
192+
};
250193

251194
const prune: DpopReplayStore["Service"]["prune"] = Effect.fn("DpopReplayStore.prune")(
252195
function* () {
@@ -288,10 +231,6 @@ export const make = Effect.gen(function* () {
288231
},
289232
{ concurrency: 1 },
290233
);
291-
292-
if (!DateTime.isLessThan(now, legacyProtectionEndsAt)) {
293-
yield* pruneLegacy();
294-
}
295234
},
296235
);
297236

@@ -308,11 +247,8 @@ export const make = Effect.gen(function* () {
308247
const now = yield* DateTime.now;
309248
const currentBucket = bucketFor(now);
310249

311-
if (DateTime.isLessThan(now, legacyProtectionEndsAt)) {
312-
// Claim the legacy marker first so a new instance cannot accept a proof
313-
// that an old instance would subsequently accept during the upgrade window.
314-
yield* claimLegacyMarker(replayKey);
315-
}
250+
// Old-version writers must be stopped before upgrade; retain their markers indefinitely.
251+
yield* checkLegacyMarker(replayKey);
316252

317253
// Claim the future bucket first so a partial failure leaves a marker that
318254
// cannot be pruned while the proof is still inside its acceptance window.

‎apps/server/src/auth/PairingGrantStore.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ export const make = Effect.gen(function* () {
492492
);
493493
if (seededGrant) {
494494
const result = validateGrantDetails(seededGrant, now, input);
495-
return isBootstrapCredentialInvalidError(result) ? yield* result : result;
495+
return Schema.is(BootstrapCredentialInvalidError)(result) ? yield* result : result;
496496
}
497497

498498
const matching = yield* pairingLinks
@@ -502,7 +502,7 @@ export const make = Effect.gen(function* () {
502502
return yield* new UnknownBootstrapCredentialError({});
503503
}
504504
const result = validateStoredGrant(matching.value, now, input);
505-
return isBootstrapCredentialInvalidError(result) ? yield* result : result;
505+
return Schema.is(BootstrapCredentialInvalidError)(result) ? yield* result : result;
506506
});
507507

508508
const consume: PairingGrantStore["Service"]["consume"] = Effect.fn("PairingGrantStore.consume")(

‎docs/internals/environment-auth.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ transaction](../../apps/server/src/persistence/AuthSessions.ts); a failed
3737
replacement must leave the old credential usable. Pairing and browser sessions
3838
do not follow this replacement rule.
3939

40+
## Replay-store upgrades require stopped older writers
41+
42+
Before upgrading to the bucketed replay store, stop every older server that shares
43+
the secrets directory and let its in-flight requests finish. Do not run older
44+
servers alongside the new store. Older servers only check canonical
45+
`dpop-proof-*.bin` markers and cannot detect proofs claimed in the new buckets.
46+
47+
The new store checks existing canonical markers but never creates or deletes
48+
them. They remain as fixed migration residue, while new replay state expires in
49+
the bucketed store. A process-local timer cannot establish that every other
50+
writer has stopped, so elapsed time since startup never disables legacy checks
51+
or permits legacy cleanup.
52+
53+
Before rolling back, stop every new-version server sharing the directory and
54+
wait at least six minutes after its last accepted request before starting an
55+
older version. This lets accepted proofs expire before their bucket markers
56+
become invisible to the older server. Keep server clocks synchronized.
57+
4058
## The environment is the filesystem boundary
4159

4260
Projects are organizational boundaries, not filesystem sandboxes.

0 commit comments

Comments
 (0)