@devries-cameron20 / t3code Mirror · read-only / commits / 358bb71048
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 22:15 UTC

fix(server): date Connect link proofs with the relay clock (#25218)

Co-authored-by: shivam <91240327+shivamhwp@users.noreply.github.com>

maria committed Nov 11, 2026 at 23:04 UTC 358bb710485ef4f6eaca045c8d2a82864f16c595
2 files changed +114 -7
apps/server/src/cloud/CloudLink.lifecycle.test.ts
+104 -4
@@ -9,10 +9,23 @@ import * as Path from "effect/Path";
9 import * as PlatformError from "effect/PlatformError";
10 import * as Schema from "effect/Schema";
11 import * as Stream from "effect/Stream";
12 -import { HttpClient, HttpClientResponse, HttpServer, type HttpClientRequest } from "effect/http";
12 +import * as TestClock from "effect/testing/TestClock";
13 +import {
14 + HttpClient,
15 + HttpClientResponse,
16 + HttpServer,
17 + HttpServerRequest,
18 + type HttpClientRequest,
19 +} from "effect/http";
20 +import { RELAY_LINK_PROOF_TYP, signRelayJwt, verifyRelayJwt } from "@t3tools/shared/relayJwt";
21 import * as NetAddress from "effect/net/NetAddress";
22
15 -import { EnvironmentId } from "@t3tools/contracts";
23 +import {
24 + AuthSessionId,
25 + type AuthEnvironmentScope,
26 + EnvironmentAuthenticatedPrincipal,
27 + EnvironmentId,
28 +} from "@t3tools/contracts";
29 import * as EnvironmentKeys from "./environmentKeys.ts";
30 import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
31 import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -28,7 +41,10 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
41 import * as AgentAwarenessRelay from "../relay/AgentAwarenessRelay.ts";
42 import { CLOUD_CLI_DESIRED_LINK_SECRET } from "./CliState.ts";
43 import * as CliTokenManager from "./CliTokenManager.ts";
31 -import { RelayManagedEndpointRecoveryRegistrationRequest } from "@t3tools/contracts/relay";
44 +import {
45 + RelayEnvironmentLinkProofPayload,
46 + RelayManagedEndpointRecoveryRegistrationRequest,
47 +} from "@t3tools/contracts/relay";
48 import {
49 CLOUD_ENDPOINT_CONFIRMED_ORIGIN,
50 CLOUD_ENDPOINT_RUNTIME_CONFIG,
@@ -186,7 +202,13 @@ describe("releaseManagedTunnelOnShutdown", () => {
202 ServerEnvironment.ServerEnvironment,
203 ServerEnvironment.ServerEnvironment.of({
204 getEnvironmentId: Effect.succeed(EnvironmentId.make("env_123")),
189 - getDescriptor: Effect.die("unused"),
205 + getDescriptor: Effect.succeed({
206 + environmentId: EnvironmentId.make("env_123"),
207 + label: "Test environment",
208 + platform: { os: "linux", arch: "x64" },
209 + serverVersion: "0.0.0-test",
210 + capabilities: { repositoryIdentity: true },
211 + }),
212 }),
213 ),
214 Effect.provideService(
@@ -252,6 +274,84 @@ describe("releaseManagedTunnelOnShutdown", () => {
274 Effect.scoped,
275 );
276
277 + it.effect.each([
278 + { direction: "ahead", skewSeconds: 600 },
279 + { direction: "behind", skewSeconds: -600 },
280 + ])(
281 + "signs a link proof accepted at relay time when the host clock is $direction",
282 + ({ skewSeconds }) => {
283 + const { store } = makeMemorySecretStore();
284 +
285 + return Effect.gen(function* () {
286 + const relayNow = 1_800_000_000;
287 + const relayIssuer = "https://relay.example.test";
288 + const relayKeys = yield* EnvironmentKeys.getOrCreateEnvironmentKeyPairFromSecretStore(
289 + makeMemorySecretStore().store,
290 + );
291 + const challenge = yield* signRelayJwt({
292 + privateKey: relayKeys.privateKey,
293 + typ: "t3-link-challenge+jwt",
294 + payload: {
295 + kind: "environment_link_challenge",
296 + iss: relayIssuer,
297 + aud: relayIssuer,
298 + sub: "user-123",
299 + jti: "challenge-jti",
300 + iat: relayNow,
301 + exp: relayNow + 300,
302 + notificationsEnabled: true,
303 + liveActivitiesEnabled: false,
304 + managedTunnelsEnabled: false,
305 + },
306 + });
307 + yield* TestClock.setTime((relayNow + skewSeconds) * 1_000);
308 + const link = yield* CloudLink.CloudLink;
309 + const proof = yield* link
310 + .linkProof(
311 + {
312 + challenge,
313 + relayIssuer,
314 + endpoint: {
315 + httpBaseUrl: "http://127.0.0.1:3773",
316 + wsBaseUrl: "ws://127.0.0.1:3773",
317 + providerKind: "manual",
318 + },
319 + origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
320 + },
321 + HttpServerRequest.fromWeb(
322 + new Request("http://127.0.0.1:3773/api/t3-connect/link-proof"),
323 + ),
324 + )
325 + .pipe(
326 + Effect.provideService(EnvironmentAuthenticatedPrincipal, {
327 + sessionId: AuthSessionId.make("test-session"),
328 + subject: "test",
329 + method: "bearer-access-token",
330 + scopes: new Set<AuthEnvironmentScope>(),
331 + }),
332 + );
333 + const environmentKeys =
334 + yield* EnvironmentKeys.getOrCreateEnvironmentKeyPairFromSecretStore(store);
335 + yield* TestClock.setTime(relayNow * 1_000);
336 + const verified = yield* verifyRelayJwt(RelayEnvironmentLinkProofPayload, {
337 + publicKey: environmentKeys.publicKey,
338 + token: proof,
339 + typ: RELAY_LINK_PROOF_TYP,
340 + issuer: "t3-env:env_123",
341 + audience: relayIssuer,
342 + });
343 +
344 + expect(verified).toMatchObject({
345 + iat: relayNow,
346 + exp: relayNow + 300,
347 + challenge,
348 + environmentId: "env_123",
349 + scopes: ["agent_activity_notifications"],
350 + });
351 + }).pipe(provideReleaseHarness({ store, applyConfigCalls: [], requests: [] }));
352 + },
353 + );
354 +
355 const managedLinkSecrets = [
356 [CLOUD_ENDPOINT_RUNTIME_CONFIG, "runtime-config"],
357 [CLOUD_ENDPOINT_CONFIRMED_ORIGIN, "confirmed-origin"],
apps/server/src/cloud/CloudLink.ts
+10 -3
@@ -29,6 +29,7 @@ import {
29 } from "@t3tools/contracts/relay";
30 import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
31 import {
32 + decodeRelayJwt,
33 normalizeRelayIssuer,
34 RELAY_HEALTH_REQUEST_TYP,
35 RELAY_HEALTH_RESPONSE_TYP,
@@ -592,16 +593,22 @@ const make = Effect.gen(function* () {
593 return yield* new CloudLinkOriginInvalidError({ origin: "endpoint" });
594 }
595 const now = yield* DateTime.now;
595 - const expiresAt = DateTime.add(now, { minutes: 5 });
596 const nowSeconds = Math.floor(now.epochMilliseconds / 1_000);
597 + // Borrow only the relay clock; the relay still verifies and consumes the challenge.
598 + const issuedAt = yield* decodeRelayJwt(request.challenge).pipe(
599 + Effect.map(({ iat }) =>
600 + typeof iat === "number" && Number.isSafeInteger(iat) && iat > 0 ? iat : nowSeconds,
601 + ),
602 + Effect.orElseSucceed(() => nowSeconds),
603 + );
604 const descriptor = yield* environment.getDescriptor;
605 const payload = {
606 iss: `t3-env:${descriptor.environmentId}`,
607 aud: normalizeRelayIssuer(request.relayIssuer),
608 sub: descriptor.environmentId,
609 jti: yield* random.uuidV4(),
603 - iat: nowSeconds,
604 - exp: Math.floor(expiresAt.epochMilliseconds / 1_000),
610 + iat: issuedAt,
611 + exp: issuedAt + 5 * 60,
612 challenge: request.challenge,
613 descriptor,
614 environmentId: descriptor.environmentId,