From a28ba8b08319eade731adc67f353fb92020e91c6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 26 Apr 2026 19:43:08 -0700 Subject: [PATCH] fix(web): keep hosted pairing to secure endpoints Co-authored-by: codex --- .../settings/ConnectionsSettings.tsx | 15 +----- .../components/settings/pairingUrls.test.ts | 24 ++++++++++ .../src/components/settings/pairingUrls.ts | 20 ++++++++ .../service.threadSubscriptions.test.ts | 47 +++++++++++++++---- apps/web/src/environments/runtime/service.ts | 2 +- 5 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 apps/web/src/components/settings/pairingUrls.test.ts create mode 100644 apps/web/src/components/settings/pairingUrls.ts diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 50af9f8859..f6ab384d9f 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -9,9 +9,9 @@ import { import { DateTime } from "effect"; import { useCopyToClipboard } from "../../hooks/useCopyToClipboard"; -import { buildHostedPairingUrl } from "../../hostedPairing"; import { cn } from "../../lib/utils"; import { formatElapsedDurationLabel, formatExpiresInLabel } from "../../timestampFormat"; +import { resolveDesktopPairingUrl, resolveHostedPairingUrl } from "./pairingUrls"; import { SettingsPageContainer, SettingsRow, @@ -244,19 +244,6 @@ function removeDesktopClientSession( return current.filter((clientSession) => clientSession.sessionId !== sessionId); } -function resolveDesktopPairingUrl(endpointUrl: string, credential: string): string { - const url = new URL(endpointUrl); - url.pathname = "/pair"; - return setPairingTokenOnUrl(url, credential).toString(); -} - -function resolveHostedPairingUrl(endpointUrl: string, credential: string): string { - return buildHostedPairingUrl({ - host: endpointUrl, - token: credential, - }); -} - function resolveCurrentOriginPairingUrl(credential: string): string { const url = new URL("/pair", window.location.href); return setPairingTokenOnUrl(url, credential).toString(); diff --git a/apps/web/src/components/settings/pairingUrls.test.ts b/apps/web/src/components/settings/pairingUrls.test.ts new file mode 100644 index 0000000000..b32f5c51b3 --- /dev/null +++ b/apps/web/src/components/settings/pairingUrls.test.ts @@ -0,0 +1,24 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { resolveDesktopPairingUrl, resolveHostedPairingUrl } from "./pairingUrls"; + +describe("settings pairing URL helpers", () => { + afterEach(() => { + vi.unstubAllEnvs(); + }); + + it("uses direct backend pairing URLs for HTTP endpoints", () => { + expect(resolveHostedPairingUrl("http://192.168.1.44:3773", "PAIRCODE")).toBeNull(); + expect(resolveDesktopPairingUrl("http://192.168.1.44:3773", "PAIRCODE")).toBe( + "http://192.168.1.44:3773/pair#token=PAIRCODE", + ); + }); + + it("uses hosted pairing URLs for HTTPS endpoints", () => { + vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes"); + + expect(resolveHostedPairingUrl("https://host.tailnet.example.ts.net:3773", "PAIRCODE")).toBe( + "https://preview.t3.codes/pair?host=https%3A%2F%2Fhost.tailnet.example.ts.net%3A3773#token=PAIRCODE", + ); + }); +}); diff --git a/apps/web/src/components/settings/pairingUrls.ts b/apps/web/src/components/settings/pairingUrls.ts new file mode 100644 index 0000000000..891fe04ad6 --- /dev/null +++ b/apps/web/src/components/settings/pairingUrls.ts @@ -0,0 +1,20 @@ +import { buildHostedPairingUrl } from "../../hostedPairing"; +import { setPairingTokenOnUrl } from "../../pairingUrl"; + +export function resolveDesktopPairingUrl(endpointUrl: string, credential: string): string { + const url = new URL(endpointUrl); + url.pathname = "/pair"; + return setPairingTokenOnUrl(url, credential).toString(); +} + +export function resolveHostedPairingUrl(endpointUrl: string, credential: string): string | null { + const url = new URL(endpointUrl); + if (url.protocol !== "https:") { + return null; + } + + return buildHostedPairingUrl({ + host: endpointUrl, + token: credential, + }); +} diff --git a/apps/web/src/environments/runtime/service.threadSubscriptions.test.ts b/apps/web/src/environments/runtime/service.threadSubscriptions.test.ts index 5f169adc9b..83c9649283 100644 --- a/apps/web/src/environments/runtime/service.threadSubscriptions.test.ts +++ b/apps/web/src/environments/runtime/service.threadSubscriptions.test.ts @@ -15,22 +15,14 @@ const mockCreateWsRpcClient = vi.fn(); const mockWaitForSavedEnvironmentRegistryHydration = vi.fn(); const mockListSavedEnvironmentRecords = vi.fn(); const mockSavedEnvironmentRegistrySubscribe = vi.fn(); +const mockGetPrimaryKnownEnvironment = vi.hoisted(() => vi.fn()); function MockWsTransport() { return undefined; } vi.mock("../primary", () => ({ - getPrimaryKnownEnvironment: vi.fn(() => ({ - id: "env-1", - label: "Primary environment", - source: "window-origin", - target: { - httpBaseUrl: "http://127.0.0.1:3000/", - wsBaseUrl: "ws://127.0.0.1:3000/", - }, - environmentId: EnvironmentId.make("env-1"), - })), + getPrimaryKnownEnvironment: mockGetPrimaryKnownEnvironment, })); vi.mock("./catalog", () => ({ @@ -144,6 +136,16 @@ describe("retainThreadDetailSubscription", () => { vi.useFakeTimers(); vi.resetModules(); vi.clearAllMocks(); + mockGetPrimaryKnownEnvironment.mockReturnValue({ + id: "env-1", + label: "Primary environment", + source: "window-origin", + target: { + httpBaseUrl: "http://127.0.0.1:3000/", + wsBaseUrl: "ws://127.0.0.1:3000/", + }, + environmentId: EnvironmentId.make("env-1"), + }); mockThreadUnsubscribe.mockImplementation(() => undefined); mockSubscribeThread.mockImplementation(() => mockThreadUnsubscribe); @@ -203,6 +205,31 @@ describe("retainThreadDetailSubscription", () => { await resetEnvironmentServiceForTests(); }); + it("does not start the primary connection until the known environment has an id", async () => { + mockGetPrimaryKnownEnvironment.mockReturnValue({ + id: "env-1", + label: "Primary environment", + source: "window-origin", + target: { + httpBaseUrl: "http://127.0.0.1:3000/", + wsBaseUrl: "ws://127.0.0.1:3000/", + }, + }); + const { + listEnvironmentConnections, + resetEnvironmentServiceForTests, + startEnvironmentConnectionService, + } = await import("./service"); + + const stop = startEnvironmentConnectionService(new QueryClient()); + + expect(mockCreateEnvironmentConnection).not.toHaveBeenCalled(); + expect(listEnvironmentConnections()).toEqual([]); + + stop(); + await resetEnvironmentServiceForTests(); + }); + it("keeps non-idle thread detail subscriptions attached until the thread becomes idle", async () => { const { retainThreadDetailSubscription, diff --git a/apps/web/src/environments/runtime/service.ts b/apps/web/src/environments/runtime/service.ts index ec920e9a7b..6c58f9e8a5 100644 --- a/apps/web/src/environments/runtime/service.ts +++ b/apps/web/src/environments/runtime/service.ts @@ -906,7 +906,7 @@ function createPrimaryEnvironmentConnection(): EnvironmentConnection { } function maybeCreatePrimaryEnvironmentConnection(): EnvironmentConnection | null { - return getPrimaryKnownEnvironment() ? createPrimaryEnvironmentConnection() : null; + return getPrimaryKnownEnvironment()?.environmentId ? createPrimaryEnvironmentConnection() : null; } async function ensureSavedEnvironmentConnection(