diff --git a/apps/server/src/cloud/relayResponse.ts b/apps/server/src/cloud/relayResponse.ts index 2074212d83172..ac16308deb780 100644 --- a/apps/server/src/cloud/relayResponse.ts +++ b/apps/server/src/cloud/relayResponse.ts @@ -65,8 +65,12 @@ function recoveryHint(error: RelayProtectedError): string { return "Unlink an unused environment in T3 Connect, then restart T3 Code on this machine."; case "RelayAuthInvalidError": return "Run `t3 connect login` to check this machine's authorization. If the stored credential was revoked, sign out with `t3 connect logout`, then run `t3 connect` again. Restart T3 Code after signing in."; - case "RelayEnvironmentLinkProofExpiredError": case "RelayEnvironmentLinkProofInvalidError": + if (error.reason === "environment_key_mismatch") { + return "T3 Connect already has this environment linked with a different key. Unlink it in T3 Connect, then restart T3 Code on this machine."; + } + return "Check this machine's date and time, update T3 Code, then restart it."; + case "RelayEnvironmentLinkProofExpiredError": return "Check this machine's date and time, update T3 Code, then restart it."; default: return "Retry when the relay is available. If this continues, include the trace ID when reporting it."; diff --git a/docs/internals/t3-connect.md b/docs/internals/t3-connect.md index ae3938fd0cd7b..7237df5cfe381 100644 --- a/docs/internals/t3-connect.md +++ b/docs/internals/t3-connect.md @@ -86,6 +86,10 @@ Managed tunnels expose only this server's own loopback HTTP origin. The link proof signs that origin from the server's listening address and ignores the caller's, so any client holding `relay:configure`, local or remote, can link without being able to point the tunnel at another service on the host. The +relay refuses to relink an active environment id under a different key: an +account bearer alone would otherwise take over that environment's tunnel and +trusted key with an origin it signed itself. Unlinking first releases the +tunnel. The relay resolves endpoints from its own managed allocations rather than a caller-supplied URL. Health and mint requests must not follow redirects. These restrictions keep endpoint discovery diff --git a/infra/relay/src/environments/EnvironmentLinker.test.ts b/infra/relay/src/environments/EnvironmentLinker.test.ts index 40ba2220dbfaa..23ae7c890fb94 100644 --- a/infra/relay/src/environments/EnvironmentLinker.test.ts +++ b/infra/relay/src/environments/EnvironmentLinker.test.ts @@ -112,6 +112,8 @@ const makeRequest = Effect.gen(function* () { function layerTest(input?: { readonly upsert?: EnvironmentLinks.EnvironmentLinks["Service"]["upsert"]; + readonly getForUser?: EnvironmentLinks.EnvironmentLinks["Service"]["getForUser"]; + readonly prepareDeprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["prepareDeprovision"]; readonly consume?: DpopProofs.DpopProofReplay["Service"]["consume"]; readonly deprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["deprovision"]; }) { @@ -130,7 +132,7 @@ function layerTest(input?: { upsert: input?.upsert ?? (() => Effect.void), listDeliveryUsersForEnvironment: () => Effect.succeed([]), listForUser: () => Effect.succeed([]), - getForUser: () => Effect.succeed(null), + getForUser: input?.getForUser ?? (() => Effect.succeed(null)), findActiveManagedForEnvironment: () => Effect.succeed([]), updateLiveActivityPreference: () => Effect.die("unexpected Live Activity preference update"), @@ -144,7 +146,7 @@ function layerTest(input?: { }), Layer.succeed(ManagedEndpointProvider.ManagedEndpointProvider, { reconcileOrigin: () => Effect.succeed("ready"), - prepareDeprovision: () => Effect.succeed(null), + prepareDeprovision: input?.prepareDeprovision ?? (() => Effect.succeed(null)), deprovision: input?.deprovision ?? (() => Effect.succeed(true)), release: () => Effect.succeed(true), provision: () => @@ -293,6 +295,90 @@ describe("EnvironmentLinker", () => { ); }); + it.effect("refuses to move an active link to a different environment key", () => { + const touched: Array = []; + const activeLink = (environmentPublicKey: string) => ({ + environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"], + label: "Link Test Environment", + endpoint: { + httpBaseUrl: "https://managed.example.test/", + wsBaseUrl: "wss://managed.example.test/ws", + providerKind: "cloudflare_tunnel" as const, + }, + environmentPublicKey, + linkedAt: "2026-01-01T00:00:00.000Z", + }); + let linkedKey = "another-environment-key"; + return Effect.gen(function* () { + const linker = yield* EnvironmentLinker.EnvironmentLinker; + const refused = yield* linker + .link({ userId: "user_123", request: (yield* makeRequest).request }) + .pipe(Effect.flip); + expect(refused).toMatchObject({ + _tag: "EnvironmentLinkProofInvalid", + reason: "environment_key_mismatch", + }); + expect(touched).toEqual([]); + + linkedKey = environmentKeyPair.publicKey.trim(); + yield* linker.link({ userId: "user_123", request: (yield* makeRequest).request }); + expect(touched).toEqual(["deprovision", "upsert"]); + }).pipe( + Effect.provide( + layerTest({ + getForUser: () => Effect.succeed(activeLink(linkedKey)), + upsert: () => Effect.sync(() => void touched.push("upsert")), + deprovision: () => Effect.sync(() => touched.push("deprovision") > 0), + }), + ), + ); + }); + + it.effect.each([{ managedTunnelsEnabled: true }, { managedTunnelsEnabled: false }])( + "finishes a failed unlink teardown before a new link: %o", + ({ managedTunnelsEnabled }) => { + const touched: Array = []; + let tornDown = false; + return Effect.gen(function* () { + const linker = yield* EnvironmentLinker.EnvironmentLinker; + const link = Effect.gen(function* () { + const { request } = yield* makeRequest; + return yield* linker.link({ + userId: "user_123", + request: { ...request, managedTunnelsEnabled }, + }); + }); + expect((yield* link.pipe(Effect.flip))._tag).toBe("ManagedEndpointDeprovisioningFailed"); + expect(touched).toEqual(["deprovision"]); + + tornDown = true; + yield* link; + expect(touched).toEqual(["deprovision", "deprovision", "upsert"]); + }).pipe( + Effect.provide( + layerTest({ + prepareDeprovision: () => + Effect.succeed({ + userId: "user_123", + environmentId: "env-link-test", + hostname: "managed.example.test", + tunnelId: "left-behind-tunnel", + tunnelName: "left-behind", + dnsRecordId: null, + readyAt: null, + origin: null, + updatedAt: "2026-01-01T00:00:00.000Z", + generation: 1, + tunnelReleasedAt: null, + }), + deprovision: () => Effect.sync(() => touched.push("deprovision") > 0 && tornDown), + upsert: () => Effect.sync(() => void touched.push("upsert")), + }), + ), + ); + }, + ); + it.effect("rejects replayed JWT ids", () => Effect.gen(function* () { const { request } = yield* makeRequest; diff --git a/infra/relay/src/environments/EnvironmentLinker.ts b/infra/relay/src/environments/EnvironmentLinker.ts index c0acd41c36c2e..c771f51841b49 100644 --- a/infra/relay/src/environments/EnvironmentLinker.ts +++ b/infra/relay/src/environments/EnvironmentLinker.ts @@ -55,6 +55,7 @@ export class EnvironmentLinkProofInvalid extends Schema.TaggedError { }); describe("relay environment unlink", () => { - it.effect("drops the unlinked endpoint's held webhooks, even if clearing fails", () => { - const cleared: Array = []; - const endpointKey = "0123456789abcdef"; - const unlink = (clearFails: boolean) => - unlinkEnvironmentRecord({ - userId: "user-1", - environmentId: "environment-1", - managedEndpointNamespace: "dev", - }).pipe( - Effect.provide( - layerRelayUnlinkTest({ - getForUser: () => Effect.succeed(linkedEnvironmentRecord), - revokeForUser: () => Effect.succeed(true), - prepareDeprovision: () => - Effect.succeed({ - userId: "user-1", - environmentId: "environment-1", - hostname: "dev-0123456789abcdef.example.test", - tunnelId: "tunnel-1", - tunnelName: `t3coderelay-managedendpoint-dev-${endpointKey}`, - dnsRecordId: "dns-1", - readyAt: "2026-07-28T00:00:00.000Z", - tunnelReleasedAt: null, - origin: null, - updatedAt: "2026-07-28T00:00:00.000Z", - generation: 1, - }), - clearInbox: (input) => - clearFails - ? Effect.fail( - new HookInbox.HookInboxError({ - operation: "clear", - endpointKey: input.endpointKey, - cause: new Error("unavailable"), - }), - ) - : Effect.sync(() => void cleared.push(input.endpointKey)), - }), - ), - ); - return Effect.gen(function* () { - expect(yield* unlink(false)).toBe(true); - expect(cleared).toEqual([endpointKey]); - expect(yield* unlink(true)).toBe(true); - }); - }); + it.effect( + "drops the unlinked endpoint's held webhooks, even if clearing or teardown fails", + () => { + const cleared: Array = []; + const endpointKey = "0123456789abcdef"; + const unlink = (clearFails: boolean, teardownFails = false) => + unlinkEnvironmentRecord({ + userId: "user-1", + environmentId: "environment-1", + managedEndpointNamespace: "dev", + }).pipe( + Effect.provide( + layerRelayUnlinkTest({ + getForUser: () => Effect.succeed(linkedEnvironmentRecord), + revokeForUser: () => Effect.succeed(true), + prepareDeprovision: () => + Effect.succeed({ + userId: "user-1", + environmentId: "environment-1", + hostname: "dev-0123456789abcdef.example.test", + tunnelId: "tunnel-1", + tunnelName: `t3coderelay-managedendpoint-dev-${endpointKey}`, + dnsRecordId: "dns-1", + readyAt: "2026-07-28T00:00:00.000Z", + tunnelReleasedAt: null, + origin: null, + updatedAt: "2026-07-28T00:00:00.000Z", + generation: 1, + }), + clearInbox: (input) => + clearFails + ? Effect.fail( + new HookInbox.HookInboxError({ + operation: "clear", + endpointKey: input.endpointKey, + cause: new Error("unavailable"), + }), + ) + : Effect.sync(() => void cleared.push(input.endpointKey)), + ...(teardownFails + ? { + deprovision: (input) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointDeprovisioningFailed({ + userId: input.userId, + environmentId: input.environmentId, + stage: "delete-tunnel", + cause: new Error("unavailable"), + }), + ), + } + : {}), + }), + ), + ); + return Effect.gen(function* () { + expect(yield* unlink(false)).toBe(true); + expect(cleared).toEqual([endpointKey]); + expect(yield* unlink(true)).toBe(true); + // A later link under another key shares the inbox, so it is emptied before teardown can fail. + yield* unlink(false, true).pipe(Effect.flip); + expect(cleared).toEqual([endpointKey, endpointKey]); + }); + }, + ); it.effect("commits database revocation before deprovisioning the managed endpoint", () => { const calls: Array = []; diff --git a/infra/relay/src/http/Api.ts b/infra/relay/src/http/Api.ts index 74d2b5bbd7a6d..49bfda68a7f95 100644 --- a/infra/relay/src/http/Api.ts +++ b/infra/relay/src/http/Api.ts @@ -527,12 +527,7 @@ export const unlinkEnvironmentRecord = Effect.fn("relay.api.client.unlinkEnviron environmentId: link.environmentId, environmentPublicKey: link.environmentPublicKey, }); - // Deprovision after revocation commits; retry even if already revoked to finish failed Cloudflare cleanup. - const deprovisioned = yield* managedEndpointProvider.deprovision({ - userId: input.userId, - environmentId: input.environmentId, - target: deprovisionTarget, - }); + // Cleared before teardown, which can fail: a later link under another key shares this inbox. const endpointKey = deprovisionTarget && input.managedEndpointNamespace ? HeldHooks.endpointKeyForTunnelName( @@ -551,6 +546,12 @@ export const unlinkEnvironmentRecord = Effect.fn("relay.api.client.unlinkEnviron ), ); } + // Deprovision after revocation commits; retry even if already revoked to finish failed Cloudflare cleanup. + const deprovisioned = yield* managedEndpointProvider.deprovision({ + userId: input.userId, + environmentId: input.environmentId, + target: deprovisionTarget, + }); if (!deprovisioned) { const key = { userId: input.userId, environmentId: input.environmentId }; const retryTarget = yield* managedEndpointProvider.prepareDeprovision(key); @@ -897,6 +898,12 @@ export const layerClientApi = HttpApiBuilder.group( reason: "origin_not_allowed", traceId, }), + ManagedEndpointDeprovisioningFailed: (_error, traceId) => + new RelayEnvironmentLinkUnavailableError({ + code: "environment_link_unavailable", + reason: "managed_endpoint_provisioning_failed", + traceId, + }), ManagedTunnelLimitExceeded: (limitError, traceId) => new RelayEnvironmentLinkLimitExceededError({ code: "environment_link_limit_exceeded", diff --git a/packages/contracts/src/relay.ts b/packages/contracts/src/relay.ts index 8dadf2437b8aa..c32475de4be3e 100644 --- a/packages/contracts/src/relay.ts +++ b/packages/contracts/src/relay.ts @@ -352,6 +352,7 @@ export const RelayEnvironmentLinkProofInvalidReason = Schema.Literals([ "challenge_invalid", "origin_not_allowed", "endpoint_not_secure", + "environment_key_mismatch", ]); export type RelayEnvironmentLinkProofInvalidReason = typeof RelayEnvironmentLinkProofInvalidReason.Type;