Skip to content

Commit 358bb71

Browse files
fix(server): date Connect link proofs with the relay clock (pingdotgg#25218)
Co-authored-by: shivam <[email protected]>
1 parent b703fdb commit 358bb71

2 files changed

Lines changed: 114 additions & 7 deletions

File tree

‎apps/server/src/cloud/CloudLink.lifecycle.test.ts‎

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ import * as Path from "effect/Path";
99
import * as PlatformError from "effect/PlatformError";
1010
import * as Schema from "effect/Schema";
1111
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";
1321
import * as NetAddress from "effect/net/NetAddress";
1422

15-
import { EnvironmentId } from "@t3tools/contracts";
23+
import {
24+
AuthSessionId,
25+
type AuthEnvironmentScope,
26+
EnvironmentAuthenticatedPrincipal,
27+
EnvironmentId,
28+
} from "@t3tools/contracts";
1629
import * as EnvironmentKeys from "./environmentKeys.ts";
1730
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
1831
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -28,7 +41,10 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
2841
import * as AgentAwarenessRelay from "../relay/AgentAwarenessRelay.ts";
2942
import { CLOUD_CLI_DESIRED_LINK_SECRET } from "./CliState.ts";
3043
import * as CliTokenManager from "./CliTokenManager.ts";
31-
import { RelayManagedEndpointRecoveryRegistrationRequest } from "@t3tools/contracts/relay";
44+
import {
45+
RelayEnvironmentLinkProofPayload,
46+
RelayManagedEndpointRecoveryRegistrationRequest,
47+
} from "@t3tools/contracts/relay";
3248
import {
3349
CLOUD_ENDPOINT_CONFIRMED_ORIGIN,
3450
CLOUD_ENDPOINT_RUNTIME_CONFIG,
@@ -186,7 +202,13 @@ describe("releaseManagedTunnelOnShutdown", () => {
186202
ServerEnvironment.ServerEnvironment,
187203
ServerEnvironment.ServerEnvironment.of({
188204
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+
}),
190212
}),
191213
),
192214
Effect.provideService(
@@ -252,6 +274,84 @@ describe("releaseManagedTunnelOnShutdown", () => {
252274
Effect.scoped,
253275
);
254276

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+
255355
const managedLinkSecrets = [
256356
[CLOUD_ENDPOINT_RUNTIME_CONFIG, "runtime-config"],
257357
[CLOUD_ENDPOINT_CONFIRMED_ORIGIN, "confirmed-origin"],

‎apps/server/src/cloud/CloudLink.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from "@t3tools/contracts/relay";
3030
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
3131
import {
32+
decodeRelayJwt,
3233
normalizeRelayIssuer,
3334
RELAY_HEALTH_REQUEST_TYP,
3435
RELAY_HEALTH_RESPONSE_TYP,
@@ -592,16 +593,22 @@ const make = Effect.gen(function* () {
592593
return yield* new CloudLinkOriginInvalidError({ origin: "endpoint" });
593594
}
594595
const now = yield* DateTime.now;
595-
const expiresAt = DateTime.add(now, { minutes: 5 });
596596
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+
);
597604
const descriptor = yield* environment.getDescriptor;
598605
const payload = {
599606
iss: `t3-env:${descriptor.environmentId}`,
600607
aud: normalizeRelayIssuer(request.relayIssuer),
601608
sub: descriptor.environmentId,
602609
jti: yield* random.uuidV4(),
603-
iat: nowSeconds,
604-
exp: Math.floor(expiresAt.epochMilliseconds / 1_000),
610+
iat: issuedAt,
611+
exp: issuedAt + 5 * 60,
605612
challenge: request.challenge,
606613
descriptor,
607614
environmentId: descriptor.environmentId,

0 commit comments

Comments
 (0)