GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): a revoked T3 Connect link can be relinked to another acc… · pingdotgg/t3code@b39eb75

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/cloud
    • CloudLink.lifecycle.test.ts
    • CloudLink.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

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 numberDiff line numberDiff line change
@@ -708,28 +708,117 @@ describe("releaseManagedTunnelOnShutdown", () => {
708708
);
709709
});
710710

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([
713745
[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"],
717750
]);
718751
const cloudMintPublicKey =
719752
"-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAqZ2u2LjMoAyG2QTHXmITJ9w6RzZtVUAv394XCcPV0zE=\n-----END PUBLIC KEY-----";
720753

721754
return Effect.gen(function* () {
722755
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+
}
730769
});
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+
);
733822
});
734823

735824
it.effect(
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/cloud/CloudLink.ts
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { makeRelayEnvironmentClient } from "../relay/relayEnvironmentClient.ts";
6767
import { loopbackUrlHostForAddress } from "../startupAccess.ts";
6868
import {
6969
type CliDesiredLinkMode,
70+
dropRevokedCloudLink,
7071
readCliDesiredCloudLink,
7172
readCliDesiredLinkMode,
7273
setCliDesiredCloudLink,
@@ -749,11 +750,11 @@ const make = Effect.gen(function* () {
749750
options?: {
750751
readonly lockHeld?: boolean;
751752
readonly confirmedOrigin?: RelayManagedEndpointOrigin;
753+
readonly replaceRevokedLink?: boolean;
752754
},
753755
) {
754756
const apply = Effect.gen(function* () {
755757
yield* validateRelayConfigPayload(payload);
756-
yield* validateLinkedCloudUser(payload.cloudUserId);
757758
yield* validateCloudMintPublicKey(payload.cloudMintPublicKey);
758759
// Reject unsupported runtimes before they can stop a healthy tunnel.
759760
if (
@@ -768,6 +769,26 @@ const make = Effect.gen(function* () {
768769
},
769770
});
770771
}
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);
771792
const endpointRuntimeJson = payload.endpointRuntime
772793
? yield* encodeEndpointRuntimeConfigJson(payload.endpointRuntime)
773794
: null;
@@ -1157,7 +1178,7 @@ const make = Effect.gen(function* () {
11571178

11581179
const applyRelayConfig = Effect.fn("environment.cloud.relayConfig")(
11591180
function* (payload: RelayEnvironmentConfigRequest) {
1160-
const result = yield* applyCloudRelayConfig(payload);
1181+
const result = yield* applyCloudRelayConfig(payload, { replaceRevokedLink: true });
11611182
if (payload.endpointRuntime?.providerKind === "cloudflare_tunnel") {
11621183
const address = httpServer.address;
11631184
if (typeof address === "string" || !("port" in address)) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.