GeezerVison is showing github.com — fix(server): a revoked T3 Connect link can be relinked to another acc… · pingdotgg/t3code@b39eb75
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cloud/CloudLink.lifecycle.test.ts +103-14Lines changed: 103 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -708,28 +708,117 @@ describe("releaseManagedTunnelOnShutdown", () => { | 708 | 708 | ); | 709 | 709 | }); | 710 | 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([ | 713 | 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"], | 717 | 750 | ]); | 718 | 751 | const cloudMintPublicKey = | 719 | 752 | "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAqZ2u2LjMoAyG2QTHXmITJ9w6RzZtVUAv394XCcPV0zE=\n-----END PUBLIC KEY-----"; | 720 | 753 |
| 721 | 754 | return Effect.gen(function* () { | 722 | 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 | + } | 730 | 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 | + ); | 733 | 822 | }); | 734 | 823 |
| 735 | 824 | it.effect( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cloud/CloudLink.ts +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,6 +67,7 @@ import { makeRelayEnvironmentClient } from "../relay/relayEnvironmentClient.ts"; | 67 | 67 | import { loopbackUrlHostForAddress } from "../startupAccess.ts"; | 68 | 68 | import { | 69 | 69 | type CliDesiredLinkMode, | | 70 | + dropRevokedCloudLink, | 70 | 71 | readCliDesiredCloudLink, | 71 | 72 | readCliDesiredLinkMode, | 72 | 73 | setCliDesiredCloudLink, | @@ -749,11 +750,11 @@ const make = Effect.gen(function* () { | 749 | 750 | options?: { | 750 | 751 | readonly lockHeld?: boolean; | 751 | 752 | readonly confirmedOrigin?: RelayManagedEndpointOrigin; | | 753 | + readonly replaceRevokedLink?: boolean; | 752 | 754 | }, | 753 | 755 | ) { | 754 | 756 | const apply = Effect.gen(function* () { | 755 | 757 | yield* validateRelayConfigPayload(payload); | 756 | | - yield* validateLinkedCloudUser(payload.cloudUserId); | 757 | 758 | yield* validateCloudMintPublicKey(payload.cloudMintPublicKey); | 758 | 759 | // Reject unsupported runtimes before they can stop a healthy tunnel. | 759 | 760 | if ( | @@ -768,6 +769,26 @@ const make = Effect.gen(function* () { | 768 | 769 | }, | 769 | 770 | }); | 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); | 771 | 792 | const endpointRuntimeJson = payload.endpointRuntime | 772 | 793 | ? yield* encodeEndpointRuntimeConfigJson(payload.endpointRuntime) | 773 | 794 | : null; | @@ -1157,7 +1178,7 @@ const make = Effect.gen(function* () { | 1157 | 1178 |
| 1158 | 1179 | const applyRelayConfig = Effect.fn("environment.cloud.relayConfig")( | 1159 | 1180 | function* (payload: RelayEnvironmentConfigRequest) { | 1160 | | - const result = yield* applyCloudRelayConfig(payload); | | 1181 | + const result = yield* applyCloudRelayConfig(payload, { replaceRevokedLink: true }); | 1161 | 1182 | if (payload.endpointRuntime?.providerKind === "cloudflare_tunnel") { | 1162 | 1183 | const address = httpServer.address; | 1163 | 1184 | if (typeof address === "string" || !("port" in address)) { | |
|