Skip to content

Commit b3ef81a

Browse files
t3dotggclaude
andcommitted
test(server): prove pruned replay markers cannot reopen a replay window
Move the replay marker prune into auth/replayMarkers.ts so the server and the tests share one prefix list and max age. - server.test.ts: a worst-case DPoP proof and worst-case cloud health and mint proofs are blocked by their markers until the window ends, and by the time check alone at REPLAY_MARKER_MAX_AGE. - replayMarkers.test.ts: the sweep removes only markers older than the max age and keeps every secret name the server stores today. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 78254b0 commit b3ef81a

6 files changed

Lines changed: 286 additions & 124 deletions

File tree

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
33
import * as Cause from "effect/Cause";
4-
import * as DateTime from "effect/DateTime";
54
import * as Deferred from "effect/Deferred";
6-
import * as Duration from "effect/Duration";
75
import * as Effect from "effect/Effect";
86
import * as FileSystem from "effect/FileSystem";
97
import * as Layer from "effect/Layer";
108
import * as Option from "effect/Option";
11-
import * as Path from "effect/Path";
129
import * as Ref from "effect/Ref";
1310
import * as PlatformError from "effect/PlatformError";
14-
import * as TestClock from "effect/testing/TestClock";
1511

16-
import { CLOUD_REPLAY_MARKER_PREFIXES } from "../cloud/http.ts";
1712
import * as ServerConfig from "../config.ts";
18-
import { DPOP_REPLAY_MARKER_PREFIX } from "./dpop.ts";
1913
import * as ServerSecretStore from "./ServerSecretStore.ts";
2014

2115
const makeServerConfigLayer = () =>
@@ -259,53 +253,6 @@ it.layer(NodeServices.layer)("ServerSecretStore.layer", (it) => {
259253
}).pipe(Effect.provide(makeRenameFailureSecretStoreLayer())),
260254
);
261255

