fix(web): keep hosted pairing to secure endpoints
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
814c206bde
commit
a28ba8b083
5 files changed
+83
-25
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -906,7 +906,7 @@ function createPrimaryEnvironmentConnection(): EnvironmentConnection {
|
||||
}
|
||||
|
||||
function maybeCreatePrimaryEnvironmentConnection(): EnvironmentConnection | null {
|
||||
return getPrimaryKnownEnvironment() ? createPrimaryEnvironmentConnection() : null;
|
||||
return getPrimaryKnownEnvironment()?.environmentId ? createPrimaryEnvironmentConnection() : null;
|
||||
}
|
||||
|
||||
async function ensureSavedEnvironmentConnection(
|
||||
|
||||
Reference in new issue
Block a user