fix(server): bound DPoP replay state #8845
Closedsolomonneas(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromrefs/pull/8845/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/auth/dpop.ts lines 90 to 90OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -112,12 +81,10 @@112- const mapped = mapDpopReplayStoreError(error);113- if (mapped._tag === "ServerAuthInvalidCredentialError") {114- yield* Effect.annotateCurrentSpan({115- "environment.dpop.failure_code": mapped.dpopFailureReason,116- });117- }118- return yield* Effect.fail(mapped);119- }),120- ),121- );122- return result.thumbprint;81+ return result;12382 });83+84+export const claimDpopProofReplay = (input: {85+ readonly thumbprint: string;86+ readonly jti: string;87+}) =>88+ DpopReplayStore.DpopReplayStore.pipe(89+ Effect.flatMap((replayStore) => replayStore.claim(input)),90+ Effect.catchIf(DpopReplayStore.isDpopReplayStoreError, (error) =>apps/server/src/auth/DpopReplayStore.ts lines 46 to 53OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +33,21 @@33+ return "Failed to initialize DPoP replay storage.";34+ }35+}36+37+export class DpopReplayStoreKeyCalculationError extends Schema.TaggedErrorClass<DpopReplayStoreKeyCalculationError>()(38+ "DpopReplayStoreKeyCalculationError",39+ dpopReplayStoreErrorContext,40+) {41+ override get message(): string {42+ return "Failed to calculate DPoP replay key.";43+ }44+}45+46+export class DpopReplayStoreClaimError extends Schema.TaggedErrorClass<DpopReplayStoreClaimError>()(47+ "DpopReplayStoreClaimError",48+ dpopReplayStoreErrorContext,49+) {50+ override get message(): string {51+ return "Failed to claim DPoP replay state.";52+ }53+}apps/server/src/auth/DpopReplayStore.ts lines 76 to 79OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +59,21 @@59+ override get message(): string {60+ return "Failed to prune DPoP replay state.";61+ }62+}63+64+export const DpopReplayStoreError = Schema.Union([65+ DpopReplayStoreSetupError,66+ DpopReplayStoreKeyCalculationError,67+ DpopReplayStoreClaimError,68+ DpopReplayStorePruneError,69+]);70+export type DpopReplayStoreError = typeof DpopReplayStoreError.Type;71+export const isDpopReplayStoreError = Schema.is(DpopReplayStoreError);72+73+const isPlatformError = (value: unknown): value is PlatformError.PlatformError =>74+ Predicate.isTagged(value, "PlatformError");75+76+export const isDpopReplayAlreadyExistsError = (error: DpopReplayStoreError): boolean =>77+ error._tag === "DpopReplayStoreClaimError" &&78+ isPlatformError(error.cause) &&79+ error.cause.reason._tag === "AlreadyExists";
- apps/server/src/auth/DpopReplayStore.ts line 121OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +101,21 @@101+ const config = yield* ServerConfig.ServerConfig;102+ const replayDirectory = path.join(config.secretsDir, REPLAY_DIRECTORY_NAME);103+ const legacyProtectionEndsAt = DateTime.add(yield* DateTime.now, {104+ milliseconds: Duration.toMillis(LEGACY_REPLAY_RETENTION),105+ });106+107+ yield* Effect.gen(function* () {108+ yield* fileSystem.makeDirectory(replayDirectory, { recursive: true });109+ yield* fileSystem.chmod(replayDirectory, 0o700);110+ }).pipe(Effect.mapError((cause) => new DpopReplayStoreSetupError({ cause })));111+112+ const ensureBucketDirectory = (bucket: number) => {113+ const directory = path.join(replayDirectory, String(bucket));114+ return Effect.gen(function* () {115+ yield* fileSystem.makeDirectory(directory, { recursive: true });116+ yield* fileSystem.chmod(directory, 0o700);117+ return directory;118+ }).pipe(Effect.mapError((cause) => new DpopReplayStoreClaimError({ cause })));119+ };120+121+ const legacyMarkerExists = (replayKey: string) => {apps/server/src/auth/http.ts line 341Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -309,3 +309,8 @@309309 ),310310 )311311 : undefined;312+ if (proof) {313+ yield* serverAuth.validateBootstrapCredentialAvailable(args.payload.subject_token, {314+ proofKeyThumbprint: proof.thumbprint,315+ });316+ yield* claimDpopProofReplay({
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/auth/DpopReplayStore.ts lines 37 to 39OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +19,21 @@19+const replayBucketPattern = /^(0|[1-9][0-9]*)$/;20+const legacyReplayMarkerPattern = /^dpop-proof-[A-Za-z0-9_-]{43}\.bin$/;21+22+const dpopReplayStoreErrorContext = {23+ cause: Schema.Defect(),24+};25+const dpopReplayStoreClaimStage = Schema.Literals([26+ "ensure-bucket",27+ "legacy-lookup",28+ "write-marker",29+]);30+const dpopReplayStorePruneStage = Schema.Literals([31+ "read-buckets",32+ "remove-bucket",33+ "read-legacy",34+ "remove-legacy",35+]);36+37+export class DpopReplayStoreSetupError extends Schema.TaggedErrorClass<DpopReplayStoreSetupError>()(38+ "DpopReplayStoreSetupError",39+ dpopReplayStoreErrorContext,
- apps/server/src/auth/DpopReplayStore.ts line 264Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +291,21 @@291+ Effect.mapError((cause) => new DpopReplayStoreKeyCalculationError({ cause })),292+ );293+ const now = yield* DateTime.now;294+ const currentBucket = bucketFor(now);295+296+ if (DateTime.isLessThan(now, legacyProtectionEndsAt)) {297+ yield* legacyMarkerExists(replayKey);298+ }299+300+ // Claim the future bucket first so a partial failure leaves a marker that301+ // cannot be pruned while the proof is still inside its acceptance window.302+ for (const bucket of [currentBucket + 1, currentBucket]) {303+ const directory = yield* ensureBucketDirectory(bucket);304+ const markerPath = path.join(directory, replayKey);305+ yield* Effect.scoped(306+ Effect.gen(function* () {307+ const marker = yield* fileSystem.open(markerPath, {308+ flag: "wx",309+ mode: 0o600,310+ });311+ yield* marker.sync;
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
478b5b59-605a-41ba-9522-eb05d3b0ef14📥 Commits
Reviewing files that changed from the base of the PR and between af2baccd100604f9885d6af97a5fa99622dd1c4f and 9eaf1d002a322928bb6a57a151554bf6f576c0dd.
📒 Files selected for processing (10)
apps/server/src/auth/DpopReplayStore.test.tsapps/server/src/auth/DpopReplayStore.tsapps/server/src/auth/EnvironmentAuth.tsapps/server/src/auth/PairingGrantStore.test.tsapps/server/src/auth/PairingGrantStore.tsapps/server/src/auth/dpop.test.tsapps/server/src/auth/dpop.tsapps/server/src/auth/http.tsapps/server/src/server.test.tsdocs/internals/environment-auth.mdIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
The change adds a bucketed filesystem-backed DPoP replay store, preserves compatibility with legacy markers, validates bootstrap credentials before claiming proofs, and integrates replay claims into token exchange authentication.
Changes#
DPoP replay protection
apps/server/src/auth/DpopReplayStore.ts,apps/server/src/auth/DpopReplayStore.test.tsapps/server/src/auth/PairingGrantStore.ts,apps/server/src/auth/PairingGrantStore.test.tsapps/server/src/auth/dpop.ts,apps/server/src/auth/http.ts,apps/server/src/auth/EnvironmentAuth.ts,apps/server/src/auth/dpop.test.ts,apps/server/src/auth/server.test.tsdocs/internals/environment-auth.mdPriority: ⬆️ High
Estimated code review effort: 4 (Complex) | ~45 minutes
Change: Bug fix · Severity of issue fixed: High
Sequence Diagram(s)#
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
9eaf1The upgrade documentation already requires the operational sequencing needed to preserve replay protection across versions.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#9040requires atomic replay state that remains bounded and bootstrap validation before replay-state writes.DpopReplayStoreuses digest-named markers underdpop-replay, writes the next buck…#9040. No unrelated change is demonstrated by the reviewed evidence.✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.