Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/server/src/cloud/relayResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
4 changes: 4 additions & 0 deletions docs/internals/t3-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 88 additions & 2 deletions infra/relay/src/environments/EnvironmentLinker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}) {
Expand All @@ -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"),
Expand All @@ -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: () =>
Expand Down Expand Up @@ -293,6 +295,90 @@ describe("EnvironmentLinker", () => {
);
});

it.effect("refuses to move an active link to a different environment key", () => {
const touched: Array<string> = [];
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<string> = [];
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;
Expand Down
35 changes: 33 additions & 2 deletions infra/relay/src/environments/EnvironmentLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class EnvironmentLinkProofInvalid extends Schema.TaggedError<EnvironmentL
"consume_proof_nonce",
"consume_challenge_nonce",
"validate_origin",
"validate_existing_link",
"validate_endpoint",
]),
cause: Schema.optional(Schema.Defect()),
Expand All @@ -70,6 +71,8 @@ export type EnvironmentLinkError =
| EnvironmentLinkProofInvalid
| DpopProofs.DpopProofReplayPersistenceError
| EnvironmentLinks.EnvironmentLinkUpsertPersistenceError
| EnvironmentLinks.EnvironmentLinkLookupPersistenceError
| ManagedEndpointProvider.ManagedEndpointDeprovisioningFailed
| EnvironmentCredentials.EnvironmentCredentialCreatePersistenceError
| ManagedEndpointProvider.ManagedEndpointProviderError;

Expand Down Expand Up @@ -291,8 +294,36 @@ const make = Effect.gen(function* () {
stage: "validate_origin",
});
}
// Non-tunnel links must release provisioned tunnel/DNS resources; best-effort deprovision is idempotent.
if (!input.request.managedTunnelsEnabled) {
// The account bearer alone must not move an active link, its tunnel and its trusted key to
// another key. Unlinking first releases the tunnel.
const existing = yield* links.getForUser({
Comment thread
juliusmarminge marked this conversation as resolved.
Resolved
userId: input.userId,
environmentId: verified.environmentId,
});
if (existing !== null && existing.environmentPublicKey !== verified.environmentPublicKey) {
Comment thread
juliusmarminge marked this conversation as resolved.
Resolved
return yield* new EnvironmentLinkProofInvalid({
userId: input.userId,
environmentId: verified.environmentId,
reason: "environment_key_mismatch",
stage: "validate_existing_link",
});
}
// An unlink whose teardown failed leaves its tunnel behind, and provisioning would reuse it by
// name. Finish the teardown before any new link, publish-only too: once that link is active
// it could turn tunnels on and inherit the old one.
if (existing === null) {
const key = { userId: input.userId, environmentId: verified.environmentId };
const leftover = yield* managedEndpointProvider.prepareDeprovision(key);
if (!(yield* managedEndpointProvider.deprovision({ ...key, target: leftover }))) {
return yield* new ManagedEndpointProvider.ManagedEndpointDeprovisioningFailed({
...key,
stage: "claim-deprovision",
cause: "an earlier tunnel for this environment is still being torn down",
});
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// An active link going publish-only releases its own tunnel/DNS; best-effort deprovision is idempotent.
if (existing !== null && !input.request.managedTunnelsEnabled) {
yield* managedEndpointProvider
.deprovision({
userId: input.userId,
Expand Down
111 changes: 65 additions & 46 deletions infra/relay/src/http/Api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,52 +1081,71 @@ describe("relay managed tunnel recovery", () => {
});

describe("relay environment unlink", () => {
it.effect("drops the unlinked endpoint's held webhooks, even if clearing fails", () => {
const cleared: Array<string> = [];
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<string> = [];
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<string> = [];
Expand Down
19 changes: 13 additions & 6 deletions infra/relay/src/http/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading