Skip to content

Commit ea39c88

Browse files
t3dotggclaude
andcommitted
refactor(server): derive the replay marker max age from the proof windows
The 15 minute max age was tied to the DPoP and cloud proof windows only by comments. Export each window from the module that owns its checks (DPOP_REPLAY_WINDOW_SECONDS, CLOUD_REPLAY_WINDOW_SECONDS, built from RELAY_JWT_CLOCK_TOLERANCE_SECONDS) and set the max age to twice the longer one, now 14 minutes. A change to either window now moves the max age too. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c0d0873 commit ea39c88

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎apps/server/src/cloud/http.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
normalizeRelayIssuer,
4141
RELAY_HEALTH_REQUEST_TYP,
4242
RELAY_HEALTH_RESPONSE_TYP,
43+
RELAY_JWT_CLOCK_TOLERANCE_SECONDS,
4344
RELAY_LINK_PROOF_TYP,
4445
RELAY_MANAGED_TUNNEL_RECOVERY_TYP,
4546
RELAY_MINT_REQUEST_TYP,
@@ -111,9 +112,17 @@ export const CLOUD_REPLAY_MARKER_PREFIXES = [
111112
CLOUD_HEALTH_NONCE_PREFIX,
112113
CLOUD_HEALTH_JTI_PREFIX,
113114
] as const;
114-
// Keep the proof window well under REPLAY_MARKER_MAX_AGE in server.ts, or pruned markers allow replays.
115115
const CLOUD_PROOF_MAX_LIFETIME_SECONDS = 5 * 60;
116116
const CLOUD_PROOF_CLOCK_SKEW_SECONDS = 60;
117+
/**
118+
* Longest time a cloud proof can pass again after its markers are written. `iat`
119+
* may be up to the skew ahead, and `verifyRelayJwt` accepts the proof until `exp`
120+
* plus its clock tolerance.
121+
*/
122+
export const CLOUD_REPLAY_WINDOW_SECONDS =
123+
CLOUD_PROOF_MAX_LIFETIME_SECONDS +
124+
CLOUD_PROOF_CLOCK_SKEW_SECONDS +
125+
RELAY_JWT_CLOCK_TOLERANCE_SECONDS;
117126
// The desktop app stops its backends within seconds of writing the marker.
118127
const DESKTOP_UPDATE_RESTART_MARKER_TTL = Duration.minutes(1);
119128
const MANAGED_ENDPOINT_PROVISION_REQUEST_TIMEOUT = Duration.minutes(2);

‎apps/server/src/server.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
127127
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
128128
import {
129129
CLOUD_REPLAY_MARKER_PREFIXES,
130+
CLOUD_REPLAY_WINDOW_SECONDS,
130131
connectHttpApiLayer,
131132
pendingServiceUpdateExists,
132133
reconcileDesiredCloudLinkIfStillDesired,
@@ -167,6 +168,7 @@ import {
167168
persistServerRuntimeState,
168169
} from "./serverRuntimeState.ts";
169170
import { orchestrationHttpApiLayer } from "./orchestration/http.ts";
171+
import { DPOP_REPLAY_WINDOW_SECONDS } from "@t3tools/shared/dpop";
170172
import * as NetService from "@t3tools/shared/Net";
171173
import * as RelayClient from "@t3tools/shared/relayClient";
172174
import { disableTailscaleServe, ensureTailscaleServe } from "@t3tools/tailscale";
@@ -499,11 +501,13 @@ const AntigravityInstallationRefreshLive = Layer.effectDiscard(
499501
}),
500502
);
501503

502-
// A replay marker only matters while its proof can still be accepted: about
503-
// 305 s for DPoP (5 min max age, 5 s future skew) and about 420 s for cloud
504-
// proofs (5 min lifetime, 60 s skew each way). 15 minutes is a 2x margin.
505-
// Markers stay on disk, so a restart inside the window still blocks replays.
506-
const REPLAY_MARKER_MAX_AGE = Duration.minutes(15);
504+
// A replay marker only matters while its proof can still be accepted. Keep each
505+
// marker for twice the longest proof window, so a change to either window moves
506+
// this too. Markers stay on disk, so a restart inside the window still blocks
507+
// replays.
508+
const REPLAY_MARKER_MAX_AGE = Duration.seconds(
509+
2 * Math.max(DPOP_REPLAY_WINDOW_SECONDS, CLOUD_REPLAY_WINDOW_SECONDS),
510+
);
507511

508512
// Sweeps expired replay markers after activation, then every 10 minutes.
509513
const ReplayMarkerPruneLive = Layer.effectDiscard(

‎packages/shared/src/dpop.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import { stableStringify } from "./relaySigning.ts";
1111

1212
const DPOP_TYP = "dpop+jwt";
1313
const DPOP_ALG = "ES256";
14-
// apps/server prunes DPoP replay markers after REPLAY_MARKER_MAX_AGE (server.ts). Keep this well under it.
1514
const DEFAULT_MAX_AGE_SECONDS = 300;
15+
const MAX_FUTURE_SKEW_SECONDS = 5;
16+
/**
17+
* Longest time a proof checked with the default max age can pass again after it
18+
* is first accepted. Replay protection must remember its jti at least this long.
19+
*/
20+
export const DPOP_REPLAY_WINDOW_SECONDS = DEFAULT_MAX_AGE_SECONDS + MAX_FUTURE_SKEW_SECONDS;
1621

1722
export const DpopPublicJwk = DpopPublicJwkSchema;
1823
export type DpopPublicJwk = DpopPublicJwkType;
@@ -178,7 +183,7 @@ export function verifyDpopProof(input: {
178183

179184
const maxAgeSeconds = input.maxAgeSeconds ?? DEFAULT_MAX_AGE_SECONDS;
180185
if (
181-
payload.value.iat > input.nowEpochSeconds + 5 ||
186+
payload.value.iat > input.nowEpochSeconds + MAX_FUTURE_SKEW_SECONDS ||
182187
input.nowEpochSeconds - payload.value.iat > maxAgeSeconds
183188
) {
184189
return {

‎packages/shared/src/relayJwt.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const RELAY_MINT_RESPONSE_TYP = "t3-env-mint+jwt";
1111
export const RELAY_HEALTH_RESPONSE_TYP = "t3-env-health+jwt";
1212
export const RELAY_ACTIVITY_PUBLISH_TYP = "t3-env-activity+jwt";
1313
export const RELAY_MANAGED_TUNNEL_RECOVERY_TYP = "t3-env-managed-tunnel-recovery+jwt";
14+
/** Clock skew `verifyRelayJwt` allows on `iat`, `exp`, and the max token age. */
15+
export const RELAY_JWT_CLOCK_TOLERANCE_SECONDS = 60;
1416

1517
export class RelayJwtError extends Schema.TaggedError<RelayJwtError>()("RelayJwtError", {
1618
operation: Schema.Literals(["sign", "verify"]),
@@ -82,10 +84,8 @@ export function verifyRelayJwt(input: {
8284
typ: input.typ,
8385
issuer: input.issuer,
8486
audience: input.audience,
85-
// apps/server prunes cloud replay markers after REPLAY_MARKER_MAX_AGE (server.ts).
86-
// Keep maxTokenAge plus clockTolerance well under it.
8787
maxTokenAge: input.maxTokenAge ?? "5 minutes",
88-
clockTolerance: 60,
88+
clockTolerance: RELAY_JWT_CLOCK_TOLERANCE_SECONDS,
8989
currentDate: DateTime.toDate(DateTime.makeUnsafe(input.nowEpochSeconds * 1_000)),
9090
});
9191
return verified.payload;

0 commit comments

Comments
 (0)