262-
it.effect("prunes only replay markers older than the max age", () =>
263-
Effect.gen(function* () {
264-
const secretStore = yield* ServerSecretStore.ServerSecretStore;
265-
const { secretsDir } = yield* ServerConfig.ServerConfig;
266-
const fileSystem = yield* FileSystem.FileSystem;
267-
const path = yield* Path.Path;
268-
const now = DateTime.makeUnsafe("2026-01-01T00:00:00Z");
269-
const setAge = (fileName: string, age: Duration.Duration) => {
270-
const mtime = DateTime.toDateUtc(DateTime.subtractDuration(now, age));
271-
return fileSystem.utimes(path.join(secretsDir, fileName), mtime, mtime);
272-
};
273-
274-
const expiredMarkers = ["dpop-proof-old", "cloud-mint-jti-old", "cloud-health-nonce-old"];
275-
const keptSecrets = [
276-
"dpop-proof-fresh",
277-
"session-signing-key",
278-
"cloud-mint-ed25519-public-key",
279-
];
280-
const pendingSetFile = "dpop-proof-pending.bin.0000.tmp";
281-
for (const name of [...expiredMarkers, ...keptSecrets]) {
282-
yield* secretStore.create(name, Uint8Array.from([1]));
283-
yield* setAge(
284-
`${name}.bin`,
285-
name.endsWith("fresh") ? Duration.minutes(1) : Duration.hours(1),
286-
);
287-
}
288-
yield* fileSystem.writeFile(path.join(secretsDir, pendingSetFile), Uint8Array.from([1]));
289-
yield* setAge(pendingSetFile, Duration.hours(1));
290-
yield* TestClock.setTime(DateTime.toEpochMillis(now));
291-
292-
yield* ServerSecretStore.pruneExpiredReplayMarkers(
293-
[DPOP_REPLAY_MARKER_PREFIX, ...CLOUD_REPLAY_MARKER_PREFIXES],
294-
Duration.minutes(15),
295-
);
296-
297-
for (const name of expiredMarkers) {
298-
assert.isTrue(Option.isNone(yield* secretStore.get(name)), name);
299-
}
300-
for (const name of keptSecrets) {
301-
assert.isTrue(Option.isSome(yield* secretStore.get(name)), name);
302-
}
303-
assert.isTrue(yield* fileSystem.exists(path.join(secretsDir, pendingSetFile)));
304-
}).pipe(
305-
Effect.provide(ServerSecretStore.layer.pipe(Layer.provideMerge(makeServerConfigLayer()))),
306-
),
307-
);
308-
309256
it.effect("propagates remove failures other than missing-file errors", () =>
310257
Effect.gen(function* () {
311258
const secretStore = yield* ServerSecretStore.ServerSecretStore;

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as Clock from "effect/Clock";
21
import * as Context from "effect/Context";
32
import * as Crypto from "effect/Crypto";
4-
import * as Duration from "effect/Duration";
53
import * as Effect from "effect/Effect";
64
import * as FileSystem from "effect/FileSystem";
75
import * as Layer from "effect/Layer";
@@ -314,44 +312,3 @@ export const make = Effect.gen(function* () {
314312
});
315313

316314
export const layer = Layer.effect(ServerSecretStore, make);
317-
318-
/**
319-
* Deletes replay markers written by `create` whose mtime is older than `maxAge`.
320-
* A marker only matters while its proof can still be accepted, so the server
321-
* runs this on a timer. Only `<prefix>*.bin` names match: other secrets and the
322-
* `*.bin.<uuid>.tmp` files from `set` are never touched.
323-
*/
324-
export const pruneExpiredReplayMarkers = Effect.fn("ServerSecretStore.pruneExpiredReplayMarkers")(
325-
function* (prefixes: ReadonlyArray<string>, maxAge: Duration.Duration) {
326-
const fileSystem = yield* FileSystem.FileSystem;
327-
const path = yield* Path.Path;
328-
const { secretsDir } = yield* ServerConfig.ServerConfig;
329-
const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(maxAge);
330-
const markers = (yield* fileSystem.readDirectory(secretsDir)).filter(
331-
(name) => name.endsWith(".bin") && prefixes.some((prefix) => name.startsWith(prefix)),
332-
);
333-
// `partition` visits every marker, so one locked file does not stop the sweep.
334-
const [failures, removed] = yield* Effect.partition(markers, (name) => {
335-
const markerPath = path.join(secretsDir, name);
336-
return fileSystem.stat(markerPath).pipe(
337-
Effect.flatMap((info) =>
338-
Option.exists(info.mtime, (mtime) => mtime.getTime() < cutoff)
339-
? fileSystem.remove(markerPath).pipe(Effect.as(true))
340-
: Effect.succeed(false),
341-
),
342-
Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(false)),
343-
);
344-
});
345-
yield* Effect.annotateCurrentSpan({
346-
"secret_store.replay_markers.matched": markers.length,
347-
"secret_store.replay_markers.removed": removed.filter(Boolean).length,
348-
"secret_store.replay_markers.failed": failures.length,
349-
});
350-
if (failures.length > 0) {
351-
yield* Effect.logWarning("Failed to prune some replay markers", {
352-
failed: failures.length,
353-
cause: failures[0],
354-
});
355-
}
356-
},
357-
);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { assert, it } from "@effect/vitest";
3+
import * as DateTime from "effect/DateTime";
4+
import * as Duration from "effect/Duration";
5+
import * as Effect from "effect/Effect";
6+
import * as FileSystem from "effect/FileSystem";
7+
import * as Layer from "effect/Layer";
8+
import * as Path from "effect/Path";
9+
import * as TestClock from "effect/testing/TestClock";
10+
11+
import * as ServerConfig from "../config.ts";
12+
import { pruneExpiredReplayMarkers, REPLAY_MARKER_MAX_AGE } from "./replayMarkers.ts";
13+
import * as ServerSecretStore from "./ServerSecretStore.ts";
14+
15+
// Every secret name the server stores today. The last three stand for names
16+
// built from an id at runtime.
17+
const REAL_SECRET_NAMES = [
18+
"server-signing-key",
19+
"asset-access-signing-key",
20+
"cloud-cli-oauth-token",
21+
"cloud-cli-desired-link",
22+
"cloud-link-ed25519-key-pair",
23+
"cloud-link-ed25519-private-key",
24+
"cloud-link-ed25519-public-key",
25+
"cloud-mint-ed25519-public-key",
26+
"cloud-endpoint-runtime-config",
27+
"cloud-endpoint-confirmed-origin",
28+
"cloud-linked-user-id",
29+
"cloud-relay-url",
30+
"cloud-relay-issuer",
31+
"cloud-relay-environment-credential",
32+
"cloud-publish-agent-activity",
33+
"provider-env-Y29kZXg-T1BFTkFJX0FQSV9LRVk",
34+
"provider-auth-0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0",
35+
"usage-limit-source-aHVi",
36+
];
37+
38+
it.layer(NodeServices.layer)("replayMarkers", (it) => {
39+
it.effect("prunes only replay markers older than the max age", () =>
40+
Effect.gen(function* () {
41+
const secretStore = yield* ServerSecretStore.ServerSecretStore;
42+
const { secretsDir } = yield* ServerConfig.ServerConfig;
43+
const fileSystem = yield* FileSystem.FileSystem;
44+
const path = yield* Path.Path;
45+
const now = DateTime.makeUnsafe("2026-01-01T00:00:00Z");
46+
const setAge = (fileName: string, age: Duration.Duration) => {
47+
const mtime = DateTime.toDateUtc(DateTime.subtractDuration(now, age));
48+
return fileSystem.utimes(path.join(secretsDir, fileName), mtime, mtime);
49+
};
50+
const writeAged = (name: string, age: Duration.Duration) =>
51+
secretStore
52+
.create(name, Uint8Array.from([1]))
53+
.pipe(Effect.andThen(setAge(`${name}.bin`, age)));
54+
55+
const justExpired = Duration.sum(REPLAY_MARKER_MAX_AGE, Duration.seconds(1));
56+
const expiredMarkers = [
57+
"dpop-proof-old",
58+
"cloud-mint-jti-old",
59+
"cloud-mint-nonce-old",
60+
"cloud-health-jti-old",
61+
"cloud-health-nonce-old",
62+
];
63+
for (const name of expiredMarkers) yield* writeAged(name, justExpired);
64+
yield* writeAged("dpop-proof-at-max-age", REPLAY_MARKER_MAX_AGE);
65+
for (const name of REAL_SECRET_NAMES) yield* writeAged(name, Duration.days(30));
66+
const pendingSetFile = "dpop-proof-pending.bin.0000.tmp";
67+
yield* fileSystem.writeFile(path.join(secretsDir, pendingSetFile), Uint8Array.from([1]));
68+
yield* setAge(pendingSetFile, Duration.days(30));
69+
yield* TestClock.setTime(DateTime.toEpochMillis(now));
70+
71+
yield* pruneExpiredReplayMarkers();
72+
73+
const remaining = yield* fileSystem.readDirectory(secretsDir);
74+
assert.deepStrictEqual(
75+
remaining.toSorted(),
76+
[
77+
...REAL_SECRET_NAMES.map((name) => `${name}.bin`),
78+
"dpop-proof-at-max-age.bin",
79+
pendingSetFile,
80+
].toSorted(),
81+
);
82+
}).pipe(
83+
Effect.provide(
84+
ServerSecretStore.layer.pipe(
85+
Layer.provideMerge(
86+
ServerConfig.layerTest(process.cwd(), { prefix: "t3-replay-markers-test-" }),
87+
),
88+
),
89+
),
90+
),
91+
);
92+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { DPOP_REPLAY_WINDOW_SECONDS } from "@t3tools/shared/dpop";
2+
import * as Clock from "effect/Clock";
3+
import * as Duration from "effect/Duration";
4+
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
7+
import * as Option from "effect/Option";
8+
import * as Path from "effect/Path";
9+
import * as Schedule from "effect/Schedule";
10+
11+
import { CLOUD_REPLAY_MARKER_PREFIXES, CLOUD_REPLAY_WINDOW_SECONDS } from "../cloud/http.ts";
12+
import * as ServerConfig from "../config.ts";
13+
import { forkParked } from "../serverActivation.ts";
14+
import { DPOP_REPLAY_MARKER_PREFIX } from "./dpop.ts";
15+
16+
const REPLAY_MARKER_PREFIXES = [DPOP_REPLAY_MARKER_PREFIX, ...CLOUD_REPLAY_MARKER_PREFIXES];
17+
18+
/**
19+
* How long a replay marker stays on disk. A marker only matters while its proof
20+
* can still be accepted, so this is twice the longest proof window. A longer
21+
* window moves it too. Markers are files, so a restart does not reset them.
22+
*/
23+
export const REPLAY_MARKER_MAX_AGE = Duration.seconds(
24+
2 * Math.max(DPOP_REPLAY_WINDOW_SECONDS, CLOUD_REPLAY_WINDOW_SECONDS),
25+
);
26+
27+
/**
28+
* Deletes replay markers whose mtime is older than `REPLAY_MARKER_MAX_AGE`.
29+
* `ServerSecretStore` saves each secret as `<name>.bin`, so only
30+
* `<marker prefix>*.bin` names match. Other secrets and the `*.bin.<uuid>.tmp`
31+
* files that `set` writes are never touched.
32+
*/
33+
export const pruneExpiredReplayMarkers = Effect.fn("replayMarkers.pruneExpired")(function* () {
34+
const fileSystem = yield* FileSystem.FileSystem;
35+
const path = yield* Path.Path;
36+
const { secretsDir } = yield* ServerConfig.ServerConfig;
37+
const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(REPLAY_MARKER_MAX_AGE);
38+
const markers = (yield* fileSystem.readDirectory(secretsDir)).filter(
39+
(name) =>
40+
name.endsWith(".bin") && REPLAY_MARKER_PREFIXES.some((prefix) => name.startsWith(prefix)),
41+
);
42+
// `partition` visits every marker, so one locked file does not stop the sweep.
43+
const [failures, removed] = yield* Effect.partition(markers, (name) => {
44+
const markerPath = path.join(secretsDir, name);
45+
return fileSystem.stat(markerPath).pipe(
46+
Effect.flatMap((info) =>
47+
Option.exists(info.mtime, (mtime) => mtime.getTime() < cutoff)
48+
? fileSystem.remove(markerPath).pipe(Effect.as(true))
49+
: Effect.succeed(false),
50+
),
51+
Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(false)),
52+
);
53+
});
54+
yield* Effect.annotateCurrentSpan({
55+
"replay_markers.matched": markers.length,
56+
"replay_markers.removed": removed.filter(Boolean).length,
57+
"replay_markers.failed": failures.length,
58+
});
59+
if (failures.length > 0) {
60+
yield* Effect.logWarning("Failed to prune some replay markers", {
61+
failed: failures.length,
62+
cause: failures[0],
63+
});
64+
}
65+
});
66+
67+
/** Prunes expired replay markers after server activation, then every 10 minutes. */
68+
export const layer = Layer.effectDiscard(
69+
forkParked(
70+
pruneExpiredReplayMarkers().pipe(
71+
Effect.catch((cause) =>
72+
Effect.logWarning("Failed to prune expired replay markers", { cause }),
73+
),
74+
Effect.repeat(Schedule.spaced(Duration.minutes(10))),
75+
),
76+
),
77+
);

0 commit comments

Comments
 (0)