refactor(server): replay guards stay in CloudLink (#16349)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
68e50db20b
commit
99db70cf1a
5 files changed
+77
-100
No files matched your search
@@ -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";
|
||||
|
||||
@@ -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* () {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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([
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user