Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

replayMarkers.test.ts

90 lines3.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as DateTime from "effect/DateTime";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import * as TestClock from "effect/testing/TestClock";
10​
11import * as ServerConfig from "../config.ts";
12import { pruneExpiredReplayMarkers, REPLAY_MARKER_MAX_AGE } from "./replayMarkers.ts";
13import * as ServerSecretStore from "./ServerSecretStore.ts";
14​
15const REAL_SECRET_NAMES = [
16 "server-signing-key",
17 "asset-access-signing-key",
18 "cloud-cli-oauth-token",
19 "cloud-cli-desired-link",
20 "cloud-link-ed25519-key-pair",
21 "cloud-link-ed25519-private-key",
22 "cloud-link-ed25519-public-key",
23 "cloud-mint-ed25519-public-key",
24 "cloud-endpoint-runtime-config",
25 "cloud-endpoint-confirmed-origin",
26 "cloud-linked-user-id",
27 "cloud-relay-url",
28 "cloud-relay-issuer",
29 "cloud-relay-environment-credential",
30 "cloud-publish-agent-activity",
31 "provider-env-Y29kZXg-T1BFTkFJX0FQSV9LRVk",
32 "provider-auth-0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0",
33 "usage-limit-source-aHVi",
34];
35​
36it.layer(NodeServices.layer)("replayMarkers", (it) => {
37 it.effect("prunes only replay markers older than the max age", () =>
38 Effect.gen(function* () {
39 const secretStore = yield* ServerSecretStore.ServerSecretStore;
40 const { secretsDir } = yield* ServerConfig.ServerConfig;
41 const fileSystem = yield* FileSystem.FileSystem;
42 const path = yield* Path.Path;
43 const now = DateTime.makeUnsafe("2026-01-01T00:00:00Z");
44 const setAge = (fileName: string, age: Duration.Duration) => {
45 const mtime = DateTime.toDateUtc(DateTime.subtractDuration(now, age));
46 return fileSystem.utimes(path.join(secretsDir, fileName), mtime, mtime);
47 };
48 const writeAged = (name: string, age: Duration.Duration) =>
49 secretStore
50 .create(name, Uint8Array.from([1]))
51 .pipe(Effect.andThen(setAge(`${name}.bin`, age)));
52​
53 const justExpired = Duration.sum(REPLAY_MARKER_MAX_AGE, Duration.seconds(1));
54 const expiredMarkers = [
55 "dpop-proof-old",
56 "cloud-mint-jti-old",
57 "cloud-mint-nonce-old",
58 "cloud-health-jti-old",
59 "cloud-health-nonce-old",
60 ];
61 for (const name of expiredMarkers) yield* writeAged(name, justExpired);
62 yield* writeAged("dpop-proof-at-max-age", REPLAY_MARKER_MAX_AGE);
63 for (const name of REAL_SECRET_NAMES) yield* writeAged(name, Duration.days(30));
64 const pendingSetFile = "dpop-proof-pending.bin.0000.tmp";
65 yield* fileSystem.writeFile(path.join(secretsDir, pendingSetFile), Uint8Array.from([1]));
66 yield* setAge(pendingSetFile, Duration.days(30));
67 yield* TestClock.setTime(DateTime.toEpochMillis(now));
68​
69 yield* pruneExpiredReplayMarkers();
70​
71 const remaining = yield* fileSystem.readDirectory(secretsDir);
72 assert.deepStrictEqual(
73 remaining.toSorted(),
74 [
75 ...REAL_SECRET_NAMES.map((name) => `${name}.bin`),
76 "dpop-proof-at-max-age.bin",
77 pendingSetFile,
78 ].toSorted(),
79 );
80 }).pipe(
81 Effect.provide(
82 ServerSecretStore.layer.pipe(
83 Layer.provideMerge(
84 ServerConfig.layerTest(process.cwd(), { prefix: "t3-replay-markers-test-" }),
85 ),
86 ),
87 ),
88 ),
89 );
90});