Skip to content
Closed
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
34 changes: 18 additions & 16 deletions apps/server/src/cloud/CloudLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import {
hasExactScope,
hasForwardedAuthorityHeaders,
isAllowedEndpointOrigin,
isLoopbackRequestUrl,
isSupportedLinkProviderKind,
linkProofScopes,
managedEndpointRuntimeConfigsMatch,
Expand All @@ -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>()(
"CloudLinkRelayConfigInvalidError",
Expand All @@ -157,12 +164,10 @@ export class CloudLinkRelayConfigInvalidError extends Schema.TaggedError<CloudLi

export class CloudLinkOriginInvalidError extends Schema.TaggedError<CloudLinkOriginInvalidError>()(
"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];
}
}

Expand Down Expand Up @@ -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" });
}
Expand Down Expand Up @@ -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")),
);
Expand Down
12 changes: 7 additions & 5 deletions apps/server/src/cloud/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand All @@ -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 });
}),
);

Expand Down
19 changes: 8 additions & 11 deletions apps/server/src/cloud/linkChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Loading