@devries-cameron20 / t3code-1 / commits / b39eb755aa
Fork 0

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

fix(server): a revoked T3 Connect link can be relinked to another account (#34194)

Co-authored-by: Nicholas Wasmiller <derped@mnpl.fyi> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Nicholas Wasmiller committed Nov 19, 2026 at 00:58 UTC b39eb755aaf8c86fa1ce56d2447d197eabc6d9d7
2 files changed +126 -16
apps/server/src/cloud/CloudLink.lifecycle.test.ts
+103 -14
@@ -708,28 +708,117 @@ describe("releaseManagedTunnelOnShutdown", () => {
708 );
709 });
710
711 - it.effect("clears the revoked mark when the environment is linked again", () => {
712 - const { store } = makeMemorySecretStore([
711 + it.effect.each([
712 + {
713 + name: "replaces a revoked link to another account",
714 + revoked: true,
715 + startup: false,
716 + cloudUserId: "user-456",
717 + },
718 + {
719 + name: "replaces a revoked link to the same account",
720 + revoked: true,
721 + startup: false,
722 + cloudUserId: "user-123",
723 + },
724 + {
725 + name: "replaces a revoked link after a partial drop removed its user id",
726 + revoked: true,
727 + startup: false,
728 + cloudUserId: "user-456",
729 + partialDrop: true,
730 + },
731 + {
732 + name: "rejects switching an active link to another account",
733 + revoked: false,
734 + startup: false,
735 + cloudUserId: "user-456",
736 + },
737 + {
738 + name: "keeps a revoked link when startup links another account",
739 + revoked: true,
740 + startup: true,
741 + cloudUserId: "user-456",
742 + },
743 + ])("$name", ({ revoked, startup, cloudUserId, partialDrop }) => {
744 + const { store, values } = makeMemorySecretStore([
745 [RELAY_URL_SECRET, "https://relay.example.test"],
714 - [CLOUD_LINKED_USER_ID, "user-123"],
715 - [RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "revoked-credential"],
716 - [CLOUD_LINK_REVOKED, "true"],
746 + ...(partialDrop ? [] : [[CLOUD_LINKED_USER_ID, "user-123"] as const]),
747 + [RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "old-credential"],
748 + ...(revoked ? [[CLOUD_LINK_REVOKED, "true"] as const] : []),
749 + [CLOUD_CLI_DESIRED_LINK_SECRET, startup ? "publish_only" : "managed"],
750 ]);
751 const cloudMintPublicKey =
752 "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAqZ2u2LjMoAyG2QTHXmITJ9w6RzZtVUAv394XCcPV0zE=\n-----END PUBLIC KEY-----";
753
754 return Effect.gen(function* () {
755 const link = yield* CloudLink.CloudLink;
723 - expect((yield* link.linkState()).linkRevoked).toBe(true);
724 - yield* link.applyRelayConfig({
725 - relayUrl: "https://relay.example.test",
726 - cloudUserId: "user-123",
727 - environmentCredential: "fresh-credential",
728 - cloudMintPublicKey,
729 - endpointRuntime: null,
756 + expect((yield* link.linkState()).linkRevoked).toBe(revoked && !partialDrop);
757 + const relink = Effect.gen(function* () {
758 + if (startup) {
759 + yield* link.reconcileDesiredLinkIfStillDesired("http://127.0.0.1:3773");
760 + } else {
761 + yield* link.applyRelayConfig({
762 + relayUrl: "https://relay.example.test",
763 + cloudUserId,
764 + environmentCredential: "fresh-credential",
765 + cloudMintPublicKey,
766 + endpointRuntime: null,
767 + });
768 + }
769 });
731 - expect(yield* link.linkState()).toMatchObject({ linked: true, linkRevoked: false });
732 - }).pipe(provideReleaseHarness({ store, applyConfigCalls: [], requests: [] }));
770 + const result = yield* relink.pipe(
771 + Effect.provide(
772 + ConfigProvider.layer(
773 + ConfigProvider.fromEnv({ env: { T3CODE_RELAY_URL: "https://relay.example.test" } }),
774 + ),
775 + ),
776 + Effect.flip,
777 + Effect.option,
778 + );
779 + if (revoked && !startup) {
780 + expect(result).toEqual(Option.none());
781 + expect(yield* link.linkState()).toMatchObject({
782 + linked: true,
783 + linkRevoked: false,
784 + cloudUserId,
785 + });
786 + expect(new TextDecoder().decode(values.get(RELAY_ENVIRONMENT_CREDENTIAL_SECRET))).toBe(
787 + "fresh-credential",
788 + );
789 + expect(values.has(CLOUD_CLI_DESIRED_LINK_SECRET)).toBe(cloudUserId === "user-123");
790 + } else {
791 + expect(Option.getOrThrow(result)).toBeInstanceOf(CloudLink.CloudLinkAccountMismatchError);
792 + expect(yield* link.linkState()).toMatchObject({
793 + cloudUserId: "user-123",
794 + linkRevoked: revoked,
795 + });
796 + expect(values.has(CLOUD_CLI_DESIRED_LINK_SECRET)).toBe(true);
797 + }
798 + }).pipe(
799 + provideReleaseHarness({
800 + store,
801 + applyConfigCalls: [],
802 + requests: [],
803 + respond: () =>
804 + Response.json({
805 + challenge: "challenge-1",
806 + expiresAt: "2099-01-01T00:00:00.000Z",
807 + ok: true,
808 + cloudUserId: "user-456",
809 + environmentId: "env_123",
810 + endpoint: {
811 + httpBaseUrl: "http://127.0.0.1:3773",
812 + wsBaseUrl: "ws://127.0.0.1:3773",
813 + providerKind: "manual",
814 + },
815 + endpointRuntime: null,
816 + relayIssuer: "https://relay.example.test",
817 + environmentCredential: "fresh-credential",
818 + cloudMintPublicKey,
819 + }),
820 + }),
821 + );
822 });
823
824 it.effect(
apps/server/src/cloud/CloudLink.ts
+23 -2
@@ -67,6 +67,7 @@ import { makeRelayEnvironmentClient } from "../relay/relayEnvironmentClient.ts";
67 import { loopbackUrlHostForAddress } from "../startupAccess.ts";
68 import {
69 type CliDesiredLinkMode,
70 + dropRevokedCloudLink,
71 readCliDesiredCloudLink,
72 readCliDesiredLinkMode,
73 setCliDesiredCloudLink,
@@ -749,11 +750,11 @@ const make = Effect.gen(function* () {
750 options?: {
751 readonly lockHeld?: boolean;
752 readonly confirmedOrigin?: RelayManagedEndpointOrigin;
753 + readonly replaceRevokedLink?: boolean;
754 },
755 ) {
756 const apply = Effect.gen(function* () {
757 yield* validateRelayConfigPayload(payload);
756 - yield* validateLinkedCloudUser(payload.cloudUserId);
758 yield* validateCloudMintPublicKey(payload.cloudMintPublicKey);
759 // Reject unsupported runtimes before they can stop a healthy tunnel.
760 if (
@@ -768,6 +769,26 @@ const make = Effect.gen(function* () {
769 },
770 });
771 }
772 + // Only an explicit link replaces a revoked one: a clock-skew 401 can mark a live link revoked.
773 + if (options?.replaceRevokedLink === true) {
774 + const [revoked, previousUserId] = yield* Effect.all([
775 + secrets.get(CLOUD_LINK_REVOKED),
776 + secrets.get(CLOUD_LINKED_USER_ID),
777 + ]);
778 + // The CLI token may belong to the old account. Release it before the drop so a failed
779 + // write leaves the revoked mark for a retry; a partial drop can leave no user id.
780 + if (
781 + Option.isSome(revoked) &&
782 + Option.match(previousUserId, {
783 + onNone: () => true,
784 + onSome: (userId) => bytesToString(userId) !== payload.cloudUserId,
785 + })
786 + ) {
787 + yield* setCliDesiredCloudLink(false).pipe(withSecrets);
788 + }
789 + yield* dropRevokedCloudLink.pipe(withSecrets);
790 + }
791 + yield* validateLinkedCloudUser(payload.cloudUserId);
792 const endpointRuntimeJson = payload.endpointRuntime
793 ? yield* encodeEndpointRuntimeConfigJson(payload.endpointRuntime)
794 : null;
@@ -1157,7 +1178,7 @@ const make = Effect.gen(function* () {
1178
1179 const applyRelayConfig = Effect.fn("environment.cloud.relayConfig")(
1180 function* (payload: RelayEnvironmentConfigRequest) {
1160 - const result = yield* applyCloudRelayConfig(payload);
1181 + const result = yield* applyCloudRelayConfig(payload, { replaceRevokedLink: true });
1182 if (payload.endpointRuntime?.providerKind === "cloudflare_tunnel") {
1183 const address = httpServer.address;
1184 if (typeof address === "string" || !("port" in address)) {