Skip to content
TypeScript

refactor(server): replay guards stay in CloudLink (#16349)

Julius Marminge99db70cf1a1f
diff --git a/apps/server/src/auth/replayMarkers.ts b/apps/server/src/auth/replayMarkers.ts
index f2a960acae1..5445a6e413d 100644
--- a/apps/server/src/auth/replayMarkers.ts
+++ b/apps/server/src/auth/replayMarkers.ts
@@ -7,7 +7,7 @@ import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schedule from "effect/Schedule";
-import { CLOUD_REPLAY_MARKER_PREFIXES } from "../cloud/linkChecks.ts";
+import { CLOUD_REPLAY_MARKER_PREFIXES } from "../cloud/CloudLink.ts";
import * as ServerConfig from "../config.ts";
import { forkParked } from "../serverActivation.ts";
import { DPOP_REPLAY_MARKER_PREFIX } from "./dpop.ts";
diff --git a/apps/server/src/cloud/CloudLink.lifecycle.test.ts b/apps/server/src/cloud/CloudLink.lifecycle.test.ts
index 329371dc07a..dd211569277 100644
--- a/apps/server/src/cloud/CloudLink.lifecycle.test.ts
+++ b/apps/server/src/cloud/CloudLink.lifecycle.test.ts
@@ -8,6 +8,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
+import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as TestClock from "effect/testing/TestClock";
import * as Stream from "effect/Stream";
@@ -40,6 +41,7 @@ import {
RELAY_URL_SECRET,
} from "./config.ts";
import * as CloudLink from "./CloudLink.ts";
+import { consumeCloudReplayGuards } from "./CloudLink.ts";
import { pendingServiceUpdateExists } from "./updateHandoff.ts";
import {
managedTunnelStartupAction,
@@ -47,6 +49,17 @@ import {
} from "./managedTunnelStartup.ts";
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
+const storeFailure = (tag: "AlreadyExists" | "PermissionDenied") =>
+ new ServerSecretStore.SecretStorePersistError({
+ resource: "cloud replay guard",
+ cause: PlatformError.systemError({
+ _tag: tag,
+ module: "FileSystem",
+ method: "open",
+ pathOrDescriptor: "cloud-replay-guard.bin",
+ }),
+ });
+
const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
// Linking wakes the awareness relay; these tests do not run it.
const idleAwarenessRelay = AgentAwarenessRelay.AgentAwarenessRelay.of({
@@ -102,6 +115,35 @@ it("preserves messages surfaced by cloud 500 responses", () => {
]);
});
+describe("consumeCloudReplayGuards", () => {
+ it.effect("reports already-created guards as replay conflicts", () =>
+ Effect.gen(function* () {
+ const consumed = yield* consumeCloudReplayGuards({
+ secrets: makeSecretStore(() => Effect.fail(storeFailure("AlreadyExists"))),
+ names: ["cloud-jti", "cloud-nonce"],
+ value: new Uint8Array(),
+ });
+
+ expect(consumed).toBe(false);
+ }),
+ );
+
+ it.effect("preserves replay-store availability failures", () =>
+ Effect.gen(function* () {
+ const failure = storeFailure("PermissionDenied");
+ const error = yield* Effect.flip(
+ consumeCloudReplayGuards({
+ secrets: makeSecretStore(() => Effect.fail(failure)),
+ names: ["cloud-jti", "cloud-nonce"],
+ value: new Uint8Array(),
+ }),
+ );
+
+ expect(error).toBe(failure);
+ }),
+ );
+});
+
describe("reconcileDesiredCloudLink", () => {
it.effect("requires stored CLI authorization without exposing an HTTP endpoint", () =>
Effect.gen(function* () {
diff --git a/apps/server/src/cloud/CloudLink.ts b/apps/server/src/cloud/CloudLink.ts
index 35d6ac72498..7289a3d814e 100644
--- a/apps/server/src/cloud/CloudLink.ts
+++ b/apps/server/src/cloud/CloudLink.ts
@@ -107,11 +107,6 @@ import {
shouldRetryRelayRequest,
} from "./relayResponse.ts";
import {
- CLOUD_HEALTH_JTI_PREFIX,
- CLOUD_HEALTH_NONCE_PREFIX,
- CLOUD_MINT_JTI_PREFIX,
- CLOUD_MINT_NONCE_PREFIX,
- consumeCloudReplayGuards,
hasBoundedCloudProofLifetime,
hasExactScope,
hasForwardedAuthorityHeaders,
@@ -124,6 +119,17 @@ import {
} from "./linkChecks.ts";
import { desktopUpdateRestartPending, pendingUpdateHandoffExists } from "./updateHandoff.ts";
+const CLOUD_MINT_NONCE_PREFIX = "cloud-mint-nonce-";
+const CLOUD_MINT_JTI_PREFIX = "cloud-mint-jti-";
+const CLOUD_HEALTH_NONCE_PREFIX = "cloud-health-nonce-";
+const CLOUD_HEALTH_JTI_PREFIX = "cloud-health-jti-";
+/** Secret store name prefixes of cloud replay markers. The server prunes expired ones. */
+export const CLOUD_REPLAY_MARKER_PREFIXES = [
+ CLOUD_MINT_NONCE_PREFIX,
+ CLOUD_MINT_JTI_PREFIX,
+ CLOUD_HEALTH_NONCE_PREFIX,
+ CLOUD_HEALTH_JTI_PREFIX,
+] as const;
const MANAGED_ENDPOINT_PROVISION_REQUEST_TIMEOUT = Duration.minutes(2);
const RELAY_CONFIG_FIELD_MESSAGES = {
relayUrl: "Relay URL must be a secure absolute HTTPS URL.",
@@ -325,6 +331,27 @@ function stringToBytes(value: string): Uint8Array {
return new TextEncoder().encode(value);
}
+/** Records each one-shot marker; false when any already existed, so the request is a replay. */
+export function consumeCloudReplayGuards(input: {
+ readonly secrets: ServerSecretStore.ServerSecretStore["Service"];
+ readonly names: ReadonlyArray<string>;
+ readonly value: Uint8Array;
+}) {
+ return Effect.forEach(
+ input.names,
+ (name) =>
+ input.secrets.create(name, input.value).pipe(
+ Effect.as(true),
+ Effect.catchIf(ServerSecretStore.isSecretStoreError, (error) =>
+ ServerSecretStore.isSecretAlreadyExistsError(error)
+ ? Effect.succeed(false)
+ : Effect.fail(error),
+ ),
+ ),
+ { concurrency: input.names.length },
+ ).pipe(Effect.map((created) => created.every(Boolean)));
+}
+
function validateCloudMintPublicKey(
publicKey: string,
): Effect.Effect<void, CloudLinkRelayConfigInvalidError> {
diff --git a/apps/server/src/cloud/linkChecks.test.ts b/apps/server/src/cloud/linkChecks.test.ts
index 1cdf3819e5e..34902b6938a 100644
--- a/apps/server/src/cloud/linkChecks.test.ts
+++ b/apps/server/src/cloud/linkChecks.test.ts
@@ -1,70 +1,12 @@
import { describe, expect, it } from "@effect/vitest";
-import * as Effect from "effect/Effect";
-import * as PlatformError from "effect/PlatformError";
import type { RelayLinkProofRequest } from "@t3tools/contracts/relay";
-import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import {
- consumeCloudReplayGuards,
isSupportedLinkProviderKind,
linkProofScopes,
parseManagedEndpointLocalOrigin,
} from "./linkChecks.ts";
-const storeFailure = (tag: "AlreadyExists" | "PermissionDenied") =>
- new ServerSecretStore.SecretStorePersistError({
- resource: "cloud replay guard",
- cause: PlatformError.systemError({
- _tag: tag,
- module: "FileSystem",
- method: "open",
- pathOrDescriptor: "cloud-replay-guard.bin",
- }),
- });
-
-const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
-
-function makeSecretStore(
- create: ServerSecretStore.ServerSecretStore["Service"]["create"],
-): ServerSecretStore.ServerSecretStore["Service"] {
- return {
- get: unusedSecretStoreOperation,
- set: unusedSecretStoreOperation,
- create,
- getOrCreateRandom: unusedSecretStoreOperation,
- remove: unusedSecretStoreOperation,
- };
-}
-
-describe("consumeCloudReplayGuards", () => {
- it.effect("reports already-created guards as replay conflicts", () =>
- Effect.gen(function* () {
- const consumed = yield* consumeCloudReplayGuards({
- secrets: makeSecretStore(() => Effect.fail(storeFailure("AlreadyExists"))),
- names: ["cloud-jti", "cloud-nonce"],
- value: new Uint8Array(),
- });
-
- expect(consumed).toBe(false);
- }),
- );
-
- it.effect("preserves replay-store availability failures", () =>
- Effect.gen(function* () {
- const failure = storeFailure("PermissionDenied");
- const error = yield* Effect.flip(
- consumeCloudReplayGuards({
- secrets: makeSecretStore(() => Effect.fail(failure)),
- names: ["cloud-jti", "cloud-nonce"],
- value: new Uint8Array(),
- }),
- );
-
- expect(error).toBe(failure);
- }),
- );
-});
-
describe("parseManagedEndpointLocalOrigin", () => {
it.each([
{
diff --git a/apps/server/src/cloud/linkChecks.ts b/apps/server/src/cloud/linkChecks.ts
index ae655d563c1..50c218e829a 100644
--- a/apps/server/src/cloud/linkChecks.ts
+++ b/apps/server/src/cloud/linkChecks.ts
@@ -1,7 +1,7 @@
/**
* Pure checks behind T3 Connect link proofs and relay requests: which local
- * origin a link may point at, which scopes a proof claims and accepts, and the
- * one-shot replay guards for signed relay requests.
+ * origin a link may point at, and which scopes and lifetimes a proof claims and
+ * accepts.
*/
import type {
RelayEnvironmentLinkProofPayload,
@@ -9,46 +9,12 @@ import type {
RelayManagedEndpointOrigin,
RelayManagedEndpointRuntimeConfig,
} from "@t3tools/contracts/relay";
-import * as Effect from "effect/Effect";
import type { HttpServerRequest } from "effect/http";
-import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
-
-export const CLOUD_MINT_NONCE_PREFIX = "cloud-mint-nonce-";
-export const CLOUD_MINT_JTI_PREFIX = "cloud-mint-jti-";
-export const CLOUD_HEALTH_NONCE_PREFIX = "cloud-health-nonce-";
-export const CLOUD_HEALTH_JTI_PREFIX = "cloud-health-jti-";
-/** Secret store name prefixes of cloud replay markers. The server prunes expired ones. */
-export const CLOUD_REPLAY_MARKER_PREFIXES = [
- CLOUD_MINT_NONCE_PREFIX,
- CLOUD_MINT_JTI_PREFIX,
- CLOUD_HEALTH_NONCE_PREFIX,
- CLOUD_HEALTH_JTI_PREFIX,
-] as const;
const CLOUD_PROOF_MAX_LIFETIME_SECONDS = 5 * 60;
const CLOUD_PROOF_CLOCK_SKEW_SECONDS = 60;
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
-export function consumeCloudReplayGuards(input: {
- readonly secrets: ServerSecretStore.ServerSecretStore["Service"];
- readonly names: ReadonlyArray<string>;
- readonly value: Uint8Array;
-}) {
- return Effect.forEach(
- input.names,
- (name) =>
- input.secrets.create(name, input.value).pipe(
- Effect.as(true),
- Effect.catchIf(ServerSecretStore.isSecretStoreError, (error) =>
- ServerSecretStore.isSecretAlreadyExistsError(error)
- ? Effect.succeed(false)
- : Effect.fail(error),
- ),
- ),
- { concurrency: input.names.length },
- ).pipe(Effect.map((created) => created.every(Boolean)));
-}
-
function normalizeHostname(hostname: string): string {
return hostname
.trim()