The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(auth): T3 Connect tolerates a host clock a few seconds behind (#19640)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Evolve Performance committed
Oct 20, 2026 at 06:55 UTC
475e01996338f2973160180aab7e7e46adffa62d
5 files changed
+85
-41
apps/server/src/auth/replayMarkers.ts
+1
-1
@@ -15,7 +15,7 @@ import { DPOP_REPLAY_MARKER_PREFIX } from "./dpop.ts";
15
const REPLAY_MARKER_PREFIXES = [DPOP_REPLAY_MARKER_PREFIX, ...CLOUD_REPLAY_MARKER_PREFIXES];
16
17
/**
18
- * A marker only matters while its proof can pass the time check (about 5 minutes
18
+ * A marker only matters while its proof can pass the time check (about 10 minutes
19
* for DPoP, 7 for cloud proofs). After that, the time check rejects a replay by
20
* itself.
21
*/
infra/relay/src/auth/DpopProofs.ts
+8
-2
@@ -5,7 +5,11 @@ import * as Layer from "effect/Layer";
5
import * as Schema from "effect/Schema";
6
import { lt } from "drizzle-orm";
7
8
-import { DpopVerificationFailureCode, verifyDpopProof } from "@t3tools/shared/dpop";
8
+import {
9
+ DPOP_MAX_CLOCK_SKEW_SECONDS,
10
+ DpopVerificationFailureCode,
11
+ verifyDpopProof,
12
+} from "@t3tools/shared/dpop";
13
import * as RelayDb from "../db.ts";
14
import { relayDpopProofs } from "../persistence/schema.ts";
15
@@ -129,7 +133,9 @@ const make = Effect.gen(function* () {
133
thumbprint: result.thumbprint,
134
jti: result.jti,
135
iat: result.iat,
132
- expiresAt: DateTime.add(input.now, { minutes: 5 }),
136
+ // The time check floors the clock to seconds, so the proof passes until the
137
+ // end of second `iat + DPOP_MAX_CLOCK_SKEW_SECONDS`.
138
+ expiresAt: DateTime.makeUnsafe((result.iat + DPOP_MAX_CLOCK_SKEW_SECONDS + 1) * 1_000),
139
});
140
if (!consumed) {
141
yield* Effect.logWarning("relay dpop proof replay rejected", {
infra/relay/src/auth/DpopProofs.verifyAndConsume.test.ts
+48
-32
@@ -84,16 +84,26 @@ function layer(
84
}
85
86
function consumeEachProofOnce() {
87
- const consumed = new Set<string>();
88
- return (values: DpopProofInsertValues) =>
89
- Effect.sync(() => {
90
- const key = `${values.thumbprint}:${values.jti}`;
91
- if (consumed.has(key)) {
92
- return [];
87
+ const consumed = new Map<string, string>();
88
+ return {
89
+ insert: (values: DpopProofInsertValues) =>
90
+ Effect.sync(() => {
91
+ const key = `${values.thumbprint}:${values.jti}`;
92
+ if (consumed.has(key)) {
93
+ return [];
94
+ }
95
+ consumed.set(key, values.expiresAt);
96
+ return [{ jti: values.jti }];
97
+ }),
98
+ pruneExpired: (now: DateTime.DateTime) => {
99
+ const cutoff = DateTime.formatIso(now);
100
+ for (const [key, expiresAt] of consumed) {
101
+ if (expiresAt < cutoff) {
102
+ consumed.delete(key);
103
+ }
104
}
94
- consumed.add(key);
95
- return [{ jti: values.jti }];
96
- });
105
+ },
106
+ };
107
}
108
109
describe("DpopProofReplay.verifyAndConsume", () => {
@@ -125,37 +135,43 @@ describe("DpopProofReplay.verifyAndConsume", () => {
135
}).pipe(Effect.provide(layer(() => Effect.die("unexpected replay persistence"))));
136
});
137
128
- it.effect("rejects replayed proofs after persistence consumes the jti once", () => {
138
+ it.effect("rejects replays until a future proof's time window closes", () => {
139
const now = DateTime.makeUnsafe("2026-05-25T12:00:00.000Z");
140
const proof = makeDpopProof({
141
method: "POST",
142
url: "https://relay.example.com/v1/environments/env/connect",
133
- iat: Math.floor(now.epochMilliseconds / 1_000),
134
- jti: "proof-1",
143
+ iat: Math.floor(now.epochMilliseconds / 1_000) + 300,
144
+ jti: "proof-future",
145
});
146
+ const persistence = consumeEachProofOnce();
147
+ const input = {
148
+ proof: proof.proof,
149
+ method: "POST",
150
+ url: "https://relay.example.com/v1/environments/env/connect",
151
+ expectedThumbprint: proof.thumbprint,
152
+ now,
153
+ };
154
155
return Effect.gen(function* () {
156
const replay = yield* DpopProofs.DpopProofReplay;
139
- const first = yield* replay.verifyAndConsume({
140
- proof: proof.proof,
141
- method: "POST",
142
- url: "https://relay.example.com/v1/environments/env/connect",
143
- expectedThumbprint: proof.thumbprint,
144
- now,
145
- });
146
- const second = yield* Effect.flip(
147
- replay.verifyAndConsume({
148
- proof: proof.proof,
149
- method: "POST",
150
- url: "https://relay.example.com/v1/environments/env/connect",
151
- expectedThumbprint: proof.thumbprint,
152
- now,
153
- }),
154
- );
157
+ expect(yield* replay.verifyAndConsume(input)).toBe(proof.thumbprint);
158
156
- expect(first).toBe(proof.thumbprint);
157
- expect(second).toMatchObject({ _tag: "DpopProofRejected", code: "replayed" });
158
- }).pipe(Effect.provide(layer(consumeEachProofOnce())));
159
+ for (const { elapsedMs, code } of [
160
+ { elapsedMs: 0, code: "replayed" },
161
+ { elapsedMs: 301_000, code: "replayed" },
162
+ { elapsedMs: 600_999, code: "replayed" },
163
+ { elapsedMs: 601_000, code: "time_window" },
164
+ ]) {
165
+ const retryAt = DateTime.makeUnsafe(now.epochMilliseconds + elapsedMs);
166
+ persistence.pruneExpired(retryAt);
167
+ const result = yield* Effect.result(replay.verifyAndConsume({ ...input, now: retryAt }));
168
+
169
+ expect(result, `Replay after ${elapsedMs}ms`).toMatchObject({
170
+ _tag: "Failure",
171
+ failure: { _tag: "DpopProofRejected", code },
172
+ });
173
+ }
174
+ }).pipe(Effect.provide(layer(persistence.insert)));
175
});
176
177
it.effect("rejects proofs missing the expected access token hash", () => {
@@ -252,6 +268,6 @@ describe("DpopProofReplay.verifyAndConsume", () => {
268
269
expect(thumbprint).toBe(proof.thumbprint);
270
expect(second._tag).toBe("Failure");
255
- }).pipe(Effect.provide(layer(consumeEachProofOnce())));
271
+ }).pipe(Effect.provide(layer(consumeEachProofOnce().insert)));
272
});
273
});
packages/shared/src/dpop.test.ts
+21
@@ -209,6 +209,27 @@ describe("verifyDpopProof", () => {
209
assert.equal(invalidSignature.code, "invalid_signature");
210
});
211
212
+ it("accepts clock offsets through 300 seconds in either direction", () => {
213
+ for (const { offset, accepted } of [
214
+ { offset: -300, accepted: true },
215
+ { offset: 300, accepted: true },
216
+ { offset: -301, accepted: false },
217
+ { offset: 301, accepted: false },
218
+ ]) {
219
+ const result = verifyDpopProof({
220
+ proof,
221
+ method: "POST",
222
+ url: "https://example.com/oauth/token",
223
+ nowEpochSeconds: 100 + offset,
224
+ });
225
+
226
+ assert.equal(result.ok, accepted, `Clock offset: ${offset}s`);
227
+ if (!result.ok) {
228
+ assert.equal(result.code, "time_window");
229
+ }
230
+ }
231
+ });
232
+
233
it("requires the RFC 9449 access token hash when an access token is expected", () => {
234
const thumbprint = computeDpopJwkThumbprint(publicJwk);
235
const accessTokenProof = signDpopProof({
packages/shared/src/dpop.ts
+7
-6
@@ -11,7 +11,11 @@ import { stableStringify } from "./relaySigning.ts";
11
12
const DPOP_TYP = "dpop+jwt";
13
const DPOP_ALG = "ES256";
14
-const DEFAULT_MAX_AGE_SECONDS = 300;
14
+/**
15
+ * How far a proof's `iat` may sit from the verifier's clock, in either direction.
16
+ * Proofs carry the client's clock, so this also bounds tolerated clock skew.
17
+ */
18
+export const DPOP_MAX_CLOCK_SKEW_SECONDS = 300;
19
20
export const DpopPublicJwk = DpopPublicJwkSchema;
21
export type DpopPublicJwk = DpopPublicJwkType;
@@ -175,11 +179,8 @@ export function verifyDpopProof(input: {
179
return { ok: false, code: "invalid_signature", reason: "Invalid DPoP signature." };
180
}
181
178
- const maxAgeSeconds = input.maxAgeSeconds ?? DEFAULT_MAX_AGE_SECONDS;
179
- if (
180
- payload.value.iat > input.nowEpochSeconds + 5 ||
181
- input.nowEpochSeconds - payload.value.iat > maxAgeSeconds
182
- ) {
182
+ const maxAgeSeconds = input.maxAgeSeconds ?? DPOP_MAX_CLOCK_SKEW_SECONDS;
183
+ if (Math.abs(input.nowEpochSeconds - payload.value.iat) > maxAgeSeconds) {
184
return {
185
ok: false,
186
code: "time_window",