diff --git a/apps/server/src/cloud/CloudLink.ts b/apps/server/src/cloud/CloudLink.ts index a4945e8696565..242c9a2473476 100644 --- a/apps/server/src/cloud/CloudLink.ts +++ b/apps/server/src/cloud/CloudLink.ts @@ -111,6 +111,7 @@ import { hasExactScope, hasForwardedAuthorityHeaders, isAllowedEndpointOrigin, + isLoopbackRequestUrl, isSupportedLinkProviderKind, linkProofScopes, managedEndpointRuntimeConfigsMatch, @@ -137,6 +138,12 @@ const RELAY_CONFIG_FIELD_MESSAGES = { cloudUserId: "Cloud user id is required.", cloudMintPublicKey: "Cloud mint public key must be a valid Ed25519 public key.", } as const; +const ORIGIN_INVALID_MESSAGES = { + endpoint: "Invalid managed endpoint origin.", + local: "Could not resolve local environment origin.", + remote: + "T3 Connect can only be turned on from the machine running this environment. On that machine, use Settings → Sharing & access or run `t3 connect link`.", +} as const; export class CloudLinkRelayConfigInvalidError extends Schema.TaggedError()( "CloudLinkRelayConfigInvalidError", @@ -157,12 +164,10 @@ export class CloudLinkRelayConfigInvalidError extends Schema.TaggedError()( "CloudLinkOriginInvalidError", - { origin: Schema.Literals(["endpoint", "local"]) }, + { origin: Schema.Literals(["endpoint", "local", "remote"]) }, ) { override get message(): string { - return this.origin === "endpoint" - ? "Invalid managed endpoint origin." - : "Could not resolve local environment origin."; + return ORIGIN_INVALID_MESSAGES[this.origin]; } } @@ -579,15 +584,10 @@ const make = Effect.gen(function* () { const makeCloudLinkProof = Effect.fn("environment.cloud.makeLinkProof")(function* ( request: RelayLinkProofRequest, requestUrl: string, - allowNonLoopbackRequestHost = false, ) { if ( !isSupportedLinkProviderKind(request) || - !isAllowedEndpointOrigin({ - origin: request.origin, - requestUrl, - allowNonLoopbackRequestHost, - }) + !isAllowedEndpointOrigin({ origin: request.origin, requestUrl }) ) { return yield* new CloudLinkOriginInvalidError({ origin: "endpoint" }); } @@ -628,16 +628,18 @@ const make = Effect.gen(function* () { const linkProof = Effect.fn("environment.cloud.linkProof")( function* (request: RelayLinkProofRequest, httpRequest: HttpServerRequest.HttpServerRequest) { const requestUrl = requestAbsoluteUrl(httpRequest); - if (requestUrl === null || hasForwardedAuthorityHeaders(httpRequest)) { + if (requestUrl === null) { return yield* new CloudLinkOriginInvalidError({ origin: "endpoint" }); } const principal = yield* EnvironmentAuthenticatedPrincipal; // WSL desktop requests use the distro IP; only the server-issued bootstrap subject qualifies. - return yield* makeCloudLinkProof( - request, - requestUrl, - principal.subject === "desktop-bootstrap", - ); + if ( + hasForwardedAuthorityHeaders(httpRequest) || + (principal.subject !== "desktop-bootstrap" && !isLoopbackRequestUrl(requestUrl)) + ) { + return yield* new CloudLinkOriginInvalidError({ origin: "remote" }); + } + return yield* makeCloudLinkProof(request, requestUrl); }, Effect.catchIf(ServerSecretStore.isSecretStoreError, internalError("generate-link-proof")), ); diff --git a/apps/server/src/cloud/http.test.ts b/apps/server/src/cloud/http.test.ts index 5d84c190a0f2a..6f9efaa17d63e 100644 --- a/apps/server/src/cloud/http.test.ts +++ b/apps/server/src/cloud/http.test.ts @@ -229,7 +229,7 @@ describe("link proof over a non-loopback desktop transport", () => { const rejected = yield* post("/api/connect/link-proof", payload, headers); expect(rejected.status).toBe(400); expect(yield* responseJson(rejected)).toMatchObject({ - message: "Invalid managed endpoint origin.", + message: expect.stringContaining("t3 connect link"), }); const loopback = yield* post( "/api/connect/link-proof", @@ -250,23 +250,27 @@ describe("link proof over a non-loopback desktop transport", () => { name: "non-loopback tunnel origin", origin: { localHttpHost: "172.24.162.233", localHttpPort: 3773 }, headers: {}, + message: "Invalid managed endpoint origin.", }, { name: "mismatched port", origin: { localHttpHost: "127.0.0.1", localHttpPort: 3774 }, headers: {}, + message: "Invalid managed endpoint origin.", }, { name: "forwarded host", origin: proofRequest.origin, headers: { "x-forwarded-host": "127.0.0.1:3773" }, + message: expect.stringContaining("t3 connect link"), }, { name: "forwarded protocol", origin: proofRequest.origin, headers: { "x-forwarded-proto": "http" }, + message: expect.stringContaining("t3 connect link"), }, - ])("rejects $name even for desktop bootstrap", ({ origin, headers }) => + ])("rejects $name even for desktop bootstrap", ({ origin, headers, message }) => Effect.gen(function* () { const { post, exchange } = yield* linkProofClient; const token = yield* exchange(DESKTOP_BOOTSTRAP_TOKEN); @@ -276,9 +280,7 @@ describe("link proof over a non-loopback desktop transport", () => { { ...headers, authorization: `Bearer ${token}` }, ); expect(response.status).toBe(400); - expect(yield* responseJson(response)).toMatchObject({ - message: "Invalid managed endpoint origin.", - }); + expect(yield* responseJson(response)).toMatchObject({ message }); }), ); diff --git a/apps/server/src/cloud/linkChecks.ts b/apps/server/src/cloud/linkChecks.ts index 607e2c08f8a53..b20567d6d3108 100644 --- a/apps/server/src/cloud/linkChecks.ts +++ b/apps/server/src/cloud/linkChecks.ts @@ -79,6 +79,10 @@ export function parseManagedEndpointLocalOrigin(localOrigin: string) { }; } +export function isLoopbackRequestUrl(requestUrl: string): boolean { + return isLoopbackHostname(new URL(requestUrl).hostname); +} + /** * Whether clients should offer to turn on T3 Connect over this session: only the desktop app's * own bootstrap session, from any address (WSL answers on a distro IP), never through a proxy. @@ -98,18 +102,11 @@ export function canLinkHere( export function isAllowedEndpointOrigin(input: { readonly origin: RelayManagedEndpointOrigin; readonly requestUrl: string; - readonly allowNonLoopbackRequestHost?: boolean; }): boolean { - if (!isLoopbackHostname(input.origin.localHttpHost)) { - return false; - } - - const url = new URL(input.requestUrl); - if (!input.allowNonLoopbackRequestHost && !isLoopbackHostname(url.hostname)) { - return false; - } - - return input.origin.localHttpPort === endpointRequestPort(url); + return ( + isLoopbackHostname(input.origin.localHttpHost) && + input.origin.localHttpPort === endpointRequestPort(new URL(input.requestUrl)) + ); } export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean {