From df12a51a6ca3fd00f03ddd0a3e0509ce718826fa Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Sat, 7 Nov 2026 14:30:53 +0100 Subject: [PATCH 01/29] feat(relay): self-hosted T3 Connect relay with OIDC sign-in (WIP) --- .dockerignore | 12 + apps/desktop/src/app/DesktopClerk.ts | 21 +- apps/mobile/app.config.ts | 4 + .../features/cloud/CloudAuthProvider.test.ts | 1 + .../src/features/cloud/CloudAuthProvider.tsx | 73 ++- .../cloud/ConnectOnboardingRouteScreen.tsx | 4 +- .../src/features/cloud/cloudAccountService.ts | 68 +- apps/mobile/src/features/cloud/cloudAuth.tsx | 13 + .../src/features/cloud/oidc/mobileOidc.ts | 70 ++ .../src/features/cloud/publicConfig.test.ts | 18 +- .../mobile/src/features/cloud/publicConfig.ts | 20 +- .../connection/CloudEnvironmentRows.tsx | 4 +- .../connection/ConnectionsNewRouteScreen.tsx | 4 +- .../settings/SettingsAuthRouteScreen.tsx | 63 +- .../SettingsNotificationsRouteScreen.tsx | 10 +- .../features/settings/SettingsRouteScreen.tsx | 7 +- apps/server/src/cli/connect.ts | 2 +- apps/server/src/cloud/CliTokenManager.test.ts | 120 +++- apps/server/src/cloud/CliTokenManager.ts | 125 +++- apps/server/src/cloud/CloudLink.ts | 20 +- apps/server/src/cloud/linkChecks.test.ts | 30 +- apps/server/src/cloud/linkChecks.ts | 22 + apps/server/src/cloud/publicConfig.test.ts | 24 + apps/server/src/cloud/publicConfig.ts | 46 +- apps/server/vite.config.ts | 4 + apps/web/src/cloud/cloudAuth.tsx | 47 ++ apps/web/src/cloud/managedAuth.tsx | 53 +- apps/web/src/cloud/oidc/browserOidc.ts | 69 ++ apps/web/src/cloud/publicConfig.test.ts | 28 +- apps/web/src/cloud/publicConfig.ts | 26 + .../useCloudLinkController.browser.test.tsx | 8 +- apps/web/src/cloud/useCloudLinkController.ts | 6 +- apps/web/src/components/HostedEmptyState.tsx | 4 +- .../clerk/BrowserManagedAuthShell.tsx | 5 +- .../components/clerk/ClerkCloudAuthBridge.tsx | 38 ++ .../clerk/ElectronManagedAuthShell.tsx | 5 +- .../RemoveT3ConnectEnvironmentDialog.tsx | 2 +- .../clerk/T3ConnectAccountPages.tsx | 2 +- .../clerk/T3ConnectSidebarSignIn.tsx | 42 +- .../clerk/useT3ConnectAuthPrompt.tsx | 12 +- .../cloud/ConnectOnboardingDialog.tsx | 11 +- .../desktop/DesktopWebLinkCoordinator.tsx | 3 + .../components/oidc/OidcCallbackSurface.tsx | 57 ++ .../components/oidc/OidcManagedAuthShell.tsx | 73 +++ .../src/components/onboarding/WelcomePage.tsx | 4 +- .../components/settings/LabsSettingsPanel.tsx | 8 +- .../settings/SavedEnvironmentRows.tsx | 2 +- apps/web/src/main.tsx | 42 +- apps/web/src/routeTree.gen.ts | 21 + apps/web/src/routes/__root.tsx | 3 +- apps/web/src/routes/oidc.callback.tsx | 7 + apps/web/vite.config.ts | 4 + infra/relay/selfhost/.env.example | 22 + infra/relay/selfhost/.gitignore | 2 + infra/relay/selfhost/Caddyfile | 5 + infra/relay/selfhost/Dockerfile | 22 + infra/relay/selfhost/README.md | 127 ++++ infra/relay/selfhost/docker-compose.yml | 79 +++ infra/relay/selfhost/gen-keys.sh | 13 + infra/relay/src/Config.ts | 24 +- .../agentActivity/ApnsDeliveryQueue.test.ts | 13 +- .../src/agentActivity/ApnsDeliveryQueue.ts | 26 +- .../src/agentActivity/FcmDeliveries.test.ts | 52 +- .../agentActivity/FcmDeliveryQueueConsumer.ts | 19 +- .../agentActivity/FcmDeliveryQueueSender.ts | 7 +- infra/relay/src/auth/SubjectTokens.test.ts | 232 +++++++ infra/relay/src/auth/SubjectTokens.ts | 220 +++++++ infra/relay/src/composition.ts | 405 ++++++++++++ .../environments/EnvironmentConnector.test.ts | 96 ++- .../src/environments/EnvironmentConnector.ts | 62 +- .../environments/ManagedEndpointProvider.ts | 302 ++++----- infra/relay/src/host/RelayHost.ts | 68 ++ infra/relay/src/host/node.test.ts | 92 +++ infra/relay/src/host/node.ts | 248 ++++++++ infra/relay/src/host/ports.ts | 48 ++ infra/relay/src/http/Api.test.ts | 10 +- infra/relay/src/http/Api.ts | 101 +-- infra/relay/src/node.ts | 102 +++ infra/relay/src/t3chat/T3ChatAccounts.ts | 3 + infra/relay/src/worker.ts | 597 +++++------------- infra/relay/vite.config.ts | 9 + knip.jsonc | 4 +- .../src/connection/errors.test.ts | 5 + .../src/relay/cloudAuthState.ts | 23 + .../src/relay/errorPresentation.ts | 3 + packages/client-runtime/src/relay/index.ts | 2 + .../src/relay/oidcController.test.ts | 249 ++++++++ .../src/relay/oidcController.ts | 261 ++++++++ packages/contracts/src/relay.ts | 1 + packages/shared/package.json | 4 + .../shared/src/hostClassification.test.ts | 34 +- packages/shared/src/hostClassification.ts | 13 + packages/shared/src/oidc.test.ts | 212 +++++++ packages/shared/src/oidc.ts | 318 ++++++++++ scripts/lib/public-config.test.ts | 6 +- scripts/lib/public-config.ts | 28 + 96 files changed, 4561 insertions(+), 975 deletions(-) create mode 100644 .dockerignore create mode 100644 apps/mobile/src/features/cloud/cloudAuth.tsx create mode 100644 apps/mobile/src/features/cloud/oidc/mobileOidc.ts create mode 100644 apps/web/src/cloud/cloudAuth.tsx create mode 100644 apps/web/src/cloud/oidc/browserOidc.ts create mode 100644 apps/web/src/components/clerk/ClerkCloudAuthBridge.tsx create mode 100644 apps/web/src/components/oidc/OidcCallbackSurface.tsx create mode 100644 apps/web/src/components/oidc/OidcManagedAuthShell.tsx create mode 100644 apps/web/src/routes/oidc.callback.tsx create mode 100644 infra/relay/selfhost/.env.example create mode 100644 infra/relay/selfhost/.gitignore create mode 100644 infra/relay/selfhost/Caddyfile create mode 100644 infra/relay/selfhost/Dockerfile create mode 100644 infra/relay/selfhost/README.md create mode 100644 infra/relay/selfhost/docker-compose.yml create mode 100644 infra/relay/selfhost/gen-keys.sh create mode 100644 infra/relay/src/auth/SubjectTokens.test.ts create mode 100644 infra/relay/src/auth/SubjectTokens.ts create mode 100644 infra/relay/src/composition.ts create mode 100644 infra/relay/src/host/RelayHost.ts create mode 100644 infra/relay/src/host/node.test.ts create mode 100644 infra/relay/src/host/node.ts create mode 100644 infra/relay/src/host/ports.ts create mode 100644 infra/relay/src/node.ts create mode 100644 infra/relay/vite.config.ts create mode 100644 packages/client-runtime/src/relay/cloudAuthState.ts create mode 100644 packages/client-runtime/src/relay/oidcController.test.ts create mode 100644 packages/client-runtime/src/relay/oidcController.ts create mode 100644 packages/shared/src/oidc.test.ts create mode 100644 packages/shared/src/oidc.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000..42acc457d0568 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +**/node_modules +.git +.t3 +.repos +.env +.env.* +**/.env +infra/relay/selfhost/secrets +infra/relay/selfhost/.env +**/dist +**/dist-* +**/*.log diff --git a/apps/desktop/src/app/DesktopClerk.ts b/apps/desktop/src/app/DesktopClerk.ts index ef9d2aad56795..95a4ec2df61dd 100644 --- a/apps/desktop/src/app/DesktopClerk.ts +++ b/apps/desktop/src/app/DesktopClerk.ts @@ -12,6 +12,7 @@ import { codexAuthDeliveryUrl, readCodexAuthHandoff } from "@t3tools/shared/code import { receiveCodexAuthCallback, CodexAuthCallbackError } from "./CodexAuthCallback.ts"; import * as ElectronShell from "../electron/ElectronShell.ts"; import { providerAuthReturnUrl } from "@t3tools/shared/providerAuthReturnUrl"; +import { isDesktopOidcCallbackUrl } from "@t3tools/shared/oidc"; import { promptLinkAppPath } from "@t3tools/shared/promptLinks"; import { readPairingAppLink } from "@t3tools/shared/remote"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -210,9 +211,25 @@ export const make = Effect.gen(function* () { return true; }; const openAppLink = (value: string | undefined) => { - if (!value || (promptLinkAppPath(value) === null && readPairingAppLink(value) === null)) + if ( + !value || + (promptLinkAppPath(value) === null && + readPairingAppLink(value) === null && + !isDesktopOidcCallbackUrl(value)) + ) return false; - void runPromise(webLinks.receive(value)); + void runPromise( + webLinks.receive(value).pipe( + Effect.andThen( + Effect.gen(function* () { + // The identity provider left the user in the system browser. + if (!isDesktopOidcCallbackUrl(value)) return; + const mainWindow = yield* electronWindow.currentMainOrFirst; + if (Option.isSome(mainWindow)) yield* electronWindow.reveal(mainWindow.value); + }), + ), + ), + ); return true; }; const args = yield* HostProcess.Arguments; diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts index 97fe5cf977acc..5fcb9d028d6a2 100644 --- a/apps/mobile/app.config.ts +++ b/apps/mobile/app.config.ts @@ -475,6 +475,10 @@ const config: ExpoConfig = { publishableKey: repoEnv.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY ?? null, jwtTemplate: repoEnv.EXPO_PUBLIC_CLERK_JWT_TEMPLATE ?? null, }, + oidc: { + issuer: repoEnv.EXPO_PUBLIC_OIDC_ISSUER ?? null, + clientId: repoEnv.EXPO_PUBLIC_OIDC_CLIENT_ID ?? null, + }, // Clerk reads these exact names at prebuild. Omit unset values: manifest null becomes truthy {}. EXPO_PUBLIC_CLERK_GOOGLE_WEB_CLIENT_ID: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_WEB_CLIENT_ID, EXPO_PUBLIC_CLERK_GOOGLE_IOS_CLIENT_ID: repoEnv.EXPO_PUBLIC_CLERK_GOOGLE_IOS_CLIENT_ID, diff --git a/apps/mobile/src/features/cloud/CloudAuthProvider.test.ts b/apps/mobile/src/features/cloud/CloudAuthProvider.test.ts index de4726f06d276..d815d0092aee5 100644 --- a/apps/mobile/src/features/cloud/CloudAuthProvider.test.ts +++ b/apps/mobile/src/features/cloud/CloudAuthProvider.test.ts @@ -45,6 +45,7 @@ vi.mock("../../state/use-composer-drafts", () => ({ vi.mock("./publicConfig", () => ({ resolveCloudPublicConfig: vi.fn(() => ({ clerk: { publishableKey: null }, + oidc: { issuer: null, clientId: null }, relay: { url: null }, })), resolveRelayClerkTokenOptions: vi.fn(), diff --git a/apps/mobile/src/features/cloud/CloudAuthProvider.tsx b/apps/mobile/src/features/cloud/CloudAuthProvider.tsx index df20ef805cd1d..c818374746099 100644 --- a/apps/mobile/src/features/cloud/CloudAuthProvider.tsx +++ b/apps/mobile/src/features/cloud/CloudAuthProvider.tsx @@ -1,15 +1,34 @@ -import { ClerkProvider, getClerkInstance } from "@clerk/expo"; +import { ClerkProvider, getClerkInstance, useAuth, useUser } from "@clerk/expo"; import { tokenCache } from "@clerk/expo/token-cache"; -import type { ReactNode } from "react"; +import type { OidcController } from "@t3tools/client-runtime/relay"; +import { type ReactNode, useSyncExternalStore } from "react"; -import { cloudAccountService, deactivateCloudRelayAccount } from "./cloudAccountService"; -import { resolveCloudPublicConfig } from "./publicConfig"; +import { + cloudAccountService, + createCloudRelayAccountService, + deactivateCloudRelayAccount, +} from "./cloudAccountService"; +import { CloudAuthContext, type CloudAuth } from "./cloudAuth"; +import { getMobileOidcController } from "./oidc/mobileOidc"; +import { + cloudAuthProvider, + resolveCloudPublicConfig, + resolveRelayClerkTokenOptions, +} from "./publicConfig"; const cloudConfig = resolveCloudPublicConfig(); -// Build-time constant: null when this build has no cloud config. -const publishableKey = cloudConfig.relay.url ? cloudConfig.clerk.publishableKey : null; +const provider = cloudAuthProvider(cloudConfig); +const oidcController = provider === "oidc" ? getMobileOidcController() : null; +// Build-time constant: null when this build has no Clerk config, or uses an OIDC provider instead. +const publishableKey = + provider !== "oidc" && cloudConfig.relay.url ? cloudConfig.clerk.publishableKey : null; -if (publishableKey) { +if (oidcController) { + // The ID token is what the relay verifies, so that is the token the account service hands out. + createCloudRelayAccountService((source) => source.getToken).attach( + oidcController, + ); +} else if (publishableKey) { // The same native singleton ClerkProvider renders with, so the service // observes it for the app's lifetime without a React subscription. cloudAccountService.attach(getClerkInstance({ publishableKey })); @@ -19,14 +38,52 @@ if (publishableKey) { deactivateCloudRelayAccount(); } +function OidcAuthBridge(props: { + readonly controller: OidcController; + readonly children: ReactNode; +}) { + const snapshot = useSyncExternalStore(props.controller.subscribe, props.controller.getSnapshot); + const value: CloudAuth = { + provider: "oidc", + isLoaded: true, + isSignedIn: snapshot.userId !== null, + userId: snapshot.userId, + accountLabel: snapshot.label, + getToken: props.controller.getToken, + signOut: props.controller.signOut, + }; + return {props.children}; +} + +function ClerkAuthBridge(props: { readonly children: ReactNode }) { + // A session with steps pending must not look signed out, or activation would look like a new sign-in. + const { isLoaded, isSignedIn, userId, getToken, signOut } = useAuth({ + treatPendingAsSignedOut: false, + }); + const { user } = useUser(); + const value: CloudAuth = { + provider: "clerk", + isLoaded, + isSignedIn: isSignedIn === true, + userId: userId ?? null, + accountLabel: user?.primaryEmailAddress?.emailAddress ?? null, + getToken: () => getToken(resolveRelayClerkTokenOptions()), + signOut: () => signOut(), + }; + return {props.children}; +} + export function CloudAuthProvider(props: { readonly children: ReactNode }) { + if (oidcController) { + return {props.children}; + } if (!publishableKey) { return props.children; } return ( - {props.children} + {props.children} ); } diff --git a/apps/mobile/src/features/cloud/ConnectOnboardingRouteScreen.tsx b/apps/mobile/src/features/cloud/ConnectOnboardingRouteScreen.tsx index 463c11aaf6485..0b384f2592abf 100644 --- a/apps/mobile/src/features/cloud/ConnectOnboardingRouteScreen.tsx +++ b/apps/mobile/src/features/cloud/ConnectOnboardingRouteScreen.tsx @@ -2,7 +2,6 @@ import { AppBar } from "../../components/ui/AppBar"; import { Button } from "../../components/ui/Button"; import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; -import { useAuth } from "@clerk/expo"; import { useNavigation } from "@react-navigation/native"; import { useState } from "react"; import { RefreshControl, View } from "react-native"; @@ -15,6 +14,7 @@ import { CloudEnvironmentRows } from "../connection/CloudEnvironmentRows"; import { splitEnvironmentSections } from "../connection/environmentSections"; import { useConnectionController } from "../connection/useConnectionController"; import { optOutOfConnectOnboarding } from "./connectOnboardingOptOut"; +import { useCloudAuth } from "./cloudAuth"; import { hasCloudPublicConfig } from "./publicConfig"; // Only reachable with cloud config: onboarding is requested after a T3 Connect @@ -26,7 +26,7 @@ export function ConnectOnboardingRouteScreen() { function ConfiguredConnectOnboardingRouteScreen() { const navigation = useNavigation(); const insets = useSafeAreaInsets(); - const { isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false }); + const { isSignedIn, userId } = useCloudAuth(); const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } = useRemoteConnections(); const { refreshRelayEnvironments } = useConnectionController(); diff --git a/apps/mobile/src/features/cloud/cloudAccountService.ts b/apps/mobile/src/features/cloud/cloudAccountService.ts index aa7bff16b1548..ee2410fb8e36d 100644 --- a/apps/mobile/src/features/cloud/cloudAccountService.ts +++ b/apps/mobile/src/features/cloud/cloudAccountService.ts @@ -1,5 +1,6 @@ import type { getClerkInstance } from "@clerk/expo"; import { + type CloudAccountSource, createCloudAccountService, ManagedRelay, setManagedRelaySession, @@ -50,8 +51,9 @@ function clerkTokenReader(clerk: ClerkInstance): () => Promise { clerk.session ? await clerk.session.getToken(resolveRelayClerkTokenOptions()) : null; } -const cleanUpCloudAccount = async ( - clerk: ClerkInstance, +const cleanUpCloudAccount = async ( + readToken: (source: S) => () => Promise, + source: S, accountId: string | null, wasActive: boolean, ) => { @@ -69,9 +71,7 @@ const cleanUpCloudAccount = async ( ...(wasActive ? [ settleAsyncResult(() => - runtime.runPromiseExit( - unregisterAgentAwarenessDeviceForCurrentUser(clerkTokenReader(clerk)), - ), + runtime.runPromiseExit(unregisterAgentAwarenessDeviceForCurrentUser(readToken(source))), ), ] : []), @@ -81,28 +81,36 @@ const cleanUpCloudAccount = async ( } }; -export const cloudAccountService = createCloudAccountService({ - deactivate: deactivateCloudRelayAccount, - // Detaching is not a sign-out: the user is usually still signed in, so keep - // lock-screen activities and the persisted registration for the next attach. - detach: () => { - releaseAgentAwarenessRelayTokenProvider(); - setManagedRelaySession(appAtomRegistry, null); - }, - // A not-yet-presented onboarding request belongs to the account that left. - onSignOutTransition: clearConnectOnboardingRequest, - cleanUp: ({ source, accountId, wasActive }) => cleanUpCloudAccount(source, accountId, wasActive), - prepare: async ({ source, accountId, isCurrent }) => { - const storedAccount = await getComposerCloudAccountId(); - if (storedAccount !== null && storedAccount !== accountId) { - await cleanUpCloudAccount(source, storedAccount, false); - } - if (!isCurrent()) return; - await restoreCloudComposerDrafts(accountId); - }, - activate: ({ source, accountId, isAccountTransition }) => { - activateCloudRelayAccount(accountId, clerkTokenReader(source)); - // Issued after cleanup so the sheet never lists the previous account's environments. - if (isAccountTransition) requestConnectOnboarding(accountId); - }, -}); +/** Shared by every sign-in provider: only how a source yields its relay token differs. */ +export function createCloudRelayAccountService( + readToken: (source: S) => () => Promise, +) { + return createCloudAccountService({ + deactivate: deactivateCloudRelayAccount, + // Detaching is not a sign-out: the user is usually still signed in, so keep + // lock-screen activities and the persisted registration for the next attach. + detach: () => { + releaseAgentAwarenessRelayTokenProvider(); + setManagedRelaySession(appAtomRegistry, null); + }, + // A not-yet-presented onboarding request belongs to the account that left. + onSignOutTransition: clearConnectOnboardingRequest, + cleanUp: ({ source, accountId, wasActive }) => + cleanUpCloudAccount(readToken, source, accountId, wasActive), + prepare: async ({ source, accountId, isCurrent }) => { + const storedAccount = await getComposerCloudAccountId(); + if (storedAccount !== null && storedAccount !== accountId) { + await cleanUpCloudAccount(readToken, source, storedAccount, false); + } + if (!isCurrent()) return; + await restoreCloudComposerDrafts(accountId); + }, + activate: ({ source, accountId, isAccountTransition }) => { + activateCloudRelayAccount(accountId, readToken(source)); + // Issued after cleanup so the sheet never lists the previous account's environments. + if (isAccountTransition) requestConnectOnboarding(accountId); + }, + }); +} + +export const cloudAccountService = createCloudRelayAccountService(clerkTokenReader); diff --git a/apps/mobile/src/features/cloud/cloudAuth.tsx b/apps/mobile/src/features/cloud/cloudAuth.tsx new file mode 100644 index 0000000000000..9c2cb2097df8b --- /dev/null +++ b/apps/mobile/src/features/cloud/cloudAuth.tsx @@ -0,0 +1,13 @@ +import { SIGNED_OUT_CLOUD_AUTH, type CloudAuthState } from "@t3tools/client-runtime/relay"; +import { createContext, useContext } from "react"; + +/** Screens read this instead of a provider's own hooks. */ +export interface CloudAuth extends CloudAuthState { + readonly signOut: () => Promise; +} + +const SIGNED_OUT: CloudAuth = { ...SIGNED_OUT_CLOUD_AUTH, signOut: async () => {} }; + +export const CloudAuthContext = createContext(SIGNED_OUT); + +export const useCloudAuth = (): CloudAuth => useContext(CloudAuthContext); diff --git a/apps/mobile/src/features/cloud/oidc/mobileOidc.ts b/apps/mobile/src/features/cloud/oidc/mobileOidc.ts new file mode 100644 index 0000000000000..928380b80b2d3 --- /dev/null +++ b/apps/mobile/src/features/cloud/oidc/mobileOidc.ts @@ -0,0 +1,70 @@ +import { + createOidcController, + type KeyValueStore, + type OidcController, +} from "@t3tools/client-runtime/relay"; +import * as AuthSession from "expo-auth-session"; +import * as SecureStore from "expo-secure-store"; +import * as WebBrowser from "expo-web-browser"; + +import { resolveCloudPublicConfig } from "../publicConfig"; + +// SecureStore keys allow only letters, digits, ".", "-" and "_". +const secureKey = (key: string) => key.replace(/[^A-Za-z0-9._-]/gu, "_"); + +// SecureStore deletes asynchronously, so the map answers until the keychain catches up. +function secureSessionStore(): KeyValueStore { + const shadow = new Map(); + return { + getItem: (key) => (shadow.has(key) ? shadow.get(key)! : SecureStore.getItem(secureKey(key))), + setItem: (key, value) => { + shadow.set(key, value); + SecureStore.setItem(secureKey(key), value); + }, + removeItem: (key) => { + shadow.set(key, null); + void SecureStore.deleteItemAsync(secureKey(key)); + }, + }; +} + +function memoryStore(): KeyValueStore { + const entries = new Map(); + return { + getItem: (key) => entries.get(key) ?? null, + setItem: (key, value) => void entries.set(key, value), + removeItem: (key) => void entries.delete(key), + }; +} + +const redirectUri = AuthSession.makeRedirectUri({ path: "oidc" }); + +let controller: OidcController | null | undefined; + +/** The one controller for this app; null when the build has no OpenID Connect provider. */ +export function getMobileOidcController(): OidcController | null { + if (controller !== undefined) return controller; + const { oidc } = resolveCloudPublicConfig(); + controller = + oidc.issuer && oidc.clientId + ? createOidcController({ + config: { issuer: oidc.issuer, clientId: oidc.clientId }, + redirectUri, + sessions: secureSessionStore(), + pending: memoryStore(), + navigate: () => { + throw new Error("The mobile app signs in through an in-app browser."); + }, + }) + : null; + return controller; +} + +/** Shows the provider's sign-in page in the system browser sheet and finishes the sign-in. */ +export function signInWithBrowser(oidc: OidcController): Promise { + return oidc.signInWithPrompt(async (authorizeUrl) => { + const result = await WebBrowser.openAuthSessionAsync(authorizeUrl, redirectUri); + if (result.type !== "success") throw new Error("Sign-in was cancelled."); + return result.url; + }); +} diff --git a/apps/mobile/src/features/cloud/publicConfig.test.ts b/apps/mobile/src/features/cloud/publicConfig.test.ts index 8c09819aa72f3..d4df6151df977 100644 --- a/apps/mobile/src/features/cloud/publicConfig.test.ts +++ b/apps/mobile/src/features/cloud/publicConfig.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vite-plus/test"; -import { resolveCloudPublicConfig } from "./publicConfig"; +import { cloudAuthProvider, resolveCloudPublicConfig } from "./publicConfig"; vi.mock("expo-constants", () => ({ default: { @@ -22,6 +22,7 @@ describe("resolveCloudPublicConfig", () => { publishableKey: "pk_test_example", jwtTemplate: "t3-relay", }, + oidc: { issuer: null, clientId: null }, relay: { url: null, }, @@ -32,4 +33,19 @@ describe("resolveCloudPublicConfig", () => { }, }); }); + + it("prefers an OpenID Connect provider over Clerk and still needs a secure relay", () => { + const configured = resolveCloudPublicConfig({ + clerk: { publishableKey: "pk_test_example", jwtTemplate: "t3-relay" }, + oidc: { issuer: "https://sso.example.test/realms/t3", clientId: "t3-code" }, + relay: { url: "https://relay.example.test" }, + }); + expect(cloudAuthProvider(configured)).toBe("oidc"); + + const insecure = resolveCloudPublicConfig({ + oidc: { issuer: "https://sso.example.test/realms/t3", clientId: "t3-code" }, + relay: { url: "http://relay.example.test" }, + }); + expect(cloudAuthProvider(insecure)).toBeNull(); + }); }); diff --git a/apps/mobile/src/features/cloud/publicConfig.ts b/apps/mobile/src/features/cloud/publicConfig.ts index a91a8024d4851..e016f649bcdb2 100644 --- a/apps/mobile/src/features/cloud/publicConfig.ts +++ b/apps/mobile/src/features/cloud/publicConfig.ts @@ -19,6 +19,10 @@ export interface CloudPublicConfig { readonly publishableKey: string | null; readonly jwtTemplate: string | null; }; + readonly oidc: { + readonly issuer: string | null; + readonly clientId: string | null; + }; readonly relay: { readonly url: string | null; }; @@ -62,6 +66,10 @@ export function resolveCloudPublicConfig(extra: ExpoExtra = Constants.expoConfig publishableKey: trimNonEmpty(extra?.clerk?.publishableKey), jwtTemplate: trimNonEmpty(extra?.clerk?.jwtTemplate), }, + oidc: { + issuer: trimNonEmpty(extra?.oidc?.issuer), + clientId: trimNonEmpty(extra?.oidc?.clientId), + }, relay: { url: normalizeSecureRelayUrl(trimNonEmpty(extra?.relay?.url) ?? ""), }, @@ -73,9 +81,17 @@ export function resolveCloudPublicConfig(extra: ExpoExtra = Constants.expoConfig } satisfies CloudPublicConfig; } +/** An OpenID Connect provider takes the place of Clerk when the build configures one. */ +export function cloudAuthProvider( + config: CloudPublicConfig = resolveCloudPublicConfig(), +): "oidc" | "clerk" | null { + if (!config.relay.url) return null; + if (config.oidc.issuer && config.oidc.clientId) return "oidc"; + return config.clerk.publishableKey && config.clerk.jwtTemplate ? "clerk" : null; +} + export function hasCloudPublicConfig(): boolean { - const config = resolveCloudPublicConfig(); - return Boolean(config.clerk.publishableKey && config.clerk.jwtTemplate && config.relay.url); + return cloudAuthProvider() !== null; } type Configured = { diff --git a/apps/mobile/src/features/connection/CloudEnvironmentRows.tsx b/apps/mobile/src/features/connection/CloudEnvironmentRows.tsx index 6d1af92d5fb91..4e95d2de97ca3 100644 --- a/apps/mobile/src/features/connection/CloudEnvironmentRows.tsx +++ b/apps/mobile/src/features/connection/CloudEnvironmentRows.tsx @@ -1,7 +1,6 @@ import { Button } from "../../components/ui/Button"; import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; import { ConnectionTraceId } from "./ConnectionTraceId"; -import { useAuth } from "@clerk/expo"; import { SymbolView } from "../../components/ui/AppSymbol"; import { hasSavedBearerRoute, @@ -26,6 +25,7 @@ import type { ConnectedEnvironmentSummary } from "../../state/remote-runtime-typ import { serverEnvironment } from "../../state/server"; import { environmentPresentations } from "../../state/presentation"; import { availableCloudEnvironmentPresentation } from "../cloud/cloudEnvironmentPresentation"; +import { useCloudAuth } from "../cloud/cloudAuth"; import { hasCloudPublicConfig } from "../cloud/publicConfig"; import { ConnectionStatusDot } from "./ConnectionStatusDot"; import { useConnectionDiagnosis } from "./ConnectionDiagnosisDetails"; @@ -55,7 +55,7 @@ export function CloudEnvironmentRows(props: CloudEnvironmentRowsProps) { } function SignedInCloudEnvironmentRows(props: CloudEnvironmentRowsProps) { - const { isSignedIn } = useAuth({ treatPendingAsSignedOut: false }); + const { isSignedIn } = useCloudAuth(); if (!isSignedIn) return ; return ; } diff --git a/apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx b/apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx index 80f2ecc77c02d..dc7bb57c37055 100644 --- a/apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx +++ b/apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx @@ -1,7 +1,6 @@ import { ListRow } from "../../components/ui/ListRow"; import * as Dialog from "../../components/ui/Dialog"; import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; -import { useAuth } from "@clerk/expo"; import { CameraView, useCameraPermissions } from "expo-camera"; import * as Haptics from "expo-haptics"; import { @@ -25,6 +24,7 @@ import { FrostedCutout } from "../../components/ui/FrostedCutout"; import { buildPairingUrl, extractPairingUrlFromQrPayload, parsePairingUrl } from "./pairing"; import { useRemoteConnections } from "../../state/use-remote-environment-registry"; import { SymbolView } from "../../components/ui/AppSymbol"; +import { useCloudAuth } from "../cloud/cloudAuth"; import { hasCloudPublicConfig } from "../cloud/publicConfig"; import { CloudEnvironmentRows } from "./CloudEnvironmentRows"; import { autoConnectReport, devAutoConnectAttemptAtom } from "./devAutoConnect"; @@ -351,7 +351,7 @@ function PairingScanCard(props: { } function T3ConnectSection() { - const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false }); + const { isLoaded, isSignedIn } = useCloudAuth(); const navigation = useNavigation(); const { connectedEnvironments, onSetEnvironmentEnabled, onRemoveEnvironmentPress } = useRemoteConnections(); diff --git a/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx index 276f51fcb3891..cc6d6025f578d 100644 --- a/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx @@ -8,8 +8,17 @@ import { useNavigation } from "@react-navigation/native"; import { useEffect, useState } from "react"; import { View } from "react-native"; -import { hasCloudPublicConfig } from "../cloud/publicConfig"; +import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; +import { ScrollView } from "react-native"; + +import * as Dialog from "../../components/ui/Dialog"; +import { useCloudAuth } from "../cloud/cloudAuth"; +import { getMobileOidcController, signInWithBrowser } from "../cloud/oidc/mobileOidc"; +import { cloudAuthProvider, hasCloudPublicConfig } from "../cloud/publicConfig"; import { T3ConnectProfilePage } from "../cloud/T3ConnectProfilePage"; +import { SettingsRow } from "./components/SettingsRow"; +import { SettingsScreen } from "./components/SettingsScreen"; +import { SettingsSection } from "./components/SettingsSection"; const USER_PROFILE_CUSTOM_PAGES = [ { @@ -22,7 +31,57 @@ const USER_PROFILE_CUSTOM_PAGES = [ // Without cloud config the app has no entry point here and appLinking drops the deep link. export function SettingsAuthRouteScreen() { - return hasCloudPublicConfig() ? : null; + if (!hasCloudPublicConfig()) return null; + return cloudAuthProvider() === "oidc" ? ( + + ) : ( + + ); +} + +/** An OpenID Connect provider hosts no profile view, so this screen only signs in and out. */ +function OidcSettingsAuthRouteScreen() { + const { isSignedIn, accountLabel, signOut } = useCloudAuth(); + const [busy, setBusy] = useState(false); + + const signIn = async () => { + const controller = getMobileOidcController(); + if (controller === null || busy) return; + setBusy(true); + const result = await settlePromise(() => signInWithBrowser(controller)); + setBusy(false); + if (result._tag === "Failure") { + const error = squashAtomCommandFailure(result); + Dialog.alert( + "Could not sign in", + error instanceof Error ? error.message : "The identity provider did not respond.", + ); + } + }; + + return ( + + + + {isSignedIn ? ( + void signOut()} + /> + ) : ( + void signIn()} + /> + )} + + + + ); } function ConfiguredSettingsAuthRouteScreen() { diff --git a/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx index cabe229f9a262..cb04ed4f02983 100644 --- a/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsNotificationsRouteScreen.tsx @@ -8,7 +8,6 @@ import { readEnvironmentScope } from "../../state/session"; import { appAtomRegistry } from "../../state/atom-registry"; import { serverEnvironment } from "../../state/server"; import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; -import { useAuth } from "@clerk/expo"; import { useAtomSet, useAtomValue } from "@effect/atom-react"; import * as Notifications from "expo-notifications"; import { useNavigation } from "@react-navigation/native"; @@ -39,7 +38,8 @@ import { subscribeAgentAwarenessRegistrationStatus, } from "../agent-awareness/remoteRegistration"; import { refreshManagedRelayEnvironments } from "../cloud/managedRelayState"; -import { hasCloudPublicConfig, resolveRelayClerkTokenOptions } from "../cloud/publicConfig"; +import { useCloudAuth } from "../cloud/cloudAuth"; +import { hasCloudPublicConfig } from "../cloud/publicConfig"; import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom"; import { runtime } from "../../lib/runtime"; import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; @@ -138,7 +138,7 @@ function ConfiguredSettingsNotificationsRouteScreen() { : agentAwarenessPlatform.subtitle; const insets = useSafeAreaInsets(); const navigation = useNavigation(); - const { getToken, isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false }); + const { getToken, isLoaded, isSignedIn } = useCloudAuth(); const { savedConnectionsById } = useSavedRemoteConnections(); const notificationStatus = useAtomValue(notificationStatusAtom); const setNotificationStatus = useAtomSet(notificationStatusAtom); @@ -273,7 +273,7 @@ function ConfiguredSettingsNotificationsRouteScreen() { } setNotificationStatus("enabled"); } - const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions())); + const tokenResult = await settlePromise(() => getToken()); if (tokenResult._tag === "Failure") { setLiveActivityStatus("disabled"); const error = squashAtomCommandFailure(tokenResult); @@ -356,7 +356,7 @@ function ConfiguredSettingsNotificationsRouteScreen() { async function disableLiveActivities() { let token: string | null = null; if (isSignedIn) { - const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions())); + const tokenResult = await settlePromise(() => getToken()); if (tokenResult._tag === "Failure") { setLiveActivityStatus("enabled"); reportAtomCommandResult(tokenResult, { diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx index a4177bfdd3376..6bd76d5b7efaf 100644 --- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx @@ -14,7 +14,6 @@ import { useSelectWorkspaceTab } from "../layout/workspace/useSelectWorkspaceTab import { useWorkspaceTabs } from "../layout/workspace/WorkspaceTabsContext"; import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; import { EmptyState } from "../../components/ui/EmptyState"; -import { useAuth, useUser } from "@clerk/expo"; import { useNavigation, useNavigationState } from "@react-navigation/native"; import { use, type ComponentProps } from "react"; import { Platform, View } from "react-native"; @@ -22,6 +21,7 @@ import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-grouping"; import { useSafeAreaInsets } from "react-native-safe-area-context"; +import { useCloudAuth } from "../cloud/cloudAuth"; import { hasCloudPublicConfig } from "../cloud/publicConfig"; import { useAdaptiveWorkspaceLayout } from "../layout/adaptive-workspace-context"; import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry"; @@ -109,14 +109,13 @@ interface SettingsListSection { function ConfiguredSettingsRouteScreen() { const navigation = useNavigation(); - const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false }); - const { user } = useUser(); + const { isLoaded, isSignedIn, accountLabel: signedInAs } = useCloudAuth(); const { savedConnectionsById } = useSavedRemoteConnections(); const accountLabel = !isLoaded ? "Checking" : !isSignedIn ? "Sign in" - : (user?.primaryEmailAddress?.emailAddress ?? "Signed in"); + : (signedInAs ?? "Signed in"); return ( Effect.succeedSome(expiredStoredToken), }), + env: Readonly> = TEST_ENV, ) => Effect.provide( CliTokenManager.layer.pipe( @@ -128,7 +129,7 @@ const provideRefreshManager = ( NodeCrypto.layer, ), ), - Layer.provideMerge(ConfigProvider.layer(ConfigProvider.fromEnv({ env: TEST_ENV }))), + Layer.provideMerge(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))), ), ); @@ -250,6 +251,123 @@ it.effect.each([ ), ); +const OIDC_ISSUER = "https://sso.example.test/realms/t3"; +const OIDC_ENV = { T3CODE_OIDC_ISSUER: OIDC_ISSUER, T3CODE_OIDC_CLIENT_ID: "t3-code" }; + +const OidcClaimsJson = Schema.fromJsonString( + Schema.Struct({ + sub: Schema.String, + iss: Schema.String, + exp: Schema.Number, + email: Schema.String, + }), +); +const encodeOidcClaims = Schema.encodeSync(OidcClaimsJson); +const oidcIdToken = (exp: number) => + `${Base64Url.encode(encodeTestIdTokenHeader({ alg: "none" }))}.${Base64Url.encode( + encodeOidcClaims({ sub: "alice", iss: OIDC_ISSUER, exp, email: "theo@example.test" }), + )}.`; + +const oidcProvider = (input: { + readonly deviceAuthorizationEndpoint?: string; + readonly tokenResponse: Readonly>; + readonly requests: Array; +}) => + HttpClient.make((request) => + Effect.sync(() => { + const body = + request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : ""; + input.requests.push({ url: request.url, params: new URLSearchParams(body) }); + return HttpClientResponse.fromWeb( + request, + request.url.endsWith("/.well-known/openid-configuration") + ? Response.json({ + issuer: OIDC_ISSUER, + authorization_endpoint: `${OIDC_ISSUER}/auth`, + token_endpoint: `${OIDC_ISSUER}/token`, + ...(input.deviceAuthorizationEndpoint === undefined + ? {} + : { device_authorization_endpoint: input.deviceAuthorizationEndpoint }), + }) + : Response.json(input.tokenResponse), + ); + }), + ); + +it.effect("refreshes against the discovered OIDC token endpoint and keeps the ID token", () => { + const requests: Array = []; + const idToken = oidcIdToken(4_000_000_000); + let stored = expiredStoredToken; + const secrets = Layer.mock(ServerSecretStore.ServerSecretStore)({ + get: () => Effect.sync(() => Option.some(stored)), + set: (_name, value) => + Effect.sync(() => { + stored = new Uint8Array(value); + }), + }); + return Effect.gen(function* () { + const manager = yield* CliTokenManager.CloudCliTokenManager; + const token = Option.getOrThrow(yield* manager.getExisting); + + // The relay verifies the ID token, so it is the credential, and the provider's lack of + // refresh token rotation keeps the stored one. + assert.equal(token.accessToken, idToken); + assert.equal(token.refreshToken, "refresh-token-1"); + assert.equal(token.expiresAtEpochMs, 4_000_000_000_000); + assert.equal(token.identity, "theo@example.test"); + const refresh = requests.find((request) => request.url === `${OIDC_ISSUER}/token`); + assert.equal(refresh?.params.get("grant_type"), "refresh_token"); + assert.equal(refresh?.params.get("client_id"), "t3-code"); + }).pipe( + provideRefreshManager( + oidcProvider({ + requests, + tokenResponse: { + access_token: "opaque-access-token", + id_token: idToken, + expires_in: 300, + token_type: "bearer", + }, + }), + secrets, + OIDC_ENV, + ), + ); +}); + +it.effect("refuses an OIDC token response that carries no ID token", () => + Effect.gen(function* () { + const manager = yield* CliTokenManager.CloudCliTokenManager; + const error = yield* manager.getExisting.pipe(Effect.flip); + assert.instanceOf(error, CliTokenManager.CloudCliCredentialRefreshError); + }).pipe( + provideRefreshManager( + oidcProvider({ + requests: [], + tokenResponse: { access_token: "opaque", expires_in: 300, token_type: "bearer" }, + }), + undefined, + OIDC_ENV, + ), + ), +); + +it.effect("reports a provider without device authorization instead of calling Clerk", () => { + const requests: Array = []; + return CliTokenManager.deviceAuthorizationLogin(() => Effect.void).pipe( + Effect.flip, + Effect.map((error) => + assert.include(String(error.cause), "does not support device authorization"), + ), + Effect.provide( + Layer.mergeAll( + Layer.succeed(HttpClient.HttpClient, oidcProvider({ requests, tokenResponse: {} })), + ConfigProvider.layer(ConfigProvider.fromEnv({ env: OIDC_ENV })), + ), + ), + ); +}); + it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))( "CliTokenManager.deviceAuthorizationLogin", (it) => { diff --git a/apps/server/src/cloud/CliTokenManager.ts b/apps/server/src/cloud/CliTokenManager.ts index 11a09f481ac4b..c72ad3fc5d15e 100644 --- a/apps/server/src/cloud/CliTokenManager.ts +++ b/apps/server/src/cloud/CliTokenManager.ts @@ -26,13 +26,26 @@ import * as HttpRouter from "effect/http/HttpRouter"; import * as HttpServerRequest from "effect/http/HttpServerRequest"; import * as HttpServerResponse from "effect/http/HttpServerResponse"; -import { buildConnectAuthorizeRequestUrl } from "@t3tools/shared/connectAuth"; +import { + buildConnectAuthorizeRequestUrl, + connectLoopbackRedirectUri, +} from "@t3tools/shared/connectAuth"; +import { + OIDC_SCOPES, + buildAuthorizeUrl, + decodeIdTokenClaims, + oidcAccountLabel, + oidcDiscoveryUrl, + parseOidcMetadata, +} from "@t3tools/shared/oidc"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ExternalLauncher from "../process/externalLauncher.ts"; import { + CLOUD_CLI_OAUTH_LOOPBACK_PORT, cloudCliOAuthConfig, hostedAppUrlConfig, + oidcSettingsConfig, type CloudCliOAuthConfig, } from "./publicConfig.ts"; import { renderLoopbackAuthorizationCompleteHtml } from "./cliAuthHtml.ts"; @@ -297,23 +310,44 @@ function bytesToString(value: Uint8Array): string { const readTokenResponse = Effect.fn("cloud.cli_token.read_token_response")(function* ( response: HttpClientResponse.HttpClientResponse, params: Record, + bearerToken: CloudCliOAuthConfig["bearerToken"], ) { const body = yield* HttpClientResponse.schemaBodyJson(OAuthTokenResponse)(response); + const refreshToken = body.refresh_token ?? params.refresh_token ?? ""; + if (bearerToken === "id_token") { + // The relay verifies the ID token, so it is the credential and its expiry is the expiry. + const claims = yield* Effect.try({ + try: () => decodeIdTokenClaims(body.id_token ?? ""), + catch: (cause) => new CloudCliAuthorizationError({ cause }), + }); + const identity = oidcAccountLabel(claims); + return { + token: { + accessToken: body.id_token ?? "", + refreshToken, + expiresAtEpochMs: claims.exp * 1_000, + identity, + } satisfies PersistedToken, + identity, + nonce: claims.nonce, + }; + } const now = yield* Clock.currentTimeMillis; const identity = idTokenIdentity(body.id_token); return { token: { accessToken: body.access_token, - refreshToken: body.refresh_token ?? params.refresh_token ?? "", + refreshToken, expiresAtEpochMs: now + body.expires_in * 1_000, ...(identity === null ? {} : { identity }), } satisfies PersistedToken, identity, + nonce: undefined, }; }); const exchangeToken = Effect.fn("cloud.cli_token.exchange")(function* ( - metadata: Pick, + metadata: Pick, params: Record, ) { const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk); @@ -321,7 +355,7 @@ const exchangeToken = Effect.fn("cloud.cli_token.exchange")(function* ( return yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe( HttpClientRequest.bodyUrlParams(params), httpClient.execute, - Effect.flatMap((response) => readTokenResponse(response, params)), + Effect.flatMap((response) => readTokenResponse(response, params, metadata.bearerToken)), Effect.timeout(OAUTH_REQUEST_TIMEOUT), Effect.catchTags({ // The enclosing operation owns the span for this inline handler. @@ -374,7 +408,37 @@ const makePkceRequest = Effect.gen(function* () { yield* digest.digest(Digest.sha256, new TextEncoder().encode(verifier)), ); const state = Base64Url.encode(yield* random.randomBytes(16)); - return { verifier, challenge, state }; + const nonce = Base64Url.encode(yield* random.randomBytes(16)); + return { verifier, challenge, state, nonce }; +}); + +/** OIDC when configured, otherwise Clerk. Discovery runs per call, so a provider change applies without a restart. */ +const resolveCloudCliOAuth = Effect.fn("cloud.cli_token.resolve_oauth")(function* () { + const oidc = yield* oidcSettingsConfig; + if (Option.isNone(oidc)) return yield* cloudCliOAuthConfig; + const { issuer, clientId } = oidc.value; + const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk); + const metadata = yield* HttpClientRequest.get(oidcDiscoveryUrl(issuer)).pipe( + httpClient.execute, + Effect.flatMap((response) => response.json), + Effect.timeout(OAUTH_REQUEST_TIMEOUT), + Effect.flatMap((body) => + Effect.try({ + try: () => parseOidcMetadata(issuer, body), + catch: (cause) => new CloudCliAuthorizationError({ cause }), + }), + ), + ); + return { + tokenEndpoint: metadata.tokenEndpoint, + deviceAuthorizationEndpoint: metadata.deviceAuthorizationEndpoint, + authorizationEndpoint: metadata.authorizationEndpoint, + bearerToken: "id_token", + clientId, + loopbackPort: CLOUD_CLI_OAUTH_LOOPBACK_PORT, + redirectUri: connectLoopbackRedirectUri(CLOUD_CLI_OAUTH_LOOPBACK_PORT), + scopes: OIDC_SCOPES, + } satisfies CloudCliOAuthConfig; }); export interface DeviceAuthorizationPrompt { @@ -388,7 +452,7 @@ const isTransportError = (error: unknown) => HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError"; const pollDeviceToken = Effect.fn("cloud.cli_token.poll_device_token")(function* ( - metadata: Pick, + metadata: Pick, deviceCode: string, initialInterval: Duration.Duration, ) { @@ -414,7 +478,7 @@ const pollDeviceToken = Effect.fn("cloud.cli_token.poll_device_token")(function* continue; } if (response.value.status >= 200 && response.value.status < 300) { - return yield* readTokenResponse(response.value, params); + return yield* readTokenResponse(response.value, params, metadata.bearerToken); } const failure = yield* HttpClientResponse.schemaBodyJson(OAuthErrorResponse)(response.value); switch (failure.error) { @@ -437,7 +501,12 @@ const pollDeviceToken = Effect.fn("cloud.cli_token.poll_device_token")(function* export const deviceAuthorizationLogin = Effect.fn("cloud.cli_token.device_authorization_login")( function* (showPrompt: (prompt: DeviceAuthorizationPrompt) => Effect.Effect) { - const metadata = yield* cloudCliOAuthConfig; + const metadata = yield* resolveCloudCliOAuth(); + if (metadata.deviceAuthorizationEndpoint === null) { + return yield* new CloudCliAuthorizationError({ + cause: "The identity provider does not support device authorization.", + }); + } const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk); const authorization = yield* HttpClientRequest.post(metadata.deviceAuthorizationEndpoint).pipe( HttpClientRequest.bodyUrlParams({ @@ -498,7 +567,7 @@ export const make = Effect.gen(function* () { }); const refresh = Effect.fn("cloud.cli_token.refresh")(function* (token: PersistedToken) { - const metadata = yield* cloudCliOAuthConfig; + const metadata = yield* resolveCloudCliOAuth(); const { token: refreshed } = yield* exchangeToken(metadata, { grant_type: "refresh_token", refresh_token: token.refreshToken, @@ -510,9 +579,8 @@ export const make = Effect.gen(function* () { }); const login = Effect.fn("cloud.cli_token.login")(function* () { - const metadata = yield* cloudCliOAuthConfig; - const hostedAppUrl = yield* hostedAppUrlConfig; - const { verifier, challenge, state } = yield* makePkceRequest; + const metadata = yield* resolveCloudCliOAuth(); + const { verifier, challenge, state, nonce } = yield* makePkceRequest; const callback = yield* Deferred.make(); const layerCallbackRoute = HttpRouter.add( "GET", @@ -543,13 +611,25 @@ export const make = Effect.gen(function* () { ), Layer.build, ); - // /connect establishes a Clerk session first; /oauth/authorize loses parameters across sign-in. - const authorizationUrl = buildConnectAuthorizeRequestUrl({ - hostedAppUrl, - state, - challenge, - loopbackPort: metadata.loopbackPort, - }); + // Clerk: /connect establishes a session first, because /oauth/authorize loses parameters across + // sign-in. An OIDC provider keeps them, so the CLI sends the user straight to it. + const authorizationUrl = + metadata.authorizationEndpoint === null + ? buildConnectAuthorizeRequestUrl({ + hostedAppUrl: yield* hostedAppUrlConfig, + state, + challenge, + loopbackPort: metadata.loopbackPort, + }) + : buildAuthorizeUrl({ + authorizationEndpoint: metadata.authorizationEndpoint, + clientId: metadata.clientId, + redirectUri: metadata.redirectUri, + scopes: metadata.scopes, + state, + nonce, + challenge, + }); yield* Console.log(formatLoopbackAuthorizationPrompt(authorizationUrl)); const authorization = yield* waitForLoopbackAuthorization({ authorizationUrl, @@ -565,13 +645,18 @@ export const make = Effect.gen(function* () { if (authorization._tag === "HeadlessRequested") { return authorization; } - const { token } = yield* exchangeToken(metadata, { + const { token, nonce: returnedNonce } = yield* exchangeToken(metadata, { grant_type: "authorization_code", code: authorization.code, redirect_uri: metadata.redirectUri, client_id: metadata.clientId, code_verifier: verifier, }); + if (metadata.authorizationEndpoint !== null && returnedNonce !== nonce) { + return yield* new CloudCliAuthorizationError({ + cause: "The ID token nonce does not match the authorization request.", + }); + } return { _tag: "Authorized", token } as const; }); diff --git a/apps/server/src/cloud/CloudLink.ts b/apps/server/src/cloud/CloudLink.ts index a4945e8696565..acbd1dea87d9a 100644 --- a/apps/server/src/cloud/CloudLink.ts +++ b/apps/server/src/cloud/CloudLink.ts @@ -39,6 +39,7 @@ import { verifyRelayJwt, } from "@t3tools/shared/relayJwt"; import { isSecureRelayUrl } from "@t3tools/shared/relayUrl"; +import * as Config from "effect/Config"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Signature from "effect/crypto/Signature"; @@ -115,6 +116,7 @@ import { linkProofScopes, managedEndpointRuntimeConfigsMatch, parseManagedEndpointLocalOrigin, + parsePublicEndpoint, requestAbsoluteUrl, } from "./linkChecks.ts"; import { desktopUpdateRestartPending, pendingUpdateHandoffExists } from "./updateHandoff.ts"; @@ -300,6 +302,9 @@ export const shouldRetryCloudLink = (error: unknown): boolean => const rollbackFailed = (cause: unknown) => Effect.logWarning("Could not roll back a T3 Connect preference", { cause }); +// Where a publish-only link says this machine can be reached; unset keeps the local placeholder. +const publicEndpointConfig = Config.option(Config.NonEmptyString("T3CODE_PUBLIC_URL")); + const requireRelayUrl = relayUrlConfig.pipe( Effect.mapError( (cause) => new CloudLinkInternalError({ operation: "relay-url-unconfigured", cause }), @@ -850,6 +855,17 @@ const make = Effect.gen(function* () { ); const mode = yield* readCliDesiredLinkMode.pipe(withSecrets); const managedTunnelsEnabled = mode !== "publish_only"; + const configuredPublicEndpoint = managedTunnelsEnabled + ? Option.none() + : yield* publicEndpointConfig.pipe( + Effect.mapError(() => new CloudLinkOriginInvalidError({ origin: "endpoint" })), + ); + const publicEndpoint = Option.isSome(configuredPublicEndpoint) + ? yield* Effect.try({ + try: () => parsePublicEndpoint(configuredPublicEndpoint.value), + catch: () => new CloudLinkOriginInvalidError({ origin: "endpoint" }), + }) + : null; const relayUrl = yield* requireRelayUrl; const challenge = yield* relayClientRequest({ url: `${relayUrl}/v1/client/environment-link-challenges`, @@ -866,8 +882,8 @@ const make = Effect.gen(function* () { challenge: challenge.challenge, relayIssuer: relayUrl, endpoint: { - httpBaseUrl: parsedOrigin.httpBaseUrl, - wsBaseUrl: parsedOrigin.wsBaseUrl, + httpBaseUrl: publicEndpoint?.httpBaseUrl ?? parsedOrigin.httpBaseUrl, + wsBaseUrl: publicEndpoint?.wsBaseUrl ?? parsedOrigin.wsBaseUrl, providerKind: managedTunnelsEnabled ? "cloudflare_tunnel" : "manual", }, origin: parsedOrigin.origin, diff --git a/apps/server/src/cloud/linkChecks.test.ts b/apps/server/src/cloud/linkChecks.test.ts index 50300e5f747d9..e8c290f1c8d5a 100644 --- a/apps/server/src/cloud/linkChecks.test.ts +++ b/apps/server/src/cloud/linkChecks.test.ts @@ -1,7 +1,11 @@ import { describe, expect, it } from "@effect/vitest"; import type { RelayLinkProofRequest } from "@t3tools/contracts/relay"; -import { linkProofScopes, parseManagedEndpointLocalOrigin } from "./linkChecks.ts"; +import { + linkProofScopes, + parseManagedEndpointLocalOrigin, + parsePublicEndpoint, +} from "./linkChecks.ts"; describe("parseManagedEndpointLocalOrigin", () => { it.each([ @@ -36,6 +40,30 @@ describe("parseManagedEndpointLocalOrigin", () => { }); }); +describe("parsePublicEndpoint", () => { + it.each([ + ["https://t3.example.dev", "https://t3.example.dev/", "wss://t3.example.dev/ws"], + [ + "https://t3.example.dev:8443/", + "https://t3.example.dev:8443/", + "wss://t3.example.dev:8443/ws", + ], + ])("builds a managed-shaped endpoint from %s", (input, httpBaseUrl, wsBaseUrl) => { + expect(parsePublicEndpoint(input)).toEqual({ httpBaseUrl, wsBaseUrl }); + }); + + it.each([ + "http://t3.example.dev", + "https://user:pass@t3.example.dev", + "https://t3.example.dev/t3", + "https://t3.example.dev?x=1", + "https://t3.example.dev#top", + " https://t3.example.dev", + ])("rejects %s", (input) => { + expect(() => parsePublicEndpoint(input)).toThrow("Invalid public endpoint"); + }); +}); + describe("link proof provider kinds", () => { const proofRequest = ( providerKind: RelayLinkProofRequest["endpoint"]["providerKind"], diff --git a/apps/server/src/cloud/linkChecks.ts b/apps/server/src/cloud/linkChecks.ts index 607e2c08f8a53..338d0799e21f7 100644 --- a/apps/server/src/cloud/linkChecks.ts +++ b/apps/server/src/cloud/linkChecks.ts @@ -95,6 +95,28 @@ export function canLinkHere( ); } +/** + * The HTTPS address a machine reaches the internet at, when the user exposes it themselves. + * Same shape as a managed endpoint, so clients connect the same way. + */ +export function parsePublicEndpoint(value: string) { + const url = new URL(value); + if ( + value !== value.trim() || + url.protocol !== "https:" || + url.username !== "" || + url.password !== "" || + url.pathname !== "/" || + url.search !== "" || + url.hash !== "" || + value.includes("?") || + value.includes("#") + ) { + throw new Error("Invalid public endpoint"); + } + return { httpBaseUrl: `${url.origin}/`, wsBaseUrl: `wss://${url.host}/ws` }; +} + export function isAllowedEndpointOrigin(input: { readonly origin: RelayManagedEndpointOrigin; readonly requestUrl: string; diff --git a/apps/server/src/cloud/publicConfig.test.ts b/apps/server/src/cloud/publicConfig.test.ts index ca1d86b8ddadb..0192166d4c006 100644 --- a/apps/server/src/cloud/publicConfig.test.ts +++ b/apps/server/src/cloud/publicConfig.test.ts @@ -1,12 +1,14 @@ import { assert, it } from "@effect/vitest"; import * as ConfigProvider from "effect/ConfigProvider"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Result from "effect/Result"; import { hostedAppUrlConfig, makeCloudCliOAuthConfig, makeRelayUrlConfig, + oidcSettingsConfig, resolveRelayClientTracingConfig, } from "./publicConfig.ts"; @@ -47,6 +49,8 @@ it.effect("derives direct Clerk OAuth endpoints from statically injected public assert.deepEqual(config, { tokenEndpoint: "https://clerk.example.test/oauth/token", deviceAuthorizationEndpoint: "https://clerk.example.test/oauth/device_authorization", + authorizationEndpoint: null, + bearerToken: "access_token", clientId: "oauth_client_embedded", loopbackPort: 34338, redirectUri: "http://127.0.0.1:34338/callback", @@ -55,6 +59,26 @@ it.effect("derives direct Clerk OAuth endpoints from statically injected public }), ); +it.effect("reads an OIDC provider from the environment and treats a partial one as absent", () => + Effect.gen(function* () { + const configured = yield* oidcSettingsConfig.pipe( + provideEnv({ + T3CODE_OIDC_ISSUER: "https://sso.example.test/realms/t3", + T3CODE_OIDC_CLIENT_ID: "t3-code", + }), + ); + assert.deepEqual(Option.getOrNull(configured), { + issuer: "https://sso.example.test/realms/t3", + clientId: "t3-code", + }); + + const partial = yield* oidcSettingsConfig.pipe( + provideEnv({ T3CODE_OIDC_ISSUER: "https://sso.example.test/realms/t3" }), + ); + assert.isTrue(Option.isNone(partial)); + }), +); + it("product opt-out disables relay tracing with either built-in or runtime credentials", () => { const fallback = { tracesUrl: "https://embedded.example.test/v1/traces", diff --git a/apps/server/src/cloud/publicConfig.ts b/apps/server/src/cloud/publicConfig.ts index 85ffbbb046588..815c3b8b44fc7 100644 --- a/apps/server/src/cloud/publicConfig.ts +++ b/apps/server/src/cloud/publicConfig.ts @@ -16,11 +16,13 @@ declare const __T3CODE_BUILD_RELAY_URL__: string | undefined; declare const __T3CODE_BUILD_CHANNEL__: "nightly" | "latest" | undefined; declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined; declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined; +declare const __T3CODE_BUILD_OIDC_ISSUER__: string | undefined; +declare const __T3CODE_BUILD_OIDC_CLIENT_ID__: string | undefined; declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined; declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined; declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined; -const CLOUD_CLI_OAUTH_LOOPBACK_PORT = 34338; +export const CLOUD_CLI_OAUTH_LOOPBACK_PORT = 34338; const CLOUD_CLI_OAUTH_SCOPES = CONNECT_OAUTH_SCOPES; function validateRelayUrl(value: string) { @@ -65,6 +67,14 @@ const buildTimeClerkCliOAuthClientId = readBuildTimeValue( ? undefined : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__, ); +const buildTimeOidcIssuer = readBuildTimeValue( + typeof __T3CODE_BUILD_OIDC_ISSUER__ === "undefined" ? undefined : __T3CODE_BUILD_OIDC_ISSUER__, +); +const buildTimeOidcClientId = readBuildTimeValue( + typeof __T3CODE_BUILD_OIDC_CLIENT_ID__ === "undefined" + ? undefined + : __T3CODE_BUILD_OIDC_CLIENT_ID__, +); const buildTimeRelayClientTracing = { tracesUrl: readBuildTimeValue( typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined" @@ -165,13 +175,29 @@ function makePublicValueConfig(name: string, fallback: string) { export interface CloudCliOAuthConfig { readonly tokenEndpoint: string; - readonly deviceAuthorizationEndpoint: string; + /** Null when the identity provider offers no device authorization (headless sign-in). */ + readonly deviceAuthorizationEndpoint: string | null; + /** + * Set for an OpenID Connect provider, which the CLI sends users to directly. Clerk routes + * through the hosted sign-in page instead, so it has none. + */ + readonly authorizationEndpoint: string | null; + /** Which token the relay verifies: Clerk's access token, or an OIDC provider's ID token. */ + readonly bearerToken: "access_token" | "id_token"; readonly clientId: string; readonly loopbackPort: number; readonly redirectUri: string; - readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES; + readonly scopes: ReadonlyArray; } +/** Present when an OpenID Connect provider is configured; it takes the place of Clerk. */ +export const oidcSettingsConfig = Config.option( + Config.all({ + issuer: makePublicValueConfig("T3CODE_OIDC_ISSUER", buildTimeOidcIssuer), + clientId: makePublicValueConfig("T3CODE_OIDC_CLIENT_ID", buildTimeOidcClientId), + }), +); + export function makeCloudCliOAuthConfig({ clerkPublishableKeyFallback = buildTimeClerkPublishableKey, clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId, @@ -205,6 +231,8 @@ export function makeCloudCliOAuthConfig({ ({ tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`, deviceAuthorizationEndpoint: `${clerkFrontendApiUrl}/oauth/device_authorization`, + authorizationEndpoint: null, + bearerToken: "access_token", clientId, loopbackPort: CLOUD_CLI_OAUTH_LOOPBACK_PORT, redirectUri: connectLoopbackRedirectUri(CLOUD_CLI_OAUTH_LOOPBACK_PORT), @@ -218,8 +246,16 @@ export function makeCloudCliOAuthConfig({ export const cloudCliOAuthConfig = makeCloudCliOAuthConfig(); -export const hasCloudPublicConfig = Boolean( - (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) && +const hasClerkPublicConfig = Boolean( (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) && (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId), ); +const hasOidcPublicConfig = Boolean( + (process.env.T3CODE_OIDC_ISSUER?.trim() || buildTimeOidcIssuer) && + (process.env.T3CODE_OIDC_CLIENT_ID?.trim() || buildTimeOidcClientId), +); + +export const hasCloudPublicConfig = Boolean( + (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) && + (hasClerkPublicConfig || hasOidcPublicConfig), +); diff --git a/apps/server/vite.config.ts b/apps/server/vite.config.ts index 02fe582953abd..7957c1bfa56ef 100644 --- a/apps/server/vite.config.ts +++ b/apps/server/vite.config.ts @@ -135,6 +135,10 @@ export default mergeConfig( __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: JSON.stringify( repoEnv.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() ?? "", ), + __T3CODE_BUILD_OIDC_ISSUER__: JSON.stringify(repoEnv.T3CODE_OIDC_ISSUER?.trim() ?? ""), + __T3CODE_BUILD_OIDC_CLIENT_ID__: JSON.stringify( + repoEnv.T3CODE_OIDC_CLIENT_ID?.trim() ?? "", + ), __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: JSON.stringify( repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() ?? "", ), diff --git a/apps/web/src/cloud/cloudAuth.tsx b/apps/web/src/cloud/cloudAuth.tsx new file mode 100644 index 0000000000000..528f9f8c5ffdb --- /dev/null +++ b/apps/web/src/cloud/cloudAuth.tsx @@ -0,0 +1,47 @@ +import { SIGNED_OUT_CLOUD_AUTH, type CloudAuthState } from "@t3tools/client-runtime/relay"; +import { createContext, useContext, type ReactNode } from "react"; + +import type { T3ConnectAccountPageUrl } from "../components/clerk/T3ConnectAccountPages"; + +/** Components read this instead of a provider's own hooks. */ +export interface CloudAuth extends CloudAuthState { + readonly signIn: () => void; + readonly signOut: () => Promise; + /** + * Also true for a Clerk session with steps still pending. Activation uses it so that such a + * session does not look like a fresh sign-in once it completes. + */ + readonly hasSession: boolean; + /** Clerk's account pages; null for providers that do not host any. */ + readonly openAccount: ((page?: T3ConnectAccountPageUrl) => void) | null; +} + +const SIGNED_OUT: CloudAuth = { + ...SIGNED_OUT_CLOUD_AUTH, + signIn: () => {}, + signOut: async () => {}, + hasSession: false, + openAccount: null, +}; + +const CloudAuthContext = createContext(SIGNED_OUT); + +export function CloudAuthProvider({ + value, + children, +}: { + readonly value: CloudAuth; + readonly children: ReactNode; +}) { + return {children}; +} + +export const useCloudAuth = (): CloudAuth => useContext(CloudAuthContext); + +/** Opens the provider's account pages. Their content is rendered once by the provider itself. */ +export function useT3ConnectAccountPage(): { + readonly open: CloudAuth["openAccount"]; + readonly portals: ReactNode; +} { + return { open: useCloudAuth().openAccount, portals: null }; +} diff --git a/apps/web/src/cloud/managedAuth.tsx b/apps/web/src/cloud/managedAuth.tsx index 59bc69f7640d2..3cd6eeb5c822e 100644 --- a/apps/web/src/cloud/managedAuth.tsx +++ b/apps/web/src/cloud/managedAuth.tsx @@ -1,5 +1,6 @@ import { useClerk } from "@clerk/react"; import { + type CloudAccountSource, createCloudAccountService, ManagedRelay, setManagedRelaySession, @@ -38,28 +39,38 @@ function clerkTokenReader(clerk: ClerkInstance): () => Promise { clerk.session ? await clerk.session.getToken(resolveRelayClerkTokenOptions()) : null; } -const managedRelayAccountService = createCloudAccountService({ - deactivate: deactivateManagedRelayAuthentication, - detach: deactivateManagedRelayAuthentication, - cleanUp: async () => { - const results = await Promise.all([ - runAtomCommand(appAtomRegistry, environmentCatalog.removeRelayEnvironments, undefined, { - reportFailure: false, - reportDefect: false, - }), - settleAsyncResult(() => - runtime.runPromiseExit( - ManagedRelay.ManagedRelayClient.pipe(Effect.flatMap((client) => client.resetTokenCache)), +/** Shared by every sign-in provider: only how a source yields its relay token differs. */ +export function createManagedRelayAccountService( + readToken: (source: S) => () => Promise, +) { + return createCloudAccountService({ + deactivate: deactivateManagedRelayAuthentication, + detach: deactivateManagedRelayAuthentication, + cleanUp: async () => { + const results = await Promise.all([ + runAtomCommand(appAtomRegistry, environmentCatalog.removeRelayEnvironments, undefined, { + reportFailure: false, + reportDefect: false, + }), + settleAsyncResult(() => + runtime.runPromiseExit( + ManagedRelay.ManagedRelayClient.pipe( + Effect.flatMap((client) => client.resetTokenCache), + ), + ), ), - ), - ]); - for (const result of results) { - reportAtomCommandResult(result, { label: "cloud account cleanup" }); - } - }, - activate: ({ source, accountId }) => - activateManagedRelayAuthentication(accountId, clerkTokenReader(source)), -}); + ]); + for (const result of results) { + reportAtomCommandResult(result, { label: "cloud account cleanup" }); + } + }, + activate: ({ source, accountId }) => + activateManagedRelayAuthentication(accountId, readToken(source)), + }); +} + +const managedRelayAccountService = + createManagedRelayAccountService(clerkTokenReader); export function ManagedRelayAuthProvider({ children }: { readonly children: ReactNode }) { // ClerkProvider creates the Clerk instance and owns its lifetime, so the diff --git a/apps/web/src/cloud/oidc/browserOidc.ts b/apps/web/src/cloud/oidc/browserOidc.ts new file mode 100644 index 0000000000000..584b1670856e2 --- /dev/null +++ b/apps/web/src/cloud/oidc/browserOidc.ts @@ -0,0 +1,69 @@ +import { isDesktopOidcCallbackUrl, OIDC_CALLBACK_PATH } from "@t3tools/shared/oidc"; + +import { resolveOidcPublicConfig } from "../publicConfig"; +import { + createOidcController, + type KeyValueStore, + type OidcController, +} from "@t3tools/client-runtime/relay"; + +// Storage throws in some private-browsing modes; a session then lasts until the tab closes. +const storageOrMemory = (read: () => Storage): KeyValueStore => { + try { + return read(); + } catch { + const entries = new Map(); + return { + getItem: (key) => entries.get(key) ?? null, + setItem: (key, value) => void entries.set(key, value), + removeItem: (key) => void entries.delete(key), + }; + } +}; + +let controller: OidcController | null = null; + +/** The one controller for this page; null when the build has no OpenID Connect provider. */ +export function getBrowserOidcController(): OidcController | null { + const config = resolveOidcPublicConfig(); + if (config === null) return null; + controller ??= createOidcController({ + config, + redirectUri: `${window.location.origin}${OIDC_CALLBACK_PATH}`, + sessions: storageOrMemory(() => window.localStorage), + pending: storageOrMemory(() => window.sessionStorage), + navigate: (url) => window.location.assign(url), + ...(navigator.locks === undefined + ? {} + : { withRefreshLock: (run) => navigator.locks.request("t3-oidc-refresh", run) }), + }); + return controller; +} + +let abandonDesktopSignIn: (() => void) | null = null; + +/** + * Electron signs in through the system browser (RFC 8252): the identity provider redirects to the + * app's private-use scheme, and the main process hands that link back over `webLinks`. + */ +export function signInThroughDesktopBrowser(controller: OidcController): Promise | null { + const bridge = window.desktopBridge; + if (bridge === undefined) return null; + return controller.signInWithPrompt(async (authorizeUrl) => { + abandonDesktopSignIn?.(); + const callback = new Promise((resolve, reject) => { + const unsubscribe = bridge.webLinks.onOpen((url) => { + if (!isDesktopOidcCallbackUrl(url)) return; + abandonDesktopSignIn = null; + unsubscribe(); + resolve(url); + }); + abandonDesktopSignIn = () => { + unsubscribe(); + reject(new Error("A newer sign-in replaced this one.")); + }; + }); + await bridge.openExternal(authorizeUrl); + return callback; + }); +} diff --git a/apps/web/src/cloud/publicConfig.test.ts b/apps/web/src/cloud/publicConfig.test.ts index 182367a410de2..d5c1cb63a3936 100644 --- a/apps/web/src/cloud/publicConfig.test.ts +++ b/apps/web/src/cloud/publicConfig.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; -import { hasCloudPublicConfig } from "./publicConfig.ts"; +import { cloudAuthProvider, hasCloudPublicConfig } from "./publicConfig.ts"; afterEach(() => { vi.unstubAllEnvs(); @@ -16,6 +16,32 @@ describe("hasCloudPublicConfig", () => { expect(hasCloudPublicConfig()).toBe(false); }); + describe("OpenID Connect provider", () => { + beforeEach(() => { + vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test"); + vi.stubEnv("VITE_OIDC_ISSUER", "https://sso.example.test/realms/t3"); + vi.stubEnv("VITE_OIDC_CLIENT_ID", "t3-code"); + }); + + it("works on any origin and takes the place of Clerk", () => { + vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_test_example"); + vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay"); + vi.stubGlobal("window", { location: new URL("http://localhost:3773") }); + + expect(hasCloudPublicConfig()).toBe(true); + expect(cloudAuthProvider()).toBe("oidc"); + }); + + it.each([ + ["a missing client id", "VITE_OIDC_CLIENT_ID", ""], + ["an insecure relay URL", "VITE_T3CODE_RELAY_URL", "http://relay.example.test"], + ])("stays off with %s", (_name, key, value) => { + vi.stubEnv(key, value); + + expect(hasCloudPublicConfig()).toBe(false); + }); + }); + describe("browser origins", () => { beforeEach(() => { vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_live_${btoa("clerk.example.com$")}`); diff --git a/apps/web/src/cloud/publicConfig.ts b/apps/web/src/cloud/publicConfig.ts index f7f53ca2c3e4f..0cfa915638afb 100644 --- a/apps/web/src/cloud/publicConfig.ts +++ b/apps/web/src/cloud/publicConfig.ts @@ -19,6 +19,8 @@ export class CloudPublicConfigMissingError extends Schema.TaggedError ({ ), })) as never, })); -vi.mock(import("@clerk/react"), async (original) => ({ +vi.mock(import("./cloudAuth"), async (original) => ({ ...(await original()), - useAuth: (() => ({ getToken: testState.getToken, isSignedIn: true })) as never, + useCloudAuth: (() => ({ getToken: testState.getToken, isSignedIn: true })) as never, })); vi.mock(import("../components/ui/toast"), async (original) => { const actual = await original(); @@ -88,10 +88,6 @@ vi.mock(import("./cloudLinkState"), async (original) => ({ refresh: testState.refreshLink, })) as never, })); -vi.mock(import("./publicConfig"), async (original) => ({ - ...(await original()), - resolveRelayClerkTokenOptions: () => ({}) as never, -})); import { CloudEnvironmentLinkError } from "./linkEnvironment"; import { useCloudLinkController, type CloudLinkDesiredState } from "./useCloudLinkController"; diff --git a/apps/web/src/cloud/useCloudLinkController.ts b/apps/web/src/cloud/useCloudLinkController.ts index c59eb5eee6855..cf011954000b6 100644 --- a/apps/web/src/cloud/useCloudLinkController.ts +++ b/apps/web/src/cloud/useCloudLinkController.ts @@ -1,5 +1,4 @@ import { useAtomValue } from "@effect/atom-react"; -import { useAuth } from "@clerk/react"; import { findErrorTraceId } from "@t3tools/client-runtime/errors"; import { AuthRelayReadScope, AuthRelayWriteScope, type EnvironmentId } from "@t3tools/contracts"; import { ManagedRelay } from "@t3tools/client-runtime/relay"; @@ -17,6 +16,7 @@ import { relayFailureReason } from "./relayFailureReason"; import { relayEnvironmentDiscovery } from "../state/relay"; import { readEnvironmentScope } from "../state/session"; import { useAtomCommand } from "../state/use-atom-command"; +import { useCloudAuth } from "./cloudAuth"; import { isCloudEnvironmentLinkError } from "./linkEnvironment"; import { refreshManagedTunnelUsage } from "./managedRelayState"; import { @@ -48,7 +48,7 @@ function tunnelLimitFromLinkFailure(cause: unknown): number | null { } export function useCloudLinkController(environmentId: EnvironmentId | null) { - const { getToken, isSignedIn } = useAuth(); + const { getToken, isSignedIn } = useCloudAuth(); const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, { reportFailure: false, }); @@ -133,7 +133,7 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) { if (!canManageLink()) return false; const wantsLink = desired.managedTunnel || desired.publish; if (wantsLink && readLinkState() === null) return false; - const tokenResult = await settlePromise(() => getToken(resolveRelayClerkTokenOptions())); + const tokenResult = await settlePromise(() => getToken()); if (!canManageLink()) return false; // A failed mutation may have partially applied, so refresh server state on every exit. diff --git a/apps/web/src/components/HostedEmptyState.tsx b/apps/web/src/components/HostedEmptyState.tsx index 627ba0b758eb1..3db51dd25f652 100644 --- a/apps/web/src/components/HostedEmptyState.tsx +++ b/apps/web/src/components/HostedEmptyState.tsx @@ -1,10 +1,10 @@ -import { useAuth } from "@clerk/react"; import type { EnvironmentId } from "@t3tools/contracts"; import { Link } from "@tanstack/react-router"; import { CheckIcon } from "lucide-react"; import { type ReactNode, useState } from "react"; import { APP_DISPLAY_NAME, DESKTOP_DOWNLOAD_URL } from "~/branding"; +import { useCloudAuth } from "~/cloud/cloudAuth"; import { hasCloudPublicConfig } from "~/cloud/publicConfig"; import { isElectron } from "../env"; import { isLocalEnvironmentDisabled } from "../localEnvironment"; @@ -64,7 +64,7 @@ export function HostedEmptyState() { } function T3ConnectSteps() { - const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false }); + const { isLoaded, hasSession: isSignedIn } = useCloudAuth(); const { openAuthPrompt } = useT3ConnectAuthPrompt(); // Adding the first computer unmounts this screen. Wait for all health checks // so every compatible registration is queued before that happens. diff --git a/apps/web/src/components/clerk/BrowserManagedAuthShell.tsx b/apps/web/src/components/clerk/BrowserManagedAuthShell.tsx index 843329421873c..1383eb303018c 100644 --- a/apps/web/src/components/clerk/BrowserManagedAuthShell.tsx +++ b/apps/web/src/components/clerk/BrowserManagedAuthShell.tsx @@ -2,6 +2,7 @@ import { ClerkProvider } from "@clerk/react"; import type { ReactNode } from "react"; import { ManagedRelayAuthProvider } from "../../cloud/managedAuth"; +import { ClerkCloudAuthBridge } from "./ClerkCloudAuthBridge"; import { clerkAppearance } from "./clerkAppearance"; /** Lazy-load Clerk so cloudless local mode never downloads it. */ @@ -14,7 +15,9 @@ export default function BrowserManagedAuthShell({ }) { return ( - {children} + + {children} + ); } diff --git a/apps/web/src/components/clerk/ClerkCloudAuthBridge.tsx b/apps/web/src/components/clerk/ClerkCloudAuthBridge.tsx new file mode 100644 index 0000000000000..e955f1e35f922 --- /dev/null +++ b/apps/web/src/components/clerk/ClerkCloudAuthBridge.tsx @@ -0,0 +1,38 @@ +import { useAuth, useClerk } from "@clerk/react"; +import type { ReactNode } from "react"; + +import { CloudAuthProvider, type CloudAuth } from "../../cloud/cloudAuth"; +import { resolveRelayClerkTokenOptions } from "../../cloud/publicConfig"; +import { isElectron } from "../../env"; +import { resolveClerkSignInProps } from "./authRedirect"; +import { useClerkAccountPage } from "./T3ConnectAccountPages"; + +/** Presents Clerk through the provider-neutral `CloudAuth` the rest of the app reads. */ +export function ClerkCloudAuthBridge({ children }: { readonly children: ReactNode }) { + const clerk = useClerk(); + const { isLoaded, isSignedIn, getToken } = useAuth(); + const { userId, isSignedIn: hasSession } = useAuth({ treatPendingAsSignedOut: false }); + const accountPage = useClerkAccountPage(); + + const value: CloudAuth = { + provider: "clerk", + isLoaded, + isSignedIn: isSignedIn === true, + hasSession: hasSession === true, + userId: userId ?? null, + accountLabel: clerk.user?.primaryEmailAddress?.emailAddress ?? null, + getToken: () => getToken(resolveRelayClerkTokenOptions()), + signIn: () => { + clerk.openSignIn(resolveClerkSignInProps(window.location.href, isElectron)); + }, + signOut: () => clerk.signOut(), + openAccount: accountPage.open, + }; + + return ( + + {children} + {accountPage.portals} + + ); +} diff --git a/apps/web/src/components/clerk/ElectronManagedAuthShell.tsx b/apps/web/src/components/clerk/ElectronManagedAuthShell.tsx index 8ed4de1ffba10..47637651e042c 100644 --- a/apps/web/src/components/clerk/ElectronManagedAuthShell.tsx +++ b/apps/web/src/components/clerk/ElectronManagedAuthShell.tsx @@ -3,6 +3,7 @@ import { ClerkProvider } from "@clerk/electron/react"; import type { ReactNode } from "react"; import { ManagedRelayAuthProvider } from "../../cloud/managedAuth"; +import { ClerkCloudAuthBridge } from "./ClerkCloudAuthBridge"; import { clerkAppearance } from "./clerkAppearance"; /** Load only lazily inside desktop; clerk-js is statically bundled and would otherwise enter every @@ -16,7 +17,9 @@ export default function ElectronManagedAuthShell({ }) { return ( - {children} + + {children} + ); } diff --git a/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx b/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx index ff398e3a398c1..35ceafe8a6365 100644 --- a/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx +++ b/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx @@ -13,7 +13,7 @@ import { AlertDialogTitle, } from "../ui/alert-dialog"; import { Button, InlineButton } from "../ui/button"; -import { useT3ConnectAccountPage } from "./T3ConnectAccountPages"; +import { useT3ConnectAccountPage } from "../../cloud/cloudAuth"; export function RemoveT3ConnectEnvironmentDialog({ environmentLabel, diff --git a/apps/web/src/components/clerk/T3ConnectAccountPages.tsx b/apps/web/src/components/clerk/T3ConnectAccountPages.tsx index 3e804bdf68d61..2386bda9d9e21 100644 --- a/apps/web/src/components/clerk/T3ConnectAccountPages.tsx +++ b/apps/web/src/components/clerk/T3ConnectAccountPages.tsx @@ -33,7 +33,7 @@ export type T3ConnectAccountPageUrl = (typeof T3_CONNECT_ACCOUNT_PAGES)[number][ type PortalTargets = Readonly>; /** Keep portals rendered while the modal can open; Clerk owns the custom-page DOM nodes. */ -export function useT3ConnectAccountPage(): { +export function useClerkAccountPage(): { readonly open: ((page?: T3ConnectAccountPageUrl) => void) | null; readonly portals: ReactNode; } { diff --git a/apps/web/src/components/clerk/T3ConnectSidebarSignIn.tsx b/apps/web/src/components/clerk/T3ConnectSidebarSignIn.tsx index 6b66bed808925..4ad5d803f8385 100644 --- a/apps/web/src/components/clerk/T3ConnectSidebarSignIn.tsx +++ b/apps/web/src/components/clerk/T3ConnectSidebarSignIn.tsx @@ -1,24 +1,38 @@ import { UserButton, useAuth } from "@clerk/react"; -import { LogInIcon } from "lucide-react"; +import { LogInIcon, LogOutIcon } from "lucide-react"; -import { hasCloudPublicConfig } from "../../cloud/publicConfig"; +import { useCloudAuth } from "../../cloud/cloudAuth"; import { SidebarMenu, SidebarMenuButton, SidebarMenuItem } from "../ui/sidebar"; import { T3_CONNECT_ACCOUNT_PAGES } from "./T3ConnectAccountPages"; import { useT3ConnectAuthPrompt } from "./useT3ConnectAuthPrompt"; export function T3ConnectSidebarSignIn() { - if (!hasCloudPublicConfig()) return null; + const { provider, isLoaded, isSignedIn } = useCloudAuth(); + const { openAuthPrompt } = useT3ConnectAuthPrompt(); + + if (provider === null || !isLoaded || isSignedIn) return null; - return ; + return ( + + + + + Sign in to T3 Connect + + + + ); } export function T3ConnectSidebarAvatar() { - if (!hasCloudPublicConfig()) return null; + const { provider } = useCloudAuth(); - return ; + if (provider === "clerk") return ; + if (provider === "oidc") return ; + return null; } -function ConfiguredT3ConnectSidebarAvatar() { +function ClerkSidebarAvatar() { const { isLoaded, isSignedIn } = useAuth(); if (!isLoaded || !isSignedIn) return null; @@ -47,18 +61,18 @@ function ConfiguredT3ConnectSidebarAvatar() { ); } -function ConfiguredT3ConnectSidebarSignIn() { - const { isLoaded, isSignedIn } = useAuth(); - const { openAuthPrompt } = useT3ConnectAuthPrompt(); +/** An OpenID Connect provider hosts no account pages, so the account is just a way to sign out. */ +function OidcSidebarAccount() { + const { isSignedIn, accountLabel, signOut } = useCloudAuth(); - if (!isLoaded || isSignedIn) return null; + if (!isSignedIn) return null; return ( - - - Sign in to T3 Connect + void signOut()} title="Sign out of T3 Connect"> + + Sign out{accountLabel ? ` (${accountLabel})` : ""} diff --git a/apps/web/src/components/clerk/useT3ConnectAuthPrompt.tsx b/apps/web/src/components/clerk/useT3ConnectAuthPrompt.tsx index b87c0d5a88b28..20bf79f12bc35 100644 --- a/apps/web/src/components/clerk/useT3ConnectAuthPrompt.tsx +++ b/apps/web/src/components/clerk/useT3ConnectAuthPrompt.tsx @@ -1,12 +1,6 @@ -import { useClerk } from "@clerk/react"; - -import { isElectron } from "../../env"; -import { resolveClerkSignInProps } from "./authRedirect"; +import { useCloudAuth } from "../../cloud/cloudAuth"; export function useT3ConnectAuthPrompt() { - const clerk = useClerk(); - const openAuthPrompt = () => { - clerk.openSignIn(resolveClerkSignInProps(window.location.href, isElectron)); - }; - return { openAuthPrompt }; + const { signIn } = useCloudAuth(); + return { openAuthPrompt: signIn }; } diff --git a/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx b/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx index faf903c76bde9..68f6635952c72 100644 --- a/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx +++ b/apps/web/src/components/cloud/ConnectOnboardingDialog.tsx @@ -1,4 +1,3 @@ -import { useAuth } from "@clerk/react"; import { useAtomValue } from "@effect/atom-react"; import { AuthRelayReadScope, @@ -26,10 +25,8 @@ import { } from "~/state/environments"; import { usePrimaryManagedPolicy } from "~/state/managedPolicy"; import { environmentSession, readEnvironmentScope, useEnvironmentScope } from "~/state/session"; -import { - type T3ConnectAccountPageUrl, - useT3ConnectAccountPage, -} from "../clerk/T3ConnectAccountPages"; +import { useCloudAuth, useT3ConnectAccountPage } from "~/cloud/cloudAuth"; +import type { T3ConnectAccountPageUrl } from "../clerk/T3ConnectAccountPages"; import { CloudEnvironmentConnectRows } from "./CloudEnvironmentConnectList"; import { TunnelLimitAlert } from "./TunnelUsage"; import { Button } from "../ui/button"; @@ -50,8 +47,8 @@ type OnboardingStep = "publish" | "devices"; const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial()); function ConfiguredConnectOnboardingDialog() { - // Pending Clerk sessions must not look signed out or activation would look like a new sign-in. - const { isLoaded, isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false }); + // Pending sessions must not look signed out or activation would look like a new sign-in. + const { isLoaded, hasSession: isSignedIn, userId } = useCloudAuth(); const [optOutState, setOptOutState] = useLocalStorage( CONNECT_ONBOARDING_OPT_OUT_STORAGE_KEY, EMPTY_CONNECT_ONBOARDING_OPT_OUT_STATE, diff --git a/apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx b/apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx index b138a8232bdb6..b6780ec67fb8a 100644 --- a/apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx +++ b/apps/web/src/components/desktop/DesktopWebLinkCoordinator.tsx @@ -5,6 +5,7 @@ import { } from "@t3tools/client-runtime/state/runtime"; import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; +import { isDesktopOidcCallbackUrl } from "@t3tools/shared/oidc"; import { promptLinkAppPath } from "@t3tools/shared/promptLinks"; import { readPairingAppLink, resolveRemotePairingTarget } from "@t3tools/shared/remote"; import { useRouter } from "@tanstack/react-router"; @@ -201,6 +202,8 @@ export function DesktopWebLinkCoordinator() { }); const openLink = useEffectEvent(async (url: string) => { + // The OpenID Connect shell listens for its own callback. + if (isDesktopOidcCallbackUrl(url)) return; const pairingUrl = readPairingAppLink(url); if (pairingUrl !== null) { await pairFromLink(pairingUrl); diff --git a/apps/web/src/components/oidc/OidcCallbackSurface.tsx b/apps/web/src/components/oidc/OidcCallbackSurface.tsx new file mode 100644 index 0000000000000..3c38314046c50 --- /dev/null +++ b/apps/web/src/components/oidc/OidcCallbackSurface.tsx @@ -0,0 +1,57 @@ +import { useRouter } from "@tanstack/react-router"; +import { useEffect, useRef, useState } from "react"; + +import { getBrowserOidcController } from "../../cloud/oidc/browserOidc"; +import { Button } from "../ui/button"; +import { StandalonePage, StandalonePageHeader } from "../ui/standalone-page"; + +/** Where the identity provider sends the user back to; finishes the sign-in and returns to the app. */ +export function OidcCallbackSurface() { + const router = useRouter(); + const [failure, setFailure] = useState(() => + getBrowserOidcController() === null + ? "This build has no OpenID Connect provider configured." + : null, + ); + const started = useRef(false); + + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- completes the redirect from the identity provider exactly once when the page loads + useEffect(() => { + if (started.current) return; + started.current = true; + const controller = getBrowserOidcController(); + if (controller === null) return; + controller + .completeSignIn(window.location.href) + .then((returnTo) => router.history.replace(returnTo)) + .catch((cause: unknown) => + setFailure(cause instanceof Error ? cause.message : "The sign-in could not be completed."), + ); + }, [router]); + + return ( + + + {failure === null ? null : ( +
+ +
+ )} +
+ ); +} diff --git a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx new file mode 100644 index 0000000000000..c2e69430fc808 --- /dev/null +++ b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx @@ -0,0 +1,73 @@ +import type { OidcController } from "@t3tools/client-runtime/relay"; +import { type ReactNode, useEffect, useSyncExternalStore } from "react"; + +import { CloudAuthProvider, type CloudAuth } from "../../cloud/cloudAuth"; +import { createManagedRelayAccountService } from "../../cloud/managedAuth"; +import { + getBrowserOidcController, + signInThroughDesktopBrowser, +} from "../../cloud/oidc/browserOidc"; +import { toastManager } from "../ui/toast"; + +// The relay verifies the ID token, so that is what the controller hands out as the relay token. +const accountService = createManagedRelayAccountService( + (controller) => controller.getToken, +); + +function reportSignInFailure(cause: unknown) { + toastManager.add({ + type: "error", + title: "Could not start sign-in", + description: cause instanceof Error ? cause.message : "The identity provider did not respond.", + }); +} + +function ConfiguredOidcShell({ + controller, + children, +}: { + readonly controller: OidcController; + readonly children: ReactNode; +}) { + const snapshot = useSyncExternalStore(controller.subscribe, controller.getSnapshot); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches the OIDC controller to the managed relay account service for the provider's lifetime + useEffect(() => accountService.attach(controller), [controller]); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- another tab signs in, refreshes, or signs out through shared storage + useEffect(() => { + const onStorage = (event: StorageEvent) => { + if (event.key === null || event.key === controller.storageKey) controller.syncFromStorage(); + }; + window.addEventListener("storage", onStorage); + return () => window.removeEventListener("storage", onStorage); + }, [controller]); + + const value: CloudAuth = { + provider: "oidc", + isLoaded: true, + isSignedIn: snapshot.userId !== null, + hasSession: snapshot.userId !== null, + userId: snapshot.userId, + accountLabel: snapshot.label, + getToken: controller.getToken, + signIn: () => { + const signedIn = + signInThroughDesktopBrowser(controller) ?? + controller.signIn(`${window.location.pathname}${window.location.search}`); + void signedIn.catch(reportSignInFailure); + }, + signOut: controller.signOut, + openAccount: null, + }; + + return {children}; +} + +/** Lazy-loaded when the build configures an OpenID Connect provider; Clerk is not loaded then. */ +export default function OidcManagedAuthShell({ children }: { readonly children: ReactNode }) { + const controller = getBrowserOidcController(); + return controller === null ? ( + children + ) : ( + {children} + ); +} diff --git a/apps/web/src/components/onboarding/WelcomePage.tsx b/apps/web/src/components/onboarding/WelcomePage.tsx index 48171ccf94457..6ef30f3a20d59 100644 --- a/apps/web/src/components/onboarding/WelcomePage.tsx +++ b/apps/web/src/components/onboarding/WelcomePage.tsx @@ -1,4 +1,4 @@ -import { useAuth } from "@clerk/react"; +import { useCloudAuth } from "../../cloud/cloudAuth"; import { useAtomValue } from "@effect/atom-react"; import type { AgentSessionProjectCandidate, @@ -960,7 +960,7 @@ function PhoneStep({ step, children }: { readonly step: number; readonly childre } function PhoneConnectLink({ environmentId }: { readonly environmentId: EnvironmentId }) { - const { isLoaded, isSignedIn, userId } = useAuth({ treatPendingAsSignedOut: false }); + const { isLoaded, isSignedIn, userId } = useCloudAuth(); const { openAuthPrompt } = useT3ConnectAuthPrompt(); const linkState = useCloudLinkState(environmentId); const refreshLinkState = useEffectEvent(linkState.refresh); diff --git a/apps/web/src/components/settings/LabsSettingsPanel.tsx b/apps/web/src/components/settings/LabsSettingsPanel.tsx index 38be7d611518e..5f4c9974e38db 100644 --- a/apps/web/src/components/settings/LabsSettingsPanel.tsx +++ b/apps/web/src/components/settings/LabsSettingsPanel.tsx @@ -1,7 +1,7 @@ -import { useAuth } from "@clerk/react"; import { Link } from "@tanstack/react-router"; import { managedCapabilityDenial } from "@t3tools/contracts"; +import { useCloudAuth } from "~/cloud/cloudAuth"; import { hasCloudPublicConfig } from "~/cloud/publicConfig"; import { useT3ConnectAuthPrompt } from "../clerk/useT3ConnectAuthPrompt"; import { Button } from "../ui/button"; @@ -77,10 +77,10 @@ function BoringSidebarMultilineRow() { /** Signed out of T3 Connect, the row offers to sign in instead of its switch. */ function GuestLinksRowWithSignIn() { - const { isSignedIn } = useAuth(); + const { isLoaded, isSignedIn } = useCloudAuth(); const { openAuthPrompt } = useT3ConnectAuthPrompt(); - // `isSignedIn` is undefined until Clerk loads. - if (isSignedIn !== false) return ; + // Until the provider loads, the row keeps its switch rather than flashing a sign-in prompt. + if (!isLoaded || isSignedIn) return ; return ( { const app = ; -// Split by runtime to keep Clerk out of local startup and clerk-js out of browser builds. -const managedAuthShellModule = - clerkPublishableKey && hasCloudPublicConfig() - ? isElectron - ? import("./components/clerk/ElectronManagedAuthShell") - : import("./components/clerk/BrowserManagedAuthShell") - : null; - -export const startup = Promise.all([ - managedAuthShellModule?.then((module) => module.default) ?? null, - router.load(), -]) - .then(([ManagedAuthShell]) => { +// Split by provider and runtime to keep sign-in code out of local startup, and clerk-js out of +// browser builds. An OpenID Connect provider, when configured, replaces Clerk entirely. +const authProvider = cloudAuthProvider(); +const managedAuthShell: Promise<(app: React.ReactNode) => React.ReactNode> | null = + authProvider === "oidc" + ? import("./components/oidc/OidcManagedAuthShell").then(({ default: Shell }) => (child) => ( + {child} + )) + : authProvider === "clerk" && clerkPublishableKey + ? (isElectron + ? import("./components/clerk/ElectronManagedAuthShell") + : import("./components/clerk/BrowserManagedAuthShell") + ).then(({ default: Shell }) => (child) => ( + {child} + )) + : null; + +export const startup = Promise.all([managedAuthShell, router.load()]) + .then(([wrapWithManagedAuth]) => { // Route chunk failures resolve router.load() and surface later through the error boundary. if (reloadScheduled) return; if (!chunkLoadFailed) clearChunkReloadGuard(); ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( - - {ManagedAuthShell && clerkPublishableKey ? ( - {app} - ) : ( - app - )} - , + {wrapWithManagedAuth ? wrapWithManagedAuth(app) : app}, ); }) .catch((error: unknown) => { diff --git a/apps/web/src/routeTree.gen.ts b/apps/web/src/routeTree.gen.ts index 1b93b3966b4ef..cae208a911d34 100644 --- a/apps/web/src/routeTree.gen.ts +++ b/apps/web/src/routeTree.gen.ts @@ -18,6 +18,7 @@ import { Route as ApproveAgentRouteImport } from './routes/approve-agent' import { Route as ChatRouteImport } from './routes/_chat' import { Route as ChatIndexRouteImport } from './routes/_chat.index' import { Route as ProjectsProjectKeyRouteImport } from './routes/projects.$projectKey' +import { Route as OidcCallbackRouteImport } from './routes/oidc.callback' import { Route as ChatUsageRouteImport } from './routes/_chat.usage' import { Route as ChatSettingsRouteImport } from './routes/_chat.settings' import { Route as ChatPullRequestsRouteImport } from './routes/_chat.pull-requests' @@ -97,6 +98,11 @@ const ProjectsProjectKeyRoute = ProjectsProjectKeyRouteImport.update({ path: '/projects/$projectKey', getParentRoute: () => rootRouteImport, } as any) +const OidcCallbackRoute = OidcCallbackRouteImport.update({ + id: '/oidc/callback', + path: '/oidc/callback', + getParentRoute: () => rootRouteImport, +} as any) const ChatUsageRoute = ChatUsageRouteImport.update({ id: '/usage', path: '/usage', @@ -286,6 +292,7 @@ export interface FileRoutesByFullPath { '/pull-requests': typeof ChatPullRequestsRoute '/settings': typeof ChatSettingsRouteWithChildren '/usage': typeof ChatUsageRoute + '/oidc/callback': typeof OidcCallbackRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute '/draft/$draftId': typeof ChatDraftDraftIdRoute @@ -329,6 +336,7 @@ export interface FileRoutesByTo { '/pull-requests': typeof ChatPullRequestsRoute '/settings': typeof ChatSettingsRouteWithChildren '/usage': typeof ChatUsageRoute + '/oidc/callback': typeof OidcCallbackRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/': typeof ChatIndexRoute '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute @@ -375,6 +383,7 @@ export interface FileRoutesById { '/_chat/pull-requests': typeof ChatPullRequestsRoute '/_chat/settings': typeof ChatSettingsRouteWithChildren '/_chat/usage': typeof ChatUsageRoute + '/oidc/callback': typeof OidcCallbackRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/_chat/': typeof ChatIndexRoute '/_chat/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute @@ -422,6 +431,7 @@ export interface FileRouteTypes { | '/pull-requests' | '/settings' | '/usage' + | '/oidc/callback' | '/projects/$projectKey' | '/$environmentId/$threadId' | '/draft/$draftId' @@ -465,6 +475,7 @@ export interface FileRouteTypes { | '/pull-requests' | '/settings' | '/usage' + | '/oidc/callback' | '/projects/$projectKey' | '/' | '/$environmentId/$threadId' @@ -510,6 +521,7 @@ export interface FileRouteTypes { | '/_chat/pull-requests' | '/_chat/settings' | '/_chat/usage' + | '/oidc/callback' | '/projects/$projectKey' | '/_chat/' | '/_chat/$environmentId/$threadId' @@ -552,6 +564,7 @@ export interface RootRouteChildren { GuestRoute: typeof GuestRoute PairRoute: typeof PairRoute WelcomeRoute: typeof WelcomeRoute + OidcCallbackRoute: typeof OidcCallbackRoute ProjectsProjectKeyRoute: typeof ProjectsProjectKeyRoute ThreadsEnvironmentIdThreadIdRoute: typeof ThreadsEnvironmentIdThreadIdRoute } @@ -621,6 +634,13 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof ProjectsProjectKeyRouteImport parentRoute: typeof rootRouteImport } + '/oidc/callback': { + id: '/oidc/callback' + path: '/oidc/callback' + fullPath: '/oidc/callback' + preLoaderRoute: typeof OidcCallbackRouteImport + parentRoute: typeof rootRouteImport + } '/_chat/usage': { id: '/_chat/usage' path: '/usage' @@ -956,6 +976,7 @@ const rootRouteChildren: RootRouteChildren = { GuestRoute: GuestRoute, PairRoute: PairRoute, WelcomeRoute: WelcomeRoute, + OidcCallbackRoute: OidcCallbackRoute, ProjectsProjectKeyRoute: ProjectsProjectKeyRoute, ThreadsEnvironmentIdThreadIdRoute: ThreadsEnvironmentIdThreadIdRoute, } diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index c4a3f15b3d126..6b878a50f82bd 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -160,7 +160,8 @@ function RootRouteBranch() { pathname === "/pair" || pathname === "/connect" || pathname === "/connect-agent" || - pathname === "/approve-agent" + pathname === "/approve-agent" || + pathname === "/oidc/callback" ) { return ; } diff --git a/apps/web/src/routes/oidc.callback.tsx b/apps/web/src/routes/oidc.callback.tsx new file mode 100644 index 0000000000000..a7aed07aeb9b1 --- /dev/null +++ b/apps/web/src/routes/oidc.callback.tsx @@ -0,0 +1,7 @@ +import { createFileRoute } from "@tanstack/react-router"; + +import { OidcCallbackSurface } from "../components/oidc/OidcCallbackSurface"; + +export const Route = createFileRoute("/oidc/callback")({ + component: OidcCallbackSurface, +}); diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 217294de3762c..a202a062dbf62 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -38,6 +38,8 @@ const configuredRelayUrl = repoEnv.VITE_T3CODE_RELAY_URL?.trim() || ""; const configuredClerkPublishableKey = repoEnv.VITE_CLERK_PUBLISHABLE_KEY?.trim() || ""; const configuredClerkJwtTemplate = repoEnv.VITE_CLERK_JWT_TEMPLATE?.trim() || ""; const configuredClerkCliOAuthClientId = repoEnv.VITE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || ""; +const configuredOidcIssuer = repoEnv.VITE_OIDC_ISSUER?.trim() || ""; +const configuredOidcClientId = repoEnv.VITE_OIDC_CLIENT_ID?.trim() || ""; const configuredRelayTracingUrl = repoEnv.VITE_RELAY_OTLP_TRACES_URL?.trim() || ""; const configuredRelayTracingDataset = repoEnv.VITE_RELAY_OTLP_TRACES_DATASET?.trim() || ""; const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim() || ""; @@ -307,6 +309,8 @@ export default defineConfig(() => { "import.meta.env.VITE_CLERK_CLI_OAUTH_CLIENT_ID": JSON.stringify( configuredClerkCliOAuthClientId, ), + "import.meta.env.VITE_OIDC_ISSUER": JSON.stringify(configuredOidcIssuer), + "import.meta.env.VITE_OIDC_CLIENT_ID": JSON.stringify(configuredOidcClientId), "import.meta.env.VITE_RELAY_OTLP_TRACES_URL": JSON.stringify(configuredRelayTracingUrl), "import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET": JSON.stringify( configuredRelayTracingDataset, diff --git a/infra/relay/selfhost/.env.example b/infra/relay/selfhost/.env.example new file mode 100644 index 0000000000000..574390a9dc2b4 --- /dev/null +++ b/infra/relay/selfhost/.env.example @@ -0,0 +1,22 @@ +# Copy to .env and fill in. Nothing here is a secret except POSTGRES_PASSWORD. + +# Public HTTPS origin clients use to reach the relay, served by Caddy. +RELAY_PUBLIC_ORIGIN=https://relay.example.com +# Host name Caddy serves and obtains a certificate for. +RELAY_DOMAIN=relay.example.com + +# Your OpenID Connect issuer, exactly as its discovery document reports it +# (including any path prefix, without a trailing slash unless the issuer has one). +OIDC_ISSUER=https://sso.example.com/realms/t3 +# The OAuth client id of the T3 Code apps. Clients send their ID token, whose `aud` is that id. +OIDC_AUDIENCE=t3-code + +# Environments you expose yourself (no tunnel): "public" brokers internet-reachable HTTPS +# addresses, "private" also LAN/Tailscale addresses, "off" refuses them. +RELAY_MANUAL_ENDPOINTS=public + +POSTGRES_PASSWORD=change-me + +# Optional: host ports, when 80 and 443 are taken. +# HTTP_PORT=80 +# HTTPS_PORT=443 diff --git a/infra/relay/selfhost/.gitignore b/infra/relay/selfhost/.gitignore new file mode 100644 index 0000000000000..305fac26699cf --- /dev/null +++ b/infra/relay/selfhost/.gitignore @@ -0,0 +1,2 @@ +.env +secrets/ diff --git a/infra/relay/selfhost/Caddyfile b/infra/relay/selfhost/Caddyfile new file mode 100644 index 0000000000000..f1938b50ebb50 --- /dev/null +++ b/infra/relay/selfhost/Caddyfile @@ -0,0 +1,5 @@ +# Caddy terminates TLS and forwards to the relay. The relay trusts its configured +# RELAY_PUBLIC_ORIGIN, not forwarded headers, so no proxy header configuration is needed. +{$RELAY_DOMAIN} { + reverse_proxy relay:8080 +} diff --git a/infra/relay/selfhost/Dockerfile b/infra/relay/selfhost/Dockerfile new file mode 100644 index 0000000000000..e2666c3c21e0a --- /dev/null +++ b/infra/relay/selfhost/Dockerfile @@ -0,0 +1,22 @@ +# Self-hosted T3 relay. Build from the repository root (see README.md in this directory). +FROM node:26-alpine AS build +RUN npm install -g pnpm@12.14.0 vite-plus@1.4.0 +WORKDIR /repo +COPY . . +# Only the relay's dependency closure: the mobile and desktop apps are not needed here. +RUN pnpm install --frozen-lockfile --ignore-scripts --filter "t3code-relay..." +# Production dependencies only; the bundle keeps third-party packages external. +RUN pnpm --filter t3code-relay deploy --prod /deploy +RUN vp pack infra/relay/src/node.ts --format esm --platform node --root infra/relay --out-dir /out --no-dts + +FROM node:26-alpine +WORKDIR /app +ENV NODE_ENV=production \ + PORT=8080 \ + RELAY_MIGRATIONS_FOLDER=/app/migrations/postgres +COPY --from=build /deploy/node_modules /app/node_modules +COPY --from=build /out /app +COPY --from=build /repo/infra/relay/migrations/postgres /app/migrations/postgres +USER node +EXPOSE 8080 +CMD ["node", "src/node.mjs"] diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md new file mode 100644 index 0000000000000..56ce3c8c0a5aa --- /dev/null +++ b/infra/relay/selfhost/README.md @@ -0,0 +1,127 @@ +# Self-hosted T3 relay + +Runs the T3 Connect control plane on your own server with Docker Compose: a relay, Postgres, +and Caddy terminating HTTPS. Sign-in comes from your own OpenID Connect provider, so no Clerk +account is needed. + +Every web, desktop, mobile and CLI client signed in to the same account sees the same linked +environments, and opens their projects and threads live through them; that is how clients stay +in sync. Thread data stays on each environment, never on the relay. + +## Setup + +Run these from the repository root. + +1. Create the cloud-mint signing keys (once): + + ```sh + infra/relay/selfhost/gen-keys.sh + ``` + +2. Configure the stack: + + ```sh + cp infra/relay/selfhost/.env.example infra/relay/selfhost/.env + ``` + + Set `RELAY_DOMAIN` and `RELAY_PUBLIC_ORIGIN` to the host name that resolves to this server, + `OIDC_ISSUER` to your provider's issuer URL, and `POSTGRES_PASSWORD` to a real password. + Keep the generated mint keys and the Postgres password in your secret manager. + +3. In your identity provider, create one public OAuth client (authorization code with PKCE, no + secret) for the T3 Code apps, and set `OIDC_AUDIENCE` to its client id. The apps send the + provider's **ID token** to the relay, so its `aud` is that client id and its `sub` identifies + the user. Allow these redirect URIs: + + | Client | Redirect URI | + | ------- | -------------------------------------------------------------- | + | Web | `https:///oidc/callback` | + | Desktop | `t3code://app/oidc/callback` (`t3code-dev://…` for dev builds) | + | Mobile | `t3code://oidc` (`t3code-dev://oidc`, `t3code-preview://oidc`) | + | CLI | `http://127.0.0.1:34338/callback`, or enable the device flow | + + Request the `offline_access` scope so sessions refresh without signing in again. + +4. Start it: + + ```sh + docker compose -f infra/relay/selfhost/docker-compose.yml --env-file infra/relay/selfhost/.env up -d --build + ``` + +5. Check it: + + ```sh + curl https:///health # {"ok":true,"service":"relay"} + ``` + + Requests without a bearer token get `401`. Caddy obtains the certificate automatically when + `RELAY_DOMAIN` is a public name that reaches this server on ports 80 and 443. + +## Backups and upgrades + +The Postgres volume is the source of truth for relay state. Back it up with your normal +Postgres tooling, for example: + +```sh +docker compose -f infra/relay/selfhost/docker-compose.yml exec -T postgres \ + pg_dump -U relay relay > relay-$(date +%Y%m%d).sql +``` + +Back up the `infra/relay/selfhost/secrets` directory separately. Restore the database and keys +before starting a new relay image; migrations run automatically and the process refuses to serve +if a migration fails. + +## Point the apps at this relay + +The apps read the relay and identity provider at build time. Set these in the repository's +`.env` before building web, desktop or mobile (`scripts/lib/public-config.ts` maps them to each +app's prefix): + +```sh +T3CODE_RELAY_URL=https://relay.example.com +T3CODE_OIDC_ISSUER=https://sso.example.com/realms/t3 +T3CODE_OIDC_CLIENT_ID=t3-code +``` + +A configured OpenID Connect provider replaces Clerk in that build. The `t3` server and CLI also +read the same three variables at runtime, so an installed server can switch without a rebuild. + +## Reach an environment without a tunnel + +With `RELAY_MANUAL_ENDPOINTS=public` (the default here), an environment can publish an HTTPS +address you already expose (reverse proxy, port forward) and the relay brokers connections to it +like a managed endpoint. Run `t3 connect --publish-only`, then start the server with +`T3CODE_PUBLIC_URL` set to that address: + +```sh +t3 connect --publish-only +T3CODE_PUBLIC_URL=https://t3.example.com t3 serve +``` + +The address must serve the environment's HTTP and WebSocket traffic over HTTPS. The relay +refuses loopback, link-local and cloud metadata addresses. `private` also accepts private +network addresses (LAN, Tailscale) for relays only your own network uses; `off` refuses all of +them. Host names are checked as written, not resolved, so only point a relay you trust at names +you control. + +## Behind another reverse proxy + +Caddy is the bundled proxy. To use your own (nginx, Traefik, a cloud load balancer), drop the +`caddy` service, point your proxy at the relay's port 8080, and keep `RELAY_PUBLIC_ORIGIN` at +the public HTTPS address. The relay builds no URLs from request headers, so forwarded `Host` or +`X-Forwarded-*` values are ignored. `OIDC_ISSUER` must equal the issuer your provider reports +publicly, including any path prefix your proxy adds. + +## What this host does not do + +- **Managed tunnels.** Cloudflare-managed endpoints are not available; use a manual endpoint + (above) or reach environments directly. +- **Push notifications.** APNs and FCM need credentials tied to the mobile app's signing + identity, so this stack turns them off. To enable them, add `APNS_ENABLED=true`, the `APNS_*` + or `FCM_SERVICE_ACCOUNT` variables and a stable `APNS_DELIVERY_JOB_SIGNING_SECRET` (for + example `openssl rand -hex 32`) to the `relay` service environment. +- **Held webhooks.** Webhooks are not stored while an environment is offline. +- **Clerk accounts and T3 Chat linking.** Sign-in is OIDC only. Adding `CLERK_SECRET_KEY`, + `CLERK_PUBLISHABLE_KEY` and `CLERK_JWT_AUDIENCE` to the `relay` service also accepts Clerk. +- **Scale.** Queues and rate limits are in memory, so run one replica. Queued push jobs do not + survive a restart. diff --git a/infra/relay/selfhost/docker-compose.yml b/infra/relay/selfhost/docker-compose.yml new file mode 100644 index 0000000000000..94299e9c3e45d --- /dev/null +++ b/infra/relay/selfhost/docker-compose.yml @@ -0,0 +1,79 @@ +# Self-hosted T3 relay: Postgres, the relay, and Caddy as the HTTPS reverse proxy. +# Usage and configuration: see README.md next to this file. +name: t3-relay + +services: + postgres: + image: postgres:16-alpine + restart: unless-stopped + environment: + POSTGRES_USER: relay + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} + POSTGRES_DB: relay + volumes: + - postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U relay -d relay"] + interval: 5s + timeout: 3s + retries: 30 + + relay: + build: + context: ../../.. + dockerfile: infra/relay/selfhost/Dockerfile + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + environment: + DATABASE_URL: postgres://relay:${POSTGRES_PASSWORD}@postgres:5432/relay + RELAY_PUBLIC_ORIGIN: ${RELAY_PUBLIC_ORIGIN:?set RELAY_PUBLIC_ORIGIN in .env} + OIDC_ISSUER: ${OIDC_ISSUER:?set OIDC_ISSUER in .env} + OIDC_AUDIENCE: ${OIDC_AUDIENCE:?set OIDC_AUDIENCE in .env} + RELAY_MANUAL_ENDPOINTS: ${RELAY_MANUAL_ENDPOINTS:-public} + CLOUD_MINT_PRIVATE_KEY_FILE: /run/secrets/cloud_mint_private_key + CLOUD_MINT_PUBLIC_KEY_FILE: /run/secrets/cloud_mint_public_key + # Push notifications need the publisher's APNs and FCM credentials; leave them off here. + APNS_ENABLED: "false" + secrets: + - cloud_mint_private_key + - cloud_mint_public_key + healthcheck: + test: + [ + "CMD", + "node", + "-e", + "fetch('http://127.0.0.1:8080/health').then((r) => { if (!r.ok) process.exit(1) }).catch(() => process.exit(1))", + ] + interval: 10s + timeout: 5s + retries: 12 + + caddy: + image: caddy:2-alpine + restart: unless-stopped + depends_on: + relay: + condition: service_healthy + ports: + - "${HTTP_PORT:-80}:80" + - "${HTTPS_PORT:-443}:443" + environment: + RELAY_DOMAIN: ${RELAY_DOMAIN:?set RELAY_DOMAIN in .env} + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile:ro + - caddy-data:/data + - caddy-config:/config + +secrets: + cloud_mint_private_key: + file: ./secrets/cloud-mint-private.pem + cloud_mint_public_key: + file: ./secrets/cloud-mint-public.pem + +volumes: + postgres-data: + caddy-data: + caddy-config: diff --git a/infra/relay/selfhost/gen-keys.sh b/infra/relay/selfhost/gen-keys.sh new file mode 100644 index 0000000000000..1b53190fde0a6 --- /dev/null +++ b/infra/relay/selfhost/gen-keys.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# Creates the relay's cloud-mint signing key pair (Ed25519). Run once and keep the private key. +set -eu +dir="$(dirname "$0")/secrets" +mkdir -p "$dir" +if [ -f "$dir/cloud-mint-private.pem" ]; then + echo "Keys already exist in $dir; not overwriting them." + exit 0 +fi +openssl genpkey -algorithm ed25519 -out "$dir/cloud-mint-private.pem" +openssl pkey -in "$dir/cloud-mint-private.pem" -pubout -out "$dir/cloud-mint-public.pem" +chmod 600 "$dir/cloud-mint-private.pem" +echo "Wrote $dir/cloud-mint-private.pem and $dir/cloud-mint-public.pem" diff --git a/infra/relay/src/Config.ts b/infra/relay/src/Config.ts index ecc71a5a96a47..1939792682e07 100644 --- a/infra/relay/src/Config.ts +++ b/infra/relay/src/Config.ts @@ -5,6 +5,8 @@ import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; +import type { SubjectTokenVerificationFailed, VerifiedSubject } from "./auth/SubjectTokens.ts"; + export const ApnsEnvironment = Schema.Literals(["sandbox", "production"]); export type ApnsEnvironment = typeof ApnsEnvironment.Type; @@ -54,15 +56,31 @@ export interface T3ChatSettings { readonly linkSecret: Redacted.Redacted; } +// Whether the relay brokers connections to endpoints an environment declares itself, with no tunnel. +// "public" accepts internet-reachable HTTPS addresses; "private" also accepts private networks. +export const ManualEndpointsMode = Schema.Literals(["off", "public", "private"]); +export type ManualEndpointsMode = typeof ManualEndpointsMode.Type; + +export interface OidcSettings { + // Must equal the issuer's discovery `issuer` exactly, so a path prefix or trailing slash matters. + readonly issuer: string; + readonly verify: ( + token: string, + ) => Effect.Effect; +} + export class RelayConfiguration extends Context.Service< RelayConfiguration, { readonly relayIssuer: string; readonly apns: ApnsCredentials | null; readonly fcmServiceAccount?: Redacted.Redacted; - readonly clerkSecretKey: Redacted.Redacted; - readonly clerkPublishableKey: string; - readonly clerkJwtAudience: string; + // Optional: a self-hosted relay can run on OIDC alone. + readonly clerkSecretKey?: Redacted.Redacted; + readonly clerkPublishableKey?: string; + readonly clerkJwtAudience?: string; + readonly oidc?: OidcSettings; + readonly manualEndpoints?: ManualEndpointsMode; readonly apnsDeliveryJobSigningSecret: Redacted.Redacted; readonly cloudMintPrivateKey: Redacted.Redacted; readonly cloudMintPublicKey: string; diff --git a/infra/relay/src/agentActivity/ApnsDeliveryQueue.test.ts b/infra/relay/src/agentActivity/ApnsDeliveryQueue.test.ts index 358698dd197b3..2c1a14dfe76c7 100644 --- a/infra/relay/src/agentActivity/ApnsDeliveryQueue.test.ts +++ b/infra/relay/src/agentActivity/ApnsDeliveryQueue.test.ts @@ -1,7 +1,5 @@ import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; import { describe, expect, it } from "@effect/vitest"; -import * as Alchemy from "alchemy"; -import * as Cloudflare from "@alchemy.run/cloudflare"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; @@ -29,18 +27,15 @@ const config: RelayConfiguration.RelayConfiguration["Service"] = { }; describe("ApnsDeliveryQueue", () => { - it.effect("does not require the deployment RuntimeContext when building the Worker layer", () => { + it.effect("builds from an injected sender without any host runtime", () => { const sent: unknown[] = []; - const sender: Cloudflare.Queues.WriteQueueClient = { - raw: Effect.die("raw queue binding is not used"), - send: (body) => + const sender = { + send: (body: unknown) => Effect.sync(() => { sent.push(body); }), - sendBatch: () => Effect.die("batch queue binding is not used"), }; - const runtimeContext = {} as Alchemy.BaseRuntimeContext; - const layer = ApnsDeliveryQueue.layerCloudflareQueues(sender, runtimeContext).pipe( + const layer = ApnsDeliveryQueue.layerWithSender(sender).pipe( Layer.provide(NodeCryptoLayer.layer), Layer.provide(RelayConfiguration.layer(config)), ); diff --git a/infra/relay/src/agentActivity/ApnsDeliveryQueue.ts b/infra/relay/src/agentActivity/ApnsDeliveryQueue.ts index 09cff5184b058..fa7aa0b4afb80 100644 --- a/infra/relay/src/agentActivity/ApnsDeliveryQueue.ts +++ b/infra/relay/src/agentActivity/ApnsDeliveryQueue.ts @@ -1,5 +1,3 @@ -import * as Alchemy from "alchemy"; -import * as Cloudflare from "@alchemy.run/cloudflare"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; @@ -25,6 +23,7 @@ import { type SignedApnsDeliveryJob, } from "./apnsDeliveryJobs.ts"; import * as RelayConfiguration from "../Config.ts"; +import type * as Ports from "../host/ports.ts"; export class ApnsDeliveryQueueSendError extends Schema.TaggedError()( "ApnsDeliveryQueueSendError", @@ -47,9 +46,7 @@ export type ApnsDeliveryQueueError = ApnsDeliveryQueueSendError; export class ApnsDeliveryQueueSender extends Context.Service< ApnsDeliveryQueueSender, { - readonly send: ( - body: SignedApnsDeliveryJob, - ) => Effect.Effect; + readonly send: Ports.DeliveryQueueSender["send"]; } >()("t3code-relay/agentActivity/ApnsDeliveryQueue/ApnsDeliveryQueueSender") {} @@ -210,22 +207,7 @@ const make = Effect.gen(function* () { export const layer = Layer.effect(ApnsDeliveryQueue, make); -export const layerCloudflareQueues = ( - sender: Cloudflare.Queues.WriteQueueClient, - alchemyRuntimeContext: Alchemy.BaseRuntimeContext, -) => +export const layerWithSender = (sender: Ports.DeliveryQueueSender) => layer.pipe( - Layer.provide( - Layer.succeed( - ApnsDeliveryQueueSender, - ApnsDeliveryQueueSender.of({ - send: Effect.fn("ApnsDeliveryQueueSender.send")( - function* (body) { - return yield* sender.send(body); - }, - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - ), - }), - ), - ), + Layer.provide(Layer.succeed(ApnsDeliveryQueueSender, ApnsDeliveryQueueSender.of(sender))), ); diff --git a/infra/relay/src/agentActivity/FcmDeliveries.test.ts b/infra/relay/src/agentActivity/FcmDeliveries.test.ts index 71cde83149284..7a4266d818daf 100644 --- a/infra/relay/src/agentActivity/FcmDeliveries.test.ts +++ b/infra/relay/src/agentActivity/FcmDeliveries.test.ts @@ -3,12 +3,10 @@ import type { RelayAgentActivityState } from "@t3tools/contracts/relay"; import { describe, expect, it } from "@effect/vitest"; import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; import * as Effect from "effect/Effect"; -import * as DateTime from "effect/DateTime"; +import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; -import * as Stream from "effect/Stream"; -import type * as Cloudflare from "@alchemy.run/cloudflare"; import * as FcmDeliveryQueueConsumer from "./FcmDeliveryQueueConsumer.ts"; import * as RelayConfiguration from "../Config.ts"; @@ -645,45 +643,25 @@ it("stops reducing five-character row fields and fits the remaining alert", () = }); describe("FCM queue message isolation", () => { + // The host acknowledges or retries each message on its own; the consumer only has to fail + // for the bad message and leave the healthy one deliverable. it.effect.each(["invalid-job", "fcm-rejection"] as const)( - "retries only the %s message and delivers the rest of its batch", + "fails only the %s message and still delivers the healthy one", (failure) => { const h = harness(); - const outcomes = new Map(); - const message = (id: string, body: unknown): Cloudflare.Queues.Message => ({ - id, - body, - timestamp: DateTime.toDateUtc(DateTime.makeUnsafe(0)), - attempts: 1, - ack: () => { - if (!outcomes.has(id)) outcomes.set(id, "ack"); - }, - retry: () => { - if (!outcomes.has(id)) outcomes.set(id, "retry"); - }, - }); - const batch = [ - message("failed", failure === "invalid-job" ? {} : h.job), - message("healthy", h.job), - ]; return Effect.gen(function* () { - yield* Stream.fromIterable(batch).pipe( - Stream.tap((item) => - Effect.sync(() => { - h.current.deliveryFailure = - item.id === "failed" && failure === "fcm-rejection" - ? new FcmClient.FcmClientError({ operation: "send", status: 400 }) - : null; - }), - ), - Stream.runForEach(FcmDeliveryQueueConsumer.processMessage), + h.current.deliveryFailure = + failure === "fcm-rejection" + ? new FcmClient.FcmClientError({ operation: "send", status: 400 }) + : null; + const failed = yield* Effect.exit( + FcmDeliveryQueueConsumer.processMessage(failure === "invalid-job" ? {} : h.job), ); - // Cloudflare ignores Alchemy's final batch acknowledgements for already-retried messages. - for (const item of batch) item.ack(); - expect([...outcomes]).toEqual([ - ["failed", "retry"], - ["healthy", "ack"], - ]); + expect(Exit.isFailure(failed)).toBe(true); + expect(h.sent).toHaveLength(0); + + h.current.deliveryFailure = null; + yield* FcmDeliveryQueueConsumer.processMessage(h.job); expect(h.sent).toHaveLength(1); expect(h.marked).toHaveLength(1); }).pipe(Effect.provide(h.layer)); diff --git a/infra/relay/src/agentActivity/FcmDeliveryQueueConsumer.ts b/infra/relay/src/agentActivity/FcmDeliveryQueueConsumer.ts index 0f34cab101139..052d8b27c42e7 100644 --- a/infra/relay/src/agentActivity/FcmDeliveryQueueConsumer.ts +++ b/infra/relay/src/agentActivity/FcmDeliveryQueueConsumer.ts @@ -1,24 +1,11 @@ -import type * as Cloudflare from "@alchemy.run/cloudflare"; import * as Effect from "effect/Effect"; import * as FcmDeliveries from "./FcmDeliveries.ts"; +// Throwing from the handler asks the host to retry the message; success acknowledges it. export const processMessage = Effect.fn("relay.fcm_delivery_queue.process_message")(function* ( - message: Cloudflare.Queues.Message, + body: unknown, ) { const deliveries = yield* FcmDeliveries.FcmDeliveries; - yield* deliveries.process(message.body).pipe( - Effect.matchEffect({ - // The enclosing operation owns the span for this inline handler. - onFailure: Effect.fnUntraced(function* (error) { - // Cloudflare keeps the first ack/retry decision; decide before Alchemy acknowledges the batch. - message.retry(); - yield* Effect.logWarning("FCM queue delivery failed; retrying message", { - messageId: message.id, - errorTag: error._tag, - }); - }), - onSuccess: () => Effect.sync(() => message.ack()), - }), - ); + yield* deliveries.process(body); }); diff --git a/infra/relay/src/agentActivity/FcmDeliveryQueueSender.ts b/infra/relay/src/agentActivity/FcmDeliveryQueueSender.ts index eb0279bbe0b41..b30b6b4d8f4b9 100644 --- a/infra/relay/src/agentActivity/FcmDeliveryQueueSender.ts +++ b/infra/relay/src/agentActivity/FcmDeliveryQueueSender.ts @@ -1,14 +1,11 @@ -import type * as Cloudflare from "@alchemy.run/cloudflare"; import * as Context from "effect/Context"; -import type * as Effect from "effect/Effect"; import type * as FcmDeliveries from "./FcmDeliveries.ts"; +import type * as Ports from "../host/ports.ts"; export class FcmDeliveryQueueSender extends Context.Service< FcmDeliveryQueueSender, { - readonly send: ( - body: FcmDeliveries.FcmDeliveryJob, - ) => Effect.Effect; + readonly send: Ports.DeliveryQueueSender["send"]; } >()("t3code-relay/agentActivity/FcmDeliveryQueueSender") {} diff --git a/infra/relay/src/auth/SubjectTokens.test.ts b/infra/relay/src/auth/SubjectTokens.test.ts new file mode 100644 index 0000000000000..23798753a340d --- /dev/null +++ b/infra/relay/src/auth/SubjectTokens.test.ts @@ -0,0 +1,232 @@ +import { verifyToken } from "@clerk/backend"; +import { expect, it } from "@effect/vitest"; +import * as Clock from "effect/Clock"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Signature from "effect/crypto/Signature"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as Jwk from "effect/jose/Jwk"; +import * as Jwks from "effect/jose/Jwks"; +import * as Jwt from "effect/jose/Jwt"; +import * as Layer from "effect/Layer"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; +import { vi } from "vite-plus/test"; + +import type * as RelayConfiguration from "../Config.ts"; +import { + makeOidcKeySet, + makeOidcVerifier, + oidcAccountId, + verifySubjectToken, +} from "./SubjectTokens.ts"; + +vi.mock("@clerk/backend", () => ({ + createClerkClient: vi.fn(), + verifyToken: vi.fn(), +})); + +const ISSUER = "https://sso.example.com/realms/t3"; +const AUDIENCE = "t3-code-relay"; +const CLERK_AUDIENCE = "t3-code-relay-clerk"; + +const crypto = Signature.layerWebCrypto.pipe(Layer.provideMerge(SecureRandom.layerWebCrypto)); +const Claims = Schema.Struct({ + iss: Schema.String, + aud: Schema.String, + sub: Schema.String, + iat: Schema.Number, + exp: Schema.Number, +}); + +const signToken = Effect.fn("test.signToken")(function* (options: { + readonly issuer?: string; + readonly audience?: string; + readonly subject?: string; + readonly expiresInSeconds?: number; + readonly privateKey: Jwk.PrivateKey; +}) { + const now = Math.floor((yield* Clock.currentTimeMillis) / 1000); + const token = yield* Jwt.sign( + Claims, + { + iss: options.issuer ?? ISSUER, + aud: options.audience ?? AUDIENCE, + sub: options.subject ?? "user-1", + iat: now, + exp: now + (options.expiresInSeconds ?? 300), + }, + options.privateKey, + { kid: "k1" }, + ); + return Redacted.value(token); +}); + +const settingsWith = (verify: RelayConfiguration.OidcSettings["verify"]) => + ({ + relayIssuer: "https://relay.example.com", + clerkSecretKey: Redacted.make("clerk-secret-key"), + clerkPublishableKey: "pk_test", + clerkJwtAudience: CLERK_AUDIENCE, + oidc: { issuer: ISSUER, verify }, + }) as unknown as RelayConfiguration.RelayConfiguration["Service"]; + +const requestUrl = (input: RequestInfo | URL) => + input instanceof Request ? input.url : String(input); + +// FetchHttpClient caches the global fetch it first sees, so tests inject theirs instead of spying. +const fakeFetch = (discovery: unknown, keys: ReadonlyArray = []) => + vi.fn(async (input: RequestInfo | URL) => + requestUrl(input).endsWith("/.well-known/openid-configuration") + ? Response.json(discovery) + : Response.json({ keys }), + ); + +const discoverWith = (fetches: ReturnType) => + makeOidcKeySet(ISSUER).pipe( + Effect.provide(FetchHttpClient.layer), + Effect.provideService(FetchHttpClient.Fetch, fetches as unknown as typeof fetch), + ); + +const fixture = Effect.gen(function* () { + const keys = yield* Jwk.generateKeyPair({ algorithm: "ES256", kid: "k1" }); + const signingKey = yield* Jwk.importPrivate(keys.privateKey, "ES256"); + const jwks = yield* Jwks.makeLocal({ keys: [keys.publicKey] }); + const verify = yield* makeOidcVerifier({ + issuer: ISSUER, + audience: AUDIENCE, + keySet: Effect.succeed(jwks), + }); + return { + publicKey: keys.publicKey, + settings: settingsWith(verify), + sign: (options: Omit[0], "privateKey"> = {}) => + signToken({ privateKey: signingKey, ...options }), + }; +}); + +it.layer(crypto)("SubjectTokens", (it) => { + it.effect("accepts a token from the configured issuer under a namespaced account id", () => + Effect.gen(function* () { + const f = yield* fixture; + const token = yield* f.sign(); + + expect(yield* verifySubjectToken(f.settings, token, { acceptClerkOAuth: false })).toEqual({ + sub: oidcAccountId(ISSUER, "user-1"), + mode: "oidc_bearer", + }); + expect(verifyToken).not.toHaveBeenCalled(); + }), + ); + + it.effect("rejects configured-issuer tokens for another audience", () => + Effect.gen(function* () { + const f = yield* fixture; + const token = yield* f.sign({ audience: "someone-else" }); + + const exit = yield* Effect.exit( + verifySubjectToken(f.settings, token, { acceptClerkOAuth: false }), + ); + expect(Exit.isFailure(exit)).toBe(true); + }), + ); + + it.effect("rejects expired configured-issuer tokens", () => + Effect.gen(function* () { + const f = yield* fixture; + const token = yield* f.sign({ expiresInSeconds: -60 }); + + const exit = yield* Effect.exit( + verifySubjectToken(f.settings, token, { acceptClerkOAuth: false }), + ); + expect(Exit.isFailure(exit)).toBe(true); + }), + ); + + it.effect("rejects tokens signed by a key the issuer does not publish", () => + Effect.gen(function* () { + const f = yield* fixture; + const attacker = yield* Jwk.generateKeyPair({ algorithm: "ES256", kid: "k1" }); + const token = yield* signToken({ + privateKey: yield* Jwk.importPrivate(attacker.privateKey, "ES256"), + }); + + const exit = yield* Effect.exit( + verifySubjectToken(f.settings, token, { acceptClerkOAuth: false }), + ); + expect(Exit.isFailure(exit)).toBe(true); + }), + ); + + it.effect("sends other issuers through Clerk unchanged", () => + Effect.gen(function* () { + const f = yield* fixture; + vi.mocked(verifyToken).mockResolvedValue({ + sub: "user_clerk", + aud: CLERK_AUDIENCE, + } as never); + const token = yield* f.sign({ issuer: "https://clerk.example.com" }); + + expect(yield* verifySubjectToken(f.settings, token, { acceptClerkOAuth: false })).toEqual({ + sub: "user_clerk", + mode: "clerk_session_bearer", + }); + expect(verifyToken).toHaveBeenCalledWith(token, { + secretKey: "clerk-secret-key", + audience: CLERK_AUDIENCE, + }); + }).pipe( + Effect.ensuring( + Effect.sync(() => { + vi.mocked(verifyToken).mockReset(); + }), + ), + ), + ); + + it.effect("keeps the configured issuer string verbatim behind a path-prefixed proxy", () => + Effect.gen(function* () { + const fetches = fakeFetch({ + issuer: ISSUER, + jwks_uri: `${ISSUER}/protocol/openid-connect/certs`, + }); + const [keySet] = yield* discoverWith(fetches); + const resolved = yield* keySet; + + expect(resolved).toBe(yield* keySet); + expect(fetches.mock.calls.map(([input]) => requestUrl(input))).toEqual([ + `${ISSUER}/.well-known/openid-configuration`, + ]); + }).pipe(Effect.scoped), + ); + + it.effect("verifies a token against the key set the issuer's discovery points to", () => + Effect.gen(function* () { + const f = yield* fixture; + const fetches = fakeFetch({ issuer: ISSUER, jwks_uri: `${ISSUER}/certs` }, [f.publicKey]); + const [keySet] = yield* discoverWith(fetches); + const verifyRemote = yield* makeOidcVerifier({ issuer: ISSUER, audience: AUDIENCE, keySet }); + + expect(yield* verifyRemote(yield* f.sign())).toEqual({ + sub: oidcAccountId(ISSUER, "user-1"), + mode: "oidc_bearer", + }); + expect(fetches.mock.calls.map(([input]) => requestUrl(input))).toEqual([ + `${ISSUER}/.well-known/openid-configuration`, + `${ISSUER}/certs`, + ]); + }).pipe(Effect.scoped), + ); + + it.effect("refuses discovery documents whose issuer differs by a trailing slash", () => + Effect.gen(function* () { + const [keySet] = yield* discoverWith( + fakeFetch({ issuer: `${ISSUER}/`, jwks_uri: `${ISSUER}/certs` }), + ); + + const error = yield* Effect.flip(keySet); + expect(error.cause).toBe("oidc_issuer_mismatch"); + }).pipe(Effect.scoped), + ); +}); diff --git a/infra/relay/src/auth/SubjectTokens.ts b/infra/relay/src/auth/SubjectTokens.ts new file mode 100644 index 0000000000000..323ff8c759a8c --- /dev/null +++ b/infra/relay/src/auth/SubjectTokens.ts @@ -0,0 +1,220 @@ +import { createClerkClient, verifyToken } from "@clerk/backend"; +import { decodeIdTokenClaims, oidcDiscoveryUrl } from "@t3tools/shared/oidc"; +import * as Effect from "effect/Effect"; +import * as Duration from "effect/Duration"; +import * as Exit from "effect/Exit"; +import * as Redacted from "effect/Redacted"; +import * as Scope from "effect/Scope"; +import * as Schema from "effect/Schema"; +import * as Signature from "effect/crypto/Signature"; +import * as Jwks from "effect/jose/Jwks"; +import * as Jwt from "effect/jose/Jwt"; +import * as HttpClient from "effect/http/HttpClient"; +import * as HttpClientRequest from "effect/http/HttpClientRequest"; + +import type * as RelayConfiguration from "../Config.ts"; + +export class SubjectTokenVerificationFailed extends Schema.TaggedError()( + "SubjectTokenVerificationFailed", + { + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "Subject token verification failed"; + } +} + +export type SubjectTokenMode = "clerk_session_bearer" | "clerk_oauth_bearer" | "oidc_bearer"; + +export interface VerifiedSubject { + readonly sub: string; + readonly mode: SubjectTokenMode; +} + +const OIDC_ACCOUNT_PREFIX = "oidc:"; +// Issuers are URLs and cannot contain a raw "|", so the first one always ends the issuer. +export const oidcAccountId = (issuer: string, subject: string): string => + `${OIDC_ACCOUNT_PREFIX}${issuer}|${subject}`; +export const isOidcAccountId = (accountId: string): boolean => + accountId.startsWith(OIDC_ACCOUNT_PREFIX); + +const OIDC_ALGORITHMS = ["RS256", "ES256", "EdDSA"] as const; +const OIDC_DISCOVERY_TIMEOUT = "10 seconds"; + +const OidcDiscoveryDocument = Schema.Struct({ + issuer: Schema.String, + jwks_uri: Schema.String, +}); +const decodeOidcDiscoveryDocument = Schema.decodeUnknownEffect(OidcDiscoveryDocument); +const isSubjectTokenVerificationFailed = Schema.is(SubjectTokenVerificationFailed); + +/** + * Discovers the issuer's key set once; `Jwks` then refetches it on its own TTL and on unknown keys. + * The issuer must match the configured string exactly, including a path prefix or trailing slash; + * a reverse proxy that rewrites the public origin should be reflected in the configured issuer. + */ +export const makeOidcKeySet = Effect.fn("relay.auth.make_oidc_key_set")(function* (issuer: string) { + const httpClient = yield* HttpClient.HttpClient; + const scope = yield* Effect.scope; + const discover = Effect.gen(function* () { + const response = yield* httpClient.execute(HttpClientRequest.get(oidcDiscoveryUrl(issuer))); + if (response.status !== 200) { + return yield* new SubjectTokenVerificationFailed({ + cause: `oidc_discovery_status_${response.status}`, + }); + } + const document = yield* response.json.pipe(Effect.flatMap(decodeOidcDiscoveryDocument)); + if (document.issuer !== issuer) { + return yield* new SubjectTokenVerificationFailed({ cause: "oidc_issuer_mismatch" }); + } + return yield* Jwks.makeRemote({ url: document.jwks_uri }).pipe( + Effect.provideService(HttpClient.HttpClient, httpClient), + Effect.provideService(Scope.Scope, scope), + ); + }); + return yield* Effect.cachedInvalidateWithTTL( + discover.pipe( + Effect.timeout(OIDC_DISCOVERY_TIMEOUT), + Effect.mapError((cause) => + isSubjectTokenVerificationFailed(cause) + ? cause + : new SubjectTokenVerificationFailed({ cause }), + ), + ), + (exit) => (Exit.isSuccess(exit) ? Duration.infinity : "1 minute"), + ); +}); + +const readUnverifiedIssuer = (token: string): string | undefined => { + try { + return decodeIdTokenClaims(token).iss; + } catch { + return undefined; + } +}; + +const OidcClaims = Schema.Struct({ sub: Schema.NonEmptyString }); + +/** Binds the crypto service at startup, since the auth middleware cannot require services. */ +export const makeOidcVerifier = Effect.fn("relay.auth.make_oidc_verifier")(function* (options: { + readonly issuer: string; + readonly audience: string; + readonly keySet: Effect.Effect; +}) { + const signature = yield* Signature.Signature; + return (token: string): Effect.Effect => + options.keySet.pipe( + Effect.flatMap((keySet) => + Jwt.verifyWithKeySet(OidcClaims, token, { + issuer: options.issuer, + audience: options.audience, + algorithms: OIDC_ALGORITHMS, + requiredClaims: ["exp"], + // The identity provider and the relay run on different clocks. + clockTolerance: "5 seconds", + }).pipe( + Effect.provideService(Jwks.Jwks, keySet), + Effect.provideService(Signature.Signature, signature), + Effect.mapError((cause) => new SubjectTokenVerificationFailed({ cause })), + ), + ), + Effect.map(({ claims }): VerifiedSubject => ({ + sub: oidcAccountId(options.issuer, claims.sub), + mode: "oidc_bearer", + })), + Effect.withSpan("relay.auth.verify_oidc_token"), + ); +}); + +interface ClerkSettings { + readonly secretKey: Redacted.Redacted; + readonly publishableKey: string; + readonly audience: string; +} + +const clerkSettingsFrom = ( + config: RelayConfiguration.RelayConfiguration["Service"], +): ClerkSettings | undefined => + config.clerkSecretKey === undefined || + config.clerkPublishableKey === undefined || + config.clerkJwtAudience === undefined + ? undefined + : { + secretKey: config.clerkSecretKey, + publishableKey: config.clerkPublishableKey, + audience: config.clerkJwtAudience, + }; + +const hasExpectedClerkAudience = (audience: unknown, expectedAudience: string): boolean => + typeof audience === "string" + ? audience === expectedAudience + : Array.isArray(audience) && + audience.some((entry) => typeof entry === "string" && entry === expectedAudience); + +const verifyClerkSessionToken = (clerk: ClerkSettings, token: string) => + Effect.tryPromise({ + try: () => + verifyToken(token, { + secretKey: Redacted.value(clerk.secretKey), + audience: clerk.audience, + }), + catch: (cause) => new SubjectTokenVerificationFailed({ cause }), + }).pipe( + Effect.withSpan("verify_clerk_bearer_token", { + attributes: { "relay.auth.token_length": token.length }, + }), + Effect.flatMap((verified) => + verified.sub && hasExpectedClerkAudience(verified.aud, clerk.audience) + ? Effect.succeed({ + sub: verified.sub, + mode: "clerk_session_bearer", + } satisfies VerifiedSubject) + : Effect.fail(new SubjectTokenVerificationFailed({ cause: "missing_relay_audience" })), + ), + ); + +const verifyClerkOAuthToken = (clerk: ClerkSettings, relayIssuer: string, token: string) => + Effect.tryPromise({ + try: async () => { + const client = createClerkClient({ + secretKey: Redacted.value(clerk.secretKey), + publishableKey: clerk.publishableKey, + }); + const state = await client.authenticateRequest( + new Request(relayIssuer, { + headers: { authorization: `Bearer ${token}` }, + }), + { acceptsToken: "oauth_token" }, + ); + const auth = state.toAuth(); + if (!state.isAuthenticated || !auth.userId) { + throw new Error("Clerk OAuth token is not authenticated."); + } + return { sub: auth.userId, mode: "clerk_oauth_bearer" } satisfies VerifiedSubject; + }, + catch: (cause) => new SubjectTokenVerificationFailed({ cause }), + }); + +/** + * Tokens from the configured OIDC issuer are verified against its keys. Anything else is treated + * as Clerk, so existing Clerk accounts keep their subject ids unchanged. + */ +export const verifySubjectToken = ( + config: RelayConfiguration.RelayConfiguration["Service"], + token: string, + options: { readonly acceptClerkOAuth: boolean }, +): Effect.Effect => { + const oidc = config.oidc; + if (oidc && readUnverifiedIssuer(token) === oidc.issuer) { + return oidc.verify(token); + } + const clerk = clerkSettingsFrom(config); + if (clerk === undefined) { + return Effect.fail(new SubjectTokenVerificationFailed({ cause: "clerk_not_configured" })); + } + const session = verifyClerkSessionToken(clerk, token); + return options.acceptClerkOAuth + ? session.pipe(Effect.catch(() => verifyClerkOAuthToken(clerk, config.relayIssuer, token))) + : session; +}; diff --git a/infra/relay/src/composition.ts b/infra/relay/src/composition.ts new file mode 100644 index 0000000000000..bf9eb47ba701a --- /dev/null +++ b/infra/relay/src/composition.ts @@ -0,0 +1,405 @@ +import * as Config from "effect/Config"; +import * as Cause from "effect/Cause"; +import * as DateTime from "effect/DateTime"; +import * as Digest from "effect/crypto/Digest"; +import * as Mac from "effect/crypto/Mac"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Signature from "effect/crypto/Signature"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as Etag from "effect/http/Etag"; +import * as HttpPlatform from "effect/http/HttpPlatform"; +import * as HttpRouter from "effect/http/HttpRouter"; +import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; +import * as HttpApiScalar from "effect/http-api/HttpApiScalar"; + +import { RelayApi } from "@t3tools/contracts/relay"; + +import { + RELAY_HTTP_ROUTER_CONFIG, + traceRelayHttpRequestWith, + withoutCapturedParentSpan, +} from "./http/Api.ts"; +import * as RelayHttpApi from "./http/Api.ts"; +import type { RelayHost } from "./host/RelayHost.ts"; +import * as Ports from "./host/ports.ts"; +import * as Observability from "./observability.ts"; +import * as DeliveryAttempts from "./agentActivity/DeliveryAttempts.ts"; +import * as AgentActivityRows from "./agentActivity/AgentActivityRows.ts"; +import * as Devices from "./agentActivity/Devices.ts"; +import * as DpopProofs from "./auth/DpopProofs.ts"; +import { makeOidcKeySet, makeOidcVerifier } from "./auth/SubjectTokens.ts"; +import * as CloudMintSigner from "./auth/CloudMintSigner.ts"; +import * as RelayTokens from "./auth/RelayTokens.ts"; +import * as EnvironmentCredentials from "./environments/EnvironmentCredentials.ts"; +import * as EnvironmentLinks from "./environments/EnvironmentLinks.ts"; +import * as ManagedEndpointAllocations from "./environments/ManagedEndpointAllocations.ts"; +import * as LiveActivities from "./agentActivity/LiveActivities.ts"; +import * as RelayDb from "./db.ts"; +import * as WebCrypto from "./WebCrypto.ts"; +import * as FcmAssertionSigner from "./agentActivity/FcmAssertionSigner.ts"; +import * as FcmClient from "./agentActivity/FcmClient.ts"; +import * as FcmDeliveryQueueSender from "./agentActivity/FcmDeliveryQueueSender.ts"; +import * as FcmDeliveries from "./agentActivity/FcmDeliveries.ts"; +import * as FcmDeliveryQueueConsumer from "./agentActivity/FcmDeliveryQueueConsumer.ts"; +import * as RelayConfiguration from "./Config.ts"; +import * as AgentActivityPublisher from "./agentActivity/AgentActivityPublisher.ts"; +import * as ApnsClient from "./agentActivity/ApnsClient.ts"; +import * as ApnsProviderTokens from "./agentActivity/ApnsProviderTokens.ts"; +import * as ApnsDeliveryQueue from "./agentActivity/ApnsDeliveryQueue.ts"; +import * as ApnsDeliveries from "./agentActivity/ApnsDeliveries.ts"; +import * as CloudflareApiPause from "./environments/CloudflareApiPause.ts"; +import * as EnvironmentConnector from "./environments/EnvironmentConnector.ts"; +import * as EnvironmentLinker from "./environments/EnvironmentLinker.ts"; +import * as EnvironmentPublishSignatures from "./environments/EnvironmentPublishSignatures.ts"; +import * as ManagedEndpointProvider from "./environments/ManagedEndpointProvider.ts"; +import * as ManagedEndpointReaper from "./environments/ManagedEndpointReaper.ts"; +import * as ManagedTunnelLimits from "./environments/ManagedTunnelLimits.ts"; +import * as T3ChatAccounts from "./t3chat/T3ChatAccounts.ts"; +import * as MobileRegistrations from "./agentActivity/MobileRegistrations.ts"; +import * as HookForwarder from "./hooks/HookForwarder.ts"; +import * as HeldHooks from "./hooks/HeldHooks.ts"; +import * as MobileTelemetry from "./mobileTelemetry/MobileTelemetry.ts"; + +const layerWebcrypto = Layer.mergeAll( + Digest.layerWebCrypto, + Mac.layerWebCrypto, + Signature.layerWebCrypto, +).pipe(Layer.provideMerge(SecureRandom.layerWebCrypto)); + +const layerHttpPlatformNotSupported = Layer.succeed(HttpPlatform.HttpPlatform, { + platform: "web", + compression: { + algorithms: new Set(), + compressResponse: (response) => Effect.succeed(response), + }, + fileResponse: Effect.fn("HttpPlatform.fileResponse")(function () { + return Effect.die("Relay API does not serve filesystem responses"); + }), + fileWebResponse: Effect.fn("HttpPlatform.fileWebResponse")(function () { + return Effect.die("Relay API does not serve file responses"); + }), +}); + +const layerRelayApi = Layer.mergeAll( + RelayHttpApi.layerHealthApi, + RelayHttpApi.layerMetadataApi, + RelayHttpApi.layerMobileApi, + RelayHttpApi.layerClientApi, + RelayHttpApi.layerTokenApi, + RelayHttpApi.layerDpopClientApi, + RelayHttpApi.layerServerApi, +); + +// Platform-neutral: the Cloudflare and Node hosts (see `host/RelayHost.ts`) supply the I/O. +export const composeRelay = Effect.fn("relay.composition.compose")(function* ( + host: RelayHost, +) { + const { relayPublicOrigin, stage } = host.deployment; + + const apnsEnabled = yield* Config.Boolean("APNS_ENABLED").pipe(Config.withDefault(true)); + const apnsCredentials = apnsEnabled + ? { + environment: yield* Config.schema(RelayConfiguration.ApnsEnvironment, "APNS_ENVIRONMENT"), + teamId: yield* Config.String("APNS_TEAM_ID"), + keyId: yield* Config.String("APNS_KEY_ID"), + bundleId: yield* Config.String("APNS_BUNDLE_ID"), + privateKey: yield* Config.Redacted("APNS_PRIVATE_KEY"), + } + : null; + const fcmServiceAccount = Option.getOrUndefined( + Option.filter( + yield* Config.option(Config.Redacted("FCM_SERVICE_ACCOUNT")), + (value) => Redacted.value(value).trim().length > 0, + ), + ); + const apnsDeliveryJobSigningSecret = host.apnsJobSigningSecret; + const apnsDeliveryQueueSender = yield* host.apnsSender; + const fcmDeliveryQueueSender = yield* host.fcmSender; + + // Clerk is optional: a self-hosted relay can authenticate with OIDC alone. + const clerkSecretKey = Option.getOrUndefined( + yield* Config.option(Config.Redacted("CLERK_SECRET_KEY")), + ); + const clerkPublishableKey = Option.getOrUndefined( + yield* Config.option(Config.String("CLERK_PUBLISHABLE_KEY")), + ); + const clerkJwtAudience = Option.getOrUndefined( + yield* Config.option(Config.String("CLERK_JWT_AUDIENCE")), + ); + // Optional second identity provider for self-hosted relays; Clerk keeps working alongside it. + const oidcIssuer = Option.getOrUndefined(yield* Config.option(Config.String("OIDC_ISSUER"))); + const oidcAudience = Option.getOrUndefined(yield* Config.option(Config.String("OIDC_AUDIENCE"))); + if (oidcIssuer !== undefined && oidcAudience === undefined) { + return yield* Effect.die("OIDC_AUDIENCE is required when OIDC_ISSUER is set."); + } + const oidcVerify = + oidcIssuer === undefined || oidcAudience === undefined + ? undefined + : yield* makeOidcVerifier({ + issuer: oidcIssuer, + audience: oidcAudience, + keySet: (yield* makeOidcKeySet(oidcIssuer))[0], + }).pipe(Effect.provide(layerWebcrypto)); + // Off unless a self-hosted relay turns it on: the hosted relay keeps refusing unmanaged links. + const manualEndpoints = yield* Config.schema( + RelayConfiguration.ManualEndpointsMode, + "RELAY_MANUAL_ENDPOINTS", + ).pipe(Config.withDefault("off")); + const t3ChatLinkSecret = Option.getOrUndefined( + Option.filter( + yield* Config.option(Config.Redacted("T3CHAT_LINK_SECRET")), + (value) => Redacted.value(value).trim().length > 0, + ), + ); + const t3ChatBaseUrl = yield* Config.String("T3CHAT_URL").pipe( + Config.withDefault("https://t3.chat"), + ); + + const cloudMintPrivateKey = Effect.map(host.mintKeyPair, (keys) => keys.privateKey); + const cloudMintPublicKey = Effect.map(host.mintKeyPair, (keys) => keys.publicKey); + const db = host.database; + const managedEndpointCleanupMode = yield* RelayConfiguration.managedEndpointCleanupModeConfig; + const legacyManagedEndpointCleanupMode = + yield* RelayConfiguration.legacyManagedEndpointCleanupModeConfig; + const legacyTunnelGraceMinutes = Option.getOrUndefined( + yield* RelayConfiguration.legacyTunnelGraceMinutesConfig, + ); + const loadSettings = Effect.gen(function* () { + return RelayConfiguration.RelayConfiguration.of({ + relayIssuer: relayPublicOrigin, + ...(fcmServiceAccount ? { fcmServiceAccount } : {}), + apns: apnsCredentials, + apnsDeliveryJobSigningSecret: yield* apnsDeliveryJobSigningSecret, + ...(clerkSecretKey === undefined ? {} : { clerkSecretKey }), + ...(clerkPublishableKey === undefined ? {} : { clerkPublishableKey }), + ...(clerkJwtAudience === undefined ? {} : { clerkJwtAudience }), + manualEndpoints, + ...(oidcIssuer === undefined || oidcVerify === undefined + ? {} + : { oidc: { issuer: oidcIssuer, verify: oidcVerify } }), + cloudMintPrivateKey: yield* cloudMintPrivateKey, + cloudMintPublicKey: yield* cloudMintPublicKey, + managedEndpointBaseDomain: host.managedEndpointBaseDomain, + managedEndpointNamespace: stage, + managedEndpointCleanupMode, + legacyManagedEndpointCleanupMode, + ...(legacyTunnelGraceMinutes === undefined ? {} : { legacyTunnelGraceMinutes }), + ...(t3ChatLinkSecret + ? { t3Chat: { baseUrl: t3ChatBaseUrl, linkSecret: t3ChatLinkSecret } } + : {}), + }); + }); + + const layerHookInbox = host.hookInbox; + + const layerRuntime = Layer.empty.pipe( + Layer.provideMerge(MobileRegistrations.layer), + Layer.provideMerge(AgentActivityPublisher.layer), + Layer.provideMerge(EnvironmentConnector.layer), + Layer.provideMerge(EnvironmentLinker.layer), + Layer.provideMerge( + Layer.mergeAll( + EnvironmentPublishSignatures.layer, + ManagedEndpointReaper.layer, + HeldHooks.layer, + ), + ), + Layer.provideMerge( + ManagedEndpointProvider.layer.pipe(Layer.provideMerge(host.managedEndpointClients)), + ), + Layer.provideMerge(DpopProofs.layer), + Layer.provideMerge(ApnsDeliveries.layer), + Layer.provideMerge( + FcmDeliveries.layer.pipe( + Layer.provide( + Layer.succeed(FcmDeliveryQueueSender.FcmDeliveryQueueSender, fcmDeliveryQueueSender), + ), + Layer.provideMerge( + FcmClient.layer.pipe( + Layer.provide(FcmAssertionSigner.layer), + Layer.provide(Layer.succeed(WebCrypto.WebCrypto, { subtle: globalThis.crypto.subtle })), + ), + ), + ), + ), + Layer.provideMerge(ApnsClient.layer.pipe(Layer.provideMerge(ApnsProviderTokens.layer))), + Layer.provideMerge(ApnsDeliveryQueue.layerWithSender(apnsDeliveryQueueSender)), + Layer.provideMerge(Layer.mergeAll(AgentActivityRows.layer, Devices.layer, layerHookInbox)), + Layer.provideMerge(EnvironmentCredentials.layer), + Layer.provideMerge( + Layer.mergeAll( + CloudflareApiPause.layer, + EnvironmentLinks.layer, + ManagedEndpointAllocations.layer, + ManagedTunnelLimits.layer, + ), + ), + Layer.provideMerge(Layer.mergeAll(LiveActivities.layer, T3ChatAccounts.layer)), + Layer.provideMerge(DeliveryAttempts.layer), + Layer.provideMerge(RelayTokens.layer.pipe(Layer.provideMerge(CloudMintSigner.layer))), + Layer.provideMerge( + RelayDb.RelayTransactions.layer.pipe(Layer.provideMerge(Layer.succeed(RelayDb.RelayDb, db))), + ), + Layer.provideMerge(Layer.effect(RelayConfiguration.RelayConfiguration, loadSettings)), + Layer.provideMerge(layerWebcrypto), + ); + + // Fails open: a limiter outage must not drop webhooks or app telemetry. + const allowWith = + ( + limit: (key: string) => Effect.Effect, + failedOpenAttribute = "relay.hook.rate_limiter_failed_open", + ) => + (key: string): Effect.Effect => + limit(key).pipe( + Effect.catch((error) => + Effect.logWarning("Rate limiter unavailable", { error: String(error) }).pipe( + Effect.andThen(Effect.annotateCurrentSpan({ [failedOpenAttribute]: true })), + Effect.as(true), + ), + ), + ); + const layerHookRateLimiter = Layer.succeed(HookForwarder.HookRateLimiter, { + allowHook: allowWith(host.rateLimits.hook), + allowEndpoint: allowWith(host.rateLimits.hookEndpoint), + }); + + // `fetch` rebuilds its layers per request; the source map cache must outlive one. + const mobileSourceMapLookups = yield* MobileTelemetry.SourceMaps.pipe( + Effect.provide(MobileTelemetry.layerSourceMaps), + Effect.provideService(MobileTelemetry.SourceMapObjects, yield* host.mobileTelemetry.sourceMaps), + ); + const layerMobileTelemetry = MobileTelemetry.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.effect(MobileTelemetry.MobileTelemetrySettings, host.mobileTelemetry.settings), + Layer.succeed(MobileTelemetry.MobileTelemetryRateLimiter, { + allow: allowWith( + host.rateLimits.mobileTelemetry, + "relay.mobile_telemetry.rate_limiter_failed_open", + ), + }), + Layer.succeed(MobileTelemetry.SourceMaps, mobileSourceMapLookups), + ), + ), + ); + + const layerApp = Layer.mergeAll( + layerRelayApi, + HookForwarder.layerApi.pipe( + Layer.provide(HookForwarder.layer), + Layer.provide(layerHookRateLimiter), + ), + MobileTelemetry.layerApi.pipe(Layer.provide(layerMobileTelemetry)), + ).pipe( + Layer.provideMerge(RelayHttpApi.layerClientAuth), + Layer.provideMerge(RelayHttpApi.layerDpopClientAuth), + Layer.provideMerge(RelayHttpApi.layerEnvironmentAuth), + Layer.provide(layerRuntime), + ); + + yield* host.consumeQueue( + "apns", + { batchSize: 10, maxRetries: 5, retryDelay: "30 seconds" }, + (body) => + ApnsDeliveries.ApnsDeliveries.pipe( + Effect.flatMap((deliveries) => deliveries.processSignedJob(body)), + Effect.asVoid, + Effect.mapError((cause) => new Ports.QueueHandlerError({ cause })), + Effect.withSpan("relay.apn_delivery_queue.process_message"), + Effect.provide(layerRuntime), + ), + ); + + yield* host.consumeQueue( + "fcm", + { batchSize: 10, maxRetries: 5, retryDelay: "30 seconds" }, + (body) => + FcmDeliveryQueueConsumer.processMessage(body).pipe( + Effect.mapError((cause) => new Ports.QueueHandlerError({ cause })), + Effect.provide(layerRuntime), + ), + ); + + yield* host.schedule( + "*/5 * * * *", + Effect.all( + [ + DpopProofs.DpopProofReplay.pipe( + Effect.flatMap((dpopProofs) => dpopProofs.pruneExpired), + Effect.andThen( + Effect.all([AgentActivityRows.AgentActivityRows, DateTime.now]).pipe( + Effect.flatMap(([activityRows, now]) => + activityRows.pruneTerminal({ + updatedBefore: DateTime.formatIso(DateTime.subtract(now, { minutes: 30 })), + }), + ), + ), + ), + Effect.andThen( + DeliveryAttempts.DeliveryAttempts.pipe( + Effect.flatMap((attempts) => attempts.pruneExpired), + ), + ), + Effect.catchCause((cause) => + Cause.hasInterrupts(cause) + ? Effect.interrupt + : Effect.logWarning("Failed to prune expired relay state", { cause }), + ), + ), + ManagedEndpointReaper.ManagedEndpointReaper.pipe( + Effect.flatMap((reaper) => reaper.sweep.pipe(Effect.timeout("2 minutes"))), + Effect.tap((result) => + result.scanned > 0 + ? Effect.logInfo("Finished managed tunnel cleanup", result) + : Effect.void, + ), + Effect.catchCause((cause) => + Cause.hasInterrupts(cause) + ? Effect.interrupt + : Effect.logWarning("Failed to clean up inactive managed tunnels", { cause }), + ), + ), + AgentActivityPublisher.AgentActivityPublisher.pipe( + Effect.flatMap((publisher) => publisher.endIdleLiveActivities), + Effect.catchCause((cause) => + Cause.hasInterrupts(cause) + ? Effect.interrupt + : Effect.logWarning("Failed to end idle Live Activities", { cause }), + ), + ), + ], + { concurrency: 3, discard: true }, + ).pipe( + Effect.withSpan("relay.cron.prune_expired_state"), + Observability.withSchemaErrorSpanAttributes, + Effect.provide(layerRuntime), + ), + ); + + const fetch = Layer.merge( + Layer.mergeAll( + HttpApiBuilder.layer(RelayApi, { openapiPath: "/openapi.json" }).pipe( + Layer.provide(layerApp), + ), + HttpApiScalar.layer(RelayApi, { path: "/docs" }), + RelayHttpApi.layerDocsRedirectRoute, + ).pipe(Layer.provide([Etag.layerWeak, layerHttpPlatformNotSupported, RelayHttpApi.layerCors])), + RelayHttpApi.layerNotFoundRoute, + ).pipe( + HttpRouter.toHttpEffect, + Effect.provideService(HttpRouter.RouterConfig, RELAY_HTTP_ROUTER_CONFIG), + withoutCapturedParentSpan, + Effect.map((httpEffect) => + traceRelayHttpRequestWith(httpEffect, Layer.empty).pipe( + Observability.withSchemaErrorSpanAttributes, + ), + ), + ); + + // The runtime is exposed so a host can build it once and provide it to every request. + return { fetch, runtime: layerRuntime }; +}); diff --git a/infra/relay/src/environments/EnvironmentConnector.test.ts b/infra/relay/src/environments/EnvironmentConnector.test.ts index b08a8b5e951fe..b00a23e921bbd 100644 --- a/infra/relay/src/environments/EnvironmentConnector.test.ts +++ b/infra/relay/src/environments/EnvironmentConnector.test.ts @@ -160,6 +160,7 @@ function layerConnectorTest( options?: { readonly links?: EnvironmentLinks.EnvironmentLinks["Service"]; readonly allocations?: ManagedEndpointAllocations.ManagedEndpointAllocations["Service"]; + readonly manualEndpoints?: RelayConfiguration.ManualEndpointsMode; }, ) { return EnvironmentConnector.layer.pipe( @@ -172,7 +173,13 @@ function layerConnectorTest( options?.allocations ?? makeAllocations(), ), ), - Layer.provide(RelayConfiguration.layer(settings)), + Layer.provide( + RelayConfiguration.layer( + options?.manualEndpoints === undefined + ? settings + : { ...settings, manualEndpoints: options.manualEndpoints }, + ), + ), Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))), ); } @@ -275,7 +282,7 @@ describe("EnvironmentConnector", () => { }).pipe(Effect.provide(layerConnectorTest(execute))); }); - it.effect("rejects manual endpoints before sending a health request", () => { + it.effect("rejects manual endpoints by default before sending a health request", () => { let requestCount = 0; const execute = () => Effect.sync(() => { @@ -318,6 +325,46 @@ describe("EnvironmentConnector", () => { ); }); + it.effect("checks a self-declared HTTPS endpoint when the relay allows manual endpoints", () => { + const seenUrls: Array = []; + const execute = (request: HttpClientRequest.HttpClientRequest) => + Effect.sync(() => { + seenUrls.push(request.url); + return HttpClientResponse.fromWeb( + request, + Response.json(signHealthResponse(decodeHealthRequestBody(requestBodyText(request)))), + ); + }); + + return Effect.gen(function* () { + const connector = yield* EnvironmentConnector.EnvironmentConnector; + const result = yield* connector.status({ + userId: "user_123", + environmentId: "env-connector-test", + }); + + expect(seenUrls).toEqual(["https://t3.example.dev/api/t3-connect/health"]); + expect(result).toMatchObject({ + status: "online", + endpoint: { providerKind: "manual", httpBaseUrl: "https://t3.example.dev/" }, + }); + }).pipe( + Effect.provide( + layerConnectorTest(execute, { + manualEndpoints: "public", + allocations: makeAllocations(null), + links: makeLinks({ + endpoint: { + httpBaseUrl: "https://t3.example.dev/", + wsBaseUrl: "wss://t3.example.dev/ws", + providerKind: "manual", + }, + }), + }), + ), + ); + }); + it.effect("rejects stale managed endpoints before sending a mint request", () => { let requestCount = 0; const spans: Array = []; @@ -704,3 +751,48 @@ describe("EnvironmentConnector", () => { }).pipe(Effect.provide(layerConnectorTest(execute))); }); }); + +describe("validateManualEndpoint", () => { + const endpoint = (httpBaseUrl: string, wsBaseUrl = httpBaseUrl.replace("https:", "wss:")) => ({ + httpBaseUrl, + wsBaseUrl, + providerKind: "manual" as const, + }); + + it.each([ + ["a plain HTTP address", "public", endpoint("http://t3.example.dev/", "ws://t3.example.dev/")], + ["a loopback address", "public", endpoint("https://127.0.0.1/")], + ["localhost", "public", endpoint("https://localhost:3773/")], + ["a private IPv4 address", "public", endpoint("https://10.0.0.5/")], + ["a link-local metadata address", "private", endpoint("https://169.254.169.254/")], + ["embedded credentials", "public", endpoint("https://user:pass@t3.example.dev/")], + [ + "different WebSocket and HTTP hosts", + "public", + endpoint("https://a.example.dev/", "wss://b.example.dev/"), + ], + ] as const)("rejects %s", (_name, manualEndpoints, value) => { + expect( + Result.isFailure( + EnvironmentConnector.validateManualEndpoint({ endpoint: value, manualEndpoints }), + ), + ).toBe(true); + }); + + it("accepts private networks only when the relay opts in", () => { + const value = endpoint("https://10.0.0.5:8443/"); + expect( + Result.isFailure( + EnvironmentConnector.validateManualEndpoint({ endpoint: value, manualEndpoints: "public" }), + ), + ).toBe(true); + expect( + Result.isSuccess( + EnvironmentConnector.validateManualEndpoint({ + endpoint: value, + manualEndpoints: "private", + }), + ), + ).toBe(true); + }); +}); diff --git a/infra/relay/src/environments/EnvironmentConnector.ts b/infra/relay/src/environments/EnvironmentConnector.ts index e9862fa1976f6..4538adbb6460f 100644 --- a/infra/relay/src/environments/EnvironmentConnector.ts +++ b/infra/relay/src/environments/EnvironmentConnector.ts @@ -27,6 +27,7 @@ import { RELAY_MINT_RESPONSE_TYP, verifyRelayJwt, } from "@t3tools/shared/relayJwt"; +import { isInternalOnlyHost, isPrivateNetworkHost } from "@t3tools/shared/hostClassification"; import { stableStringify } from "@t3tools/shared/relaySigning"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -69,6 +70,8 @@ function environmentConnectNotAuthorizedReasonMessage( return "the managed endpoint hostname is invalid"; case "managed_endpoint_mismatch": return "the linked endpoint does not match its managed allocation"; + case "manual_endpoint_not_allowed": + return "the linked endpoint is not an address this relay may contact"; } } @@ -368,6 +371,62 @@ export function validateManagedEndpoint(input: { return Result.succeed(endpoint); } +const parseEndpointUrl = (value: string): URL | null => { + try { + return new URL(value); + } catch { + return null; + } +}; + +/** + * An environment that declares its own address has no allocation to check it against, and the + * relay will send requests to it. Require a secure address that is not an internal one, so a + * linked environment cannot point the relay at its own network. + * DNS names that resolve to private addresses are not detected here. + */ +export function validateManualEndpoint(input: { + readonly endpoint: RelayManagedEndpoint; + readonly manualEndpoints: RelayConfiguration.ManualEndpointsMode; +}): Result.Result { + const { endpoint } = input; + const http = parseEndpointUrl(endpoint.httpBaseUrl); + const ws = parseEndpointUrl(endpoint.wsBaseUrl); + const rejection = + http === null || ws === null || http.protocol !== "https:" || ws.protocol !== "wss:" + ? "scheme" + : http.username !== "" || http.password !== "" || ws.username !== "" || ws.password !== "" + ? "credentials" + : http.host !== ws.host + ? "authority_mismatch" + : isInternalOnlyHost(http.hostname) + ? "internal_address" + : input.manualEndpoints !== "private" && isPrivateNetworkHost(http.hostname) + ? "private_network" + : null; + return rejection === null + ? Result.succeed(endpoint) + : Result.fail({ + reason: "manual_endpoint_not_allowed", + attributes: { "relay.authorization.manual_endpoint_rejection": rejection }, + }); +} + +/** Managed links resolve through their allocation; self-declared links only when the relay allows them. */ +export function validateConnectEndpoint(input: { + readonly link: EnvironmentLinks.RelayLinkedEnvironmentRecord; + readonly allocation: ManagedEndpointAllocations.ManagedEndpointAllocation | null; + readonly baseDomain: string | undefined; + readonly manualEndpoints: RelayConfiguration.ManualEndpointsMode; +}): Result.Result { + return input.link.endpoint.providerKind === "manual" && input.manualEndpoints !== "off" + ? validateManualEndpoint({ + endpoint: input.link.endpoint, + manualEndpoints: input.manualEndpoints, + }) + : validateManagedEndpoint(input); +} + const make = Effect.gen(function* () { const links = yield* EnvironmentLinks.EnvironmentLinks; const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations; @@ -387,10 +446,11 @@ const make = Effect.gen(function* () { readonly link: EnvironmentLinks.RelayLinkedEnvironmentRecord; readonly allocation: ManagedEndpointAllocations.ManagedEndpointAllocation | null; }) { - const result = validateManagedEndpoint({ + const result = validateConnectEndpoint({ link: input.link, allocation: input.allocation, baseDomain: settings.managedEndpointBaseDomain, + manualEndpoints: settings.manualEndpoints ?? "off", }); if (Result.isSuccess(result)) { return result.success; diff --git a/infra/relay/src/environments/ManagedEndpointProvider.ts b/infra/relay/src/environments/ManagedEndpointProvider.ts index 188ad0e7b7148..5ee0ff0d7cb3d 100644 --- a/infra/relay/src/environments/ManagedEndpointProvider.ts +++ b/infra/relay/src/environments/ManagedEndpointProvider.ts @@ -1576,162 +1576,168 @@ const make = Effect.gen(function* () { export const layer = Layer.effect(ManagedEndpointProvider, make); -export const layerCloudflareBindings = ( +// Cloudflare host adapter: the tunnel and DNS clients the provider needs, behind its API pause. +export const layerCloudflareClients = ( tunnelClient: Cloudflare.Tunnel.ReadWriteTunnelClient, dnsClient: Cloudflare.DNS.ReadWriteDnsClient, alchemyRuntimeContext: Alchemy.BaseRuntimeContext, ) => { // Created once per isolate so its cached pause survives layer rebuilds. const cloudflare = makeCloudflareApiGate(alchemyRuntimeContext); - return layer.pipe( - Layer.provideMerge( - Layer.unwrap( - Effect.gen(function* () { - const pause = yield* CloudflareApiPause.CloudflareApiPause; - const gate = cloudflare(pause); - return Layer.mergeAll( - layerTunnelClient({ - get: (tunnelId) => - gate(tunnelClient.get(tunnelId)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "get", - tunnelId, - cause, - }), - ), - ), - list: (request) => - gate(tunnelClient.list(request)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "list", - ...(request.name === undefined ? {} : { tunnelName: request.name }), - cause, - }), - ), - ), - create: (request) => - gate(tunnelClient.create(request)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "create", - tunnelName: request.name, - cause, - }), - ), - ), - putConfiguration: (tunnelId, config) => - gate(tunnelClient.putConfiguration(tunnelId, config)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "put-configuration", - tunnelId, - cause, - }), - ), - ), - getConfiguration: (tunnelId) => - gate(tunnelClient.getConfiguration(tunnelId)).pipe( - Effect.map(({ config }) => ({ ingress: config?.ingress ?? [] })), - // Cloudflare reports a tunnel without remote configuration as code 1055, not 404. - Effect.catchTags({ - TunnelConfigurationNotFound: () => Effect.succeed({ ingress: [] }), + return Layer.unwrap( + Effect.gen(function* () { + const pause = yield* CloudflareApiPause.CloudflareApiPause; + const gate = cloudflare(pause); + return Layer.mergeAll( + layerTunnelClient({ + get: (tunnelId) => + gate(tunnelClient.get(tunnelId)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "get", + tunnelId, + cause, }), - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "get-configuration", - tunnelId, - cause, - }), - ), - ), - getToken: (tunnelId) => - gate(tunnelClient.getToken(tunnelId)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "get-token", - tunnelId, - cause, - }), - ), - ), - delete: (tunnelId) => - gate(tunnelClient.delete(tunnelId)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointTunnelClientError({ - operation: "delete", - tunnelId, - cause, - }), - ), - ), - }), - layerDnsClient({ - listRecords: (hostname) => - gate(dnsClient.listDnsRecords({ search: hostname })).pipe( - Effect.map((response) => - response.result.filter( - (record): record is typeof record & { readonly id: string } => - typeof record.id === "string" && - normalizeHostname(record.name) === normalizeHostname(hostname), - ), - ), - Effect.mapError( - (cause) => - new ManagedEndpointDnsClientError({ - operation: "list-records", - hostname, - cause, - }), - ), - ), - createRecord: (request) => - gate(dnsClient.createDnsRecord(request)).pipe( - Effect.map((response) => ({ id: response.id })), - Effect.mapError( - (cause) => - new ManagedEndpointDnsClientError({ - operation: "create-record", - hostname: request.name, - cause, - }), - ), - ), - updateRecord: (dnsRecordId, request) => - gate(dnsClient.updateDnsRecord(dnsRecordId, request)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointDnsClientError({ - operation: "update-record", - hostname: request.name, - dnsRecordId, - cause, - }), - ), - ), - deleteRecord: (dnsRecordId) => - gate(dnsClient.deleteDnsRecord(dnsRecordId)).pipe( - Effect.mapError( - (cause) => - new ManagedEndpointDnsClientError({ - operation: "delete-record", - dnsRecordId, - cause, - }), - ), + ), + ), + list: (request) => + gate(tunnelClient.list(request)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "list", + ...(request.name === undefined ? {} : { tunnelName: request.name }), + cause, + }), + ), + ), + create: (request) => + gate(tunnelClient.create(request)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "create", + tunnelName: request.name, + cause, + }), + ), + ), + putConfiguration: (tunnelId, config) => + gate(tunnelClient.putConfiguration(tunnelId, config)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "put-configuration", + tunnelId, + cause, + }), + ), + ), + getConfiguration: (tunnelId) => + gate(tunnelClient.getConfiguration(tunnelId)).pipe( + Effect.map(({ config }) => ({ ingress: config?.ingress ?? [] })), + // Cloudflare reports a tunnel without remote configuration as code 1055, not 404. + Effect.catchTags({ + TunnelConfigurationNotFound: () => Effect.succeed({ ingress: [] }), + }), + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "get-configuration", + tunnelId, + cause, + }), + ), + ), + getToken: (tunnelId) => + gate(tunnelClient.getToken(tunnelId)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "get-token", + tunnelId, + cause, + }), + ), + ), + delete: (tunnelId) => + gate(tunnelClient.delete(tunnelId)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointTunnelClientError({ + operation: "delete", + tunnelId, + cause, + }), + ), + ), + }), + layerDnsClient({ + listRecords: (hostname) => + gate(dnsClient.listDnsRecords({ search: hostname })).pipe( + Effect.map((response) => + response.result.filter( + (record): record is typeof record & { readonly id: string } => + typeof record.id === "string" && + normalizeHostname(record.name) === normalizeHostname(hostname), ), - }), - ); + ), + Effect.mapError( + (cause) => + new ManagedEndpointDnsClientError({ + operation: "list-records", + hostname, + cause, + }), + ), + ), + createRecord: (request) => + gate(dnsClient.createDnsRecord(request)).pipe( + Effect.map((response) => ({ id: response.id })), + Effect.mapError( + (cause) => + new ManagedEndpointDnsClientError({ + operation: "create-record", + hostname: request.name, + cause, + }), + ), + ), + updateRecord: (dnsRecordId, request) => + gate(dnsClient.updateDnsRecord(dnsRecordId, request)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointDnsClientError({ + operation: "update-record", + hostname: request.name, + dnsRecordId, + cause, + }), + ), + ), + deleteRecord: (dnsRecordId) => + gate(dnsClient.deleteDnsRecord(dnsRecordId)).pipe( + Effect.mapError( + (cause) => + new ManagedEndpointDnsClientError({ + operation: "delete-record", + dnsRecordId, + cause, + }), + ), + ), }), - ), - ), + ); + }), ); }; + +export const layerCloudflareBindings = ( + tunnelClient: Cloudflare.Tunnel.ReadWriteTunnelClient, + dnsClient: Cloudflare.DNS.ReadWriteDnsClient, + alchemyRuntimeContext: Alchemy.BaseRuntimeContext, +) => + layer.pipe( + Layer.provideMerge(layerCloudflareClients(tunnelClient, dnsClient, alchemyRuntimeContext)), + ); diff --git a/infra/relay/src/host/RelayHost.ts b/infra/relay/src/host/RelayHost.ts new file mode 100644 index 0000000000000..c6644557f56e9 --- /dev/null +++ b/infra/relay/src/host/RelayHost.ts @@ -0,0 +1,68 @@ +import type * as Effect from "effect/Effect"; +import type * as HttpClient from "effect/http/HttpClient"; +import type * as Layer from "effect/Layer"; +import type * as Redacted from "effect/Redacted"; + +import type * as CloudflareApiPause from "../environments/CloudflareApiPause.ts"; +import type * as RelayDb from "../db.ts"; +import type * as HookInbox from "../hooks/HookInbox.ts"; +import type * as ManagedEndpointProvider from "../environments/ManagedEndpointProvider.ts"; +import type * as MobileTelemetry from "../mobileTelemetry/MobileTelemetry.ts"; +import type * as Ports from "./ports.ts"; +import type { SignedApnsDeliveryJob } from "../agentActivity/apnsDeliveryJobs.ts"; +import type { FcmDeliveryJob } from "../agentActivity/FcmDeliveries.ts"; + +/** + * Everything the relay needs from its platform. `composition.ts` is platform-neutral; `worker.ts` + * (Cloudflare) and `node.ts` (self-hosted) each build one of these. + * + * `R` is whatever the host's background work (queue consumers, cron) needs from its runtime. + */ +export interface RelayHost { + readonly deployment: { + readonly relayPublicOrigin: string; + readonly stage: string; + }; + /** Relay-signed mint keys; every replica must see the same pair. */ + readonly mintKeyPair: Effect.Effect<{ + readonly privateKey: Redacted.Redacted; + readonly publicKey: string; + }>; + readonly apnsJobSigningSecret: Effect.Effect>; + /** Managed tunnel base domain; undefined disables managed endpoints. */ + readonly managedEndpointBaseDomain: string | undefined; + readonly database: RelayDb.RelayDb["Service"]; + /** Tunnel and DNS clients behind managed endpoints; inert on hosts without managed tunnels. */ + readonly managedEndpointClients: Layer.Layer< + | ManagedEndpointProvider.ManagedEndpointTunnelClient + | ManagedEndpointProvider.ManagedEndpointDnsClient, + never, + CloudflareApiPause.CloudflareApiPause | HttpClient.HttpClient + >; + readonly hookInbox: Layer.Layer; + /** Each limiter may fail; the composition decides to fail open. */ + readonly rateLimits: { + readonly hook: (key: string) => Effect.Effect; + readonly hookEndpoint: (key: string) => Effect.Effect; + readonly mobileTelemetry: ( + key: string, + ) => Effect.Effect; + }; + readonly mobileTelemetry: { + readonly settings: Effect.Effect; + readonly sourceMaps: Effect.Effect; + }; + readonly apnsSender: Effect.Effect>; + readonly fcmSender: Effect.Effect>; + /** A failing handler asks the host to retry that message; the host owns retries and dead-lettering. */ + readonly consumeQueue: ( + name: "apns" | "fcm", + options: Omit, + handler: (body: unknown) => Effect.Effect, + ) => Effect.Effect; + /** Runs `effect` on the cron schedule; the host owns retries and overlap policy. */ + readonly schedule: ( + cron: string, + effect: Effect.Effect, + ) => Effect.Effect; +} diff --git a/infra/relay/src/host/node.test.ts b/infra/relay/src/host/node.test.ts new file mode 100644 index 0000000000000..27d8329b83eb9 --- /dev/null +++ b/infra/relay/src/host/node.test.ts @@ -0,0 +1,92 @@ +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as Layer from "effect/Layer"; +import * as Queue from "effect/Queue"; +import * as Redacted from "effect/Redacted"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as TestClock from "effect/testing/TestClock"; + +import type * as RelayDb from "../db.ts"; +import { fixedWindowLimit, makeNodeHost } from "./node.ts"; +import * as Ports from "./ports.ts"; + +const layer = Layer.mergeAll(SecureRandom.layerWebCrypto, FetchHttpClient.layer); + +const makeHost = makeNodeHost({ + relayPublicOrigin: "https://relay.example.com", + stage: "selfhost", + mintKeyPair: { privateKey: Redacted.make("private"), publicKey: "public" }, + // The queue and scheduler never touch the database. + database: {} as RelayDb.RelayDb["Service"], + mobileTelemetry: { token: undefined, traces: "", logs: "", metrics: "" }, +}); + +describe("fixedWindowLimit", () => { + it.effect("allows the limit per key within a window and resets after it", () => + Effect.gen(function* () { + const limit = fixedWindowLimit(2, 60); + + expect([yield* limit("a"), yield* limit("a"), yield* limit("a")]).toEqual([ + true, + true, + false, + ]); + expect(yield* limit("b")).toBe(true); + + yield* TestClock.adjust("60 seconds"); + expect(yield* limit("a")).toBe(true); + }), + ); +}); + +describe("self-hosted queue and scheduler", () => { + it.effect("retries a failed job after the retry delay and drops it after maxRetries", () => + Effect.gen(function* () { + const host = yield* makeHost; + const sender = yield* host.fcmSender; + const attempts = yield* Queue.unbounded(); + let seen = 0; + yield* host.consumeQueue( + "fcm", + { batchSize: 10, maxRetries: 3, retryDelay: "30 seconds" }, + () => + Queue.offer(attempts, (seen += 1)).pipe( + Effect.andThen(Effect.fail(new Ports.QueueHandlerError({ cause: "rejected" }))), + ), + ); + + yield* sender.send({} as never); + expect(yield* Queue.take(attempts)).toBe(1); + yield* TestClock.adjust("30 seconds"); + expect(yield* Queue.take(attempts)).toBe(2); + yield* TestClock.adjust("30 seconds"); + expect(yield* Queue.take(attempts)).toBe(3); + + yield* TestClock.adjust("30 seconds"); + expect(yield* Queue.size(attempts)).toBe(0); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + + it.effect("runs a `*/N * * * *` schedule every N minutes and rejects other expressions", () => + Effect.gen(function* () { + const host = yield* makeHost; + const runs = yield* Queue.unbounded(); + let count = 0; + yield* host.schedule( + "*/5 * * * *", + Effect.suspend(() => Queue.offer(runs, (count += 1))).pipe(Effect.asVoid), + ); + + yield* TestClock.adjust("5 minutes"); + expect(yield* Queue.take(runs)).toBe(1); + yield* TestClock.adjust("5 minutes"); + expect(yield* Queue.take(runs)).toBe(2); + + expect(Exit.isFailure(yield* Effect.exit(host.schedule("0 * * * *", Effect.void)))).toBe( + true, + ); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); +}); diff --git a/infra/relay/src/host/node.ts b/infra/relay/src/host/node.ts new file mode 100644 index 0000000000000..472debb2b234e --- /dev/null +++ b/infra/relay/src/host/node.ts @@ -0,0 +1,248 @@ +import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Queue from "effect/Queue"; +import * as Redacted from "effect/Redacted"; +import type * as Scope from "effect/Scope"; +import * as SecureRandom from "effect/crypto/SecureRandom"; + +import type * as RelayDb from "../db.ts"; +import * as HookForwarder from "../hooks/HookForwarder.ts"; +import * as HookInbox from "../hooks/HookInbox.ts"; +import * as ManagedEndpointProvider from "../environments/ManagedEndpointProvider.ts"; +import * as MobileTelemetry from "../mobileTelemetry/MobileTelemetry.ts"; +import * as Ports from "./ports.ts"; +import type { RelayHost } from "./RelayHost.ts"; + +export interface NodeHostOptions { + readonly relayPublicOrigin: string; + readonly stage: string; + readonly mintKeyPair: { + readonly privateKey: Redacted.Redacted; + readonly publicKey: string; + }; + /** Stable signing secret shared by every relay replica. */ + readonly apnsJobSigningSecret?: Redacted.Redacted; + readonly database: RelayDb.RelayDb["Service"]; + /** Base URLs of an Axiom-compatible ingest; empty disables mobile telemetry forwarding. */ + readonly mobileTelemetry: { + readonly token: Redacted.Redacted | undefined; + readonly traces: string; + readonly logs: string; + readonly metrics: string; + }; +} + +interface QueuedJob { + readonly body: unknown; + readonly attempts: number; +} + +// Self-hosted relay: queues and rate limits live in this process, so pending pushes do not +// survive a restart. Cron supports only `*/N * * * *`, the one schedule the relay uses. +const MINUTE_STEP_CRON = /^\*\/(\d+) \* \* \* \*$/; + +export const fixedWindowLimit = (limit: number, periodSeconds: number) => { + const windows = new Map(); + const periodMillis = periodSeconds * 1000; + return (key: string): Effect.Effect => + Effect.map(Clock.currentTimeMillis, (now) => { + if (windows.size > 10_000) { + for (const [windowKey, window] of windows) { + if (now - window.windowStart >= periodMillis) windows.delete(windowKey); + } + } + const window = windows.get(key); + if (window === undefined || now - window.windowStart >= periodMillis) { + windows.set(key, { windowStart: now, count: 1 }); + return true; + } + window.count += 1; + return window.count <= limit; + }); +}; + +const inertTunnelClient: ManagedEndpointProvider.ManagedEndpointTunnelClient["Service"] = { + get: (tunnelId) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ + operation: "get", + tunnelId, + cause: "managed tunnels are not available on this relay", + }), + ), + list: () => Effect.succeed({ result: [], resultInfo: null }), + create: (request) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ + operation: "create", + tunnelName: request.name, + cause: "managed tunnels are not available on this relay", + }), + ), + putConfiguration: (tunnelId) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ + operation: "put-configuration", + tunnelId, + cause: "managed tunnels are not available on this relay", + }), + ), + getConfiguration: (tunnelId) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ + operation: "get-configuration", + tunnelId, + cause: "managed tunnels are not available on this relay", + }), + ), + getToken: (tunnelId) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ + operation: "get-token", + tunnelId, + cause: "managed tunnels are not available on this relay", + }), + ), + delete: () => Effect.void, +}; + +const inertDnsClient: ManagedEndpointProvider.ManagedEndpointDnsClient["Service"] = { + listRecords: () => Effect.succeed([]), + createRecord: (request) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointDnsClientError({ + operation: "create-record", + hostname: request.name, + cause: "managed tunnels are not available on this relay", + }), + ), + updateRecord: (dnsRecordId) => + Effect.fail( + new ManagedEndpointProvider.ManagedEndpointDnsClientError({ + operation: "update-record", + dnsRecordId, + cause: "managed tunnels are not available on this relay", + }), + ), + deleteRecord: () => Effect.void, +}; + +const inertHookInbox = Layer.succeed(HookInbox.HookInbox, { + // Holding webhooks while an environment is offline needs the Cloudflare Durable Object inbox. + hold: Effect.fn("HookInbox.hold")(() => Effect.succeed(false)), + wake: Effect.fn("HookInbox.wake")(() => Effect.succeed(false)), + clear: Effect.fn("HookInbox.clear")(() => Effect.void), +}); + +/** + * Builds the self-hosted `RelayHost`. Background work (queue consumers, cron) runs as scoped + * fibers, so the caller's scope owns it. + */ +export const makeNodeHost = Effect.fn("relay.node.host")(function* (options: NodeHostOptions) { + const random = yield* SecureRandom.SecureRandom; + const apnsQueue = yield* Queue.unbounded(); + const fcmQueue = yield* Queue.unbounded(); + const queueFor = (name: "apns" | "fcm") => (name === "apns" ? apnsQueue : fcmQueue); + + const senderFor = (queue: Queue.Queue): Ports.DeliveryQueueSender => ({ + send: (body) => Queue.offer(queue, { body, attempts: 0 }).pipe(Effect.asVoid), + }); + + const hookLimit = fixedWindowLimit( + HookForwarder.RELAY_HOOK_RATE_LIMIT.limit, + HookForwarder.RELAY_HOOK_RATE_LIMIT.periodSeconds, + ); + const hookEndpointLimit = fixedWindowLimit( + HookForwarder.RELAY_HOOK_ENDPOINT_RATE_LIMIT.limit, + HookForwarder.RELAY_HOOK_ENDPOINT_RATE_LIMIT.periodSeconds, + ); + const mobileTelemetryLimit = fixedWindowLimit( + MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.limit, + MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.periodSeconds, + ); + + const host: RelayHost = { + deployment: { relayPublicOrigin: options.relayPublicOrigin, stage: options.stage }, + mintKeyPair: Effect.succeed(options.mintKeyPair), + apnsJobSigningSecret: + options.apnsJobSigningSecret === undefined + ? Effect.map(random.randomBytes(32), (bytes) => + Redacted.make(Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("")), + ) + : Effect.succeed(options.apnsJobSigningSecret), + managedEndpointBaseDomain: undefined, + database: options.database, + managedEndpointClients: Layer.mergeAll( + Layer.succeed(ManagedEndpointProvider.ManagedEndpointTunnelClient, inertTunnelClient), + Layer.succeed(ManagedEndpointProvider.ManagedEndpointDnsClient, inertDnsClient), + ), + hookInbox: inertHookInbox, + rateLimits: { + hook: hookLimit, + hookEndpoint: hookEndpointLimit, + mobileTelemetry: mobileTelemetryLimit, + }, + mobileTelemetry: { + settings: Effect.succeed( + MobileTelemetry.MobileTelemetrySettings.of({ + axiomToken: options.mobileTelemetry.token ?? Redacted.make(""), + destinations: { + traces: { url: options.mobileTelemetry.traces, dataset: "" }, + logs: { url: options.mobileTelemetry.logs, dataset: "" }, + metrics: { url: options.mobileTelemetry.metrics, dataset: "" }, + }, + }), + ), + sourceMaps: Effect.succeed({ + get: Effect.fn("SourceMapObjects.get")(() => Effect.undefined), + }), + }, + apnsSender: Effect.succeed(senderFor(apnsQueue)), + fcmSender: Effect.succeed(senderFor(fcmQueue)), + consumeQueue: (name, consumerOptions, handler) => { + const queue = queueFor(name); + const deliver = (job: QueuedJob) => + handler(job.body).pipe( + Effect.catchCause((cause) => { + if (Cause.hasInterrupts(cause)) return Effect.failCause(cause); + const attempts = job.attempts + 1; + if (attempts >= consumerOptions.maxRetries) { + return Effect.logWarning("Dropping relay delivery job after retries", { + queue: name, + attempts, + }); + } + return Effect.logWarning("Retrying relay delivery job", { queue: name, attempts }).pipe( + Effect.andThen( + Effect.forkScoped( + Effect.sleep(consumerOptions.retryDelay).pipe( + Effect.andThen(Queue.offer(queue, { body: job.body, attempts })), + ), + ), + ), + ); + }), + ); + return Queue.take(queue).pipe( + Effect.flatMap(deliver), + Effect.forever, + Effect.forkScoped, + Effect.asVoid, + ); + }, + schedule: (cron, effect) => { + const match = MINUTE_STEP_CRON.exec(cron); + if (match === null) { + return Effect.die(new Error(`Unsupported cron schedule on a self-hosted relay: ${cron}`)); + } + const every = Duration.minutes(Number(match[1])); + return Effect.forkScoped( + Effect.forever(Effect.sleep(every).pipe(Effect.andThen(effect))), + ).pipe(Effect.asVoid); + }, + }; + return host; +}); diff --git a/infra/relay/src/host/ports.ts b/infra/relay/src/host/ports.ts new file mode 100644 index 0000000000000..a1ac2f52d60e7 --- /dev/null +++ b/infra/relay/src/host/ports.ts @@ -0,0 +1,48 @@ +import type * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; + +// Host-neutral shapes the relay logic depends on. Cloudflare and Node hosts implement them. + +export class DeliveryQueueSendError extends Schema.TaggedError()( + "DeliveryQueueSendError", + { + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "Could not enqueue a relay delivery job."; + } +} + +export class QueueHandlerError extends Schema.TaggedError()( + "QueueHandlerError", + { + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "A relay delivery job failed and will be retried."; + } +} + +export class RateLimitUnavailableError extends Schema.TaggedError()( + "RateLimitUnavailableError", + { + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "The rate limiter is unavailable."; + } +} + +export interface DeliveryQueueSender { + readonly send: (body: A) => Effect.Effect; +} + +export interface DeliveryQueueConsumerOptions { + readonly batchSize: number; + readonly maxRetries: number; + readonly retryDelay: `${number} seconds`; + readonly deadLetterQueue?: string; +} diff --git a/infra/relay/src/http/Api.test.ts b/infra/relay/src/http/Api.test.ts index 3abb8b28f4d6a..fa4203ff7955d 100644 --- a/infra/relay/src/http/Api.test.ts +++ b/infra/relay/src/http/Api.test.ts @@ -50,10 +50,10 @@ import { relayDpopFailureReason, traceRelayHttpRequestWith, unlinkEnvironmentRecord, - verifyRelayClientBearerToken, verifyEnvironmentTunnelRecoveryProof, withoutCapturedParentSpan, } from "./Api.ts"; +import { verifySubjectToken } from "../auth/SubjectTokens.ts"; import * as RelayHttpApi from "./Api.ts"; import * as RelayConfiguration from "../Config.ts"; import * as RelayDb from "../db.ts"; @@ -393,7 +393,9 @@ describe("relay client authentication", () => { aud: relaySettings.clerkJwtAudience, } as never); - expect(yield* verifyRelayClientBearerToken(relaySettings, "session-token")).toEqual({ + expect( + yield* verifySubjectToken(relaySettings, "session-token", { acceptClerkOAuth: true }), + ).toEqual({ sub: "user_session", mode: "clerk_session_bearer", }); @@ -422,7 +424,9 @@ describe("relay client authentication", () => { }), } as never); - expect(yield* verifyRelayClientBearerToken(relaySettings, "oauth-token")).toEqual({ + expect( + yield* verifySubjectToken(relaySettings, "oauth-token", { acceptClerkOAuth: true }), + ).toEqual({ sub: "user_oauth", mode: "clerk_oauth_bearer", }); diff --git a/infra/relay/src/http/Api.ts b/infra/relay/src/http/Api.ts index f06d441c12c48..3dba95c90b902 100644 --- a/infra/relay/src/http/Api.ts +++ b/infra/relay/src/http/Api.ts @@ -1,4 +1,3 @@ -import { createClerkClient, verifyToken } from "@clerk/backend"; import { sql as drizzleSql } from "drizzle-orm"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Context from "effect/Context"; @@ -64,6 +63,7 @@ import * as AgentActivityRows from "../agentActivity/AgentActivityRows.ts"; import * as Devices from "../agentActivity/Devices.ts"; import * as DpopProofs from "../auth/DpopProofs.ts"; import * as RelayTokens from "../auth/RelayTokens.ts"; +import { isOidcAccountId, verifySubjectToken } from "../auth/SubjectTokens.ts"; import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts"; import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts"; import * as HeldHooks from "../hooks/HeldHooks.ts"; @@ -316,7 +316,7 @@ export const layerClientAuth = Layer.effect( return { clientBearer: Effect.fn("relay.auth.client.bearer")(function* (httpEffect, { credential }) { const token = readHttpAuthorizationCredential(credential); - const verified = yield* verifyRelayClientBearerToken(config, token).pipe( + const verified = yield* verifySubjectToken(config, token, { acceptClerkOAuth: true }).pipe( Effect.tapError((error) => Effect.annotateCurrentSpan( "relay.auth.clerk_verification_failure", @@ -984,7 +984,9 @@ export const layerClientApi = HttpApiBuilder.group( Effect.fn("relay.api.client.getT3ChatLink")(function* () { yield* appendRelayCredentialResponseHeaders; const { userId } = yield* RelayClientPrincipal; - const link = t3Chat.available + // T3 Chat matches accounts by their Clerk email, so other identity providers never link. + const t3ChatAvailable = t3Chat.available && !isOidcAccountId(userId); + const link = t3ChatAvailable ? yield* t3Chat .getLink({ userId }) .pipe(Effect.catch(() => relayInternalErrorResponse("upstream_unavailable"))) @@ -993,7 +995,7 @@ export const layerClientApi = HttpApiBuilder.group( .getOverride({ userId }) .pipe(Effect.catch(() => relayInternalErrorResponse("internal_error"))); return { - available: t3Chat.available, + available: t3ChatAvailable, link, ...(maxTunnelsOverride === null ? {} : { maxTunnelsOverride }), }; @@ -1003,6 +1005,7 @@ export const layerClientApi = HttpApiBuilder.group( "createT3ChatLinkRequest", Effect.fn("relay.api.client.createT3ChatLinkRequest")(function* () { const { userId } = yield* RelayClientPrincipal; + if (isOidcAccountId(userId)) return yield* relayInternalErrorResponse("internal_error"); const url = yield* t3Chat.createLinkUrl({ userId }).pipe( Effect.catchTags({ T3ChatNotConfigured: () => relayInternalErrorResponse("internal_error"), @@ -1051,12 +1054,10 @@ export const layerTokenApi = HttpApiBuilder.group( return yield* new HttpApiError.Unauthorized({}); } - const verified = yield* verifyClerkBearerToken(config, args.payload.subject_token).pipe( - Effect.catch(() => relayAuthInvalidError("invalid_bearer")), - ); - if (!verified.sub || !hasExpectedClerkAudience(verified.aud, config.clerkJwtAudience)) { - return yield* relayAuthInvalidError("invalid_bearer"); - } + // The exchange stays session-only: CLI OAuth tokens are not accepted here. + const verified = yield* verifySubjectToken(config, args.payload.subject_token, { + acceptClerkOAuth: false, + }).pipe(Effect.catch(() => relayAuthInvalidError("invalid_bearer"))); const proofKeyThumbprint = yield* requireDpopProof().pipe( Effect.provideService(DpopProofs.DpopProofReplay, dpopProofs), ); @@ -1441,17 +1442,6 @@ export const layerServerApi = HttpApiBuilder.group( }), ); -class ClerkTokenVerificationFailed extends Schema.TaggedError()( - "ClerkTokenVerificationFailed", - { - cause: Schema.Defect(), - }, -) { - override get message(): string { - return "Clerk token verification failed"; - } -} - const isHttpUnauthorized = Schema.is(HttpApiError.Unauthorized); const isDpopProofRejected = Schema.is(DpopProofs.DpopProofRejected); @@ -1661,75 +1651,6 @@ function clerkVerificationFailureReason(cause: unknown): string { return "unknown"; } -function hasExpectedClerkAudience(audience: unknown, expectedAudience: string): boolean { - return typeof audience === "string" - ? audience === expectedAudience - : Array.isArray(audience) && - audience.some((entry) => typeof entry === "string" && entry === expectedAudience); -} - -function verifyClerkBearerToken( - config: RelayConfiguration.RelayConfiguration["Service"], - token: string, -) { - return Effect.tryPromise({ - try: () => - verifyToken(token, { - secretKey: Redacted.value(config.clerkSecretKey), - audience: config.clerkJwtAudience, - }), - catch: (cause) => new ClerkTokenVerificationFailed({ cause }), - }).pipe( - Effect.withSpan("verify_clerk_bearer_token", { - attributes: { "relay.auth.token_length": token.length }, - }), - ); -} - -function verifyClerkOAuthBearerToken( - config: RelayConfiguration.RelayConfiguration["Service"], - token: string, -) { - return Effect.tryPromise({ - try: async () => { - const client = createClerkClient({ - secretKey: Redacted.value(config.clerkSecretKey), - publishableKey: config.clerkPublishableKey, - }); - const state = await client.authenticateRequest( - new Request(config.relayIssuer, { - headers: { authorization: `Bearer ${token}` }, - }), - { acceptsToken: "oauth_token" }, - ); - const auth = state.toAuth(); - if (!state.isAuthenticated || !auth.userId) { - throw new Error("Clerk OAuth token is not authenticated."); - } - return { sub: auth.userId }; - }, - catch: (cause) => new ClerkTokenVerificationFailed({ cause }), - }); -} - -export function verifyRelayClientBearerToken( - config: RelayConfiguration.RelayConfiguration["Service"], - token: string, -) { - return verifyClerkBearerToken(config, token).pipe( - Effect.flatMap((verified) => - verified.sub && hasExpectedClerkAudience(verified.aud, config.clerkJwtAudience) - ? Effect.succeed({ sub: verified.sub, mode: "clerk_session_bearer" as const }) - : Effect.fail(new ClerkTokenVerificationFailed({ cause: "missing_relay_audience" })), - ), - Effect.catch(() => - verifyClerkOAuthBearerToken(config, token).pipe( - Effect.map((verified) => ({ ...verified, mode: "clerk_oauth_bearer" as const })), - ), - ), - ); -} - const requireDpopPrincipalScope = Effect.fn("relay.api.require_dpop_principal_scope")(function* ( scope: RelayDpopAccessTokenScope, ) { diff --git a/infra/relay/src/node.ts b/infra/relay/src/node.ts new file mode 100644 index 0000000000000..3446afa3bee7a --- /dev/null +++ b/infra/relay/src/node.ts @@ -0,0 +1,102 @@ +// Self-hosted relay entrypoint. The same composition as the Cloudflare Worker (`composition.ts`), +// served from Node with Postgres. See `host/node.ts` for what differs and why. +// @effect-diagnostics nodeBuiltinImport:off - the Node HTTP server is the platform entry point. +import * as NodeHttp from "node:http"; + +import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; +import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as PgClient from "@effect/sql-pg/PgClient"; +import { makeWithDefaults } from "drizzle-orm/effect-postgres"; +import { migrate } from "drizzle-orm/effect-postgres/migrator"; +import * as Config from "effect/Config"; +import * as Effect from "effect/Effect"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as HttpServer from "effect/http/HttpServer"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as SecureRandom from "effect/crypto/SecureRandom"; + +import { composeRelay } from "./composition.ts"; +import { makeNodeHost } from "./host/node.ts"; + +const program = Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const readSecret = (path: string) => + fs.readFileString(path).pipe(Effect.map((text) => text.trim())); + + const relayPublicOrigin = yield* Config.String("RELAY_PUBLIC_ORIGIN"); + const migrationsFolder = yield* Config.String("RELAY_MIGRATIONS_FOLDER").pipe( + Config.withDefault("migrations/postgres"), + ); + const mintPrivateKey = yield* readSecret(yield* Config.String("CLOUD_MINT_PRIVATE_KEY_FILE")); + const mintPublicKey = yield* readSecret(yield* Config.String("CLOUD_MINT_PUBLIC_KEY_FILE")); + // Compose passes unset variables as empty strings; an empty secret must count as absent. + const nonEmpty = (value: Redacted.Redacted) => Redacted.value(value).trim().length > 0; + const apnsJobSigningSecret = Option.filter( + yield* Config.option(Config.Redacted("APNS_DELIVERY_JOB_SIGNING_SECRET")), + nonEmpty, + ); + const mobileTelemetryToken = Option.filter( + yield* Config.option(Config.Redacted("MOBILE_TELEMETRY_TOKEN")), + nonEmpty, + ); + + const database = yield* makeWithDefaults(); + // A failed migration leaves the database in an unknown state; refuse to start instead of serving. + // @effect-diagnostics-next-line anyUnknownInErrorContext:off - drizzle's migrate types its error as any + yield* migrate(database, { migrationsFolder, migrationsTable: "relay_migrations" }).pipe( + Effect.orDie, + ); + + const host = yield* makeNodeHost({ + relayPublicOrigin, + stage: "selfhost", + mintKeyPair: { + privateKey: Redacted.make(mintPrivateKey), + publicKey: mintPublicKey, + }, + ...(apnsJobSigningSecret._tag === "Some" + ? { apnsJobSigningSecret: apnsJobSigningSecret.value } + : {}), + database, + mobileTelemetry: { + token: mobileTelemetryToken._tag === "Some" ? mobileTelemetryToken.value : undefined, + traces: yield* Config.String("MOBILE_TELEMETRY_TRACES_URL").pipe(Config.withDefault("")), + logs: yield* Config.String("MOBILE_TELEMETRY_LOGS_URL").pipe(Config.withDefault("")), + metrics: yield* Config.String("MOBILE_TELEMETRY_METRICS_URL").pipe(Config.withDefault("")), + }, + }); + + const relay = yield* composeRelay(host); + // Built once: every request reuses the same services, as the Worker's per-isolate layers do. + const context = yield* Layer.build(relay.runtime); + const handler = yield* relay.fetch.pipe(Effect.provideContext(context)); + yield* HttpServer.serveEffect(handler.pipe(Effect.provideContext(context))); + yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin }); + // Serving is scoped to this program; keep it alive until the process is stopped. + return yield* Effect.never; +}); + +const serverLayer = Layer.unwrap( + Effect.map(Config.Int("PORT").pipe(Config.withDefault(8080)), (port) => + NodeHttpServer.layer(NodeHttp.createServer, { port, host: "0.0.0.0" }), + ), +); + +NodeRuntime.runMain( + program.pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + serverLayer, + NodeServices.layer, + FetchHttpClient.layer, + SecureRandom.layerWebCrypto, + PgClient.layerConfig({ url: Config.Redacted("DATABASE_URL") }), + ), + ), + ), +); diff --git a/infra/relay/src/t3chat/T3ChatAccounts.ts b/infra/relay/src/t3chat/T3ChatAccounts.ts index d81bc049fd8dc..e5598218e314b 100644 --- a/infra/relay/src/t3chat/T3ChatAccounts.ts +++ b/infra/relay/src/t3chat/T3ChatAccounts.ts @@ -118,6 +118,9 @@ export const make = Effect.gen(function* () { ) { return yield* Effect.tryPromise({ try: async () => { + if (config.clerkSecretKey === undefined || config.clerkPublishableKey === undefined) { + throw new Error("Clerk is not configured."); + } const user = await createClerkClient({ secretKey: Redacted.value(config.clerkSecretKey), publishableKey: config.clerkPublishableKey, diff --git a/infra/relay/src/worker.ts b/infra/relay/src/worker.ts index e9da9a216726f..3b50ca7da4fd0 100644 --- a/infra/relay/src/worker.ts +++ b/infra/relay/src/worker.ts @@ -2,109 +2,30 @@ import * as Alchemy from "alchemy"; import * as Axiom from "@alchemy.run/axiom"; import * as Cloudflare from "@alchemy.run/cloudflare"; import * as Drizzle from "@alchemy.run/drizzle/Postgres"; -import * as Config from "effect/Config"; import * as Cause from "effect/Cause"; -import * as DateTime from "effect/DateTime"; -import * as Digest from "effect/crypto/Digest"; -import * as Mac from "effect/crypto/Mac"; -import * as SecureRandom from "effect/crypto/SecureRandom"; -import * as Signature from "effect/crypto/Signature"; import * as Effect from "effect/Effect"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as HttpMiddleware from "effect/http/HttpMiddleware"; import * as Layer from "effect/Layer"; -import * as Option from "effect/Option"; -import * as Redacted from "effect/Redacted"; import * as Stream from "effect/Stream"; -import * as Etag from "effect/http/Etag"; -import * as HttpMiddleware from "effect/http/HttpMiddleware"; -import * as HttpPlatform from "effect/http/HttpPlatform"; -import * as HttpRouter from "effect/http/HttpRouter"; -import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; -import * as HttpApiScalar from "effect/http-api/HttpApiScalar"; -import { RelayApi } from "@t3tools/contracts/relay"; - -import { - RELAY_HTTP_ROUTER_CONFIG, - traceRelayHttpRequestWith, - withoutCapturedParentSpan, -} from "./http/Api.ts"; -import * as CloudMintSigner from "./auth/CloudMintSigner.ts"; -import * as RelayHttpApi from "./http/Api.ts"; -import { ManagedEndpointZone, RelayApiZone, RelayDeploymentConfig } from "./zone.ts"; -import { RelayObservability } from "./observability.ts"; -import * as Observability from "./observability.ts"; -import * as DeliveryAttempts from "./agentActivity/DeliveryAttempts.ts"; -import * as AgentActivityRows from "./agentActivity/AgentActivityRows.ts"; -import * as Devices from "./agentActivity/Devices.ts"; -import * as DpopProofs from "./auth/DpopProofs.ts"; -import * as RelayTokens from "./auth/RelayTokens.ts"; -import * as EnvironmentCredentials from "./environments/EnvironmentCredentials.ts"; -import * as EnvironmentLinks from "./environments/EnvironmentLinks.ts"; -import * as ManagedEndpointAllocations from "./environments/ManagedEndpointAllocations.ts"; -import * as LiveActivities from "./agentActivity/LiveActivities.ts"; +import { composeRelay } from "./composition.ts"; import * as RelayDb from "./db.ts"; +import * as HookForwarder from "./hooks/HookForwarder.ts"; +import * as HookInbox from "./hooks/HookInbox.ts"; +import * as HookInboxObject from "./hooks/HookInboxObject.ts"; +import * as ManagedEndpointProvider from "./environments/ManagedEndpointProvider.ts"; +import * as MobileTelemetry from "./mobileTelemetry/MobileTelemetry.ts"; +import { RelayObservability } from "./observability.ts"; import { RelayApnsDeliveryDeadLetterQueue, RelayApnsDeliveryQueue, - RelayFcmDeliveryQueue, RelayFcmDeliveryDeadLetterQueue, + RelayFcmDeliveryQueue, } from "./queues.ts"; -import * as WebCrypto from "./WebCrypto.ts"; -import * as FcmAssertionSigner from "./agentActivity/FcmAssertionSigner.ts"; -import * as FcmClient from "./agentActivity/FcmClient.ts"; -import * as FcmDeliveryQueueSender from "./agentActivity/FcmDeliveryQueueSender.ts"; -import * as FcmDeliveries from "./agentActivity/FcmDeliveries.ts"; -import * as FcmDeliveryQueueConsumer from "./agentActivity/FcmDeliveryQueueConsumer.ts"; -import * as RelayConfiguration from "./Config.ts"; -import * as AgentActivityPublisher from "./agentActivity/AgentActivityPublisher.ts"; -import * as ApnsClient from "./agentActivity/ApnsClient.ts"; -import * as ApnsProviderTokens from "./agentActivity/ApnsProviderTokens.ts"; -import * as ApnsDeliveryQueue from "./agentActivity/ApnsDeliveryQueue.ts"; -import * as ApnsDeliveries from "./agentActivity/ApnsDeliveries.ts"; -import * as CloudflareApiPause from "./environments/CloudflareApiPause.ts"; -import * as EnvironmentConnector from "./environments/EnvironmentConnector.ts"; -import * as EnvironmentLinker from "./environments/EnvironmentLinker.ts"; -import * as EnvironmentPublishSignatures from "./environments/EnvironmentPublishSignatures.ts"; -import * as ManagedEndpointProvider from "./environments/ManagedEndpointProvider.ts"; -import * as ManagedEndpointReaper from "./environments/ManagedEndpointReaper.ts"; -import * as ManagedTunnelLimits from "./environments/ManagedTunnelLimits.ts"; -import * as T3ChatAccounts from "./t3chat/T3ChatAccounts.ts"; -import * as MobileRegistrations from "./agentActivity/MobileRegistrations.ts"; -import * as HookForwarder from "./hooks/HookForwarder.ts"; -import * as HeldHooks from "./hooks/HeldHooks.ts"; -import * as HookInbox from "./hooks/HookInbox.ts"; -import * as HookInboxObject from "./hooks/HookInboxObject.ts"; -import * as MobileTelemetry from "./mobileTelemetry/MobileTelemetry.ts"; - -const layerWebcrypto = Layer.mergeAll( - Digest.layerWebCrypto, - Mac.layerWebCrypto, - Signature.layerWebCrypto, -).pipe(Layer.provideMerge(SecureRandom.layerWebCrypto)); - -const layerHttpPlatformNotSupported = Layer.succeed(HttpPlatform.HttpPlatform, { - platform: "web", - compression: { - algorithms: new Set(), - compressResponse: (response) => Effect.succeed(response), - }, - fileResponse: Effect.fn("HttpPlatform.fileResponse")(function () { - return Effect.die("Relay API does not serve filesystem responses"); - }), - fileWebResponse: Effect.fn("HttpPlatform.fileWebResponse")(function () { - return Effect.die("Relay API does not serve file responses"); - }), -}); - -const layerRelayApi = Layer.mergeAll( - RelayHttpApi.layerHealthApi, - RelayHttpApi.layerMetadataApi, - RelayHttpApi.layerMobileApi, - RelayHttpApi.layerClientApi, - RelayHttpApi.layerTokenApi, - RelayHttpApi.layerDpopClientApi, - RelayHttpApi.layerServerApi, -); +import { ManagedEndpointZone, RelayApiZone, RelayDeploymentConfig } from "./zone.ts"; +import type { RelayHost } from "./host/RelayHost.ts"; +import * as Ports from "./host/ports.ts"; const CloudMintKeyPair = Alchemy.KeyPair("CloudMintKeyPair"); const ApnsDeliveryJobSigningSecret = Alchemy.makeRandom("ApnsDeliveryJobSigningSecret", { @@ -113,6 +34,8 @@ const ApnsDeliveryJobSigningSecret = Alchemy.makeRandom("ApnsDeliveryJobSigningS export class Api extends Cloudflare.Worker()("Api") {} +// Cloudflare host: wires the platform-neutral relay (`composition.ts`) to Workers, Queues, +// Durable Objects, Hyperdrive, Rate Limiting, R2 and Cloudflare-managed tunnels. export const layer = Api.make( RelayDeploymentConfig.pipe( Effect.map(({ relayPublicDomain }) => ({ @@ -140,60 +63,28 @@ export const layer = Api.make( const managedEndpointZone = yield* ManagedEndpointZone; const randomApnsDeliveryJobSigningSecret = yield* ApnsDeliveryJobSigningSecret; const observability = yield* RelayObservability; + const alchemyRuntimeContext: Alchemy.BaseRuntimeContext = yield* Cloudflare.Worker; + const inRuntime = (effect: Effect.Effect) => + effect.pipe(Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext)); - const apnsEnabled = yield* Config.Boolean("APNS_ENABLED").pipe(Config.withDefault(true)); - const apnsCredentials = apnsEnabled - ? { - environment: yield* Config.schema(RelayConfiguration.ApnsEnvironment, "APNS_ENVIRONMENT"), - teamId: yield* Config.String("APNS_TEAM_ID"), - keyId: yield* Config.String("APNS_KEY_ID"), - bundleId: yield* Config.String("APNS_BUNDLE_ID"), - privateKey: yield* Config.Redacted("APNS_PRIVATE_KEY"), - } - : null; - const fcmServiceAccount = Option.getOrUndefined( - Option.filter( - yield* Config.option(Config.Redacted("FCM_SERVICE_ACCOUNT")), - (value) => Redacted.value(value).trim().length > 0, - ), - ); - const apnsDeliveryJobSigningSecret = yield* randomApnsDeliveryJobSigningSecret; - const apnsDeliveryQueueSender = yield* Cloudflare.Queues.WriteQueue(apnsDeliveryQueue); - const fcmDeliveryQueueSender = yield* Cloudflare.Queues.WriteQueue(fcmDeliveryQueue); - - const clerkSecretKey = yield* Config.Redacted("CLERK_SECRET_KEY"); - const clerkPublishableKey = yield* Config.String("CLERK_PUBLISHABLE_KEY"); - const clerkJwtAudience = yield* Config.String("CLERK_JWT_AUDIENCE"); - const t3ChatLinkSecret = Option.getOrUndefined( - Option.filter( - yield* Config.option(Config.Redacted("T3CHAT_LINK_SECRET")), - (value) => Redacted.value(value).trim().length > 0, - ), - ); - const t3ChatBaseUrl = yield* Config.String("T3CHAT_URL").pipe( - Config.withDefault("https://t3.chat"), - ); + const cloudMintPrivateKey = yield* yield* cloudMintKeyPair.privateKey; + const cloudMintPublicKey = yield* yield* cloudMintKeyPair.publicKey; + const apnsJobSigningSecret = yield* yield* randomApnsDeliveryJobSigningSecret; + const apnsWriteQueue = yield* Cloudflare.Queues.WriteQueue(apnsDeliveryQueue); + const fcmWriteQueue = yield* Cloudflare.Queues.WriteQueue(fcmDeliveryQueue); - const cloudMintPrivateKey = yield* cloudMintKeyPair.privateKey; - const cloudMintPublicKey = yield* cloudMintKeyPair.publicKey; const hyperdrive = yield* Cloudflare.Hyperdrive.Connect(yield* RelayDb.RelayHyperdrive); // Named statements collide behind Hyperdrive transaction pooling (Effect-TS/effect#8320); use unnamed statements. const db = yield* Drizzle.Postgres(hyperdrive.connectionString, { client: { prepare: false }, }); + const managedEndpointBaseDomain = yield* yield* managedEndpointZone.name; - const managedEndpointTunnelBinding = yield* Cloudflare.Tunnel.ReadWriteTunnel(); + const tunnel = yield* Cloudflare.Tunnel.ReadWriteTunnel(); // Keep Worker custom-domain reconciliation ordered after API zone provisioning. yield* yield* relayApiZone.zoneId; - const managedEndpointDnsBinding = yield* Cloudflare.DNS.ReadWriteDns(managedEndpointZone); - const managedEndpointZoneName = yield* managedEndpointZone.name; - const managedEndpointCleanupMode = yield* RelayConfiguration.managedEndpointCleanupModeConfig; - const legacyManagedEndpointCleanupMode = - yield* RelayConfiguration.legacyManagedEndpointCleanupModeConfig; - const legacyTunnelGraceMinutes = Option.getOrUndefined( - yield* RelayConfiguration.legacyTunnelGraceMinutesConfig, - ); - // Endpoint keys and their hashes differ by stage, including in shared accounts. + const dns = yield* Cloudflare.DNS.ReadWriteDns(managedEndpointZone); + const hookRateLimit = yield* Cloudflare.RateLimit("HOOK_RATE_LIMIT", { namespaceId: 1001, simple: { @@ -208,7 +99,6 @@ export const layer = Api.make( period: HookForwarder.RELAY_HOOK_ENDPOINT_RATE_LIMIT.periodSeconds, }, }); - const hookInboxes = yield* HookInboxObject.HookInboxObject; const mobileTelemetryRateLimit = yield* Cloudflare.RateLimit("MOBILE_TELEMETRY_RATE_LIMIT", { namespaceId: 1003, simple: { @@ -216,44 +106,20 @@ export const layer = Api.make( period: MobileTelemetry.MOBILE_TELEMETRY_RATE_LIMIT.periodSeconds, }, }); - const mobileSourceMaps = yield* Cloudflare.R2.ReadBucket(observability.mobileSourceMaps); - const mobileTelemetryToken = yield* observability.mobileTelemetryIngestToken.token; - const mobileTracesUrl = yield* observability.traces.otelTracesEndpoint; - const mobileTracesDataset = yield* observability.traces.name; - const mobileLogsUrl = yield* observability.mobileLogs.otelLogsEndpoint; - const mobileLogsDataset = yield* observability.mobileLogs.name; - const mobileMetricsUrl = yield* observability.mobileMetrics.otelMetricsEndpoint; - const mobileMetricsDataset = yield* observability.mobileMetrics.name; - - const alchemyRuntimeContext: Alchemy.BaseRuntimeContext = yield* Cloudflare.Worker; - - const loadSettings = Effect.gen(function* () { - return RelayConfiguration.RelayConfiguration.of({ - relayIssuer: relayPublicOrigin, - ...(fcmServiceAccount ? { fcmServiceAccount } : {}), - apns: apnsCredentials, - apnsDeliveryJobSigningSecret: yield* apnsDeliveryJobSigningSecret, - clerkSecretKey, - clerkPublishableKey, - clerkJwtAudience, - cloudMintPrivateKey: yield* cloudMintPrivateKey, - cloudMintPublicKey: yield* cloudMintPublicKey, - managedEndpointBaseDomain: yield* managedEndpointZoneName, - managedEndpointNamespace: stage, - managedEndpointCleanupMode, - legacyManagedEndpointCleanupMode, - ...(legacyTunnelGraceMinutes === undefined ? {} : { legacyTunnelGraceMinutes }), - ...(t3ChatLinkSecret - ? { t3Chat: { baseUrl: t3ChatBaseUrl, linkSecret: t3ChatLinkSecret } } - : {}), - }); - }); + // Fails open at the composition; here a limiter error is only reported as unavailable. + const limitKey = + (limiter: typeof hookRateLimit) => + (key: string): Effect.Effect => + inRuntime(limiter.limit({ key })).pipe( + Effect.map((result) => result.success), + Effect.mapError((cause) => new Ports.RateLimitUnavailableError({ cause })), + ); + const hookInboxes = yield* HookInboxObject.HookInboxObject; const inboxCall = (operation: HookInbox.HookInboxError["operation"], endpointKey: string) => (effect: Effect.Effect) => - effect.pipe( - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), + inRuntime(effect).pipe( Effect.catchCause((cause) => Effect.fail( new HookInbox.HookInboxError({ @@ -264,300 +130,129 @@ export const layer = Api.make( ), ), ); - const layerHookInbox = Layer.succeed(HookInbox.HookInbox, { - hold: Effect.fn("HookInbox.hold")(function ({ endpointKey, baseUrl, hook }) { - return hookInboxes - .getByName(endpointKey) - .hold(hook, baseUrl) - .pipe(inboxCall("hold", endpointKey)); - }), - wake: Effect.fn("HookInbox.wake")(function ({ endpointKey, baseUrl }) { - return hookInboxes - .getByName(endpointKey) - .wake(baseUrl) - .pipe(inboxCall("wake", endpointKey)); - }), - clear: Effect.fn("HookInbox.clear")(function ({ endpointKey }) { - return hookInboxes.getByName(endpointKey).clear().pipe(inboxCall("clear", endpointKey)); - }), - }); - - const layerRuntime = Layer.empty.pipe( - Layer.provideMerge(MobileRegistrations.layer), - Layer.provideMerge(AgentActivityPublisher.layer), - Layer.provideMerge(EnvironmentConnector.layer), - Layer.provideMerge(EnvironmentLinker.layer), - Layer.provideMerge( - Layer.mergeAll( - EnvironmentPublishSignatures.layer, - ManagedEndpointReaper.layer, - HeldHooks.layer, - ), - ), - Layer.provideMerge( - ManagedEndpointProvider.layerCloudflareBindings( - managedEndpointTunnelBinding, - managedEndpointDnsBinding, - alchemyRuntimeContext, - ), - ), - Layer.provideMerge(DpopProofs.layer), - Layer.provideMerge(ApnsDeliveries.layer), - Layer.provideMerge( - FcmDeliveries.layer.pipe( - Layer.provide( - Layer.succeed(FcmDeliveryQueueSender.FcmDeliveryQueueSender, { - send: Effect.fn("FcmDeliveryQueueSender.send")( - function* (body) { - return yield* fcmDeliveryQueueSender.send(body); - }, - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - ), - }), - ), - Layer.provideMerge( - FcmClient.layer.pipe( - Layer.provide(FcmAssertionSigner.layer), - Layer.provide( - Layer.succeed(WebCrypto.WebCrypto, { subtle: globalThis.crypto.subtle }), - ), - ), - ), - ), - ), - Layer.provideMerge(ApnsClient.layer.pipe(Layer.provideMerge(ApnsProviderTokens.layer))), - Layer.provideMerge( - ApnsDeliveryQueue.layerCloudflareQueues(apnsDeliveryQueueSender, alchemyRuntimeContext), - ), - Layer.provideMerge(Layer.mergeAll(AgentActivityRows.layer, Devices.layer, layerHookInbox)), - Layer.provideMerge(EnvironmentCredentials.layer), - Layer.provideMerge( - Layer.mergeAll( - CloudflareApiPause.layer, - EnvironmentLinks.layer, - ManagedEndpointAllocations.layer, - ManagedTunnelLimits.layer, - ), - ), - Layer.provideMerge(Layer.mergeAll(LiveActivities.layer, T3ChatAccounts.layer)), - Layer.provideMerge(DeliveryAttempts.layer), - Layer.provideMerge(RelayTokens.layer.pipe(Layer.provideMerge(CloudMintSigner.layer))), - Layer.provideMerge( - RelayDb.RelayTransactions.layer.pipe( - Layer.provideMerge(Layer.succeed(RelayDb.RelayDb, db)), - ), - ), - Layer.provideMerge(Layer.effect(RelayConfiguration.RelayConfiguration, loadSettings)), - Layer.provideMerge(layerWebcrypto), - ); - // Fails open: a limiter outage must not drop webhooks or app telemetry. - const allowWith = - ( - limiter: typeof hookRateLimit, - failedOpenAttribute = "relay.hook.rate_limiter_failed_open", - ) => - (key: string): Effect.Effect => - limiter.limit({ key }).pipe( - Effect.map((result) => result.success), - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - Effect.catch((error) => - Effect.logWarning("Rate limiter unavailable", { error: error.message }).pipe( - Effect.andThen(Effect.annotateCurrentSpan({ [failedOpenAttribute]: true })), - Effect.as(true), - ), - ), - ); - const layerHookRateLimiter = Layer.succeed(HookForwarder.HookRateLimiter, { - allowHook: allowWith(hookRateLimit), - allowEndpoint: allowWith(hookEndpointRateLimit), - }); + const mobileSourceMaps = yield* Cloudflare.R2.ReadBucket(observability.mobileSourceMaps); + const mobileTelemetryToken = yield* observability.mobileTelemetryIngestToken.token; + const mobileTracesUrl = yield* observability.traces.otelTracesEndpoint; + const mobileTracesDataset = yield* observability.traces.name; + const mobileLogsUrl = yield* observability.mobileLogs.otelLogsEndpoint; + const mobileLogsDataset = yield* observability.mobileLogs.name; + const mobileMetricsUrl = yield* observability.mobileMetrics.otelMetricsEndpoint; + const mobileMetricsDataset = yield* observability.mobileMetrics.name; - // `fetch` rebuilds its layers per request; the source map cache must outlive one. - const mobileSourceMapLookups = yield* MobileTelemetry.SourceMaps.pipe( - Effect.provide(MobileTelemetry.layerSourceMaps), - Effect.provideService(MobileTelemetry.SourceMapObjects, { - get: (key) => - mobileSourceMaps.get(key).pipe( - Effect.map((object) => - object === null - ? undefined - : { - size: object.size, - text: object.text().pipe( - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - Effect.mapError( - (cause) => new MobileTelemetry.SourceMapObjectError({ key, cause }), - ), - ), - }, - ), - Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext), - Effect.mapError((cause) => new MobileTelemetry.SourceMapObjectError({ key, cause })), - ), + const host: RelayHost = { + deployment: { relayPublicOrigin, stage }, + mintKeyPair: Effect.succeed({ + privateKey: cloudMintPrivateKey, + publicKey: cloudMintPublicKey, }), - ); - const layerMobileTelemetry = MobileTelemetry.layer.pipe( - Layer.provide( - Layer.mergeAll( - Layer.effect( - MobileTelemetry.MobileTelemetrySettings, - Effect.gen(function* () { - return MobileTelemetry.MobileTelemetrySettings.of({ - axiomToken: yield* mobileTelemetryToken, - destinations: { - traces: { url: yield* mobileTracesUrl, dataset: yield* mobileTracesDataset }, - logs: { url: yield* mobileLogsUrl, dataset: yield* mobileLogsDataset }, - metrics: { url: yield* mobileMetricsUrl, dataset: yield* mobileMetricsDataset }, - }, - }); - }), - ), - Layer.succeed(MobileTelemetry.MobileTelemetryRateLimiter, { - allow: allowWith( - mobileTelemetryRateLimit, - "relay.mobile_telemetry.rate_limiter_failed_open", - ), - }), - Layer.succeed(MobileTelemetry.SourceMaps, mobileSourceMapLookups), - ), - ), - ); - - const layerApp = Layer.mergeAll( - layerRelayApi, - HookForwarder.layerApi.pipe( - Layer.provide(HookForwarder.layer), - Layer.provide(layerHookRateLimiter), + apnsJobSigningSecret: Effect.succeed(apnsJobSigningSecret), + managedEndpointBaseDomain, + database: db, + managedEndpointClients: ManagedEndpointProvider.layerCloudflareClients( + tunnel, + dns, + alchemyRuntimeContext, ), - MobileTelemetry.layerApi.pipe(Layer.provide(layerMobileTelemetry)), - ).pipe( - Layer.provideMerge(RelayHttpApi.layerClientAuth), - Layer.provideMerge(RelayHttpApi.layerDpopClientAuth), - Layer.provideMerge(RelayHttpApi.layerEnvironmentAuth), - Layer.provide(layerRuntime), - ); - - yield* Cloudflare.Queues.consumeQueueMessages( - apnsDeliveryQueue, - { - batchSize: 10, - maxRetries: 5, - maxWaitTime: "5 seconds", - retryDelay: "30 seconds", - deadLetterQueue: apnsDeliveryDeadLetterQueue.queueName as unknown as string, + hookInbox: Layer.succeed(HookInbox.HookInbox, { + hold: Effect.fn("HookInbox.hold")(function ({ endpointKey, baseUrl, hook }) { + return hookInboxes + .getByName(endpointKey) + .hold(hook, baseUrl) + .pipe(inboxCall("hold", endpointKey)); + }), + wake: Effect.fn("HookInbox.wake")(function ({ endpointKey, baseUrl }) { + return hookInboxes + .getByName(endpointKey) + .wake(baseUrl) + .pipe(inboxCall("wake", endpointKey)); + }), + clear: Effect.fn("HookInbox.clear")(function ({ endpointKey }) { + return hookInboxes.getByName(endpointKey).clear().pipe(inboxCall("clear", endpointKey)); + }), + }), + rateLimits: { + hook: limitKey(hookRateLimit), + hookEndpoint: limitKey(hookEndpointRateLimit), + mobileTelemetry: limitKey(mobileTelemetryRateLimit), }, - (stream) => - stream.pipe( - Stream.withSpan("relay.apn_delivery_queue.process_batch"), - Stream.runForEach((message) => - ApnsDeliveries.ApnsDeliveries.pipe( - Effect.flatMap((deliveries) => deliveries.processSignedJob(message.body)), - Effect.withSpan("relay.apn_delivery_queue.process_message"), + mobileTelemetry: { + settings: Effect.gen(function* () { + return MobileTelemetry.MobileTelemetrySettings.of({ + axiomToken: yield* mobileTelemetryToken, + destinations: { + traces: { url: yield* mobileTracesUrl, dataset: yield* mobileTracesDataset }, + logs: { url: yield* mobileLogsUrl, dataset: yield* mobileLogsDataset }, + metrics: { url: yield* mobileMetricsUrl, dataset: yield* mobileMetricsDataset }, + }, + }); + }), + sourceMaps: Effect.succeed({ + get: (key) => + inRuntime(mobileSourceMaps.get(key)).pipe( + Effect.map((object) => + object === null + ? undefined + : { + size: object.size, + text: inRuntime(object.text()).pipe( + Effect.mapError( + (cause) => new MobileTelemetry.SourceMapObjectError({ key, cause }), + ), + ), + }, + ), + Effect.mapError((cause) => new MobileTelemetry.SourceMapObjectError({ key, cause })), ), - ), - Effect.provide(layerRuntime), - ), - ); - - yield* Cloudflare.Queues.consumeQueueMessages( - fcmDeliveryQueue, - { - batchSize: 10, - maxRetries: 5, - maxWaitTime: "1 second", - retryDelay: "30 seconds", - deadLetterQueue: fcmDeliveryDeadLetterQueue.queueName as unknown as string, + }), }, - (stream) => - stream.pipe( - Stream.withSpan("relay.fcm_delivery_queue.process_batch"), - Stream.runForEach(FcmDeliveryQueueConsumer.processMessage), - Effect.provide(layerRuntime), - ), - ); - - yield* Cloudflare.Workers.cron("*/5 * * * *", () => - Effect.all( - [ - DpopProofs.DpopProofReplay.pipe( - Effect.flatMap((dpopProofs) => dpopProofs.pruneExpired), - Effect.andThen( - Effect.all([AgentActivityRows.AgentActivityRows, DateTime.now]).pipe( - Effect.flatMap(([activityRows, now]) => - activityRows.pruneTerminal({ - updatedBefore: DateTime.formatIso(DateTime.subtract(now, { minutes: 30 })), + apnsSender: Effect.succeed({ + send: (body) => + inRuntime(apnsWriteQueue.send(body)).pipe( + Effect.mapError((cause) => new Ports.DeliveryQueueSendError({ cause })), + ), + }), + fcmSender: Effect.succeed({ + send: (body) => + inRuntime(fcmWriteQueue.send(body)).pipe( + Effect.mapError((cause) => new Ports.DeliveryQueueSendError({ cause })), + ), + }), + // Acknowledge or retry each message on its own, so one bad job cannot replay its batch. + consumeQueue: (name, options, handler) => + Cloudflare.Queues.consumeQueueMessages( + name === "apns" ? apnsDeliveryQueue : fcmDeliveryQueue, + { + batchSize: options.batchSize, + maxRetries: options.maxRetries, + maxWaitTime: name === "apns" ? "5 seconds" : "1 second", + retryDelay: options.retryDelay, + deadLetterQueue: (name === "apns" + ? apnsDeliveryDeadLetterQueue + : fcmDeliveryDeadLetterQueue + ).queueName as unknown as string, + }, + (stream) => + stream.pipe( + Stream.withSpan(`relay.${name}_delivery_queue.process_batch`), + Stream.runForEach((message) => + handler(message.body).pipe( + Effect.matchEffect({ + onFailure: () => Effect.sync(() => message.retry()), + onSuccess: () => Effect.sync(() => message.ack()), }), ), ), ), - Effect.andThen( - DeliveryAttempts.DeliveryAttempts.pipe( - Effect.flatMap((attempts) => attempts.pruneExpired), - ), - ), - Effect.catchCause((cause) => - Cause.hasInterrupts(cause) - ? Effect.interrupt - : Effect.logWarning("Failed to prune expired relay state", { cause }), - ), - ), - ManagedEndpointReaper.ManagedEndpointReaper.pipe( - Effect.flatMap((reaper) => reaper.sweep.pipe(Effect.timeout("2 minutes"))), - Effect.tap((result) => - result.scanned > 0 - ? Effect.logInfo("Finished managed tunnel cleanup", result) - : Effect.void, - ), - Effect.catchCause((cause) => - Cause.hasInterrupts(cause) - ? Effect.interrupt - : Effect.logWarning("Failed to clean up inactive managed tunnels", { cause }), - ), - ), - AgentActivityPublisher.AgentActivityPublisher.pipe( - Effect.flatMap((publisher) => publisher.endIdleLiveActivities), - Effect.catchCause((cause) => - Cause.hasInterrupts(cause) - ? Effect.interrupt - : Effect.logWarning("Failed to end idle Live Activities", { cause }), - ), - ), - ], - { concurrency: 3, discard: true }, - ).pipe( - Effect.withSpan("relay.cron.prune_expired_state"), - Observability.withSchemaErrorSpanAttributes, - Effect.provide(layerRuntime), - ), - ); - - const fetch = Layer.merge( - Layer.mergeAll( - HttpApiBuilder.layer(RelayApi, { openapiPath: "/openapi.json" }).pipe( - Layer.provide(layerApp), ), - HttpApiScalar.layer(RelayApi, { path: "/docs" }), - RelayHttpApi.layerDocsRedirectRoute, - ).pipe( - Layer.provide([Etag.layerWeak, layerHttpPlatformNotSupported, RelayHttpApi.layerCors]), - ), - RelayHttpApi.layerNotFoundRoute, - ).pipe( - HttpRouter.toHttpEffect, - Effect.provideService(HttpRouter.RouterConfig, RELAY_HTTP_ROUTER_CONFIG), - withoutCapturedParentSpan, - Effect.map((httpEffect) => - traceRelayHttpRequestWith(httpEffect, Layer.empty).pipe( - Observability.withSchemaErrorSpanAttributes, - ), - ), - ); + schedule: (cron, effect) => Cloudflare.Workers.cron(cron, () => effect), + }; - return { fetch }; + return yield* composeRelay(host); }).pipe( Effect.provide( Layer.empty.pipe( + Layer.provideMerge(FetchHttpClient.layer), Layer.provideMerge(Cloudflare.Hyperdrive.ConnectBinding), Layer.provideMerge(Cloudflare.Workers.CronEventSourceLive), Layer.provideMerge(Cloudflare.Queues.WriteQueueBinding), @@ -587,4 +282,8 @@ export const layer = Api.make( ), ); +type CloudflareEventSources = + | Layer.Success + | Layer.Success; + export default layer; diff --git a/infra/relay/vite.config.ts b/infra/relay/vite.config.ts new file mode 100644 index 0000000000000..a03550cf2cbf2 --- /dev/null +++ b/infra/relay/vite.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from "vite-plus"; + +export default defineConfig({ + pack: { + deps: { + alwaysBundle: ["@t3tools/*"], + }, + }, +}); diff --git a/knip.jsonc b/knip.jsonc index ed4bf19129645..d6a0149825ad5 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -145,8 +145,8 @@ ], }, "infra/relay": { - // Alchemy loads src/worker.ts as the Worker entry and reads its default export. - "entry": ["alchemy.run.ts!", "src/persistence/schema.ts!", "src/worker.ts!"], + // Alchemy loads the worker entry; the self-hosted image loads src/node.ts directly. + "entry": ["alchemy.run.ts!", "src/persistence/schema.ts!", "src/worker.ts!", "src/node.ts!"], }, "packages/*": { // These workspace packages are private to this monorepo. diff --git a/packages/client-runtime/src/connection/errors.test.ts b/packages/client-runtime/src/connection/errors.test.ts index 55619af5a413e..044156a38b154 100644 --- a/packages/client-runtime/src/connection/errors.test.ts +++ b/packages/client-runtime/src/connection/errors.test.ts @@ -58,6 +58,11 @@ describe("mapManagedRelayError", () => { reason: "permission", detail: expect.stringContaining("turn T3 Connect on"), }); + expect(mapConnectRefusal("manual_endpoint_not_allowed")).toMatchObject({ + _tag: "ConnectionBlockedError", + reason: "permission", + detail: expect.stringContaining("HTTPS address"), + }); expect(mapConnectRefusal(undefined)).toMatchObject({ _tag: "ConnectionBlockedError", reason: "permission", diff --git a/packages/client-runtime/src/relay/cloudAuthState.ts b/packages/client-runtime/src/relay/cloudAuthState.ts new file mode 100644 index 0000000000000..5a0261a363c6a --- /dev/null +++ b/packages/client-runtime/src/relay/cloudAuthState.ts @@ -0,0 +1,23 @@ +/** + * What an app needs from whichever provider signs the user in to T3 Connect, Clerk or an OpenID + * Connect server. Signing in differs by platform, so each app adds its own actions. + */ +export interface CloudAuthState { + readonly provider: "clerk" | "oidc" | null; + readonly isLoaded: boolean; + readonly isSignedIn: boolean; + readonly userId: string | null; + /** A person-readable name for the signed-in account. */ + readonly accountLabel: string | null; + /** The bearer token the relay verifies; null when signed out. */ + readonly getToken: () => Promise; +} + +export const SIGNED_OUT_CLOUD_AUTH: CloudAuthState = { + provider: null, + isLoaded: true, + isSignedIn: false, + userId: null, + accountLabel: null, + getToken: async () => null, +}; diff --git a/packages/client-runtime/src/relay/errorPresentation.ts b/packages/client-runtime/src/relay/errorPresentation.ts index a446cb2489361..3ace5e5b8decc 100644 --- a/packages/client-runtime/src/relay/errorPresentation.ts +++ b/packages/client-runtime/src/relay/errorPresentation.ts @@ -53,6 +53,9 @@ export function relayProtectedErrorMessage(error: RelayProtectedError): string { if (error.reason === "endpoint_provider_not_managed") { return "This environment is linked only to publish agent activity, so it cannot accept connections. On that computer, open Settings > Sharing & access and turn T3 Connect on."; } + if (error.reason === "manual_endpoint_not_allowed") { + return "The address this environment published is not one the relay may contact. It must be an HTTPS address reachable from the internet, not localhost or a private network."; + } return error.reason ? `Relay rejected the environment connection request (${error.reason}).` : "Relay rejected the environment connection request."; diff --git a/packages/client-runtime/src/relay/index.ts b/packages/client-runtime/src/relay/index.ts index 9a6635a5f9b95..1481c3e8cdbb5 100644 --- a/packages/client-runtime/src/relay/index.ts +++ b/packages/client-runtime/src/relay/index.ts @@ -1,6 +1,8 @@ export * as Discovery from "./discovery.ts"; export * as DpopProof from "./dpopProof.ts"; export * from "./cloudAccountService.ts"; +export * from "./cloudAuthState.ts"; +export * from "./oidcController.ts"; export * from "./errorPresentation.ts"; export * as ManagedRelay from "./managedRelay.ts"; export * from "./managedRelayState.ts"; diff --git a/packages/client-runtime/src/relay/oidcController.test.ts b/packages/client-runtime/src/relay/oidcController.test.ts new file mode 100644 index 0000000000000..1a898a546d653 --- /dev/null +++ b/packages/client-runtime/src/relay/oidcController.test.ts @@ -0,0 +1,249 @@ +import { base64UrlEncode } from "@t3tools/shared/oidc"; +import { describe, expect, it } from "vite-plus/test"; + +import { createOidcController, type KeyValueStore } from "./oidcController.ts"; + +const ISSUER = "https://sso.example.com/realms/t3"; +const REDIRECT_URI = "https://app.example.com/oidc/callback"; + +const memoryStore = (): KeyValueStore & { readonly entries: Map } => { + const entries = new Map(); + return { + entries, + getItem: (key) => entries.get(key) ?? null, + setItem: (key, value) => void entries.set(key, value), + removeItem: (key) => void entries.delete(key), + }; +}; + +const encodeJson = (value: unknown) => + base64UrlEncode(new TextEncoder().encode(JSON.stringify(value))); +const idToken = (claims: Record) => + `${encodeJson({ alg: "ES256" })}.${encodeJson({ + sub: "alice", + iss: ISSUER, + email: "alice@example.com", + ...claims, + })}.signature`; + +interface Harness { + readonly controller: ReturnType; + readonly sessions: ReturnType; + readonly pending: ReturnType; + readonly navigations: Array; + readonly tokenRequests: Array; + clock: number; + tokenReply: () => Response; +} + +function makeHarness(): Harness { + const sessions = memoryStore(); + const pending = memoryStore(); + const harness: Harness = { + sessions, + pending, + navigations: [], + tokenRequests: [], + clock: 1_000_000, + tokenReply: () => Response.json({}, { status: 500 }), + controller: undefined as never, + }; + const fetchImpl = async (input: string | URL | Request, init?: RequestInit) => { + const url = String(input); + if (url.endsWith("/.well-known/openid-configuration")) { + return Response.json({ + issuer: ISSUER, + authorization_endpoint: `${ISSUER}/auth`, + token_endpoint: `${ISSUER}/token`, + }); + } + harness.tokenRequests.push(new URLSearchParams(String(init?.body))); + return harness.tokenReply(); + }; + return Object.assign(harness, { + controller: createOidcController({ + config: { issuer: ISSUER, clientId: "t3-code" }, + redirectUri: REDIRECT_URI, + sessions, + pending, + navigate: (url) => harness.navigations.push(url), + now: () => harness.clock, + fetch: fetchImpl as typeof fetch, + }), + }); +} + +/** Signs in through the redirect and callback, with the provider answering `tokens`. */ +async function signInWith(harness: Harness, claims: Record = {}) { + await harness.controller.signIn("/settings/connections"); + const authorize = new URL(harness.navigations[0]!); + harness.tokenReply = () => + Response.json({ + id_token: idToken({ + exp: harness.clock / 1000 + 3600, + nonce: authorize.searchParams.get("nonce"), + ...claims, + }), + refresh_token: "refresh-1", + }); + const callback = `${REDIRECT_URI}?code=code-1&state=${authorize.searchParams.get("state")}`; + return { authorize, returnTo: await harness.controller.completeSignIn(callback) }; +} + +describe("sign-in redirect", () => { + it("sends the user to the provider with PKCE, state and a nonce", async () => { + const harness = makeHarness(); + await harness.controller.signIn("/settings/connections"); + + const url = new URL(harness.navigations[0]!); + expect(url.origin + url.pathname).toBe(`${ISSUER}/auth`); + expect(Object.fromEntries(url.searchParams)).toMatchObject({ + response_type: "code", + client_id: "t3-code", + redirect_uri: REDIRECT_URI, + code_challenge_method: "S256", + }); + expect(url.searchParams.get("state")).toBeTruthy(); + expect(url.searchParams.get("nonce")).toBeTruthy(); + }); + + it("completes the callback, stores the session and returns to the page that asked", async () => { + const harness = makeHarness(); + const seen: Array = []; + harness.controller.addListener((resources) => seen.push(resources.user?.id ?? null)); + + const { returnTo } = await signInWith(harness); + + expect(returnTo).toBe("/settings/connections"); + expect(harness.tokenRequests[0]?.get("grant_type")).toBe("authorization_code"); + expect(harness.tokenRequests[0]?.get("code")).toBe("code-1"); + expect(harness.controller.getSnapshot()).toEqual({ + userId: "alice", + label: "alice@example.com", + }); + expect(seen).toEqual([null, "alice"]); + expect(harness.pending.entries.size).toBe(0); + }); + + it("signs in through an in-app browser that hands back the redirect URL", async () => { + const harness = makeHarness(); + + await harness.controller.signInWithPrompt(async (authorizeUrl) => { + const url = new URL(authorizeUrl); + harness.tokenReply = () => + Response.json({ + id_token: idToken({ + exp: harness.clock / 1000 + 3600, + nonce: url.searchParams.get("nonce"), + }), + refresh_token: "refresh-1", + }); + return `${REDIRECT_URI}?code=code-1&state=${url.searchParams.get("state")}`; + }); + + expect(harness.controller.getSnapshot().userId).toBe("alice"); + expect(harness.navigations).toEqual([]); + }); + + it.each([ + ["a state this tab never issued", (state: string) => `?code=c&state=${state}x`], + ["a refusal from the provider", () => "?error=access_denied"], + ["no code", (state: string) => `?state=${state}`], + ])("rejects %s without asking for tokens", async (_name, query) => { + const harness = makeHarness(); + await harness.controller.signIn(); + const state = new URL(harness.navigations[0]!).searchParams.get("state")!; + + await expect( + harness.controller.completeSignIn(`${REDIRECT_URI}${query(state)}`), + ).rejects.toThrow(); + + expect(harness.tokenRequests).toEqual([]); + expect(harness.controller.getSnapshot().userId).toBeNull(); + // The request is spent, so replaying the callback cannot succeed either. + expect(harness.pending.entries.size).toBe(0); + }); + + it("never returns to another origin", async () => { + const harness = makeHarness(); + await harness.controller.signIn("https://evil.example.com/"); + const authorize = new URL(harness.navigations[0]!); + harness.tokenReply = () => + Response.json({ + id_token: idToken({ exp: 2_000_000, nonce: authorize.searchParams.get("nonce") }), + }); + + expect( + await harness.controller.completeSignIn( + `${REDIRECT_URI}?code=c&state=${authorize.searchParams.get("state")}`, + ), + ).toBe("/"); + }); +}); + +describe("tokens", () => { + it("returns the stored ID token while it has a minute to spare", async () => { + const harness = makeHarness(); + await signInWith(harness); + const requests = harness.tokenRequests.length; + + const token = await harness.controller.getToken(); + + expect(token).toBeTruthy(); + expect(harness.tokenRequests).toHaveLength(requests); + }); + + it("refreshes an expiring token once for concurrent callers", async () => { + const harness = makeHarness(); + await signInWith(harness); + harness.clock += 3600 * 1000; + harness.tokenReply = () => + Response.json({ id_token: idToken({ exp: harness.clock / 1000 + 3600 }) }); + const before = harness.tokenRequests.length; + + const tokens = await Promise.all([ + harness.controller.getToken(), + harness.controller.getToken(), + ]); + + expect(tokens[0]).toBe(tokens[1]); + expect(harness.tokenRequests.slice(before)).toHaveLength(1); + expect(harness.tokenRequests.at(-1)?.get("grant_type")).toBe("refresh_token"); + expect(harness.tokenRequests.at(-1)?.get("refresh_token")).toBe("refresh-1"); + }); + + it("signs out when the provider ends the session, but keeps it through an outage", async () => { + const harness = makeHarness(); + await signInWith(harness); + harness.clock += 3600 * 1000; + + harness.tokenReply = () => Response.json({}, { status: 503 }); + await expect(harness.controller.getToken()).rejects.toThrow(); + expect(harness.controller.getSnapshot().userId).toBe("alice"); + + harness.tokenReply = () => Response.json({ error: "invalid_grant" }, { status: 400 }); + expect(await harness.controller.getToken()).toBeNull(); + expect(harness.controller.getSnapshot().userId).toBeNull(); + expect(harness.sessions.entries.size).toBe(0); + }); + + it("clears the session and the in-flight sign-in on sign out", async () => { + const harness = makeHarness(); + await signInWith(harness); + + await harness.controller.signOut(); + + expect(harness.controller.getSnapshot().userId).toBeNull(); + expect(await harness.controller.getToken()).toBeNull(); + }); + + it("adopts a session another tab removed", async () => { + const harness = makeHarness(); + await signInWith(harness); + + harness.sessions.entries.clear(); + harness.controller.syncFromStorage(); + + expect(harness.controller.getSnapshot().userId).toBeNull(); + }); +}); diff --git a/packages/client-runtime/src/relay/oidcController.ts b/packages/client-runtime/src/relay/oidcController.ts new file mode 100644 index 0000000000000..4ff63bf53bd3f --- /dev/null +++ b/packages/client-runtime/src/relay/oidcController.ts @@ -0,0 +1,261 @@ +import type { CloudAccountSource } from "./cloudAccountService.ts"; +import { + OidcError, + buildAuthorizeUrl, + createPkcePair, + discoverOidc, + exchangeAuthorizationCode, + isIdTokenFresh, + isPermanentRefreshFailure, + oidcAccountLabel, + randomToken, + refreshOidcTokens, + type OidcMetadata, + type OidcTokens, +} from "@t3tools/shared/oidc"; + +/** The slice of `Storage` the controller needs. */ +export interface KeyValueStore { + readonly getItem: (key: string) => string | null; + readonly setItem: (key: string, value: string) => void; + readonly removeItem: (key: string) => void; +} + +interface StoredSession { + readonly idToken: string; + readonly refreshToken: string | null; + readonly expiresAtEpochMs: number; + readonly userId: string; + readonly label: string; +} + +interface PendingAuthorization { + readonly state: string; + readonly nonce: string; + readonly verifier: string; + readonly returnTo: string; +} + +export interface OidcSnapshot { + readonly userId: string | null; + readonly label: string | null; +} + +export interface OidcControllerDeps { + readonly config: { readonly issuer: string; readonly clientId: string }; + readonly redirectUri: string; + /** Survives reloads and is shared between tabs. */ + readonly sessions: KeyValueStore; + /** Holds the sign-in in flight for this tab only. */ + readonly pending: KeyValueStore; + readonly navigate: (url: string) => void; + readonly now?: () => number; + readonly fetch?: typeof fetch; + /** Serializes refreshes across tabs: providers that rotate refresh tokens reject a stale one. */ + readonly withRefreshLock?: (run: () => Promise) => Promise; +} + +const SIGNED_OUT: OidcSnapshot = { userId: null, label: null }; +const PENDING_KEY = "t3-oidc-pending"; + +const parseJson = (value: string | null): A | null => { + if (value === null) return null; + try { + return JSON.parse(value) as A; + } catch { + return null; + } +}; + +// The return path comes from this tab's own storage, but still must never leave the origin. +const safeReturnTo = (value: string): string => + value.startsWith("/") && !value.startsWith("//") ? value : "/"; + +export function createOidcController(deps: OidcControllerDeps) { + const now = deps.now ?? Date.now; + const fetchImpl = deps.fetch ?? fetch; + const withRefreshLock = deps.withRefreshLock ?? ((run: () => Promise) => run()); + const storageKey = `t3-oidc-session:${deps.config.issuer}:${deps.config.clientId}`; + + const readSession = (): StoredSession | null => + parseJson(deps.sessions.getItem(storageKey)); + let session = readSession(); + const toSnapshot = (value: StoredSession | null): OidcSnapshot => + value === null ? SIGNED_OUT : { userId: value.userId, label: value.label }; + let snapshot = toSnapshot(session); + const listeners = new Set<() => void>(); + + const publish = (next: StoredSession | null) => { + session = next; + if (next === null) { + deps.sessions.removeItem(storageKey); + } else { + deps.sessions.setItem(storageKey, JSON.stringify(next)); + } + const nextSnapshot = toSnapshot(next); + if (nextSnapshot.userId !== snapshot.userId || nextSnapshot.label !== snapshot.label) { + snapshot = nextSnapshot; + for (const listener of listeners) listener(); + } + }; + + const toStored = (tokens: OidcTokens): StoredSession => ({ + idToken: tokens.idToken, + refreshToken: tokens.refreshToken, + expiresAtEpochMs: tokens.expiresAtEpochMs, + userId: tokens.claims.sub, + label: oidcAccountLabel(tokens.claims), + }); + + let metadataPromise: Promise | null = null; + const metadata = () => { + metadataPromise ??= discoverOidc(deps.config.issuer, fetchImpl).catch((error: unknown) => { + metadataPromise = null; + throw error; + }); + return metadataPromise; + }; + + const startAuthorization = async (returnTo: string): Promise => { + const [oidc, pkce] = await Promise.all([metadata(), createPkcePair()]); + const request: PendingAuthorization = { + state: randomToken(16), + nonce: randomToken(16), + verifier: pkce.verifier, + returnTo: safeReturnTo(returnTo), + }; + deps.pending.setItem(PENDING_KEY, JSON.stringify(request)); + return buildAuthorizeUrl({ + authorizationEndpoint: oidc.authorizationEndpoint, + clientId: deps.config.clientId, + redirectUri: deps.redirectUri, + state: request.state, + nonce: request.nonce, + challenge: pkce.challenge, + }); + }; + + /** Leaves the page for the provider; the redirect back is finished by `completeSignIn`. */ + const signIn = async (returnTo = "/"): Promise => { + deps.navigate(await startAuthorization(returnTo)); + }; + + /** Finishes the redirect back from the provider and returns where to send the user next. */ + const completeSignIn = async (callbackUrl: string): Promise => { + const request = parseJson(deps.pending.getItem(PENDING_KEY)); + // One attempt per redirect: a replayed callback must not find the request again. + deps.pending.removeItem(PENDING_KEY); + const params = new URL(callbackUrl).searchParams; + const providerError = params.get("error"); + if (providerError !== null) { + throw new OidcError({ + code: "token_request_failed", + message: `The provider refused the sign-in (${providerError}).`, + oauthError: providerError, + }); + } + const code = params.get("code"); + if (request === null || code === null || params.get("state") !== request.state) { + throw new OidcError({ + code: "token_request_failed", + message: "The sign-in response does not match a request from this tab.", + }); + } + const tokens = await exchangeAuthorizationCode({ + metadata: await metadata(), + clientId: deps.config.clientId, + redirectUri: deps.redirectUri, + code, + verifier: request.verifier, + nonce: request.nonce, + fetch: fetchImpl, + }); + publish(toStored(tokens)); + return safeReturnTo(request.returnTo); + }; + + /** + * For apps that show the provider in an in-app browser: `prompt` opens the URL and resolves with + * the URL the provider redirected back to. + */ + const signInWithPrompt = async (prompt: (authorizeUrl: string) => Promise) => + completeSignIn(await prompt(await startAuthorization("/"))); + + let refreshing: Promise | null = null; + const refresh = (): Promise => { + refreshing ??= withRefreshLock(async () => { + // Another tab may have refreshed while this one waited for the lock. + const current = readSession(); + if (current !== null && isIdTokenFresh(current, now())) { + publish(current); + return current.idToken; + } + if (current === null || current.refreshToken === null) { + publish(null); + return null; + } + try { + const tokens = await refreshOidcTokens({ + metadata: await metadata(), + clientId: deps.config.clientId, + refreshToken: current.refreshToken, + fetch: fetchImpl, + }); + publish(toStored(tokens)); + return tokens.idToken; + } catch (error) { + if (isPermanentRefreshFailure(error)) { + publish(null); + return null; + } + throw error; + } + }).finally(() => { + refreshing = null; + }); + return refreshing; + }; + + /** The ID token the relay verifies. Null when signed out; throws when the provider is unreachable. */ + const getToken = async (): Promise => { + if (session === null) return null; + return isIdTokenFresh(session, now()) ? session.idToken : refresh(); + }; + + const signOut = async (): Promise => { + deps.pending.removeItem(PENDING_KEY); + publish(null); + }; + + /** Adopts a session another tab wrote or removed. */ + const syncFromStorage = () => publish(readSession()); + + const subscribe = (listener: () => void) => { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; + }; + + const addListener: CloudAccountSource["addListener"] = (listener) => { + const emit = () => + listener({ user: snapshot.userId === null ? null : { id: snapshot.userId } }); + emit(); + return subscribe(emit); + }; + + return { + storageKey, + getSnapshot: () => snapshot, + subscribe, + addListener, + signIn, + signInWithPrompt, + completeSignIn, + getToken, + signOut, + syncFromStorage, + }; +} + +export type OidcController = ReturnType; diff --git a/packages/contracts/src/relay.ts b/packages/contracts/src/relay.ts index 8ebc9df23a0bb..d7c8d9212b8f9 100644 --- a/packages/contracts/src/relay.ts +++ b/packages/contracts/src/relay.ts @@ -473,6 +473,7 @@ export const RelayEnvironmentConnectNotAuthorizedReason = Schema.Literals([ "managed_endpoint_allocation_not_ready", "managed_endpoint_hostname_invalid", "managed_endpoint_mismatch", + "manual_endpoint_not_allowed", ]); export type RelayEnvironmentConnectNotAuthorizedReason = typeof RelayEnvironmentConnectNotAuthorizedReason.Type; diff --git a/packages/shared/package.json b/packages/shared/package.json index 5d9a87ac7719d..c5410141af14e 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -199,6 +199,10 @@ "types": "./src/telemetryId.ts", "import": "./src/telemetryId.ts" }, + "./oidc": { + "types": "./src/oidc.ts", + "import": "./src/oidc.ts" + }, "./relayAuth": { "types": "./src/relayAuth.ts", "import": "./src/relayAuth.ts" diff --git a/packages/shared/src/hostClassification.test.ts b/packages/shared/src/hostClassification.test.ts index 4110c9f207a54..23403f6ad6ce9 100644 --- a/packages/shared/src/hostClassification.test.ts +++ b/packages/shared/src/hostClassification.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { isPublicFaviconHost } from "./hostClassification.ts"; +import { isInternalOnlyHost, isPublicFaviconHost } from "./hostClassification.ts"; describe("isPublicFaviconHost", () => { it("treats public hosts as public", () => { @@ -109,3 +109,35 @@ describe("isPublicFaviconHost", () => { expect(isPublicFaviconHost("10.0.0")).toBe(true); }); }); + +describe("isInternalOnlyHost", () => { + it("flags loopback, link-local, and unspecified addresses", () => { + for (const host of [ + "localhost", + "app.localhost", + "127.0.0.1", + "0.0.0.0", + "169.254.169.254", + "::1", + "::", + "fe80::1", + "[fe80::abcd]", + "::ffff:169.254.169.254", + ]) { + expect(isInternalOnlyHost(host), host).toBe(true); + } + }); + + it("leaves private networks and public hosts alone", () => { + for (const host of [ + "10.0.0.5", + "192.168.1.10", + "172.16.0.1", + "100.64.0.1", + "t3.example.dev", + "8.8.8.8", + ]) { + expect(isInternalOnlyHost(host), host).toBe(false); + } + }); +}); diff --git a/packages/shared/src/hostClassification.ts b/packages/shared/src/hostClassification.ts index 29648b45df42d..ba61fe0f51e8d 100644 --- a/packages/shared/src/hostClassification.ts +++ b/packages/shared/src/hostClassification.ts @@ -87,6 +87,19 @@ export const isTailnetHost = (host: string): boolean => { return ipv6 !== null && ipv6PrefixMatches(ipv6, [0xfd7a, 0x115c, 0xa1e0, 0, 0, 0, 0, 0], 48); }; +/** Addresses that never name another machine: loopback, link-local (cloud metadata) and unspecified. */ +export const isInternalOnlyHost = (host: string): boolean => { + const normalized = normalizeHostname(host); + if (isLocalLoopbackHost(normalized) || normalized.endsWith(".localhost")) return true; + const ipv4 = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized); + if (ipv4) return ipv4[0] === 0 || ipv4[0] === 127 || (ipv4[0] === 169 && ipv4[1] === 254); + const ipv6 = parseIpv6Address(normalized); + return ( + ipv6 !== null && + (ipv6.every((part) => part === 0) || ipv6PrefixMatches(ipv6, [0xfe80, 0, 0, 0, 0, 0, 0, 0], 10)) + ); +}; + export const isSharedAddressSpaceHost = (host: string): boolean => { const parts = parseIpv4Address(host); return parts !== null && parts[0] === 100 && parts[1]! >= 64 && parts[1]! <= 127; diff --git a/packages/shared/src/oidc.test.ts b/packages/shared/src/oidc.test.ts new file mode 100644 index 0000000000000..a63df83ad2ec0 --- /dev/null +++ b/packages/shared/src/oidc.test.ts @@ -0,0 +1,212 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { + OidcError, + base64UrlEncode, + buildAuthorizeUrl, + createPkcePair, + decodeIdTokenClaims, + discoverOidc, + exchangeAuthorizationCode, + isDesktopOidcCallbackUrl, + isIdTokenFresh, + isPermanentRefreshFailure, + oidcAccountLabel, + oidcDiscoveryUrl, + refreshOidcTokens, + type OidcMetadata, +} from "./oidc.ts"; + +const ISSUER = "https://sso.example.com/realms/t3"; +const metadata: OidcMetadata = { + issuer: ISSUER, + authorizationEndpoint: `${ISSUER}/protocol/openid-connect/auth`, + tokenEndpoint: `${ISSUER}/protocol/openid-connect/token`, + deviceAuthorizationEndpoint: null, +}; + +const encodeJson = (value: unknown) => + base64UrlEncode(new TextEncoder().encode(JSON.stringify(value))); +const idToken = (claims: Record) => + `${encodeJson({ alg: "ES256" })}.${encodeJson({ sub: "alice", iss: ISSUER, exp: 2_000_000_000, ...claims })}.signature`; + +const jsonResponse = (body: unknown, status = 200) => Response.json(body, { status }); + +describe("discoverOidc", () => { + it("reads the endpoints and composes the URL without a double slash", async () => { + const urls: Array = []; + const found = await discoverOidc(`${ISSUER}/`, async (url) => { + urls.push(url); + return jsonResponse({ + issuer: `${ISSUER}/`, + authorization_endpoint: metadata.authorizationEndpoint, + token_endpoint: metadata.tokenEndpoint, + device_authorization_endpoint: `${ISSUER}/device`, + }); + }); + + expect(urls).toEqual([`${ISSUER}/.well-known/openid-configuration`]); + expect(oidcDiscoveryUrl(ISSUER)).toBe(`${ISSUER}/.well-known/openid-configuration`); + expect(found.deviceAuthorizationEndpoint).toBe(`${ISSUER}/device`); + }); + + it.each([ + ["an issuer that differs by a trailing slash", { issuer: `${ISSUER}/` }, "issuer_mismatch"], + [ + "a plain HTTP token endpoint", + { token_endpoint: "http://sso.example.com/token" }, + "insecure_endpoint", + ], + ["a missing token endpoint", { token_endpoint: undefined }, "discovery_failed"], + ])("rejects %s", async (_name, override, code) => { + const failure = await discoverOidc(ISSUER, async () => + jsonResponse({ + issuer: ISSUER, + authorization_endpoint: metadata.authorizationEndpoint, + token_endpoint: metadata.tokenEndpoint, + ...override, + }), + ).catch((error: unknown) => error); + + expect(failure).toBeInstanceOf(OidcError); + expect((failure as OidcError).code).toBe(code); + }); +}); + +describe("PKCE and authorization URL", () => { + it("derives the RFC 7636 appendix B challenge", async () => { + const pair = await createPkcePair("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"); + expect(pair.challenge).toBe("E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"); + }); + + it("builds an authorization-code request with S256 and a nonce", () => { + const url = new URL( + buildAuthorizeUrl({ + authorizationEndpoint: metadata.authorizationEndpoint, + clientId: "t3-code", + redirectUri: "https://app.example.com/oidc/callback", + state: "state-1", + nonce: "nonce-1", + challenge: "challenge-1", + }), + ); + + expect(Object.fromEntries(url.searchParams)).toEqual({ + response_type: "code", + client_id: "t3-code", + redirect_uri: "https://app.example.com/oidc/callback", + scope: "openid profile email offline_access", + state: "state-1", + nonce: "nonce-1", + code_challenge: "challenge-1", + code_challenge_method: "S256", + }); + }); +}); + +describe("token requests", () => { + const exchange = (body: unknown, nonce = "nonce-1") => + exchangeAuthorizationCode({ + metadata, + clientId: "t3-code", + redirectUri: "https://app.example.com/oidc/callback", + code: "code-1", + verifier: "verifier-1", + nonce, + fetch: async () => jsonResponse(body), + }); + + it("exchanges a code for the ID token and its expiry", async () => { + let sentBody = ""; + const tokens = await exchangeAuthorizationCode({ + metadata, + clientId: "t3-code", + redirectUri: "https://app.example.com/oidc/callback", + code: "code-1", + verifier: "verifier-1", + nonce: "nonce-1", + fetch: async (_url, init) => { + sentBody = String(init?.body); + return jsonResponse({ + id_token: idToken({ nonce: "nonce-1", email: "alice@example.com" }), + refresh_token: "refresh-1", + }); + }, + }); + + expect(Object.fromEntries(new URLSearchParams(sentBody))).toEqual({ + grant_type: "authorization_code", + code: "code-1", + redirect_uri: "https://app.example.com/oidc/callback", + client_id: "t3-code", + code_verifier: "verifier-1", + }); + expect(tokens).toMatchObject({ + refreshToken: "refresh-1", + expiresAtEpochMs: 2_000_000_000_000, + }); + expect(oidcAccountLabel(tokens.claims)).toBe("alice@example.com"); + }); + + it("rejects an ID token with another nonce or none at all", async () => { + await expect(exchange({ id_token: idToken({ nonce: "other" }) })).rejects.toMatchObject({ + code: "nonce_mismatch", + }); + await expect(exchange({ access_token: "opaque" })).rejects.toMatchObject({ + code: "missing_id_token", + }); + }); + + it("keeps the current refresh token when the provider does not rotate it", async () => { + const tokens = await refreshOidcTokens({ + metadata, + clientId: "t3-code", + refreshToken: "refresh-1", + fetch: async () => jsonResponse({ id_token: idToken({}) }), + }); + expect(tokens.refreshToken).toBe("refresh-1"); + }); + + it("tells a rejected refresh apart from a temporary failure", async () => { + const rejected = await refreshOidcTokens({ + metadata, + clientId: "t3-code", + refreshToken: "refresh-1", + fetch: async () => jsonResponse({ error: "invalid_grant" }, 400), + }).catch((error: unknown) => error); + const unavailable = await refreshOidcTokens({ + metadata, + clientId: "t3-code", + refreshToken: "refresh-1", + fetch: async () => jsonResponse({}, 503), + }).catch((error: unknown) => error); + + expect(isPermanentRefreshFailure(rejected)).toBe(true); + expect(isPermanentRefreshFailure(unavailable)).toBe(false); + }); +}); + +describe("ID token helpers", () => { + it("refuses a token without the required claims", () => { + expect(() => decodeIdTokenClaims("not-a-jwt")).toThrow(OidcError); + expect(() => decodeIdTokenClaims(`a.${encodeJson({ iss: ISSUER })}.c`)).toThrow(OidcError); + }); + + it("treats a token as fresh only with a minute to spare", () => { + expect(isIdTokenFresh({ expiresAtEpochMs: 200_000 }, 100_000)).toBe(true); + expect(isIdTokenFresh({ expiresAtEpochMs: 150_000 }, 100_000)).toBe(false); + }); +}); + +describe("isDesktopOidcCallbackUrl", () => { + it.each([ + ["t3code://app/oidc/callback?code=c&state=s", true], + ["t3code-dev://app/oidc/callback?code=c&state=s", true], + ["t3code://app/settings?code=c", false], + ["https://app/oidc/callback?code=c", false], + ["t3code://evil/oidc/callback?code=c", false], + ["not a url", false], + ])("%s → %s", (value, expected) => { + expect(isDesktopOidcCallbackUrl(value)).toBe(expected); + }); +}); diff --git a/packages/shared/src/oidc.ts b/packages/shared/src/oidc.ts new file mode 100644 index 0000000000000..d68d90b2dda1e --- /dev/null +++ b/packages/shared/src/oidc.ts @@ -0,0 +1,318 @@ +import * as Schema from "effect/Schema"; +// OpenID Connect client helpers shared by the web app, the mobile app, and the CLI. +// The relay accepts the ID token as the bearer, so clients need no provider-specific access +// token audience. The relay verifies the signature; clients only read the claims. + +export const OIDC_SCOPES = ["openid", "profile", "email", "offline_access"] as const; + +export interface OidcMetadata { + readonly issuer: string; + readonly authorizationEndpoint: string; + readonly tokenEndpoint: string; + readonly deviceAuthorizationEndpoint: string | null; +} + +export interface OidcIdTokenClaims { + readonly sub: string; + readonly iss: string; + readonly exp: number; + readonly nonce?: string; + readonly email?: string; + readonly name?: string; + readonly preferred_username?: string; +} + +export interface OidcTokens { + readonly idToken: string; + readonly refreshToken: string | null; + /** The ID token's own expiry, which is what the relay checks. */ + readonly expiresAtEpochMs: number; + readonly claims: OidcIdTokenClaims; +} + +export class OidcError extends Schema.TaggedError()("OidcError", { + code: Schema.Literals([ + "discovery_failed", + "issuer_mismatch", + "insecure_endpoint", + "token_request_failed", + "missing_id_token", + "invalid_id_token", + "nonce_mismatch", + ]), + message: Schema.String, + /** HTTP status of a failed token request. */ + status: Schema.optional(Schema.Number), + /** OAuth `error` code from a rejected token request, e.g. `invalid_grant`. */ + oauthError: Schema.optional(Schema.String), + cause: Schema.optional(Schema.Defect()), +}) {} + +type FetchLike = (input: string, init?: RequestInit) => Promise; + +const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "[::1]"]); + +// Endpoints carry codes and tokens, so they must be HTTPS; loopback HTTP is for local development. +const isSecureEndpoint = (value: string): boolean => { + try { + const url = new URL(value); + return ( + url.protocol === "https:" || + (url.protocol === "http:" && LOOPBACK_HOSTNAMES.has(url.hostname)) + ); + } catch { + return false; + } +}; + +export const OIDC_CALLBACK_PATH = "/oidc/callback"; + +/** The desktop app's OS-delivered redirect back from the identity provider (RFC 8252 private-use scheme). */ +export function isDesktopOidcCallbackUrl(value: string | undefined): boolean { + if (!value || !URL.canParse(value)) return false; + const url = new URL(value); + return ( + (url.protocol === "t3code:" || url.protocol === "t3code-dev:") && + url.host === "app" && + url.pathname === OIDC_CALLBACK_PATH + ); +} + +export const oidcDiscoveryUrl = (issuer: string): string => + `${issuer.replace(/\/+$/u, "")}/.well-known/openid-configuration`; + +const readString = (document: Record, key: string): string | null => { + const value = document[key]; + return typeof value === "string" && value.length > 0 ? value : null; +}; + +/** The issuer must equal the configured string exactly, including any path prefix. */ +export function parseOidcMetadata(issuer: string, body: unknown): OidcMetadata { + const document = (typeof body === "object" && body !== null ? body : {}) as Record< + string, + unknown + >; + if (document.issuer !== issuer) { + throw new OidcError({ + code: "issuer_mismatch", + message: "The OIDC discovery issuer does not match the configured one.", + }); + } + const authorizationEndpoint = readString(document, "authorization_endpoint"); + const tokenEndpoint = readString(document, "token_endpoint"); + if (authorizationEndpoint === null || tokenEndpoint === null) { + throw new OidcError({ + code: "discovery_failed", + message: "OIDC discovery has no authorization or token endpoint.", + }); + } + const deviceAuthorizationEndpoint = readString(document, "device_authorization_endpoint"); + for (const endpoint of [authorizationEndpoint, tokenEndpoint, deviceAuthorizationEndpoint]) { + if (endpoint !== null && !isSecureEndpoint(endpoint)) { + throw new OidcError({ code: "insecure_endpoint", message: "OIDC endpoints must use HTTPS." }); + } + } + return { issuer, authorizationEndpoint, tokenEndpoint, deviceAuthorizationEndpoint }; +} + +export async function discoverOidc( + issuer: string, + fetchImpl: FetchLike = fetch, +): Promise { + const response = await fetchImpl(oidcDiscoveryUrl(issuer)); + if (!response.ok) { + throw new OidcError({ + code: "discovery_failed", + message: `OIDC discovery returned ${response.status}.`, + status: response.status, + }); + } + return parseOidcMetadata(issuer, await response.json()); +} + +export const base64UrlEncode = (bytes: Uint8Array): string => + btoa(String.fromCharCode(...bytes)) + .replaceAll("+", "-") + .replaceAll("/", "_") + .replace(/=+$/u, ""); + +const base64UrlDecodeToString = (value: string): string => { + const padded = value.replaceAll("-", "+").replaceAll("_", "/"); + const binary = atob(padded.padEnd(Math.ceil(padded.length / 4) * 4, "=")); + return new TextDecoder().decode(Uint8Array.from(binary, (char) => char.charCodeAt(0))); +}; + +export const randomToken = (byteLength = 32): string => + base64UrlEncode(crypto.getRandomValues(new Uint8Array(byteLength))); + +export interface PkcePair { + readonly verifier: string; + readonly challenge: string; +} + +export async function createPkcePair(verifier: string = randomToken(32)): Promise { + const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)); + return { verifier, challenge: base64UrlEncode(new Uint8Array(digest)) }; +} + +export function buildAuthorizeUrl(input: { + readonly authorizationEndpoint: string; + readonly clientId: string; + readonly redirectUri: string; + readonly scopes?: ReadonlyArray; + readonly state: string; + readonly nonce: string; + readonly challenge: string; +}): string { + const url = new URL(input.authorizationEndpoint); + url.searchParams.set("response_type", "code"); + url.searchParams.set("client_id", input.clientId); + url.searchParams.set("redirect_uri", input.redirectUri); + url.searchParams.set("scope", (input.scopes ?? OIDC_SCOPES).join(" ")); + url.searchParams.set("state", input.state); + url.searchParams.set("nonce", input.nonce); + url.searchParams.set("code_challenge", input.challenge); + url.searchParams.set("code_challenge_method", "S256"); + return url.toString(); +} + +export function decodeIdTokenClaims(idToken: string): OidcIdTokenClaims { + const payload = idToken.split(".")[1]; + if (payload === undefined) { + throw new OidcError({ code: "invalid_id_token", message: "The ID token is not a JWT." }); + } + let claims: Record; + try { + claims = JSON.parse(base64UrlDecodeToString(payload)) as Record; + } catch (cause) { + throw new OidcError({ + code: "invalid_id_token", + message: "The ID token payload is not valid JSON.", + cause, + }); + } + const { sub, iss, exp } = claims; + if (typeof sub !== "string" || typeof iss !== "string" || typeof exp !== "number") { + throw new OidcError({ + code: "invalid_id_token", + message: "The ID token lacks sub, iss, or exp.", + }); + } + const optionalString = (key: string): { [key: string]: string } => + typeof claims[key] === "string" ? { [key]: claims[key] as string } : {}; + return { + sub, + iss, + exp, + ...optionalString("nonce"), + ...optionalString("email"), + ...optionalString("name"), + ...optionalString("preferred_username"), + }; +} + +/** A person-readable label for the signed-in account. */ +export const oidcAccountLabel = (claims: OidcIdTokenClaims): string => + claims.email ?? claims.preferred_username ?? claims.name ?? claims.sub; + +const ID_TOKEN_REFRESH_SKEW_MS = 60_000; + +export const isIdTokenFresh = ( + tokens: Pick, + now: number, +): boolean => tokens.expiresAtEpochMs - ID_TOKEN_REFRESH_SKEW_MS > now; + +async function requestTokens( + metadata: OidcMetadata, + params: Record, + fetchImpl: FetchLike, +): Promise<{ readonly idToken: string; readonly refreshToken: string | null }> { + const response = await fetchImpl(metadata.tokenEndpoint, { + method: "POST", + headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" }, + body: new URLSearchParams(params).toString(), + }); + const body = (await response.json().catch(() => ({}))) as Record; + if (!response.ok) { + throw new OidcError({ + code: "token_request_failed", + message: `The token request failed with ${response.status}.`, + status: response.status, + ...(typeof body.error === "string" ? { oauthError: body.error } : {}), + }); + } + if (typeof body.id_token !== "string") { + throw new OidcError({ + code: "missing_id_token", + message: "The token response has no id_token.", + }); + } + return { + idToken: body.id_token, + refreshToken: typeof body.refresh_token === "string" ? body.refresh_token : null, + }; +} + +const toTokens = (idToken: string, refreshToken: string | null): OidcTokens => { + const claims = decodeIdTokenClaims(idToken); + return { idToken, refreshToken, expiresAtEpochMs: claims.exp * 1000, claims }; +}; + +export async function exchangeAuthorizationCode(input: { + readonly metadata: OidcMetadata; + readonly clientId: string; + readonly redirectUri: string; + readonly code: string; + readonly verifier: string; + /** The nonce sent with the authorization request; the ID token must echo it. */ + readonly nonce: string; + readonly fetch?: FetchLike; +}): Promise { + const response = await requestTokens( + input.metadata, + { + grant_type: "authorization_code", + code: input.code, + redirect_uri: input.redirectUri, + client_id: input.clientId, + code_verifier: input.verifier, + }, + input.fetch ?? fetch, + ); + const tokens = toTokens(response.idToken, response.refreshToken); + if (tokens.claims.nonce !== input.nonce) { + throw new OidcError({ + code: "nonce_mismatch", + message: "The ID token nonce does not match the request.", + }); + } + return tokens; +} + +export async function refreshOidcTokens(input: { + readonly metadata: OidcMetadata; + readonly clientId: string; + readonly refreshToken: string; + readonly fetch?: FetchLike; +}): Promise { + const response = await requestTokens( + input.metadata, + { grant_type: "refresh_token", refresh_token: input.refreshToken, client_id: input.clientId }, + input.fetch ?? fetch, + ); + // Providers that do not rotate refresh tokens omit it; keep the one we have. + return toTokens(response.idToken, response.refreshToken ?? input.refreshToken); +} + +const isOidcError = Schema.is(OidcError); + +/** True when a refresh failure means the session is over rather than temporarily unavailable. */ +export const isPermanentRefreshFailure = (error: unknown): boolean => + isOidcError(error) && + error.code === "token_request_failed" && + (error.oauthError === "invalid_grant" || + (error.status !== undefined && + error.status >= 400 && + error.status < 500 && + error.status !== 408 && + error.status !== 429)); diff --git a/scripts/lib/public-config.test.ts b/scripts/lib/public-config.test.ts index 2770e3457310c..e80405d4605e9 100644 --- a/scripts/lib/public-config.test.ts +++ b/scripts/lib/public-config.test.ts @@ -19,7 +19,7 @@ describe("loadRepoEnv", () => { const repoRoot = makeTemporaryDirectory(); NodeFS.writeFileSync( NodePath.join(repoRoot, ".env"), - "T3CODE_CLERK_PUBLISHABLE_KEY=pk_root\nT3CODE_CLERK_JWT_TEMPLATE=template_root\nT3CODE_CLERK_CLI_OAUTH_CLIENT_ID=oauth_root\nT3CODE_RELAY_URL=https://root.example.test\n", + "T3CODE_CLERK_PUBLISHABLE_KEY=pk_root\nT3CODE_CLERK_JWT_TEMPLATE=template_root\nT3CODE_CLERK_CLI_OAUTH_CLIENT_ID=oauth_root\nT3CODE_OIDC_ISSUER=https://sso.root.example.test\nT3CODE_RELAY_URL=https://root.example.test\n", ); NodeFS.writeFileSync( NodePath.join(repoRoot, ".env.local"), @@ -35,6 +35,8 @@ describe("loadRepoEnv", () => { T3CODE_CLERK_PUBLISHABLE_KEY: "pk_ci", T3CODE_CLERK_JWT_TEMPLATE: "template_ci", T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: "oauth_ci", + T3CODE_OIDC_ISSUER: "https://sso.ci.example.test", + T3CODE_OIDC_CLIENT_ID: "oidc_ci", T3CODE_RELAY_URL: "https://ci.example.test", }, repoRoot, @@ -49,6 +51,8 @@ describe("loadRepoEnv", () => { EXPO_PUBLIC_CLERK_JWT_TEMPLATE: "template_ci", T3CODE_RELAY_URL: "https://ci.example.test", VITE_T3CODE_RELAY_URL: "https://ci.example.test", + VITE_OIDC_ISSUER: "https://sso.ci.example.test", + EXPO_PUBLIC_OIDC_CLIENT_ID: "oidc_ci", }); }); }); diff --git a/scripts/lib/public-config.ts b/scripts/lib/public-config.ts index 7fe8d05386cd1..0d2fb37904b65 100644 --- a/scripts/lib/public-config.ts +++ b/scripts/lib/public-config.ts @@ -8,6 +8,8 @@ export interface T3CodePublicConfig { readonly clerkPublishableKey: string | undefined; readonly clerkJwtTemplate: string | undefined; readonly clerkCliOAuthClientId: string | undefined; + readonly oidcIssuer: string | undefined; + readonly oidcClientId: string | undefined; readonly relayUrl: string | undefined; readonly mobileOtlpTracesUrl: string | undefined; readonly mobileOtlpTracesDataset: string | undefined; @@ -56,6 +58,20 @@ export function loadRepoEnv({ VITE_CLERK_CLI_OAUTH_CLIENT_ID: config.clerkCliOAuthClientId, } : {}), + ...(config.oidcIssuer + ? { + T3CODE_OIDC_ISSUER: config.oidcIssuer, + VITE_OIDC_ISSUER: config.oidcIssuer, + EXPO_PUBLIC_OIDC_ISSUER: config.oidcIssuer, + } + : {}), + ...(config.oidcClientId + ? { + T3CODE_OIDC_CLIENT_ID: config.oidcClientId, + VITE_OIDC_CLIENT_ID: config.oidcClientId, + EXPO_PUBLIC_OIDC_CLIENT_ID: config.oidcClientId, + } + : {}), ...(config.relayUrl ? { T3CODE_RELAY_URL: config.relayUrl, @@ -120,6 +136,18 @@ function resolvePublicConfig(...sources: readonly Environment[]): T3CodePublicCo "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID", "VITE_CLERK_CLI_OAUTH_CLIENT_ID", ), + oidcIssuer: firstNonEmpty( + sources, + "T3CODE_OIDC_ISSUER", + "VITE_OIDC_ISSUER", + "EXPO_PUBLIC_OIDC_ISSUER", + ), + oidcClientId: firstNonEmpty( + sources, + "T3CODE_OIDC_CLIENT_ID", + "VITE_OIDC_CLIENT_ID", + "EXPO_PUBLIC_OIDC_CLIENT_ID", + ), relayUrl: firstNonEmpty(sources, "T3CODE_RELAY_URL", "VITE_T3CODE_RELAY_URL"), mobileOtlpTracesUrl: firstNonEmpty( sources, From 1ec1f9646be56361034c87033d35b3998b31103b Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Sun, 8 Nov 2026 17:04:51 +0100 Subject: [PATCH 02/29] feat(connect): choose a self-hosted relay in settings Clients pick T3 Connect or a self-hosted relay at runtime. The relay describes its sign-in provider at /.well-known/t3-relay, so users only enter its address, and links skip managed tunnels on relays without them. --- apps/mobile/src/Stack.tsx | 6 + .../src/features/cloud/linkEnvironment.ts | 13 +- .../src/features/cloud/publicConfig.test.ts | 26 ++++ .../mobile/src/features/cloud/publicConfig.ts | 52 +++++++- .../SettingsConnectServiceRouteScreen.tsx | 119 +++++++++++++++++ .../features/settings/SettingsRouteScreen.tsx | 21 ++- .../components/settings-page-targets.ts | 1 + apps/web/src/cloud/publicConfig.ts | 48 ++++++- apps/web/src/cloud/useCloudLinkController.ts | 14 +- .../settings/ConnectionsSettings.tsx | 2 + .../settings/SavedEnvironmentRows.tsx | 6 +- .../settings/T3ConnectServiceSettings.tsx | 126 ++++++++++++++++++ .../src/components/settings/settingsSearch.ts | 6 + docs/user/remote-access.md | 32 +++++ infra/relay/selfhost/README.md | 19 ++- infra/relay/src/Config.ts | 2 + infra/relay/src/auth/SubjectTokens.test.ts | 2 +- infra/relay/src/composition.ts | 4 +- infra/relay/src/http/Api.ts | 10 ++ packages/client-runtime/package.json | 4 + .../src/relay/selfHostedRelay.test.ts | 62 +++++++++ .../src/relay/selfHostedRelay.ts | 72 ++++++++++ packages/contracts/src/relay.ts | 19 ++- 23 files changed, 642 insertions(+), 24 deletions(-) create mode 100644 apps/mobile/src/features/settings/SettingsConnectServiceRouteScreen.tsx create mode 100644 apps/web/src/components/settings/T3ConnectServiceSettings.tsx create mode 100644 packages/client-runtime/src/relay/selfHostedRelay.test.ts create mode 100644 packages/client-runtime/src/relay/selfHostedRelay.ts diff --git a/apps/mobile/src/Stack.tsx b/apps/mobile/src/Stack.tsx index b8ab00673db12..2baa5856b8547 100644 --- a/apps/mobile/src/Stack.tsx +++ b/apps/mobile/src/Stack.tsx @@ -126,6 +126,7 @@ import { UsageLimitAccountScreen } from "./features/usage/UsageLimitsPooled"; import { UsageRouteScreen } from "./features/usage/UsageRouteScreen"; import { SettingsAboutRouteScreen } from "./features/settings/SettingsAboutRouteScreen"; import { SettingsNotificationsRouteScreen } from "./features/settings/SettingsNotificationsRouteScreen"; +import { SettingsConnectServiceRouteScreen } from "./features/settings/SettingsConnectServiceRouteScreen"; import { SettingsRouteScreen } from "./features/settings/SettingsRouteScreen"; import { SettingsLabsRouteScreen } from "./features/settings/ThreadSettingsSections"; import { SettingsEnvironmentFilterProvider } from "./features/settings/settings-environment-filter"; @@ -217,6 +218,11 @@ const SETTINGS_SCREENS = { linking: "settings/notifications", options: settingsPage("Notifications"), }), + SettingsConnectService: createNativeStackScreen({ + screen: SettingsConnectServiceRouteScreen, + linking: "settings/connect-service", + options: settingsPage("T3 Connect service"), + }), SettingsThreads: createNativeStackScreen({ screen: SettingsThreadsRouteScreen, linking: "settings/thread-preferences", diff --git a/apps/mobile/src/features/cloud/linkEnvironment.ts b/apps/mobile/src/features/cloud/linkEnvironment.ts index f072f5c96d83d..1e16b626ca483 100644 --- a/apps/mobile/src/features/cloud/linkEnvironment.ts +++ b/apps/mobile/src/features/cloud/linkEnvironment.ts @@ -173,13 +173,18 @@ export const linkEnvironmentToCloudWithPreference = Effect.fn( Effect.mapError(cloudEnvironmentLinkError("Could not load the mobile device id.")), ); const liveActivitiesEnabled = input.liveActivitiesEnabled; + // Without managed tunnels, the relay brokers the address this device already reaches. + const managedTunnelsEnabled = resolveCloudPublicConfig().relay.managedTunnels; + const providerKind = managedTunnelsEnabled + ? MANAGED_ENDPOINT_PROVIDER_KIND + : ("manual" satisfies RelayManagedEndpointProviderKind); const challenge = yield* relayClient .createEnvironmentLinkChallenge({ clerkToken: input.clerkToken, payload: { notificationsEnabled: true, liveActivitiesEnabled, - managedTunnelsEnabled: true, + managedTunnelsEnabled, }, }) .pipe( @@ -197,7 +202,7 @@ export const linkEnvironmentToCloudWithPreference = Effect.fn( endpoint: { httpBaseUrl: input.connection.httpBaseUrl, wsBaseUrl: input.connection.wsBaseUrl, - providerKind: MANAGED_ENDPOINT_PROVIDER_KIND, + providerKind, }, origin: endpointOrigin(input.connection.httpBaseUrl), }, @@ -211,7 +216,7 @@ export const linkEnvironmentToCloudWithPreference = Effect.fn( proof, notificationsEnabled: true, liveActivitiesEnabled, - managedTunnelsEnabled: true, + managedTunnelsEnabled, }, }) .pipe( @@ -219,7 +224,7 @@ export const linkEnvironmentToCloudWithPreference = Effect.fn( ); yield* ensureLinkedEnvironmentMatches({ expectedEnvironmentId: input.connection.environmentId, - expectedProviderKind: MANAGED_ENDPOINT_PROVIDER_KIND, + expectedProviderKind: providerKind, link, }); diff --git a/apps/mobile/src/features/cloud/publicConfig.test.ts b/apps/mobile/src/features/cloud/publicConfig.test.ts index d4df6151df977..4fced19982583 100644 --- a/apps/mobile/src/features/cloud/publicConfig.test.ts +++ b/apps/mobile/src/features/cloud/publicConfig.test.ts @@ -2,6 +2,8 @@ import { describe, expect, it, vi } from "vite-plus/test"; import { cloudAuthProvider, resolveCloudPublicConfig } from "./publicConfig"; +vi.mock("expo-secure-store", () => ({ getItem: () => null })); + vi.mock("expo-constants", () => ({ default: { expoConfig: { @@ -25,6 +27,7 @@ describe("resolveCloudPublicConfig", () => { oidc: { issuer: null, clientId: null }, relay: { url: null, + managedTunnels: true, }, observability: { tracesUrl: null, @@ -48,4 +51,27 @@ describe("resolveCloudPublicConfig", () => { }); expect(cloudAuthProvider(insecure)).toBeNull(); }); + + it("lets a relay chosen in settings replace the build's T3 Connect", () => { + const configured = resolveCloudPublicConfig( + { + clerk: { publishableKey: "pk_test_example", jwtTemplate: "t3-relay" }, + relay: { url: "https://relay.t3.example" }, + observability: { + tracesUrl: "https://traces.t3.example", + tracesDataset: "relay", + tracesToken: "token", + }, + }, + { + relayUrl: "https://relay.example.test", + oidcIssuer: "https://sso.example.test", + oidcClientId: "t3-code", + managedTunnels: false, + }, + ); + expect(configured.relay).toEqual({ url: "https://relay.example.test", managedTunnels: false }); + expect(configured.observability.tracesUrl).toBeNull(); + expect(cloudAuthProvider(configured)).toBe("oidc"); + }); }); diff --git a/apps/mobile/src/features/cloud/publicConfig.ts b/apps/mobile/src/features/cloud/publicConfig.ts index e016f649bcdb2..111e5c985ebe2 100644 --- a/apps/mobile/src/features/cloud/publicConfig.ts +++ b/apps/mobile/src/features/cloud/publicConfig.ts @@ -1,4 +1,11 @@ +import { + encodeSelfHostedRelay, + parseStoredSelfHostedRelay, + SELF_HOSTED_RELAY_STORAGE_KEY, + type SelfHostedRelay, +} from "@t3tools/client-runtime/relay/self-hosted"; import Constants from "expo-constants"; +import * as SecureStore from "expo-secure-store"; import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth"; import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; import * as Schema from "effect/Schema"; @@ -25,6 +32,8 @@ export interface CloudPublicConfig { }; readonly relay: { readonly url: string | null; + /** False for a relay that cannot provision tunnels, so links use the environment's own address. */ + readonly managedTunnels: boolean; }; readonly observability: { readonly tracesUrl: string | null; @@ -60,25 +69,62 @@ function normalizeSecureUrl(value: unknown): string | null { } } -export function resolveCloudPublicConfig(extra: ExpoExtra = Constants.expoConfig?.extra) { +let selfHostedRelay: SelfHostedRelay | null | undefined; + +/** The relay the user chose in settings; it replaces the build's T3 Connect until cleared. */ +export function readSelfHostedRelay(): SelfHostedRelay | null { + if (selfHostedRelay !== undefined) return selfHostedRelay; + try { + selfHostedRelay = parseStoredSelfHostedRelay( + SecureStore.getItem(SELF_HOSTED_RELAY_STORAGE_KEY), + ); + } catch { + selfHostedRelay = null; + } + return selfHostedRelay; +} + +/** Sign-in is wired once at startup, so callers reload the app after a change. */ +export async function saveSelfHostedRelay(relay: SelfHostedRelay | null): Promise { + if (relay === null) await SecureStore.deleteItemAsync(SELF_HOSTED_RELAY_STORAGE_KEY); + else await SecureStore.setItemAsync(SELF_HOSTED_RELAY_STORAGE_KEY, encodeSelfHostedRelay(relay)); + selfHostedRelay = relay; +} + +export function resolveCloudPublicConfig( + extra: ExpoExtra = Constants.expoConfig?.extra, + selfHosted: SelfHostedRelay | null = readSelfHostedRelay(), +): CloudPublicConfig { + if (selfHosted !== null) { + return { + clerk: { publishableKey: null, jwtTemplate: null }, + oidc: { issuer: selfHosted.oidcIssuer, clientId: selfHosted.oidcClientId }, + relay: { url: selfHosted.relayUrl, managedTunnels: selfHosted.managedTunnels }, + // The build's relay telemetry belongs to its own T3 Connect, not to this relay. + observability: { tracesUrl: null, tracesDataset: null, tracesToken: null }, + }; + } + const oidcIssuer = trimNonEmpty(extra?.oidc?.issuer); return { clerk: { publishableKey: trimNonEmpty(extra?.clerk?.publishableKey), jwtTemplate: trimNonEmpty(extra?.clerk?.jwtTemplate), }, oidc: { - issuer: trimNonEmpty(extra?.oidc?.issuer), + issuer: oidcIssuer, clientId: trimNonEmpty(extra?.oidc?.clientId), }, relay: { url: normalizeSecureRelayUrl(trimNonEmpty(extra?.relay?.url) ?? ""), + // A build configured for OIDC targets a self-hosted relay, which has no managed tunnels. + managedTunnels: oidcIssuer === null, }, observability: { tracesUrl: normalizeSecureUrl(extra?.observability?.tracesUrl), tracesDataset: trimNonEmpty(extra?.observability?.tracesDataset), tracesToken: trimNonEmpty(extra?.observability?.tracesToken), }, - } satisfies CloudPublicConfig; + }; } /** An OpenID Connect provider takes the place of Clerk when the build configures one. */ diff --git a/apps/mobile/src/features/settings/SettingsConnectServiceRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsConnectServiceRouteScreen.tsx new file mode 100644 index 0000000000000..3a44d72376658 --- /dev/null +++ b/apps/mobile/src/features/settings/SettingsConnectServiceRouteScreen.tsx @@ -0,0 +1,119 @@ +import { + discoverSelfHostedRelay, + type SelfHostedRelay, +} from "@t3tools/client-runtime/relay/self-hosted"; +import * as Updates from "expo-updates"; +import { useState } from "react"; +import { ScrollView, TextInput, View } from "react-native"; + +import { AppText as Text } from "../../components/ui/AppText"; +import { ControlPill } from "../../components/ui/ControlPill"; +import { ListChoiceRow } from "../../components/ui/ListChoiceRow"; +import * as Dialog from "../../components/ui/Dialog"; +import { readSelfHostedRelay, saveSelfHostedRelay } from "../cloud/publicConfig"; +import { SettingsScreen } from "./components/SettingsScreen"; +import { SettingsSection } from "./components/SettingsSection"; + +// Sign-in and the relay client are wired once at startup, so a new choice reloads the app. +async function switchTo(relay: SelfHostedRelay | null) { + await saveSelfHostedRelay(relay); + await Updates.reloadAsync(); +} + +/** Chooses between the build's T3 Connect and a relay the user runs themselves. */ +export function SettingsConnectServiceRouteScreen() { + const current = readSelfHostedRelay(); + const [selfHosted, setSelfHosted] = useState(current !== null); + const [address, setAddress] = useState(current?.relayUrl ?? ""); + const [error, setError] = useState(null); + const [busy, setBusy] = useState(false); + + const run = async (next: () => Promise) => { + setError(null); + setBusy(true); + try { + await switchTo(await next()); + } catch (cause) { + setError(cause instanceof Error ? cause.message : "Could not reach the relay."); + setBusy(false); + } + }; + + return ( + + + + { + setSelfHosted(false); + setError(null); + }} + /> + setSelfHosted(true)} + /> + + {selfHosted ? ( + + + + {current + ? `Using ${current.relayUrl}. Sign-in details are read from the relay.` + : "Sign-in details are read from the relay. Signing in starts over after the switch."} + + {error ? ( + + {error} + + ) : null} + + void run(() => discoverSelfHostedRelay(address))} + /> + + + ) : current ? ( + + + Dialog.confirm({ + title: "Switch back to T3 Connect?", + message: + "Sign-in starts over, and environments linked to your relay stay there until you link them again.", + confirmText: "Switch", + onConfirm: () => void run(async () => null), + }) + } + /> + + ) : null} + + + ); +} diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx index 6bd76d5b7efaf..3a4f89723261d 100644 --- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx @@ -22,7 +22,7 @@ import { deriveProjectGroupLabel } from "@t3tools/client-runtime/state/project-g import { useSafeAreaInsets } from "react-native-safe-area-context"; import { useCloudAuth } from "../cloud/cloudAuth"; -import { hasCloudPublicConfig } from "../cloud/publicConfig"; +import { hasCloudPublicConfig, readSelfHostedRelay } from "../cloud/publicConfig"; import { useAdaptiveWorkspaceLayout } from "../layout/adaptive-workspace-context"; import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry"; import { SettingsRow } from "./components/SettingsRow"; @@ -129,6 +129,7 @@ function ConfiguredSettingsRouteScreen() { onPress: () => navigation.navigate("SettingsAuth"), }, environmentsRow(Object.keys(savedConnectionsById).length), + connectServiceRow(), { id: "SettingsNotifications", icon: "bell.badge", @@ -142,7 +143,23 @@ function ConfiguredSettingsRouteScreen() { function LocalSettingsRouteScreen() { const { savedConnectionsById } = useSavedRemoteConnections(); - return ; + return ( + + ); +} + +function connectServiceRow(): SettingsListRow { + const relay = readSelfHostedRelay(); + return { + id: "SettingsConnectService", + icon: "server.rack", + label: "T3 Connect service", + value: relay ? new URL(relay.relayUrl).host : "T3 Connect", + valuePosition: "trailing", + target: "SettingsConnectService", + }; } function environmentsRow(count: number): SettingsListRow { diff --git a/apps/mobile/src/features/settings/components/settings-page-targets.ts b/apps/mobile/src/features/settings/components/settings-page-targets.ts index eadd9c46fff7b..1ad8f6a399ab6 100644 --- a/apps/mobile/src/features/settings/components/settings-page-targets.ts +++ b/apps/mobile/src/features/settings/components/settings-page-targets.ts @@ -2,6 +2,7 @@ export const SETTINGS_PAGE_TARGETS = [ "SettingsEnvironments", "SettingsNotifications", + "SettingsConnectService", "SettingsThreads", "SettingsAbout", "SettingsArchive", diff --git a/apps/web/src/cloud/publicConfig.ts b/apps/web/src/cloud/publicConfig.ts index 0cfa915638afb..75fe44cb6c1c8 100644 --- a/apps/web/src/cloud/publicConfig.ts +++ b/apps/web/src/cloud/publicConfig.ts @@ -2,6 +2,12 @@ import { clerkFrontendApiHostnameFromPublishableKey, relayClerkTokenOptions, } from "@t3tools/shared/relayAuth"; +import { + encodeSelfHostedRelay, + parseStoredSelfHostedRelay, + SELF_HOSTED_RELAY_STORAGE_KEY, + type SelfHostedRelay, +} from "@t3tools/client-runtime/relay/self-hosted"; import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; import * as Schema from "effect/Schema"; @@ -22,6 +28,8 @@ export interface CloudPublicConfig { readonly oidcIssuer: string | null; readonly oidcClientId: string | null; readonly relayUrl: string | null; + /** False for a relay that cannot provision tunnels, so links use the environment's own address. */ + readonly managedTunnels: boolean; readonly relayTracing: { readonly tracesUrl: string | null; readonly tracesDataset: string | null; @@ -42,17 +50,55 @@ function normalizeSecureUrl(value: string): string | null { } } +let selfHostedRelay: SelfHostedRelay | null | undefined; + +/** The relay the user chose in settings; it replaces the build's T3 Connect until cleared. */ +export function readSelfHostedRelay(): SelfHostedRelay | null { + if (selfHostedRelay !== undefined) return selfHostedRelay; + try { + selfHostedRelay = parseStoredSelfHostedRelay( + window.localStorage.getItem(SELF_HOSTED_RELAY_STORAGE_KEY), + ); + } catch { + selfHostedRelay = null; + } + return selfHostedRelay; +} + +/** Sign-in is wired once at startup, so callers reload the page after a change. */ +export function saveSelfHostedRelay(relay: SelfHostedRelay | null): void { + if (relay === null) window.localStorage.removeItem(SELF_HOSTED_RELAY_STORAGE_KEY); + else window.localStorage.setItem(SELF_HOSTED_RELAY_STORAGE_KEY, encodeSelfHostedRelay(relay)); + selfHostedRelay = relay; +} + export function resolveCloudPublicConfig(): CloudPublicConfig { + const selfHosted = typeof window === "undefined" ? null : readSelfHostedRelay(); + if (selfHosted !== null) { + return { + clerkPublishableKey: null, + clerkJwtTemplate: null, + oidcIssuer: selfHosted.oidcIssuer, + oidcClientId: selfHosted.oidcClientId, + relayUrl: selfHosted.relayUrl, + managedTunnels: selfHosted.managedTunnels, + // The build's relay telemetry belongs to its own T3 Connect, not to this relay. + relayTracing: { tracesUrl: null, tracesDataset: null, tracesToken: null }, + }; + } + const oidcIssuer = trimNonEmpty(import.meta.env.VITE_OIDC_ISSUER as string | undefined); return { clerkPublishableKey: trimNonEmpty( import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined, ), clerkJwtTemplate: trimNonEmpty(import.meta.env.VITE_CLERK_JWT_TEMPLATE as string | undefined), - oidcIssuer: trimNonEmpty(import.meta.env.VITE_OIDC_ISSUER as string | undefined), + oidcIssuer, oidcClientId: trimNonEmpty(import.meta.env.VITE_OIDC_CLIENT_ID as string | undefined), relayUrl: normalizeSecureRelayUrl( (import.meta.env.VITE_T3CODE_RELAY_URL as string | undefined) ?? "", ), + // A build configured for OIDC targets a self-hosted relay, which has no managed tunnels. + managedTunnels: oidcIssuer === null, relayTracing: { tracesUrl: normalizeSecureUrl( (import.meta.env.VITE_RELAY_OTLP_TRACES_URL as string | undefined) ?? "", diff --git a/apps/web/src/cloud/useCloudLinkController.ts b/apps/web/src/cloud/useCloudLinkController.ts index cf011954000b6..8a29f360cc607 100644 --- a/apps/web/src/cloud/useCloudLinkController.ts +++ b/apps/web/src/cloud/useCloudLinkController.ts @@ -18,6 +18,7 @@ import { readEnvironmentScope } from "../state/session"; import { useAtomCommand } from "../state/use-atom-command"; import { useCloudAuth } from "./cloudAuth"; import { isCloudEnvironmentLinkError } from "./linkEnvironment"; +import { resolveCloudPublicConfig } from "./publicConfig"; import { refreshManagedTunnelUsage } from "./managedRelayState"; import { linkEnvironment as linkEnvironmentAtom, @@ -95,7 +96,13 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) { const linkData = cloudLinkState.data; const linkRevoked = linkData?.linkRevoked ?? false; - const managedTunnelActive = linkData ? isManagedTunnelActive(linkData, relayProviderKind) : false; + // Without managed tunnels, a link is how other devices find the environment's own address. + const { managedTunnels } = resolveCloudPublicConfig(); + const managedTunnelActive = linkData + ? managedTunnels + ? isManagedTunnelActive(linkData, relayProviderKind) + : linkData.linked && linkData.linkRevoked !== true + : false; const managedTunnelOutOfSync = linkData !== null && !managedTunnelActive && isManagedTunnelActive(linkData, undefined); const publishAgentActivity = cloudLinkState.data?.publishAgentActivity ?? false; @@ -174,15 +181,16 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) { return false; } const currentManagedTunnel = isManagedTunnelActive(currentLinkState, relayProviderKind); + const desiredManagedTunnel = desired.managedTunnel && managedTunnels; if ( !currentLinkState.linked || currentLinkState.linkRevoked === true || - currentManagedTunnel !== desired.managedTunnel + currentManagedTunnel !== desiredManagedTunnel ) { const linkResult = await linkEnvironment({ target, clerkToken, - mode: desired.managedTunnel ? "managed" : "publish_only", + mode: desiredManagedTunnel ? "managed" : "publish_only", }); // Any link attempt can change the account's tunnel count. refreshManagedTunnelUsage(); diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index da46823292cad..1bbfbdce3e228 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -42,6 +42,7 @@ import { LocalEnvironmentSetting } from "./LocalEnvironmentSetting"; import { searchableSetting } from "./settingsSearch"; import { EnvironmentIconMenu } from "./EnvironmentIconPicker"; import { FoldedSettingsSection } from "./FoldedSettingsSection"; +import { T3ConnectServiceSettings } from "./T3ConnectServiceSettings"; import { LinkedEnvironmentsSettings } from "./LinkedEnvironmentsSettings"; import { usePeerLinkHosts } from "~/state/peerLinks"; import { LoadBalancingSettings } from "./LoadBalancingSettings"; @@ -1591,6 +1592,7 @@ function ConnectionsSettingsContent({ ) : null} + - {signedOut || connectDenial !== undefined ? null : ( + {signedOut || + connectDenial !== undefined || + !resolveCloudPublicConfig().managedTunnels ? null : ( )} diff --git a/apps/web/src/components/settings/T3ConnectServiceSettings.tsx b/apps/web/src/components/settings/T3ConnectServiceSettings.tsx new file mode 100644 index 0000000000000..c681cf4d5ed86 --- /dev/null +++ b/apps/web/src/components/settings/T3ConnectServiceSettings.tsx @@ -0,0 +1,126 @@ +import { + discoverSelfHostedRelay, + type SelfHostedRelay, +} from "@t3tools/client-runtime/relay/self-hosted"; +import { useState } from "react"; + +import { readSelfHostedRelay, saveSelfHostedRelay } from "~/cloud/publicConfig"; +import { Button } from "../ui/button"; +import { Input } from "../ui/input"; +import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; +import { SettingsRow, SettingsSection } from "./settingsLayout"; +import { searchableSetting } from "./settingsSearch"; + +type Service = "default" | "self-hosted"; + +const SERVICE_LABELS: Record = { + default: "T3 Connect", + "self-hosted": "Self-hosted relay", +}; + +const hostOf = (url: string) => { + try { + return new URL(url).host; + } catch { + return url; + } +}; + +// Sign-in and the relay client are wired once at startup, so a new choice reloads the app. +const switchTo = (relay: SelfHostedRelay | null) => { + saveSelfHostedRelay(relay); + window.location.reload(); +}; + +/** Chooses between the build's T3 Connect and a relay the user runs themselves. */ +export function T3ConnectServiceSettings() { + const current = readSelfHostedRelay(); + const [service, setService] = useState(current ? "self-hosted" : "default"); + const [address, setAddress] = useState(current?.relayUrl ?? ""); + const [error, setError] = useState(null); + const [isChecking, setIsChecking] = useState(false); + + const connect = async () => { + setError(null); + setIsChecking(true); + try { + switchTo(await discoverSelfHostedRelay(address)); + } catch (cause) { + setError(cause instanceof Error ? cause.message : "Could not reach the relay."); + setIsChecking(false); + } + }; + + return ( + + { + if (value === null) return; + setService(value); + setError(null); + }} + > + + {SERVICE_LABELS[service]} + + + {(Object.keys(SERVICE_LABELS) as Array).map((value) => ( + + {SERVICE_LABELS[value]} + + ))} + + + } + /> + {service === "self-hosted" ? ( + { + event.preventDefault(); + void connect(); + }} + > + setAddress(event.target.value)} + /> + + + } + /> + ) : current ? ( + switchTo(null)}> + Use T3 Connect + + } + /> + ) : null} + + ); +} diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.ts index f7492035874da..cf1b16413afcc 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -1283,6 +1283,12 @@ export const SETTINGS_SEARCH_ITEMS = [ to: "/settings/connections", searchTerms: ["add pair backend host code ssh config agent tunnel saved t3 connect"], }, + { + id: "t3-connect-service", + title: "T3 Connect service", + to: "/settings/connections", + searchTerms: ["self-hosted relay own server oidc openid sso sign-in provider"], + }, { id: "load-balancing", title: "Load balancing", diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index 75910d9405c39..c35e1d2a8be74 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -64,6 +64,38 @@ opens and approve. The new limit applies the next time you add a machine. If you cancel or downgrade T3 Chat, machines you already linked stay linked, but you cannot add more until you are under the limit again. +### Use your own relay + +T3 Connect can run through a relay you host, with sign-in from your own OpenID +Connect provider instead of a T3 account. Set up the relay first with +[the self-hosting guide](../../infra/relay/selfhost/README.md). + +On each web, desktop, and mobile app, open **Settings → Connections → T3 Connect +service**, choose **Self-hosted relay**, and enter the relay's address. The app +reads the sign-in provider from the relay, reloads, and asks you to sign in. +Choose **T3 Connect** there to switch back. Environments you linked through your +relay stay linked to it until you turn T3 Connect off and on again. + +A self-hosted relay has no managed tunnels. Linking an environment still works, +but your other devices reach it only at an HTTPS address you expose yourself, +such as a reverse proxy or [Tailscale HTTPS](#tailscale-https). Start the host +with that address in `T3CODE_PUBLIC_URL`. Tailscale and LAN addresses also need +`RELAY_MANUAL_ENDPOINTS=private` on the relay. + +For a command-line host, set the relay before `t3 connect`: + +```bash +export T3CODE_RELAY_URL=https://relay.example.com +export T3CODE_OIDC_ISSUER=https://sso.example.com +export T3CODE_OIDC_CLIENT_ID=t3-code +t3 connect +``` + +To sign in from app.t3.codes, your identity provider must also allow +`https://app.t3.codes/oidc/callback` as a redirect URI. Push notifications and +Live Activities need the mobile app publisher's credentials, so a self-hosted +relay does not send them. + ## Pair over a LAN or private network Use direct pairing when the other device can reach the host's network address. diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md index 56ce3c8c0a5aa..845dd4ad315f3 100644 --- a/infra/relay/selfhost/README.md +++ b/infra/relay/selfhost/README.md @@ -73,9 +73,17 @@ if a migration fails. ## Point the apps at this relay -The apps read the relay and identity provider at build time. Set these in the repository's -`.env` before building web, desktop or mobile (`scripts/lib/public-config.ts` maps them to each -app's prefix): +In each app, open **Settings → Connections → T3 Connect service**, choose **Self-hosted relay**, +and enter `https://`. The app reads the identity provider from the relay's +`/.well-known/t3-relay` (`OIDC_ISSUER`, and `OIDC_AUDIENCE` as the client id), so nothing else +needs typing. Check what the apps will see with: + +```sh +curl https:///.well-known/t3-relay +# {"oidc":{"issuer":"https://sso.example.com/realms/t3","clientId":"t3-code"},"managedTunnels":false} +``` + +The `t3` server and CLI read the relay from the environment instead: ```sh T3CODE_RELAY_URL=https://relay.example.com @@ -83,8 +91,9 @@ T3CODE_OIDC_ISSUER=https://sso.example.com/realms/t3 T3CODE_OIDC_CLIENT_ID=t3-code ``` -A configured OpenID Connect provider replaces Clerk in that build. The `t3` server and CLI also -read the same three variables at runtime, so an installed server can switch without a rebuild. +A fork can also make the relay its build's default by setting the same three variables in the +repository's `.env` before building web, desktop or mobile (`scripts/lib/public-config.ts` maps +them to each app's prefix). A configured OpenID Connect provider replaces Clerk in that build. ## Reach an environment without a tunnel diff --git a/infra/relay/src/Config.ts b/infra/relay/src/Config.ts index 1939792682e07..a22042ac29b15 100644 --- a/infra/relay/src/Config.ts +++ b/infra/relay/src/Config.ts @@ -64,6 +64,8 @@ export type ManualEndpointsMode = typeof ManualEndpointsMode.Type; export interface OidcSettings { // Must equal the issuer's discovery `issuer` exactly, so a path prefix or trailing slash matters. readonly issuer: string; + /** The apps' OAuth client id, which is also the ID token audience the relay accepts. */ + readonly clientId: string; readonly verify: ( token: string, ) => Effect.Effect; diff --git a/infra/relay/src/auth/SubjectTokens.test.ts b/infra/relay/src/auth/SubjectTokens.test.ts index 23798753a340d..dc0525a5f3df4 100644 --- a/infra/relay/src/auth/SubjectTokens.test.ts +++ b/infra/relay/src/auth/SubjectTokens.test.ts @@ -69,7 +69,7 @@ const settingsWith = (verify: RelayConfiguration.OidcSettings["verify"]) => clerkSecretKey: Redacted.make("clerk-secret-key"), clerkPublishableKey: "pk_test", clerkJwtAudience: CLERK_AUDIENCE, - oidc: { issuer: ISSUER, verify }, + oidc: { issuer: ISSUER, clientId: "t3-code", verify }, }) as unknown as RelayConfiguration.RelayConfiguration["Service"]; const requestUrl = (input: RequestInfo | URL) => diff --git a/infra/relay/src/composition.ts b/infra/relay/src/composition.ts index bf9eb47ba701a..561f8f4b716d0 100644 --- a/infra/relay/src/composition.ts +++ b/infra/relay/src/composition.ts @@ -177,9 +177,9 @@ export const composeRelay = Effect.fn("relay.composition.compose")(function* ...(clerkPublishableKey === undefined ? {} : { clerkPublishableKey }), ...(clerkJwtAudience === undefined ? {} : { clerkJwtAudience }), manualEndpoints, - ...(oidcIssuer === undefined || oidcVerify === undefined + ...(oidcIssuer === undefined || oidcAudience === undefined || oidcVerify === undefined ? {} - : { oidc: { issuer: oidcIssuer, verify: oidcVerify } }), + : { oidc: { issuer: oidcIssuer, clientId: oidcAudience, verify: oidcVerify } }), cloudMintPrivateKey: yield* cloudMintPrivateKey, cloudMintPublicKey: yield* cloudMintPublicKey, managedEndpointBaseDomain: host.managedEndpointBaseDomain, diff --git a/infra/relay/src/http/Api.ts b/infra/relay/src/http/Api.ts index 3dba95c90b902..9ce5989098c6a 100644 --- a/infra/relay/src/http/Api.ts +++ b/infra/relay/src/http/Api.ts @@ -454,6 +454,16 @@ export const layerMetadataApi = HttpApiBuilder.group( dpop_bound_access_tokens_required: true, dpop_signing_alg_values_supported: ["ES256"], }), + ) + .handle("service", () => + Effect.succeed({ + oidc: settings.oidc + ? { issuer: settings.oidc.issuer, clientId: settings.oidc.clientId } + : null, + managedTunnels: Boolean( + settings.managedEndpointBaseDomain && settings.managedEndpointNamespace, + ), + }), ); }), ); diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 0e4b701aed3aa..2b821b8b7a1a6 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -7,6 +7,10 @@ "types": "./src/assemblyHighlighting.ts", "default": "./src/assemblyHighlighting.ts" }, + "./relay/self-hosted": { + "types": "./src/relay/selfHostedRelay.ts", + "default": "./src/relay/selfHostedRelay.ts" + }, "./zip/bounded-zip": { "types": "./src/zip/boundedZip.ts", "default": "./src/zip/boundedZip.ts" diff --git a/packages/client-runtime/src/relay/selfHostedRelay.test.ts b/packages/client-runtime/src/relay/selfHostedRelay.test.ts new file mode 100644 index 0000000000000..0146e42c91dbb --- /dev/null +++ b/packages/client-runtime/src/relay/selfHostedRelay.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { + discoverSelfHostedRelay, + encodeSelfHostedRelay, + parseStoredSelfHostedRelay, +} from "./selfHostedRelay.ts"; + +const OIDC = { issuer: "https://sso.example.com", clientId: "t3-code" }; + +const replying = (reply: () => Response) => { + const requests: Array = []; + const fetchImpl = (async (input: RequestInfo | URL) => { + requests.push(String(input)); + return reply(); + }) as typeof fetch; + return { requests, fetchImpl }; +}; + +describe("discoverSelfHostedRelay", () => { + it("reads the sign-in provider from the relay, accepting a bare host name", async () => { + const { requests, fetchImpl } = replying(() => + Response.json({ oidc: OIDC, managedTunnels: false }), + ); + await expect(discoverSelfHostedRelay(" relay.example.com/ ", fetchImpl)).resolves.toEqual({ + relayUrl: "https://relay.example.com", + oidcIssuer: OIDC.issuer, + oidcClientId: OIDC.clientId, + managedTunnels: false, + }); + expect(requests).toEqual(["https://relay.example.com/.well-known/t3-relay"]); + }); + + it.each([ + ["http://relay.example.com", () => Response.json({ oidc: OIDC, managedTunnels: false })], + ["relay.example.com", () => new Response("not found", { status: 404 })], + ["relay.example.com", () => Response.json({ oidc: { issuer: OIDC.issuer } })], + ["relay.example.com", () => Response.json({ oidc: null, managedTunnels: true })], + ])("rejects %s when the relay cannot be used", async (address, reply) => { + const { fetchImpl } = replying(reply); + await expect(discoverSelfHostedRelay(address, fetchImpl)).rejects.toThrow(); + }); +}); + +describe("parseStoredSelfHostedRelay", () => { + it("round-trips a stored relay and ignores anything else", () => { + const relay = { + relayUrl: "https://relay.example.com", + oidcIssuer: OIDC.issuer, + oidcClientId: OIDC.clientId, + managedTunnels: false, + }; + expect(parseStoredSelfHostedRelay(encodeSelfHostedRelay(relay))).toEqual(relay); + expect(parseStoredSelfHostedRelay(null)).toBeNull(); + expect(parseStoredSelfHostedRelay("{")).toBeNull(); + expect( + parseStoredSelfHostedRelay( + encodeSelfHostedRelay({ ...relay, relayUrl: "http://relay.example.com" }), + ), + ).toBeNull(); + }); +}); diff --git a/packages/client-runtime/src/relay/selfHostedRelay.ts b/packages/client-runtime/src/relay/selfHostedRelay.ts new file mode 100644 index 0000000000000..8f8534772f96f --- /dev/null +++ b/packages/client-runtime/src/relay/selfHostedRelay.ts @@ -0,0 +1,72 @@ +import { RelayServiceMetadata } from "@t3tools/contracts/relay"; +import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; + +/** Where each app keeps the relay a user chose instead of the build's default T3 Connect. */ +export const SELF_HOSTED_RELAY_STORAGE_KEY = "t3-connect-self-hosted-relay"; + +export const SelfHostedRelay = Schema.Struct({ + relayUrl: Schema.String, + oidcIssuer: Schema.String, + oidcClientId: Schema.String, + managedTunnels: Schema.Boolean, +}); +export type SelfHostedRelay = typeof SelfHostedRelay.Type; + +const decodeStoredRelay = Schema.decodeUnknownOption(Schema.fromJsonString(SelfHostedRelay)); +const decodeServiceMetadata = Schema.decodeUnknownOption(RelayServiceMetadata); + +/** Null for anything but a relay this module stored, so a corrupt entry falls back to the default. */ +export function parseStoredSelfHostedRelay(value: string | null): SelfHostedRelay | null { + if (value === null) return null; + const relay = Option.getOrNull(decodeStoredRelay(value)); + return relay !== null && normalizeSecureRelayUrl(relay.relayUrl) === relay.relayUrl + ? relay + : null; +} + +export const encodeSelfHostedRelay = (relay: SelfHostedRelay): string => JSON.stringify(relay); + +class SelfHostedRelayError extends Error { + override readonly name = "SelfHostedRelayError"; +} + +/** Asks the relay at a user-entered address how to sign in, so the user only types the address. */ +export async function discoverSelfHostedRelay( + address: string, + fetchImpl: typeof fetch = fetch, +): Promise { + const trimmed = address.trim(); + const relayUrl = normalizeSecureRelayUrl( + /^[a-z][a-z0-9+.-]*:\/\//iu.test(trimmed) ? trimmed : `https://${trimmed}`, + ); + if (relayUrl === null) { + throw new SelfHostedRelayError( + "Enter the relay's HTTPS address, for example https://relay.example.com.", + ); + } + const response = await fetchImpl(`${relayUrl}/.well-known/t3-relay`, { + headers: { accept: "application/json" }, + }).catch(() => null); + if (response === null) throw new SelfHostedRelayError(`Could not reach ${relayUrl}.`); + const metadata = response.ok + ? Option.getOrNull(decodeServiceMetadata(await response.json().catch(() => null))) + : null; + if (metadata === null) { + throw new SelfHostedRelayError( + `${relayUrl} did not answer as a T3 relay. Check the address, or update the relay.`, + ); + } + if (metadata.oidc === null) { + throw new SelfHostedRelayError( + `${relayUrl} signs in with Clerk. Configure OIDC on the relay to use it from here.`, + ); + } + return { + relayUrl, + oidcIssuer: metadata.oidc.issuer, + oidcClientId: metadata.oidc.clientId, + managedTunnels: metadata.managedTunnels, + }; +} diff --git a/packages/contracts/src/relay.ts b/packages/contracts/src/relay.ts index d7c8d9212b8f9..5de2322ea6bf6 100644 --- a/packages/contracts/src/relay.ts +++ b/packages/contracts/src/relay.ts @@ -822,6 +822,20 @@ export const RelayProtectedResourceMetadata = Schema.Struct({ dpop_signing_alg_values_supported: Schema.Array(Schema.Literal("ES256")), }); +/** What a client needs to use a relay given only its URL. */ +export const RelayServiceMetadata = Schema.Struct({ + oidc: Schema.NullOr( + Schema.Struct({ + issuer: TrimmedNonEmptyString, + clientId: TrimmedNonEmptyString, + }), + ).annotate({ description: "The OpenID Connect provider clients sign in with; null for Clerk." }), + managedTunnels: Schema.Boolean.annotate({ + description: "Whether the relay can provision managed tunnels for linked environments.", + }), +}); +export type RelayServiceMetadata = typeof RelayServiceMetadata.Type; + export const RelayEnvironmentUnlinkParams = Schema.Struct({ environmentId: EnvironmentId, }); @@ -1021,8 +1035,11 @@ const RelayMetadataGroup = HttpApiGroup.make("metadata") HttpApiEndpoint.get("protectedResource", "/.well-known/oauth-protected-resource", { success: RelayProtectedResourceMetadata, }).annotate(OpenApi.Summary, "Read OAuth protected-resource metadata"), + HttpApiEndpoint.get("service", "/.well-known/t3-relay", { + success: RelayServiceMetadata, + }).annotate(OpenApi.Summary, "Read how clients sign in to this relay"), ) - .annotate(OpenApi.Description, "OAuth and DPoP discovery metadata."); + .annotate(OpenApi.Description, "OAuth, DPoP and relay discovery metadata."); export const RelayRegisterDeviceEndpoint = HttpApiEndpoint.post( "registerDevice", From c31844114efde220cacf150c1ec6b11b9dc5692b Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 09:26:47 +0100 Subject: [PATCH 03/29] fix(relay): self-hosted relay accepts DPoP proofs behind a TLS proxy The Node relay saw requests as http:// behind the proxy, so every DPoP proof signed for https:// failed with url_mismatch. Requests now take their scheme and host from RELAY_PUBLIC_ORIGIN. --- infra/relay/src/host/node.test.ts | 24 +++++++++++++++++++++++- infra/relay/src/host/node.ts | 15 +++++++++++++++ infra/relay/src/node.ts | 6 ++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/infra/relay/src/host/node.test.ts b/infra/relay/src/host/node.test.ts index 27d8329b83eb9..4c29906ca4b47 100644 --- a/infra/relay/src/host/node.test.ts +++ b/infra/relay/src/host/node.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as HttpServerRequest from "effect/http/HttpServerRequest"; import * as Layer from "effect/Layer"; import * as Queue from "effect/Queue"; import * as Redacted from "effect/Redacted"; @@ -9,7 +10,7 @@ import * as SecureRandom from "effect/crypto/SecureRandom"; import * as TestClock from "effect/testing/TestClock"; import type * as RelayDb from "../db.ts"; -import { fixedWindowLimit, makeNodeHost } from "./node.ts"; +import { fixedWindowLimit, makeNodeHost, withPublicOrigin } from "./node.ts"; import * as Ports from "./ports.ts"; const layer = Layer.mergeAll(SecureRandom.layerWebCrypto, FetchHttpClient.layer); @@ -23,6 +24,27 @@ const makeHost = makeNodeHost({ mobileTelemetry: { token: undefined, traces: "", logs: "", metrics: "" }, }); +describe("withPublicOrigin", () => { + it.effect("gives handlers the public URL a proxy terminated TLS for", () => + Effect.gen(function* () { + const proxied = HttpServerRequest.fromWeb( + new Request("http://relay:8080/v1/client/dpop-token?x=1", { method: "POST" }), + ); + const url = yield* Effect.map( + HttpServerRequest.HttpServerRequest, + HttpServerRequest.toURL, + ).pipe( + withPublicOrigin("https://relay.example.com"), + Effect.provideService(HttpServerRequest.HttpServerRequest, proxied), + ); + + expect(url._tag === "Some" && url.value.href).toBe( + "https://relay.example.com/v1/client/dpop-token?x=1", + ); + }), + ); +}); + describe("fixedWindowLimit", () => { it.effect("allows the limit per key within a window and resets after it", () => Effect.gen(function* () { diff --git a/infra/relay/src/host/node.ts b/infra/relay/src/host/node.ts index 472debb2b234e..48c2ca47c70ce 100644 --- a/infra/relay/src/host/node.ts +++ b/infra/relay/src/host/node.ts @@ -2,6 +2,8 @@ import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Headers from "effect/http/Headers"; +import * as HttpServerRequest from "effect/http/HttpServerRequest"; import * as Layer from "effect/Layer"; import * as Queue from "effect/Queue"; import * as Redacted from "effect/Redacted"; @@ -16,6 +18,19 @@ import * as MobileTelemetry from "../mobileTelemetry/MobileTelemetry.ts"; import * as Ports from "./ports.ts"; import type { RelayHost } from "./RelayHost.ts"; +/** + * TLS ends at the proxy, so the Node request is plain HTTP. DPoP proofs sign the public URL, + * so every request is rewritten to the configured origin instead of trusting forwarded headers. + */ +export const withPublicOrigin = (relayPublicOrigin: string) => { + const origin = new URL(relayPublicOrigin); + const protocol = origin.protocol === "https:" ? "https" : "http"; + return (handler: Effect.Effect) => + Effect.updateService(handler, HttpServerRequest.HttpServerRequest, (request) => + request.modify({ protocol, headers: Headers.set(request.headers, "host", origin.host) }), + ); +}; + export interface NodeHostOptions { readonly relayPublicOrigin: string; readonly stage: string; diff --git a/infra/relay/src/node.ts b/infra/relay/src/node.ts index 3446afa3bee7a..441932b3c46b9 100644 --- a/infra/relay/src/node.ts +++ b/infra/relay/src/node.ts @@ -20,7 +20,7 @@ import * as Redacted from "effect/Redacted"; import * as SecureRandom from "effect/crypto/SecureRandom"; import { composeRelay } from "./composition.ts"; -import { makeNodeHost } from "./host/node.ts"; +import { makeNodeHost, withPublicOrigin } from "./host/node.ts"; const program = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -74,7 +74,9 @@ const program = Effect.gen(function* () { // Built once: every request reuses the same services, as the Worker's per-isolate layers do. const context = yield* Layer.build(relay.runtime); const handler = yield* relay.fetch.pipe(Effect.provideContext(context)); - yield* HttpServer.serveEffect(handler.pipe(Effect.provideContext(context))); + yield* HttpServer.serveEffect( + handler.pipe(withPublicOrigin(relayPublicOrigin), Effect.provideContext(context)), + ); yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin }); // Serving is scoped to this program; keep it alive until the process is stopped. return yield* Effect.never; From a665036d691abdc01c41b78c25d44ee6543b4fee Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 11:12:32 +0100 Subject: [PATCH 04/29] feat(relay): carry environments over a relay channel without a tunnel A self-hosted relay had no way to reach an environment without a public address. With RELAY_CHANNEL_HOST set, a publish-only link with no contactable address gets a t3_relay endpoint on the relay's own wildcard host. The environment dials one WebSocket to the relay, which forwards HTTP and WebSocket traffic for that host over it, so clients connect as they would through a tunnel. --- apps/server/src/cloud/CloudLink.ts | 34 +- apps/server/src/cloud/ConnectReachability.ts | 10 +- .../src/cloud/ManagedEndpointRuntime.test.ts | 28 ++ .../src/cloud/ManagedEndpointRuntime.ts | 313 ++++++++++-------- .../src/cloud/RelayChannelClient.test.ts | 32 ++ apps/server/src/cloud/RelayChannelClient.ts | 220 ++++++++++++ apps/web/src/cloud/useCloudLinkController.ts | 1 - docs/user/remote-access.md | 10 +- infra/relay/selfhost/.env.example | 5 + infra/relay/selfhost/README.md | 27 +- infra/relay/selfhost/docker-compose.yml | 1 + infra/relay/src/Config.ts | 1 + infra/relay/src/composition.ts | 1 + .../environments/EnvironmentConnector.test.ts | 35 ++ .../src/environments/EnvironmentConnector.ts | 15 + .../environments/EnvironmentLinker.test.ts | 135 +++++--- .../src/environments/EnvironmentLinker.ts | 29 +- .../environments/RelayChannelEndpoint.test.ts | 33 ++ .../src/environments/RelayChannelEndpoint.ts | 39 +++ infra/relay/src/host/RelayChannels.test.ts | 128 +++++++ infra/relay/src/host/RelayChannels.ts | 263 +++++++++++++++ infra/relay/src/host/RelayHost.ts | 2 + infra/relay/src/host/node.ts | 2 + infra/relay/src/node.ts | 17 +- packages/contracts/src/relay.ts | 47 +++ 25 files changed, 1230 insertions(+), 198 deletions(-) create mode 100644 apps/server/src/cloud/RelayChannelClient.test.ts create mode 100644 apps/server/src/cloud/RelayChannelClient.ts create mode 100644 infra/relay/src/environments/RelayChannelEndpoint.test.ts create mode 100644 infra/relay/src/environments/RelayChannelEndpoint.ts create mode 100644 infra/relay/src/host/RelayChannels.test.ts create mode 100644 infra/relay/src/host/RelayChannels.ts diff --git a/apps/server/src/cloud/CloudLink.ts b/apps/server/src/cloud/CloudLink.ts index acbd1dea87d9a..fe9fd90701546 100644 --- a/apps/server/src/cloud/CloudLink.ts +++ b/apps/server/src/cloud/CloudLink.ts @@ -305,6 +305,11 @@ const rollbackFailed = (cause: unknown) => // Where a publish-only link says this machine can be reached; unset keeps the local placeholder. const publicEndpointConfig = Config.option(Config.NonEmptyString("T3CODE_PUBLIC_URL")); +function localOriginUrl(origin: RelayManagedEndpointOrigin): string { + const host = origin.localHttpHost; + return `http://${host.includes(":") && !host.startsWith("[") ? `[${host}]` : host}:${origin.localHttpPort}`; +} + const requireRelayUrl = relayUrlConfig.pipe( Effect.mapError( (cause) => new CloudLinkInternalError({ operation: "relay-url-unconfigured", cause }), @@ -724,6 +729,17 @@ const make = Effect.gen(function* () { ]); if (Option.isNone(runtimeBytes)) return false; const config = Option.getOrNull(decodeRuntimeConfig(bytesToString(runtimeBytes.value))); + if (config?.providerKind === "t3_relay") { + // The channel dials out and forwards to whatever port this server listens on now. + const status = yield* endpointRuntime.applyConfig(config, localOrigin); + if (status.status !== "running") { + return yield* new CloudLinkEndpointUnavailableError({ + reason: "runtime-not-started", + endpointRuntimeStatus: status, + }); + } + return true; + } if (config === null || config.providerKind !== "cloudflare_tunnel") return false; // Require the relay-confirmed port unless falling back after the relay stayed unreachable. if (requireConfirmedOrigin) { @@ -764,7 +780,8 @@ const make = Effect.gen(function* () { // Reject unsupported runtimes before they can stop a healthy tunnel. if ( payload.endpointRuntime !== null && - payload.endpointRuntime.providerKind !== "cloudflare_tunnel" + payload.endpointRuntime.providerKind !== "cloudflare_tunnel" && + payload.endpointRuntime.providerKind !== "t3_relay" ) { return yield* new CloudLinkEndpointUnavailableError({ reason: "runtime-not-started", @@ -804,7 +821,10 @@ const make = Effect.gen(function* () { endpointRuntimeStatus: { status: "disabled" }, } satisfies EnvironmentCloudRelayConfigResult; } - const endpointRuntimeStatus = yield* endpointRuntime.applyConfig(payload.endpointRuntime); + const endpointRuntimeStatus = yield* endpointRuntime.applyConfig( + payload.endpointRuntime, + localOriginUrl(options.confirmedOrigin), + ); if (endpointRuntimeStatus.status !== "running") { return yield* new CloudLinkEndpointUnavailableError({ reason: "runtime-not-started", @@ -1178,6 +1198,16 @@ const make = Effect.gen(function* () { const applyRelayConfig = Effect.fn("environment.cloud.relayConfig")( function* (payload: RelayEnvironmentConfigRequest) { const result = yield* applyCloudRelayConfig(payload); + if (payload.endpointRuntime?.providerKind === "t3_relay") { + const address = httpServer.address; + if (typeof address === "string" || !("port" in address)) { + return yield* new CloudLinkInternalError({ operation: "resolve-server-origin" }); + } + yield* startManagedTunnelIfOriginConfirmed( + `http://${loopbackUrlHostForAddress(address)}:${address.port}`, + ); + return result; + } if (payload.endpointRuntime?.providerKind === "cloudflare_tunnel") { const address = httpServer.address; if (typeof address === "string" || !("port" in address)) { diff --git a/apps/server/src/cloud/ConnectReachability.ts b/apps/server/src/cloud/ConnectReachability.ts index aa9697673295c..abe516dab4f3e 100644 --- a/apps/server/src/cloud/ConnectReachability.ts +++ b/apps/server/src/cloud/ConnectReachability.ts @@ -21,7 +21,7 @@ import { HttpClient } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; -import { CLOUD_ENDPOINT_RUNTIME_CONFIG } from "./config.ts"; +import { CLOUD_ENDPOINT_RUNTIME_CONFIG, decodeRuntimeConfig } from "./config.ts"; import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts"; // cloudflared finds the edge through the SRV record `_v2-origintunneld._tcp.argotunnel.com`, @@ -272,8 +272,14 @@ const make = Effect.gen(function* () { ); const gatherFacts = Effect.gen(function* () { - const managedTunnelConfigured = Option.isSome( + // A relay channel dials out with no local tunnel connector to inspect. + const managedTunnelConfigured = Option.exists( yield* secrets.get(CLOUD_ENDPOINT_RUNTIME_CONFIG).pipe(Effect.orElseSucceed(Option.none)), + (bytes) => + !Option.exists( + decodeRuntimeConfig(new TextDecoder().decode(bytes)), + (config) => config.providerKind === "t3_relay", + ), ); const connector = managedTunnelConfigured ? yield* runtime.inspectConnector : null; const endpoint = diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts index d1f95140fa0a6..83e7d3536e3f6 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts @@ -912,6 +912,34 @@ describe("CloudManagedEndpointRuntime", () => { }), ); + it.effect("swaps the Cloudflare connector for a relay channel and stops both", () => + Effect.gen(function* () { + const killed: Array = []; + const spawner = ChildProcessSpawner.make(() => + Effect.gen(function* () { + const handle = makeHandle({ pid: 100, onKill: () => killed.push(100) }); + yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore)); + return handle; + }), + ); + const runtime = yield* buildCloudManagedEndpointRuntime(spawner); + const channel = { + providerKind: "t3_relay", + connectorToken: "credential", + channelUrl: "ws://127.0.0.1:9/v1/environments/env-1/channel", + } as const; + + yield* runtime.applyConfig({ providerKind: "cloudflare_tunnel", connectorToken: "token-1" }); + expect(yield* runtime.applyConfig(channel, "http://127.0.0.1:3773")).toEqual({ + status: "running", + providerKind: "t3_relay", + }); + expect(killed).toEqual([100]); + expect((yield* runtime.applyConfig(channel)).status).toBe("failed"); + expect(yield* runtime.applyConfig(null)).toEqual({ status: "disabled" }); + }), + ); + it.effect("supervises the active connector and restarts it after process exit", () => Effect.gen(function* () { const spawned: Array = []; diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.ts b/apps/server/src/cloud/ManagedEndpointRuntime.ts index 66ef48f547730..c56458f501236 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.ts @@ -9,6 +9,7 @@ import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; @@ -28,6 +29,7 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ServerConfig from "../config.ts"; import * as ManagedLimits from "../policy/managedLimits.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; +import * as RelayChannelClient from "./RelayChannelClient.ts"; export type CloudManagedEndpointRuntimeStatus = | { @@ -48,6 +50,10 @@ export type CloudManagedEndpointRuntimeStatus = readonly tunnelId?: string; readonly tunnelName?: string; } + | { + readonly status: "running"; + readonly providerKind: "t3_relay"; + } | { readonly status: "unsupported"; readonly providerKind: RelayManagedEndpointRuntimeConfig["providerKind"]; @@ -65,8 +71,10 @@ export interface ConnectorFacts { export class CloudManagedEndpointRuntime extends Context.Service< CloudManagedEndpointRuntime, { + /** `localOrigin` is where a `t3_relay` channel forwards traffic; tunnels ignore it. */ readonly applyConfig: ( config: RelayManagedEndpointRuntimeConfig | null, + localOrigin?: string, ) => Effect.Effect; /** `null` when no connector is running. */ readonly inspectConnector: Effect.Effect; @@ -223,6 +231,10 @@ export const make = Effect.gen(function* () { const restartDelayRef = yield* Ref.make(0); const linkStateSemaphore = yield* Semaphore.make(1); const runtimeScope = yield* Effect.scope; + const channelRef = yield* Ref.make<{ + readonly key: string; + readonly fiber: Fiber.Fiber; + } | null>(null); const installInFlightRef = yield* Ref.make(false); const lastInstallFailureAtRef = yield* Ref.make(null); const prunedRef = yield* Ref.make(false); @@ -315,6 +327,41 @@ export const make = Effect.gen(function* () { yield* stopConnector(active); }); + const stopChannel = Effect.gen(function* () { + const channel = yield* Ref.getAndSet(channelRef, null); + if (channel) yield* Fiber.interrupt(channel.fiber); + }); + + const reconcileChannel = Effect.fn("CloudManagedEndpointRuntime.reconcileChannel")(function* ( + config: RelayManagedEndpointRuntimeConfig, + localOrigin: string | undefined, + ) { + yield* stopActive; + if (config.channelUrl === undefined || localOrigin === undefined) { + yield* stopChannel; + return { + status: "failed", + providerKind: "t3_relay", + failure: "spawn-failed", + reason: "The relay channel has no address or no local server to forward to.", + } satisfies CloudManagedEndpointRuntimeStatus; + } + const key = `${config.channelUrl}|${config.connectorToken}|${localOrigin}`; + if ((yield* Ref.get(channelRef))?.key !== key) { + yield* stopChannel; + const fiber = yield* RelayChannelClient.runRelayChannel({ + channelUrl: config.channelUrl, + token: config.connectorToken, + localOrigin, + }).pipe(withRelayClientLog, Effect.forkIn(runtimeScope)); + yield* Ref.set(channelRef, { key, fiber }); + } + return { + status: "running", + providerKind: "t3_relay", + } satisfies CloudManagedEndpointRuntimeStatus; + }); + // Lives until connector exit and starts replacements; avoid nested lifetime spans. const superviseConnector = Effect.fnUntraced( function* (connector: ActiveConnector) { @@ -680,153 +727,159 @@ export const make = Effect.gen(function* () { ); }; - reconcileConfig = Effect.fn("CloudManagedEndpointRuntime.reconcileConfig")(function* (config) { - if (!config || config.providerKind !== "cloudflare_tunnel") { + reconcileConfig = Effect.fn("CloudManagedEndpointRuntime.reconcileConfig")( + function* (config, localOrigin) { + if (config?.providerKind === "t3_relay") { + return yield* reconcileChannel(config, localOrigin); + } + yield* stopChannel; + if (!config || config.providerKind !== "cloudflare_tunnel") { + yield* stopActive; + return config + ? { status: "unsupported", providerKind: config.providerKind } + : { status: "disabled" }; + } + + const nextConfigKey = runtimeConfigKey(config); + const active = yield* Ref.get(activeRef); + if (active?.configKey === nextConfigKey) { + const isRunning = yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)); + if (isRunning) { + return { + status: "running", + providerKind: "cloudflare_tunnel", + pid: Number(active.child.pid), + ...(active.config.tunnelId ? { tunnelId: active.config.tunnelId } : {}), + ...(active.config.tunnelName ? { tunnelName: active.config.tunnelName } : {}), + } satisfies CloudManagedEndpointRuntimeStatus; + } + } + yield* stopActive; - return config - ? { status: "unsupported", providerKind: config.providerKind } - : { status: "disabled" }; - } - const nextConfigKey = runtimeConfigKey(config); - const active = yield* Ref.get(activeRef); - if (active?.configKey === nextConfigKey) { - const isRunning = yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)); - if (isRunning) { + const executable = yield* relayClient.resolve; + if (needsPinnedRelayClient(executable)) { + yield* ensurePinnedRelayClient; + yield* startPinnedRelayClientRetries; + } + if (executable.status !== "available") { return { - status: "running", + status: "failed", providerKind: "cloudflare_tunnel", - pid: Number(active.child.pid), - ...(active.config.tunnelId ? { tunnelId: active.config.tunnelId } : {}), - ...(active.config.tunnelName ? { tunnelName: active.config.tunnelName } : {}), + failure: executable.status === "unsupported" ? "unsupported-platform" : "not-installed", + reason: + executable.status === "unsupported" + ? `Relay client is unsupported on ${executable.platform}-${executable.arch}.` + : "The relay client is not installed.", + ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), + ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), } satisfies CloudManagedEndpointRuntimeStatus; } - } - - yield* stopActive; - - const executable = yield* relayClient.resolve; - if (needsPinnedRelayClient(executable)) { - yield* ensurePinnedRelayClient; - yield* startPinnedRelayClientRetries; - } - if (executable.status !== "available") { - return { - status: "failed", - providerKind: "cloudflare_tunnel", - failure: executable.status === "unsupported" ? "unsupported-platform" : "not-installed", - reason: - executable.status === "unsupported" - ? `Relay client is unsupported on ${executable.platform}-${executable.arch}.` - : "The relay client is not installed.", - ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), - ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), - } satisfies CloudManagedEndpointRuntimeStatus; - } - const http2 = yield* Ref.get(http2Ref); - const connectorScope = yield* Scope.make("sequential"); - const child = yield* spawner - .spawn( - ChildProcess.make( - executable.executablePath, - [ - "tunnel", - "--no-autoupdate", - "--loglevel", - "info", - "--output", - "default", - "--metrics", - "127.0.0.1:0", - ...(http2 ? ["--protocol", "http2"] : []), - "run", - ], - { - detached: false, - env: { - ...env, - TUNNEL_TOKEN: config.connectorToken, + const http2 = yield* Ref.get(http2Ref); + const connectorScope = yield* Scope.make("sequential"); + const child = yield* spawner + .spawn( + ChildProcess.make( + executable.executablePath, + [ + "tunnel", + "--no-autoupdate", + "--loglevel", + "info", + "--output", + "default", + "--metrics", + "127.0.0.1:0", + ...(http2 ? ["--protocol", "http2"] : []), + "run", + ], + { + detached: false, + env: { + ...env, + TUNNEL_TOKEN: config.connectorToken, + }, + shell: false, + stderr: "pipe", + stdout: "pipe", }, - shell: false, - stderr: "pipe", - stdout: "pipe", - }, - ), - ) - .pipe( - Effect.provideService(Scope.Scope, connectorScope), - Effect.tap((child) => - Effect.logInfo("Relay client process started; waiting for tunnel connection", { - pid: Number(child.pid), - source: executable.source, - version: executable.version, - protocol: http2 ? "http2" : "auto", - tunnelId: config.tunnelId, - tunnelName: config.tunnelName, - }), - ), - Effect.catch((cause) => - Effect.logWarning("Failed to start relay client", { - cause, - tunnelId: config.tunnelId, - tunnelName: config.tunnelName, - }).pipe( - Effect.andThen(Scope.close(connectorScope, Exit.void).pipe(Effect.ignore)), - Effect.as({ - status: "failed", - providerKind: "cloudflare_tunnel", - failure: "spawn-failed", - reason: String(cause), - ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), - ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), - } satisfies CloudManagedEndpointRuntimeStatus), ), - ), - ); + ) + .pipe( + Effect.provideService(Scope.Scope, connectorScope), + Effect.tap((child) => + Effect.logInfo("Relay client process started; waiting for tunnel connection", { + pid: Number(child.pid), + source: executable.source, + version: executable.version, + protocol: http2 ? "http2" : "auto", + tunnelId: config.tunnelId, + tunnelName: config.tunnelName, + }), + ), + Effect.catch((cause) => + Effect.logWarning("Failed to start relay client", { + cause, + tunnelId: config.tunnelId, + tunnelName: config.tunnelName, + }).pipe( + Effect.andThen(Scope.close(connectorScope, Exit.void).pipe(Effect.ignore)), + Effect.as({ + status: "failed", + providerKind: "cloudflare_tunnel", + failure: "spawn-failed", + reason: String(cause), + ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), + ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), + } satisfies CloudManagedEndpointRuntimeStatus), + ), + ), + ); - if ("status" in child && child.status === "failed") { - return child; - } + if ("status" in child && child.status === "failed") { + return child; + } + + if (!("status" in child)) { + const connector = { + child, + executable, + scope: connectorScope, + configKey: nextConfigKey, + config, + startedAtMillis: yield* Clock.currentTimeMillis, + registered: yield* Deferred.make(), + metricsAddress: yield* Deferred.make(), + protocol: yield* Ref.make(null), + } satisfies ActiveConnector; + yield* Ref.set(activeRef, connector); + yield* Effect.forkIn(observeConnectorOutput(connector), connectorScope); + yield* Effect.forkIn(superviseConnector(connector), connectorScope); + yield* Effect.forkIn(watchConnectorRegistration(connector), connectorScope); + yield* Effect.forkIn(watchConnectorReadiness(connector), connectorScope); + return { + status: "running", + providerKind: "cloudflare_tunnel", + pid: Number(child.pid), + ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), + ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), + } satisfies CloudManagedEndpointRuntimeStatus; + } - if (!("status" in child)) { - const connector = { - child, - executable, - scope: connectorScope, - configKey: nextConfigKey, - config, - startedAtMillis: yield* Clock.currentTimeMillis, - registered: yield* Deferred.make(), - metricsAddress: yield* Deferred.make(), - protocol: yield* Ref.make(null), - } satisfies ActiveConnector; - yield* Ref.set(activeRef, connector); - yield* Effect.forkIn(observeConnectorOutput(connector), connectorScope); - yield* Effect.forkIn(superviseConnector(connector), connectorScope); - yield* Effect.forkIn(watchConnectorRegistration(connector), connectorScope); - yield* Effect.forkIn(watchConnectorReadiness(connector), connectorScope); return { - status: "running", + status: "failed", providerKind: "cloudflare_tunnel", - pid: Number(child.pid), + failure: "spawn-failed", + reason: "Relay client did not start.", ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), } satisfies CloudManagedEndpointRuntimeStatus; - } - - return { - status: "failed", - providerKind: "cloudflare_tunnel", - failure: "spawn-failed", - reason: "Relay client did not start.", - ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}), - ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}), - } satisfies CloudManagedEndpointRuntimeStatus; - }); + }, + ); const applyConfig = Effect.fn("CloudManagedEndpointRuntime.applyConfig")( - (requested: RelayManagedEndpointRuntimeConfig | null) => + (requested: RelayManagedEndpointRuntimeConfig | null, localOrigin?: string) => reconcileSemaphore.withPermits(1)( Effect.gen(function* () { const config = connectAllowed ? requested : null; @@ -840,7 +893,7 @@ export const make = Effect.gen(function* () { yield* Ref.set(restartDelayRef, 0); } yield* Ref.set(desiredConfigRef, config); - return yield* reconcileConfig(config); + return yield* reconcileConfig(config, localOrigin); }), ), withRelayClientLog, diff --git a/apps/server/src/cloud/RelayChannelClient.test.ts b/apps/server/src/cloud/RelayChannelClient.test.ts new file mode 100644 index 0000000000000..4add59e2cde38 --- /dev/null +++ b/apps/server/src/cloud/RelayChannelClient.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "@effect/vitest"; + +import { forwardedRequestHeaders, localTargetUrl } from "./RelayChannelClient.ts"; + +describe("localTargetUrl", () => { + it.each([ + ["a path", "/api/state?x=1", "http://127.0.0.1:3773/api/state?x=1"], + ["an absolute URL", "http://169.254.169.254/latest", null], + ["a protocol-relative URL", "//evil.test/x", null], + ["a relative path", "api/state", null], + ] as const)("maps %s", (_name, path, expected) => { + expect(localTargetUrl("http://127.0.0.1:3773/", path)).toBe(expected); + }); +}); + +describe("forwardedRequestHeaders", () => { + it("presents the public host over HTTPS and drops connection headers", () => { + expect( + forwardedRequestHeaders({ + host: "t3-abc.relay.test", + connection: "upgrade", + "sec-websocket-key": "k", + authorization: "DPoP token", + }), + ).toEqual({ + host: "t3-abc.relay.test", + authorization: "DPoP token", + "x-forwarded-proto": "https", + "x-forwarded-host": "t3-abc.relay.test", + }); + }); +}); diff --git a/apps/server/src/cloud/RelayChannelClient.ts b/apps/server/src/cloud/RelayChannelClient.ts new file mode 100644 index 0000000000000..00719decfbe03 --- /dev/null +++ b/apps/server/src/cloud/RelayChannelClient.ts @@ -0,0 +1,220 @@ +import { RelayChannelFrame } from "@t3tools/contracts/relay"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Schedule from "effect/Schedule"; +import * as Schema from "effect/Schema"; +import { request as undiciRequest, WebSocket, type Dispatcher } from "undici"; + +type HttpRequestFrame = Extract; +type WsOpenFrame = Extract; + +// The relay and this server each own their connection; these do not cross the channel. +const CONNECTION_HEADERS = new Set([ + "connection", + "keep-alive", + "upgrade", + "transfer-encoding", + "content-length", + "sec-websocket-key", + "sec-websocket-version", + "sec-websocket-extensions", + "sec-websocket-accept", +]); +const KEEPALIVE_INTERVAL = "25 seconds"; +const RECONNECT_DELAY = "3 seconds"; + +const decodeFrame = Schema.decodeUnknownOption(RelayChannelFrame); + +function parseFrame(data: unknown): RelayChannelFrame | null { + try { + const text = typeof data === "string" ? data : new TextDecoder().decode(data as ArrayBuffer); + return Option.getOrNull(decodeFrame(JSON.parse(text))); + } catch { + return null; + } +} + +/** Requests reach this server as they would through a TLS-terminating tunnel. */ +export function forwardedRequestHeaders( + headers: Readonly>, +): Record { + const forwarded = Object.fromEntries( + Object.entries(headers).filter(([name]) => !CONNECTION_HEADERS.has(name.toLowerCase())), + ); + return { + ...forwarded, + "x-forwarded-proto": "https", + ...(headers.host ? { "x-forwarded-host": headers.host } : {}), + }; +} + +/** The local URL for a forwarded path; anything but an origin-relative path is refused. */ +export function localTargetUrl(localOrigin: string, path: string): string | null { + return /^\/(?!\/)/u.test(path) ? `${localOrigin.replace(/\/+$/u, "")}${path}` : null; +} + +function flattenHeaders(headers: Readonly>) { + const flat: Record = {}; + for (const [name, value] of Object.entries(headers)) { + if (value === undefined || CONNECTION_HEADERS.has(name)) continue; + flat[name] = Array.isArray(value) ? value.join(", ") : value; + } + return flat; +} + +async function forwardHttp(localOrigin: string, frame: HttpRequestFrame) { + const failed = { type: "http-response", id: frame.id, status: 502, headers: {} } as const; + const url = localTargetUrl(localOrigin, frame.path); + if (url === null) return failed; + try { + const response = await undiciRequest(url, { + method: frame.method as Dispatcher.HttpMethod, + headers: forwardedRequestHeaders(frame.headers), + ...(frame.body ? { body: Buffer.from(frame.body, "base64") } : {}), + }); + const body = Buffer.from(await response.body.arrayBuffer()); + return { + type: "http-response", + id: frame.id, + status: response.statusCode, + headers: flattenHeaders(response.headers), + ...(body.byteLength > 0 ? { body: body.toString("base64") } : {}), + } as const; + } catch { + return failed; + } +} + +function openLocalSocket( + localOrigin: string, + frame: WsOpenFrame, + send: (frame: RelayChannelFrame) => void, +): WebSocket | null { + const url = localTargetUrl(localOrigin.replace(/^http/u, "ws"), frame.path); + if (url === null) return null; + // shortcut: WebSocket subprotocols are not negotiated; the environment's /ws authenticates by ticket. + const { + host: _host, + "sec-websocket-protocol": _protocol, + ...headers + } = forwardedRequestHeaders(frame.headers); + const socket = new WebSocket(url, { headers }); + socket.binaryType = "arraybuffer"; + socket.addEventListener("open", () => send({ type: "ws-opened", id: frame.id })); + socket.addEventListener("message", (event) => + send( + typeof event.data === "string" + ? { type: "ws-message", id: frame.id, data: event.data } + : { + type: "ws-message", + id: frame.id, + data: Buffer.from(event.data as ArrayBuffer).toString("base64"), + binary: true, + }, + ), + ); + socket.addEventListener("close", (event) => + send({ type: "ws-close", id: frame.id, code: event.code, reason: event.reason }), + ); + return socket; +} + +function openChannel(input: RelayChannelInput) { + const channel = new WebSocket(input.channelUrl, { + headers: { authorization: `Bearer ${input.token}` }, + }); + const locals = new Map(); + let opened = false; + const send = (frame: RelayChannelFrame) => { + if (channel.readyState === WebSocket.OPEN) channel.send(JSON.stringify(frame)); + }; + const closeLocals = () => { + for (const socket of locals.values()) socket.close(); + locals.clear(); + }; + channel.addEventListener("open", () => { + opened = true; + }); + channel.addEventListener("message", (event) => { + const frame = parseFrame(event.data); + switch (frame?.type) { + case "http-request": + void forwardHttp(input.localOrigin, frame).then(send); + return; + case "ws-open": { + const socket = openLocalSocket(input.localOrigin, frame, (reply) => { + if (reply.type === "ws-close") locals.delete(frame.id); + send(reply); + }); + if (socket === null) send({ type: "ws-close", id: frame.id, code: 1008 }); + else locals.set(frame.id, socket); + return; + } + case "ws-message": { + const socket = locals.get(frame.id); + if (socket?.readyState === WebSocket.OPEN) { + socket.send(frame.binary ? Buffer.from(frame.data, "base64") : frame.data); + } + return; + } + case "ws-close": + locals.get(frame.id)?.close(); + locals.delete(frame.id); + return; + default: + return; + } + }); + const closed = new Promise((resolve) => + channel.addEventListener("close", (event) => { + closeLocals(); + resolve(opened ? `closed with code ${event.code}` : "could not connect"); + }), + ); + return { + send, + closed, + dispose: () => { + closeLocals(); + channel.close(); + }, + }; +} + +/** One channel connection; succeeds with why it ended. */ +const connectOnce = Effect.fn("RelayChannelClient.connectOnce")(function* ( + input: RelayChannelInput, +) { + const channel = yield* Effect.acquireRelease( + Effect.sync(() => openChannel(input)), + (opened) => Effect.sync(opened.dispose), + ); + // Proxies drop idle WebSockets; a ping keeps the channel open between requests. + yield* Effect.sync(() => channel.send({ type: "ping" })).pipe( + Effect.repeat(Schedule.spaced(KEEPALIVE_INTERVAL)), + Effect.forkScoped, + ); + return yield* Effect.promise(() => channel.closed); +}, Effect.scoped); + +export interface RelayChannelInput { + readonly channelUrl: string; + /** The environment credential the relay issued for this link. */ + readonly token: string; + /** Where this server listens, e.g. `http://127.0.0.1:3773`. */ + readonly localOrigin: string; +} + +/** Keeps this environment reachable through its relay channel until interrupted. */ +export const runRelayChannel = Effect.fn("RelayChannelClient.run")(function* ( + input: RelayChannelInput, +) { + yield* Effect.logInfo("Connecting the T3 Connect relay channel", { + channelUrl: input.channelUrl, + }); + return yield* connectOnce(input).pipe( + Effect.tap((reason) => Effect.logWarning("T3 Connect relay channel ended", { reason })), + Effect.andThen(Effect.sleep(RECONNECT_DELAY)), + Effect.forever, + ); +}); diff --git a/apps/web/src/cloud/useCloudLinkController.ts b/apps/web/src/cloud/useCloudLinkController.ts index 8a29f360cc607..17ad4f42cdb3a 100644 --- a/apps/web/src/cloud/useCloudLinkController.ts +++ b/apps/web/src/cloud/useCloudLinkController.ts @@ -26,7 +26,6 @@ import { updateEnvironmentPreferences as updateEnvironmentPreferencesAtom, } from "./linkEnvironmentAtoms"; import { readCachedCloudLinkState, useCloudLinkState } from "./cloudLinkState"; -import { resolveRelayClerkTokenOptions } from "./publicConfig"; import { isManagedTunnelActive } from "./t3ConnectState"; export interface CloudLinkDesiredState { diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index c35e1d2a8be74..8eceeb234ac2e 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -76,10 +76,12 @@ reads the sign-in provider from the relay, reloads, and asks you to sign in. Choose **T3 Connect** there to switch back. Environments you linked through your relay stay linked to it until you turn T3 Connect off and on again. -A self-hosted relay has no managed tunnels. Linking an environment still works, -but your other devices reach it only at an HTTPS address you expose yourself, -such as a reverse proxy or [Tailscale HTTPS](#tailscale-https). Start the host -with that address in `T3CODE_PUBLIC_URL`. Tailscale and LAN addresses also need +A self-hosted relay has no managed tunnels. If the relay sets +`RELAY_CHANNEL_HOST`, a linked environment with no public address is carried +through the relay itself, so there is nothing to expose. Otherwise your other +devices reach it only at an HTTPS address you expose yourself, such as a reverse +proxy or [Tailscale HTTPS](#tailscale-https): start the host with that address in +`T3CODE_PUBLIC_URL`. Tailscale and LAN addresses also need `RELAY_MANUAL_ENDPOINTS=private` on the relay. For a command-line host, set the relay before `t3 connect`: diff --git a/infra/relay/selfhost/.env.example b/infra/relay/selfhost/.env.example index 574390a9dc2b4..ce67718c45a24 100644 --- a/infra/relay/selfhost/.env.example +++ b/infra/relay/selfhost/.env.example @@ -15,6 +15,11 @@ OIDC_AUDIENCE=t3-code # addresses, "private" also LAN/Tailscale addresses, "off" refuses them. RELAY_MANUAL_ENDPOINTS=public +# Optional: carry environments with no public address through the relay itself. Each one gets +# the host this pattern names, with `*` replaced by a per-environment key. Needs wildcard DNS +# and a wildcard certificate at your proxy (see README). Leave empty to turn it off. +# RELAY_CHANNEL_HOST=t3-*.example.com + POSTGRES_PASSWORD=change-me # Optional: host ports, when 80 and 443 are taken. diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md index 845dd4ad315f3..7e4d7556c1293 100644 --- a/infra/relay/selfhost/README.md +++ b/infra/relay/selfhost/README.md @@ -97,10 +97,19 @@ them to each app's prefix). A configured OpenID Connect provider replaces Clerk ## Reach an environment without a tunnel -With `RELAY_MANUAL_ENDPOINTS=public` (the default here), an environment can publish an HTTPS -address you already expose (reverse proxy, port forward) and the relay brokers connections to it -like a managed endpoint. Run `t3 connect --publish-only`, then start the server with -`T3CODE_PUBLIC_URL` set to that address: +**Through the relay (no address to expose).** Set `RELAY_CHANNEL_HOST` to a host pattern such as +`t3-*.example.com`. An environment linked without its own public address then keeps one outbound +WebSocket open to the relay, and the relay serves it at `https://t3-.example.com`, forwarding +HTTP and WebSocket traffic over that socket. Nothing to open on the environment's network. Route +the pattern to the relay at your proxy with a wildcard DNS record and a wildcard certificate (the +bundled Caddy serves only `RELAY_DOMAIN`; a wildcard certificate needs Caddy's DNS challenge). +All of that environment's traffic passes through the relay host, and channels live in the relay +process, so keep a single replica. + +**At an address you expose.** With `RELAY_MANUAL_ENDPOINTS=public` (the default here), an +environment can publish an HTTPS address you already expose (reverse proxy, port forward) and the +relay brokers connections to it like a managed endpoint. Run `t3 connect --publish-only`, then +start the server with `T3CODE_PUBLIC_URL` set to that address: ```sh t3 connect --publish-only @@ -111,20 +120,20 @@ The address must serve the environment's HTTP and WebSocket traffic over HTTPS. refuses loopback, link-local and cloud metadata addresses. `private` also accepts private network addresses (LAN, Tailscale) for relays only your own network uses; `off` refuses all of them. Host names are checked as written, not resolved, so only point a relay you trust at names -you control. +you control. A published address takes precedence over a relay channel. ## Behind another reverse proxy Caddy is the bundled proxy. To use your own (nginx, Traefik, a cloud load balancer), drop the `caddy` service, point your proxy at the relay's port 8080, and keep `RELAY_PUBLIC_ORIGIN` at -the public HTTPS address. The relay builds no URLs from request headers, so forwarded `Host` or -`X-Forwarded-*` values are ignored. `OIDC_ISSUER` must equal the issuer your provider reports +the public HTTPS address. The relay builds no URLs from request headers, so `X-Forwarded-*` +values are ignored. It reads `Host` only to match `RELAY_CHANNEL_HOST`, so pass it through. `OIDC_ISSUER` must equal the issuer your provider reports publicly, including any path prefix your proxy adds. ## What this host does not do -- **Managed tunnels.** Cloudflare-managed endpoints are not available; use a manual endpoint - (above) or reach environments directly. +- **Managed tunnels.** Cloudflare-managed endpoints are not available; use a relay channel or + a manual endpoint (above), or reach environments directly. - **Push notifications.** APNs and FCM need credentials tied to the mobile app's signing identity, so this stack turns them off. To enable them, add `APNS_ENABLED=true`, the `APNS_*` or `FCM_SERVICE_ACCOUNT` variables and a stable `APNS_DELIVERY_JOB_SIGNING_SECRET` (for diff --git a/infra/relay/selfhost/docker-compose.yml b/infra/relay/selfhost/docker-compose.yml index 94299e9c3e45d..1af20ec34c5cd 100644 --- a/infra/relay/selfhost/docker-compose.yml +++ b/infra/relay/selfhost/docker-compose.yml @@ -32,6 +32,7 @@ services: OIDC_ISSUER: ${OIDC_ISSUER:?set OIDC_ISSUER in .env} OIDC_AUDIENCE: ${OIDC_AUDIENCE:?set OIDC_AUDIENCE in .env} RELAY_MANUAL_ENDPOINTS: ${RELAY_MANUAL_ENDPOINTS:-public} + RELAY_CHANNEL_HOST: ${RELAY_CHANNEL_HOST:-} CLOUD_MINT_PRIVATE_KEY_FILE: /run/secrets/cloud_mint_private_key CLOUD_MINT_PUBLIC_KEY_FILE: /run/secrets/cloud_mint_public_key # Push notifications need the publisher's APNs and FCM credentials; leave them off here. diff --git a/infra/relay/src/Config.ts b/infra/relay/src/Config.ts index a22042ac29b15..55eed65a595ac 100644 --- a/infra/relay/src/Config.ts +++ b/infra/relay/src/Config.ts @@ -87,6 +87,7 @@ export class RelayConfiguration extends Context.Service< readonly cloudMintPrivateKey: Redacted.Redacted; readonly cloudMintPublicKey: string; readonly managedEndpointBaseDomain: string | undefined; + readonly channelHost?: string; readonly managedEndpointNamespace: string | undefined; readonly managedEndpointCleanupMode?: ManagedEndpointCleanupMode; readonly legacyManagedEndpointCleanupMode?: ManagedEndpointCleanupMode; diff --git a/infra/relay/src/composition.ts b/infra/relay/src/composition.ts index 561f8f4b716d0..ffd97fb91c180 100644 --- a/infra/relay/src/composition.ts +++ b/infra/relay/src/composition.ts @@ -183,6 +183,7 @@ export const composeRelay = Effect.fn("relay.composition.compose")(function* cloudMintPrivateKey: yield* cloudMintPrivateKey, cloudMintPublicKey: yield* cloudMintPublicKey, managedEndpointBaseDomain: host.managedEndpointBaseDomain, + ...(host.channelHost ? { channelHost: host.channelHost } : {}), managedEndpointNamespace: stage, managedEndpointCleanupMode, legacyManagedEndpointCleanupMode, diff --git a/infra/relay/src/environments/EnvironmentConnector.test.ts b/infra/relay/src/environments/EnvironmentConnector.test.ts index b00a23e921bbd..c55fd19c39747 100644 --- a/infra/relay/src/environments/EnvironmentConnector.test.ts +++ b/infra/relay/src/environments/EnvironmentConnector.test.ts @@ -796,3 +796,38 @@ describe("validateManualEndpoint", () => { ).toBe(true); }); }); + +describe("validateConnectEndpoint with relay channels", () => { + const link = (hostname: string) => + ({ + environmentId: "env-connector-test" as never, + label: "Channel Environment", + endpoint: { + httpBaseUrl: `https://${hostname}/`, + wsBaseUrl: `wss://${hostname}/ws`, + providerKind: "t3_relay", + }, + linkedAt: "2026-05-25T00:00:00.000Z", + environmentPublicKey: environmentKeyPair.publicKey, + }) satisfies EnvironmentLinks.RelayLinkedEnvironmentRecord; + const own = `t3-${"a".repeat(32)}.relay.example.test`; + const validate = (hostname: string, channelHostname: string | null) => + EnvironmentConnector.validateConnectEndpoint({ + link: link(hostname), + allocation: null, + baseDomain: undefined, + manualEndpoints: "off", + channelHostname, + }); + + it.each([ + ["another environment's channel", `t3-${"b".repeat(32)}.relay.example.test`, own], + ["a relay without channels", own, null], + ] as const)("rejects %s", (_name, hostname, channelHostname) => { + expect(Result.isFailure(validate(hostname, channelHostname))).toBe(true); + }); + + it("accepts the environment's own channel", () => { + expect(Result.isSuccess(validate(own, own))).toBe(true); + }); +}); diff --git a/infra/relay/src/environments/EnvironmentConnector.ts b/infra/relay/src/environments/EnvironmentConnector.ts index 4538adbb6460f..c7de177dd76c1 100644 --- a/infra/relay/src/environments/EnvironmentConnector.ts +++ b/infra/relay/src/environments/EnvironmentConnector.ts @@ -46,6 +46,7 @@ import * as HttpClientError from "effect/http/HttpClientError"; import { diagnoseEndpointFailure } from "./endpointFailure.ts"; import * as EnvironmentLinks from "./EnvironmentLinks.ts"; import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts"; +import { channelEndpoint, channelHostname, channelKey } from "./RelayChannelEndpoint.ts"; import * as RelayConfiguration from "../Config.ts"; import * as CloudMintSigner from "../auth/CloudMintSigner.ts"; import { isManagedEndpointHostname } from "../deploymentConfig.ts"; @@ -418,7 +419,17 @@ export function validateConnectEndpoint(input: { readonly allocation: ManagedEndpointAllocations.ManagedEndpointAllocation | null; readonly baseDomain: string | undefined; readonly manualEndpoints: RelayConfiguration.ManualEndpointsMode; + /** This environment's relay channel host, or null when the relay carries no channels. */ + readonly channelHostname?: string | null; }): Result.Result { + if (input.link.endpoint.providerKind === "t3_relay") { + const expected = input.channelHostname ? channelEndpoint(input.channelHostname) : null; + return expected !== null && + input.link.endpoint.httpBaseUrl === expected.httpBaseUrl && + input.link.endpoint.wsBaseUrl === expected.wsBaseUrl + ? Result.succeed(expected) + : Result.fail({ reason: "endpoint_provider_not_managed" }); + } return input.link.endpoint.providerKind === "manual" && input.manualEndpoints !== "off" ? validateManualEndpoint({ endpoint: input.link.endpoint, @@ -451,6 +462,10 @@ const make = Effect.gen(function* () { allocation: input.allocation, baseDomain: settings.managedEndpointBaseDomain, manualEndpoints: settings.manualEndpoints ?? "off", + channelHostname: + settings.channelHost === undefined + ? null + : channelHostname(settings.channelHost, yield* channelKey(input.link.environmentId)), }); if (Result.isSuccess(result)) { return result.success; diff --git a/infra/relay/src/environments/EnvironmentLinker.test.ts b/infra/relay/src/environments/EnvironmentLinker.test.ts index b770b52d22f4e..f50045f143e36 100644 --- a/infra/relay/src/environments/EnvironmentLinker.test.ts +++ b/infra/relay/src/environments/EnvironmentLinker.test.ts @@ -109,7 +109,57 @@ const makeRequest = Effect.gen(function* () { }; }); +const makePublishOnlyRequest = Effect.gen(function* () { + const now = yield* DateTime.now; + const expiresAt = DateTime.add(now, { minutes: 5 }); + const relayTokens = yield* RelayTokens.RelayTokens; + const challenge = yield* relayTokens.issueLinkChallenge({ + userId: "user_123", + request: { + notificationsEnabled: true, + liveActivitiesEnabled: true, + managedTunnelsEnabled: false, + }, + jti: "publish-only-challenge-jti", + issuedAtEpochSeconds: Math.floor(now.epochMilliseconds / 1_000), + expiresAtEpochSeconds: Math.floor(expiresAt.epochMilliseconds / 1_000), + }); + const payload = { + iss: "t3-env:env-link-test", + aud: "https://relay.example.test", + sub: "env-link-test", + jti: "publish-only-proof-jti", + iat: Math.floor(now.epochMilliseconds / 1_000), + exp: Math.floor(expiresAt.epochMilliseconds / 1_000), + challenge, + environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"], + descriptor: { + environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"], + label: "Link Test Environment", + platform: { os: "darwin", arch: "arm64" }, + serverVersion: "0.0.0-test", + capabilities: { repositoryIdentity: true }, + }, + environmentPublicKey: environmentKeyPair.publicKey.trim(), + endpoint: { + httpBaseUrl: "http://127.0.0.1:3773/", + wsBaseUrl: "ws://127.0.0.1:3773/", + providerKind: "manual", + }, + origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 }, + scopes: ["agent_activity_notifications"], + } satisfies RelayEnvironmentLinkProofPayload; + const request = { + proof: signTestJwt(payload, RELAY_LINK_PROOF_TYP, environmentKeyPair.privateKey), + notificationsEnabled: true, + liveActivitiesEnabled: true, + managedTunnelsEnabled: false, + } satisfies RelayEnvironmentLinkRequest; + return request; +}); + function layerTest(input?: { + readonly channelHost?: string; readonly upsert?: EnvironmentLinks.EnvironmentLinks["Service"]["upsert"]; readonly consume?: DpopProofs.DpopProofReplay["Service"]["consume"]; readonly deprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["deprovision"]; @@ -118,7 +168,9 @@ function layerTest(input?: { Layer.provideMerge(RelayTokens.layer.pipe(Layer.provide(CloudMintSigner.layer))), Layer.provide( Layer.mergeAll( - RelayConfiguration.layer(config), + RelayConfiguration.layer( + input?.channelHost ? { ...config, channelHost: input.channelHost } : config, + ), Layer.succeed(DpopProofs.DpopProofReplay, { verifyAndConsume: () => Effect.die("unexpected DPoP proof verification"), consume: input?.consume ?? (() => Effect.succeed(true)), @@ -185,51 +237,7 @@ describe("EnvironmentLinker", () => { let persistedEndpoint: string | null = null; let deprovisionedEnvironmentId: string | null = null; return Effect.gen(function* () { - const now = yield* DateTime.now; - const expiresAt = DateTime.add(now, { minutes: 5 }); - const relayTokens = yield* RelayTokens.RelayTokens; - const challenge = yield* relayTokens.issueLinkChallenge({ - userId: "user_123", - request: { - notificationsEnabled: true, - liveActivitiesEnabled: true, - managedTunnelsEnabled: false, - }, - jti: "publish-only-challenge-jti", - issuedAtEpochSeconds: Math.floor(now.epochMilliseconds / 1_000), - expiresAtEpochSeconds: Math.floor(expiresAt.epochMilliseconds / 1_000), - }); - const payload = { - iss: "t3-env:env-link-test", - aud: "https://relay.example.test", - sub: "env-link-test", - jti: "publish-only-proof-jti", - iat: Math.floor(now.epochMilliseconds / 1_000), - exp: Math.floor(expiresAt.epochMilliseconds / 1_000), - challenge, - environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"], - descriptor: { - environmentId: "env-link-test" as RelayEnvironmentLinkProofPayload["environmentId"], - label: "Link Test Environment", - platform: { os: "darwin", arch: "arm64" }, - serverVersion: "0.0.0-test", - capabilities: { repositoryIdentity: true }, - }, - environmentPublicKey: environmentKeyPair.publicKey.trim(), - endpoint: { - httpBaseUrl: "http://127.0.0.1:3773/", - wsBaseUrl: "ws://127.0.0.1:3773/", - providerKind: "manual", - }, - origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 }, - scopes: ["agent_activity_notifications"], - } satisfies RelayEnvironmentLinkProofPayload; - const request = { - proof: signTestJwt(payload, RELAY_LINK_PROOF_TYP, environmentKeyPair.privateKey), - notificationsEnabled: true, - liveActivitiesEnabled: true, - managedTunnelsEnabled: false, - } satisfies RelayEnvironmentLinkRequest; + const request = yield* makePublishOnlyRequest; const linker = yield* EnvironmentLinker.EnvironmentLinker; const result = yield* linker.link({ userId: "user_123", request }); expect(result.environmentCredential).toBe("t3env_credential_secret"); @@ -253,6 +261,41 @@ describe("EnvironmentLinker", () => { ); }); + it.effect( + "carries a publish-only environment with no public address over a relay channel", + () => { + let persistedEndpoint: string | null = null; + return Effect.gen(function* () { + const request = yield* makePublishOnlyRequest; + const linker = yield* EnvironmentLinker.EnvironmentLinker; + const result = yield* linker.link({ userId: "user_123", request }); + expect(result.endpoint).toEqual({ + httpBaseUrl: expect.stringMatching( + /^https:\/\/t3-[0-9a-f]{32}\.relay\.example\.test\/$/u, + ), + wsBaseUrl: expect.stringMatching(/^wss:\/\/t3-[0-9a-f]{32}\.relay\.example\.test\/ws$/u), + providerKind: "t3_relay", + }); + expect(persistedEndpoint).toBe(result.endpoint.httpBaseUrl); + expect(result.endpointRuntime).toEqual({ + providerKind: "t3_relay", + connectorToken: "t3env_credential_secret", + channelUrl: "https://relay.example.test/v1/environments/env-link-test/channel", + }); + }).pipe( + Effect.provide( + layerTest({ + channelHost: "t3-*.relay.example.test", + upsert: (input) => + Effect.sync(() => { + persistedEndpoint = input.endpoint.httpBaseUrl; + }), + }), + ), + ); + }, + ); + it.effect("rejects a tampered compact proof before persistence", () => { let persisted = false; return Effect.gen(function* () { diff --git a/infra/relay/src/environments/EnvironmentLinker.ts b/infra/relay/src/environments/EnvironmentLinker.ts index 6a58f6ab4f378..fa2dcab68a856 100644 --- a/infra/relay/src/environments/EnvironmentLinker.ts +++ b/infra/relay/src/environments/EnvironmentLinker.ts @@ -14,13 +14,16 @@ import * as Signature from "effect/crypto/Signature"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as DpopProofs from "../auth/DpopProofs.ts"; import * as RelayTokens from "../auth/RelayTokens.ts"; +import { validateManualEndpoint } from "./EnvironmentConnector.ts"; import * as EnvironmentCredentials from "./EnvironmentCredentials.ts"; import * as EnvironmentLinks from "./EnvironmentLinks.ts"; import * as ManagedEndpointProvider from "./ManagedEndpointProvider.ts"; +import { channelEndpoint, channelHostname, channelKey } from "./RelayChannelEndpoint.ts"; import * as RelayConfiguration from "../Config.ts"; export class EnvironmentLinkProofExpired extends Schema.TaggedError()( @@ -312,7 +315,21 @@ const make = Effect.gen(function* () { origin: verified.origin, }) : null; - const endpoint = provisioned?.endpoint ?? verified.endpoint; + // A publish-only link with no address the relay may contact is carried over a relay channel. + const channel = + !input.request.managedTunnelsEnabled && + config.channelHost !== undefined && + Result.isFailure( + validateManualEndpoint({ + endpoint: verified.endpoint, + manualEndpoints: config.manualEndpoints ?? "off", + }), + ) + ? channelEndpoint( + channelHostname(config.channelHost, yield* channelKey(verified.environmentId)), + ) + : null; + const endpoint = provisioned?.endpoint ?? channel ?? verified.endpoint; // Only advertised managed endpoints need secure URLs; publish-only links route out of band. if (input.request.managedTunnelsEnabled && !isSecureManagedEndpoint(endpoint)) { return yield* new EnvironmentLinkProofInvalid({ @@ -330,7 +347,15 @@ const make = Effect.gen(function* () { return { environmentId: verified.environmentId, endpoint, - endpointRuntime: provisioned?.runtime ?? null, + endpointRuntime: + provisioned?.runtime ?? + (channel + ? { + providerKind: "t3_relay" as const, + connectorToken: environmentCredential, + channelUrl: `${normalizeRelayIssuer(config.relayIssuer)}/v1/environments/${encodeURIComponent(verified.environmentId)}/channel`, + } + : null), environmentCredential, }; }), diff --git a/infra/relay/src/environments/RelayChannelEndpoint.test.ts b/infra/relay/src/environments/RelayChannelEndpoint.test.ts new file mode 100644 index 0000000000000..09f110f727d9d --- /dev/null +++ b/infra/relay/src/environments/RelayChannelEndpoint.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; + +import { channelHostname, channelKey, channelKeyForHost } from "./RelayChannelEndpoint.ts"; + +const key = "0123456789abcdef0123456789abcdef"; + +describe("channelKeyForHost", () => { + it.each([ + ["the channel host", `t3-${key}.relay.example.test`, key], + ["a forwarded port and mixed case", `T3-${key.toUpperCase()}.Relay.Example.Test:443`, key], + ["the relay's own host", "relay.example.test", null], + ["a short key", "t3-abc.relay.example.test", null], + ["a nested label", `x.t3-${key}.relay.example.test`, null], + ["a lookalike domain", `t3-${key}.relay.example.test.evil.test`, null], + ] as const)("reads %s", (_name, host, expected) => { + expect(channelKeyForHost("t3-*.relay.example.test", host)).toBe(expected); + }); +}); + +describe("channelKey", () => { + it.effect("gives each environment its own stable host label", () => + Effect.gen(function* () { + const first = yield* channelKey("env-a"); + expect(first).toMatch(/^[0-9a-f]{32}$/u); + expect(yield* channelKey("env-a")).toBe(first); + expect(yield* channelKey("env-b")).not.toBe(first); + expect( + channelKeyForHost("t3-*.example.test", channelHostname("t3-*.example.test", first)), + ).toBe(first); + }), + ); +}); diff --git a/infra/relay/src/environments/RelayChannelEndpoint.ts b/infra/relay/src/environments/RelayChannelEndpoint.ts new file mode 100644 index 0000000000000..876380f9a6535 --- /dev/null +++ b/infra/relay/src/environments/RelayChannelEndpoint.ts @@ -0,0 +1,39 @@ +import type { RelayManagedEndpoint } from "@t3tools/contracts/relay"; +import * as Effect from "effect/Effect"; + +const CHANNEL_KEY_PATTERN = /^[0-9a-f]{32}$/u; + +/** Host label for an environment's relay channel; hashed so the host does not reveal the environment id. */ +export const channelKey = (environmentId: string) => + Effect.promise(() => + crypto.subtle.digest("SHA-256", new TextEncoder().encode(`t3-relay-channel:${environmentId}`)), + ).pipe( + Effect.map((digest) => + Array.from(new Uint8Array(digest).slice(0, 16), (byte) => + byte.toString(16).padStart(2, "0"), + ).join(""), + ), + ); + +export function channelHostname(template: string, key: string): string { + return template.toLowerCase().replace("*", key); +} + +export function channelEndpoint(hostname: string): RelayManagedEndpoint { + return { + httpBaseUrl: `https://${hostname}/`, + wsBaseUrl: `wss://${hostname}/ws`, + providerKind: "t3_relay", + }; +} + +/** The channel key a request's `Host` names under `template`, or null when it is not a channel host. */ +export function channelKeyForHost(template: string, host: string): string | null { + const [prefix = "", suffix] = template.toLowerCase().split("*"); + const hostname = host.toLowerCase().replace(/:\d+$/u, ""); + if (suffix === undefined || !hostname.startsWith(prefix) || !hostname.endsWith(suffix)) { + return null; + } + const key = hostname.slice(prefix.length, hostname.length - suffix.length); + return CHANNEL_KEY_PATTERN.test(key) ? key : null; +} diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts new file mode 100644 index 0000000000000..6831a2f3ef091 --- /dev/null +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -0,0 +1,128 @@ +import * as NodeHttp from "node:http"; + +import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as HttpServer from "effect/http/HttpServer"; +import * as HttpServerResponse from "effect/http/HttpServerResponse"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; + +import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts"; +import { channelHostname, channelKey } from "../environments/RelayChannelEndpoint.ts"; +import { withRelayChannels } from "./RelayChannels.ts"; + +const CHANNEL_HOST = "t3-*.relay.test"; + +const layer = Layer.mergeAll( + NodeHttpServer.layer(() => NodeHttp.createServer(), { port: 0, host: "127.0.0.1" }), + Layer.succeed(EnvironmentCredentials.EnvironmentCredentials, { + create: () => Effect.die("unused"), + authenticate: (token) => + Effect.succeed( + token === "good" + ? Option.some({ credentialId: "c", environmentId: "env-1", environmentPublicKey: "k" }) + : Option.none(), + ), + revokeForEnvironmentPublicKey: () => Effect.die("unused"), + }), +); + +const serveRelay = Effect.gen(function* () { + yield* HttpServer.serveEffect( + withRelayChannels(CHANNEL_HOST)(Effect.succeed(HttpServerResponse.text("relay api"))), + ); + const { address } = yield* HttpServer.HttpServer; + return "port" in address ? address.port : 0; +}); + +/** A fake environment that answers each forwarded request with what it received. */ +const connectEnvironment = (port: number, token: string) => + Effect.acquireRelease( + Effect.callback((resume) => { + const socket = new WebSocket(`ws://127.0.0.1:${port}/v1/environments/env-1/channel`, { + headers: { authorization: `Bearer ${token}` }, + } as never); + socket.addEventListener("message", (event) => { + const frame = JSON.parse(String(event.data)); + // The relay pings once the channel is registered. + if (frame.type === "ping") resume(Effect.succeed(socket)); + if (frame.type === "http-request") { + const echo = `${frame.method} ${frame.path} ${frame.headers.host}`; + socket.send( + JSON.stringify({ + type: "http-response", + id: frame.id, + status: 201, + headers: { "content-type": "text/plain", "x-environment": "yes" }, + body: Buffer.from(echo).toString("base64"), + }), + ); + } + }); + socket.addEventListener("close", (event) => resume(Effect.fail(`closed ${event.code}`))); + }), + (socket) => Effect.sync(() => socket.close()), + ); + +const get = (port: number, host: string, path = "/") => + Effect.callback<{ readonly status: number; readonly body: string; readonly env?: string }>( + (resume) => { + const request = NodeHttp.request( + { host: "127.0.0.1", port, path, headers: { host } }, + (response) => { + let body = ""; + response.setEncoding("utf8"); + response.on("data", (chunk: string) => { + body += chunk; + }); + response.on("end", () => + resume( + Effect.succeed({ + status: response.statusCode ?? 0, + body, + ...(typeof response.headers["x-environment"] === "string" + ? { env: response.headers["x-environment"] } + : {}), + }), + ), + ); + }, + ); + request.on("error", (error) => resume(Effect.die(error))); + request.end(); + }, + ); + +describe("withRelayChannels", () => { + it.live("forwards a channel host to its environment and leaves other hosts to the relay", () => + Effect.gen(function* () { + const port = yield* serveRelay; + const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1")); + yield* connectEnvironment(port, "good"); + + expect(yield* get(port, host, "/api/state?x=1")).toEqual({ + status: 201, + body: `GET /api/state?x=1 ${host}`, + env: "yes", + }); + expect(yield* get(port, "relay.test")).toEqual({ status: 200, body: "relay api" }); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + + it.live("answers 502 for an environment that has no channel open", () => + Effect.gen(function* () { + const port = yield* serveRelay; + const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1")); + expect((yield* get(port, host)).status).toBe(502); + }).pipe(Effect.provide(layer)), + ); + + it.live("refuses a channel opened with another credential", () => + Effect.gen(function* () { + const port = yield* serveRelay; + const opened = yield* Effect.exit(connectEnvironment(port, "bad")); + expect(opened._tag).toBe("Failure"); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); +}); diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts new file mode 100644 index 0000000000000..8d340b63c2c6a --- /dev/null +++ b/infra/relay/src/host/RelayChannels.ts @@ -0,0 +1,263 @@ +import { RelayChannelFrame } from "@t3tools/contracts/relay"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as HttpServerRequest from "effect/http/HttpServerRequest"; +import * as HttpServerResponse from "effect/http/HttpServerResponse"; +import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; +import * as Schema from "effect/Schema"; +import * as Socket from "effect/socket/Socket"; + +import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts"; +import { channelKey, channelKeyForHost } from "../environments/RelayChannelEndpoint.ts"; + +type HttpResponseFrame = Extract; +type SocketFrame = Extract< + RelayChannelFrame, + { readonly type: "ws-opened" | "ws-message" | "ws-close" } +>; + +interface Channel { + readonly send: (frame: RelayChannelFrame) => Effect.Effect; + readonly responses: Map>; + readonly sockets: Map>; +} + +const CHANNEL_PATH = /^\/v1\/environments\/([^/?]+)\/channel(?:\?.*)?$/u; +// Per-connection headers stay on their own side of the relay. +const HOP_BY_HOP_HEADERS = new Set([ + "connection", + "keep-alive", + "upgrade", + "transfer-encoding", + "content-length", +]); + +const decodeFrame = Schema.decodeUnknownOption(RelayChannelFrame); + +function parseFrame(chunk: string | Uint8Array): Option.Option { + try { + return decodeFrame( + JSON.parse(typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)), + ); + } catch { + return Option.none(); + } +} + +function withoutHopByHop(headers: Readonly>): Record { + return Object.fromEntries( + Object.entries(headers).filter(([name]) => !HOP_BY_HOP_HEADERS.has(name.toLowerCase())), + ); +} + +// Only these codes may be sent in a close frame; 1005 and 1006 only describe a close. +function sendableCloseCode(code: number | undefined): number { + return code !== undefined && + (code === 1000 || + (code >= 1001 && code <= 1003) || + (code >= 1007 && code <= 1014) || + (code >= 3000 && code <= 4999)) + ? code + : 1000; +} + +const isWebSocketUpgrade = (request: HttpServerRequest.HttpServerRequest) => + request.headers.upgrade?.toLowerCase() === "websocket"; + +/** + * Carries environments with no public address: each one dials `/v1/environments/:id/channel`, + * and requests to its channel host (`RELAY_CHANNEL_HOST`) are forwarded over that socket. + * Channels live in this process, so a relay serving them must run as a single replica. + */ +export const withRelayChannels = (channelHost: string) => { + const channels = new Map(); + let nextId = 0; + + const dispatch = (channel: Channel, chunk: string | Uint8Array) => { + const frame = Option.getOrNull(parseFrame(chunk)); + if (frame?.type === "http-response") { + const response = channel.responses.get(frame.id); + return response ? Deferred.succeed(response, frame).pipe(Effect.asVoid) : Effect.void; + } + if (frame?.type === "ws-opened" || frame?.type === "ws-message" || frame?.type === "ws-close") { + const inbox = channel.sockets.get(frame.id); + return inbox ? Queue.offer(inbox, frame).pipe(Effect.asVoid) : Effect.void; + } + return Effect.void; + }; + + const serveChannel = Effect.fn("RelayChannels.serveChannel")(function* ( + environmentId: string, + request: HttpServerRequest.HttpServerRequest, + ) { + const key = yield* channelKey(environmentId); + const socket = yield* request.upgrade; + const writer = yield* socket.writer; + const reader = yield* socket.reader; + const channel: Channel = { + send: (frame) => writer.write(JSON.stringify(frame)).pipe(Effect.ignore), + responses: new Map(), + sockets: new Map(), + }; + channels.set(key, channel); + // Tells the environment its channel now receives traffic. + yield* channel.send({ type: "ping" }); + yield* Effect.logInfo("Relay channel connected", { environmentId }); + yield* Effect.addFinalizer(() => + Effect.gen(function* () { + if (channels.get(key) === channel) channels.delete(key); + for (const [id, response] of channel.responses) { + yield* Deferred.succeed(response, { + type: "http-response", + id, + status: 502, + headers: {}, + }); + } + for (const [id, inbox] of channel.sockets) { + yield* Queue.offer(inbox, { type: "ws-close", id, code: 1012 }); + } + yield* Effect.logInfo("Relay channel disconnected", { environmentId }); + }), + ); + yield* reader.pull.pipe( + Effect.flatMap((chunks) => + Effect.forEach(chunks, (chunk) => dispatch(channel, chunk), { discard: true }), + ), + Effect.forever, + Effect.ignore, + ); + return HttpServerResponse.empty(); + }, Effect.scoped); + + const forwardHttp = Effect.fn("RelayChannels.forwardHttp")(function* ( + channel: Channel, + request: HttpServerRequest.HttpServerRequest, + ) { + const id = ++nextId; + const body = new Uint8Array(yield* request.arrayBuffer); + const response = yield* Deferred.make(); + channel.responses.set(id, response); + yield* channel.send({ + type: "http-request", + id, + method: request.method, + path: request.url, + headers: withoutHopByHop(request.headers), + ...(body.byteLength > 0 ? { body: Buffer.from(body).toString("base64") } : {}), + }); + const result = yield* Deferred.await(response).pipe( + Effect.timeoutOption("60 seconds"), + Effect.ensuring(Effect.sync(() => channel.responses.delete(id))), + ); + if (Option.isNone(result)) { + return HttpServerResponse.text("The environment did not answer in time.", { status: 504 }); + } + const { "content-type": contentType, ...headers } = withoutHopByHop(result.value.headers); + return HttpServerResponse.uint8Array( + result.value.body ? Buffer.from(result.value.body, "base64") : new Uint8Array(), + { status: result.value.status, headers, ...(contentType ? { contentType } : {}) }, + ); + }); + + const forwardWebSocket = Effect.fn("RelayChannels.forwardWebSocket")(function* ( + channel: Channel, + request: HttpServerRequest.HttpServerRequest, + ) { + const id = ++nextId; + const inbox = yield* Queue.unbounded(); + channel.sockets.set(id, inbox); + yield* Effect.addFinalizer(() => Effect.sync(() => channel.sockets.delete(id))); + yield* channel.send({ + type: "ws-open", + id, + path: request.url, + headers: withoutHopByHop(request.headers), + }); + const opened = yield* Queue.take(inbox).pipe(Effect.timeoutOption("15 seconds")); + if (Option.isNone(opened) || opened.value.type !== "ws-opened") { + yield* channel.send({ type: "ws-close", id }); + return HttpServerResponse.text("The environment refused the WebSocket.", { status: 502 }); + } + const socket = yield* request.upgrade; + const writer = yield* socket.writer; + const reader = yield* socket.reader; + const toClient = Queue.take(inbox).pipe( + Effect.flatMap((frame) => + frame.type === "ws-message" + ? writer.write(frame.binary ? Buffer.from(frame.data, "base64") : frame.data) + : frame.type === "ws-close" + ? writer + .write(new Socket.CloseEvent(sendableCloseCode(frame.code), frame.reason)) + .pipe(Effect.andThen(Effect.fail("closed" as const))) + : Effect.void, + ), + Effect.forever, + ); + const toEnvironment = reader.pull.pipe( + Effect.flatMap((chunks) => + Effect.forEach( + chunks, + (chunk) => + channel.send( + typeof chunk === "string" + ? { type: "ws-message", id, data: chunk } + : { + type: "ws-message", + id, + data: Buffer.from(chunk).toString("base64"), + binary: true, + }, + ), + { discard: true }, + ), + ), + Effect.forever, + ); + yield* Effect.raceFirst(toClient, toEnvironment).pipe(Effect.ignore); + yield* channel.send({ type: "ws-close", id }); + return HttpServerResponse.empty(); + }, Effect.scoped); + + const authenticateChannel = Effect.fn("RelayChannels.authenticateChannel")(function* ( + request: HttpServerRequest.HttpServerRequest, + pathId: string, + ) { + const token = /^Bearer +(.+)$/iu.exec(request.headers.authorization ?? "")?.[1]; + if (token === undefined) return null; + const credentials = yield* EnvironmentCredentials.EnvironmentCredentials; + const principal = yield* credentials + .authenticate(token) + .pipe(Effect.orElseSucceed(() => Option.none())); + return Option.isSome(principal) && principal.value.environmentId === decodeURIComponent(pathId) + ? principal.value.environmentId + : null; + }); + + return (handler: Effect.Effect) => + Effect.gen(function* () { + const request = yield* HttpServerRequest.HttpServerRequest; + const channelPath = CHANNEL_PATH.exec(request.url); + if (channelPath?.[1] !== undefined && isWebSocketUpgrade(request)) { + const environmentId = yield* authenticateChannel(request, channelPath[1]); + if (environmentId === null) return HttpServerResponse.empty({ status: 401 }); + return yield* serveChannel(environmentId, request).pipe( + Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 })), + ); + } + const key = channelKeyForHost(channelHost, request.headers.host ?? ""); + if (key === null) return yield* handler; + const channel = channels.get(key); + if (channel === undefined) { + return HttpServerResponse.text("This environment is not connected to the relay.", { + status: 502, + }); + } + return yield* ( + isWebSocketUpgrade(request) + ? forwardWebSocket(channel, request) + : forwardHttp(channel, request) + ).pipe(Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 }))); + }); +}; diff --git a/infra/relay/src/host/RelayHost.ts b/infra/relay/src/host/RelayHost.ts index c6644557f56e9..20217005ff0ba 100644 --- a/infra/relay/src/host/RelayHost.ts +++ b/infra/relay/src/host/RelayHost.ts @@ -31,6 +31,8 @@ export interface RelayHost { readonly apnsJobSigningSecret: Effect.Effect>; /** Managed tunnel base domain; undefined disables managed endpoints. */ readonly managedEndpointBaseDomain: string | undefined; + /** Host template like `t3-*.example.com` for environments reached over a relay channel; undefined disables them. */ + readonly channelHost?: string | undefined; readonly database: RelayDb.RelayDb["Service"]; /** Tunnel and DNS clients behind managed endpoints; inert on hosts without managed tunnels. */ readonly managedEndpointClients: Layer.Layer< diff --git a/infra/relay/src/host/node.ts b/infra/relay/src/host/node.ts index 48c2ca47c70ce..2dc155b07ca18 100644 --- a/infra/relay/src/host/node.ts +++ b/infra/relay/src/host/node.ts @@ -34,6 +34,7 @@ export const withPublicOrigin = (relayPublicOrigin: string) => { export interface NodeHostOptions { readonly relayPublicOrigin: string; readonly stage: string; + readonly channelHost?: string | undefined; readonly mintKeyPair: { readonly privateKey: Redacted.Redacted; readonly publicKey: string; @@ -189,6 +190,7 @@ export const makeNodeHost = Effect.fn("relay.node.host")(function* (options: Nod ) : Effect.succeed(options.apnsJobSigningSecret), managedEndpointBaseDomain: undefined, + channelHost: options.channelHost, database: options.database, managedEndpointClients: Layer.mergeAll( Layer.succeed(ManagedEndpointProvider.ManagedEndpointTunnelClient, inertTunnelClient), diff --git a/infra/relay/src/node.ts b/infra/relay/src/node.ts index 441932b3c46b9..692acc6c67293 100644 --- a/infra/relay/src/node.ts +++ b/infra/relay/src/node.ts @@ -21,6 +21,7 @@ import * as SecureRandom from "effect/crypto/SecureRandom"; import { composeRelay } from "./composition.ts"; import { makeNodeHost, withPublicOrigin } from "./host/node.ts"; +import { withRelayChannels } from "./host/RelayChannels.ts"; const program = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -28,6 +29,12 @@ const program = Effect.gen(function* () { fs.readFileString(path).pipe(Effect.map((text) => text.trim())); const relayPublicOrigin = yield* Config.String("RELAY_PUBLIC_ORIGIN"); + const channelHost = Option.getOrUndefined( + Option.filter( + yield* Config.option(Config.String("RELAY_CHANNEL_HOST")), + (value) => value.trim().length > 0, + ), + )?.trim(); const migrationsFolder = yield* Config.String("RELAY_MIGRATIONS_FOLDER").pipe( Config.withDefault("migrations/postgres"), ); @@ -54,6 +61,7 @@ const program = Effect.gen(function* () { const host = yield* makeNodeHost({ relayPublicOrigin, stage: "selfhost", + channelHost, mintKeyPair: { privateKey: Redacted.make(mintPrivateKey), publicKey: mintPublicKey, @@ -75,9 +83,14 @@ const program = Effect.gen(function* () { const context = yield* Layer.build(relay.runtime); const handler = yield* relay.fetch.pipe(Effect.provideContext(context)); yield* HttpServer.serveEffect( - handler.pipe(withPublicOrigin(relayPublicOrigin), Effect.provideContext(context)), + handler.pipe( + withPublicOrigin(relayPublicOrigin), + // Outermost, so channel hosts are matched against the Host the proxy forwarded. + channelHost ? withRelayChannels(channelHost) : (effect) => effect, + Effect.provideContext(context), + ), ); - yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin }); + yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin, channelHost }); // Serving is scoped to this program; keep it alive until the process is stopped. return yield* Effect.never; }); diff --git a/packages/contracts/src/relay.ts b/packages/contracts/src/relay.ts index 5de2322ea6bf6..0bc772b3293dd 100644 --- a/packages/contracts/src/relay.ts +++ b/packages/contracts/src/relay.ts @@ -180,9 +180,56 @@ export const RelayManagedEndpointRuntimeConfig = Schema.Struct({ connectorToken: TrimmedNonEmptyString, tunnelId: Schema.optional(TrimmedNonEmptyString), tunnelName: Schema.optional(TrimmedNonEmptyString), + /** `t3_relay` only: the relay WebSocket the environment dials to carry its traffic. */ + channelUrl: Schema.optional(TrimmedNonEmptyString), }); export type RelayManagedEndpointRuntimeConfig = typeof RelayManagedEndpointRuntimeConfig.Type; +const RelayChannelHeaders = Schema.Record(Schema.String, Schema.String); + +/** + * Frames on a `t3_relay` channel: the relay forwards client HTTP requests and WebSockets to the + * environment over one socket the environment opened. Bodies and binary messages are base64. + */ +export const RelayChannelFrame = Schema.Union([ + Schema.Struct({ type: Schema.Literal("ping") }), + Schema.Struct({ + type: Schema.Literal("http-request"), + id: Schema.Int, + method: Schema.String, + path: Schema.String, + headers: RelayChannelHeaders, + body: Schema.optional(Schema.String), + }), + Schema.Struct({ + type: Schema.Literal("http-response"), + id: Schema.Int, + status: Schema.Int, + headers: RelayChannelHeaders, + body: Schema.optional(Schema.String), + }), + Schema.Struct({ + type: Schema.Literal("ws-open"), + id: Schema.Int, + path: Schema.String, + headers: RelayChannelHeaders, + }), + Schema.Struct({ type: Schema.Literal("ws-opened"), id: Schema.Int }), + Schema.Struct({ + type: Schema.Literal("ws-message"), + id: Schema.Int, + data: Schema.String, + binary: Schema.optional(Schema.Boolean), + }), + Schema.Struct({ + type: Schema.Literal("ws-close"), + id: Schema.Int, + code: Schema.optional(Schema.Int), + reason: Schema.optional(Schema.String), + }), +]); +export type RelayChannelFrame = typeof RelayChannelFrame.Type; + export const RelayManagedEndpointRecoveryRequest = Schema.Struct({ cloudUserId: TrimmedNonEmptyString, origin: RelayManagedEndpointOrigin, From dd82dfa58715518bd58fe2dd6e32540b501ce578 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 11:22:48 +0100 Subject: [PATCH 05/29] fix(relay): bind relay channels to the environment key and drop revoked ones Environment ids are public, so another keypair claiming the same id could take over its channel host. The channel host now covers the environment's public key, and the relay re-checks the credential on each ping. --- .../src/environments/EnvironmentConnector.ts | 5 +- .../src/environments/EnvironmentLinker.ts | 5 +- .../environments/RelayChannelEndpoint.test.ts | 8 +-- .../src/environments/RelayChannelEndpoint.ts | 12 +++-- infra/relay/src/host/RelayChannels.test.ts | 54 ++++++++++++------- infra/relay/src/host/RelayChannels.ts | 33 +++++++++--- 6 files changed, 84 insertions(+), 33 deletions(-) diff --git a/infra/relay/src/environments/EnvironmentConnector.ts b/infra/relay/src/environments/EnvironmentConnector.ts index c7de177dd76c1..8ea5315a559d6 100644 --- a/infra/relay/src/environments/EnvironmentConnector.ts +++ b/infra/relay/src/environments/EnvironmentConnector.ts @@ -465,7 +465,10 @@ const make = Effect.gen(function* () { channelHostname: settings.channelHost === undefined ? null - : channelHostname(settings.channelHost, yield* channelKey(input.link.environmentId)), + : channelHostname( + settings.channelHost, + yield* channelKey(input.link.environmentId, input.link.environmentPublicKey), + ), }); if (Result.isSuccess(result)) { return result.success; diff --git a/infra/relay/src/environments/EnvironmentLinker.ts b/infra/relay/src/environments/EnvironmentLinker.ts index fa2dcab68a856..361250781f16a 100644 --- a/infra/relay/src/environments/EnvironmentLinker.ts +++ b/infra/relay/src/environments/EnvironmentLinker.ts @@ -326,7 +326,10 @@ const make = Effect.gen(function* () { }), ) ? channelEndpoint( - channelHostname(config.channelHost, yield* channelKey(verified.environmentId)), + channelHostname( + config.channelHost, + yield* channelKey(verified.environmentId, verified.environmentPublicKey), + ), ) : null; const endpoint = provisioned?.endpoint ?? channel ?? verified.endpoint; diff --git a/infra/relay/src/environments/RelayChannelEndpoint.test.ts b/infra/relay/src/environments/RelayChannelEndpoint.test.ts index 09f110f727d9d..e931e868d136f 100644 --- a/infra/relay/src/environments/RelayChannelEndpoint.test.ts +++ b/infra/relay/src/environments/RelayChannelEndpoint.test.ts @@ -21,10 +21,12 @@ describe("channelKeyForHost", () => { describe("channelKey", () => { it.effect("gives each environment its own stable host label", () => Effect.gen(function* () { - const first = yield* channelKey("env-a"); + const first = yield* channelKey("env-a", "key-a"); expect(first).toMatch(/^[0-9a-f]{32}$/u); - expect(yield* channelKey("env-a")).toBe(first); - expect(yield* channelKey("env-b")).not.toBe(first); + expect(yield* channelKey("env-a", "key-a")).toBe(first); + expect(yield* channelKey("env-b", "key-a")).not.toBe(first); + // Environment ids are public; another key claiming the same id gets another host. + expect(yield* channelKey("env-a", "key-b")).not.toBe(first); expect( channelKeyForHost("t3-*.example.test", channelHostname("t3-*.example.test", first)), ).toBe(first); diff --git a/infra/relay/src/environments/RelayChannelEndpoint.ts b/infra/relay/src/environments/RelayChannelEndpoint.ts index 876380f9a6535..71ec9e53eaa16 100644 --- a/infra/relay/src/environments/RelayChannelEndpoint.ts +++ b/infra/relay/src/environments/RelayChannelEndpoint.ts @@ -3,10 +3,16 @@ import * as Effect from "effect/Effect"; const CHANNEL_KEY_PATTERN = /^[0-9a-f]{32}$/u; -/** Host label for an environment's relay channel; hashed so the host does not reveal the environment id. */ -export const channelKey = (environmentId: string) => +/** + * Host label for an environment's relay channel. Environment ids are public, so the key also + * covers the environment's public key: only the holder of a credential for that key can serve it. + */ +export const channelKey = (environmentId: string, environmentPublicKey: string) => Effect.promise(() => - crypto.subtle.digest("SHA-256", new TextEncoder().encode(`t3-relay-channel:${environmentId}`)), + crypto.subtle.digest( + "SHA-256", + new TextEncoder().encode(`t3-relay-channel:${environmentId}:${environmentPublicKey.trim()}`), + ), ).pipe( Effect.map((digest) => Array.from(new Uint8Array(digest).slice(0, 16), (byte) => diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index 6831a2f3ef091..7ab7e074a10dc 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -1,3 +1,4 @@ +// @effect-diagnostics nodeBuiltinImport:off - fetch forbids the Host header these requests route by. import * as NodeHttp from "node:http"; import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; @@ -14,19 +15,20 @@ import { withRelayChannels } from "./RelayChannels.ts"; const CHANNEL_HOST = "t3-*.relay.test"; -const layer = Layer.mergeAll( - NodeHttpServer.layer(() => NodeHttp.createServer(), { port: 0, host: "127.0.0.1" }), - Layer.succeed(EnvironmentCredentials.EnvironmentCredentials, { - create: () => Effect.die("unused"), - authenticate: (token) => - Effect.succeed( - token === "good" - ? Option.some({ credentialId: "c", environmentId: "env-1", environmentPublicKey: "k" }) - : Option.none(), - ), - revokeForEnvironmentPublicKey: () => Effect.die("unused"), - }), -); +const makeLayer = (credentialValid: () => boolean = () => true) => + Layer.mergeAll( + NodeHttpServer.layer(() => NodeHttp.createServer(), { port: 0, host: "127.0.0.1" }), + Layer.succeed(EnvironmentCredentials.EnvironmentCredentials, { + create: () => Effect.die("unused"), + authenticate: (token) => + Effect.sync(() => + token === "good" && credentialValid() + ? Option.some({ credentialId: "c", environmentId: "env-1", environmentPublicKey: "k" }) + : Option.none(), + ), + revokeForEnvironmentPublicKey: () => Effect.die("unused"), + }), + ); const serveRelay = Effect.gen(function* () { yield* HttpServer.serveEffect( @@ -98,7 +100,7 @@ describe("withRelayChannels", () => { it.live("forwards a channel host to its environment and leaves other hosts to the relay", () => Effect.gen(function* () { const port = yield* serveRelay; - const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1")); + const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); yield* connectEnvironment(port, "good"); expect(yield* get(port, host, "/api/state?x=1")).toEqual({ @@ -107,22 +109,38 @@ describe("withRelayChannels", () => { env: "yes", }); expect(yield* get(port, "relay.test")).toEqual({ status: 200, body: "relay api" }); - }).pipe(Effect.scoped, Effect.provide(layer)), + }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); it.live("answers 502 for an environment that has no channel open", () => Effect.gen(function* () { const port = yield* serveRelay; - const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1")); + const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); expect((yield* get(port, host)).status).toBe(502); - }).pipe(Effect.provide(layer)), + }).pipe(Effect.provide(makeLayer())), ); + it.live("closes a channel once its credential is revoked", () => { + let valid = true; + return Effect.gen(function* () { + const port = yield* serveRelay; + const socket = yield* connectEnvironment(port, "good"); + valid = false; + const closed = Effect.callback((resume) => + socket.addEventListener("close", () => resume(Effect.void)), + ); + socket.send(JSON.stringify({ type: "ping" })); + yield* closed; + const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); + expect((yield* get(port, host)).status).toBe(502); + }).pipe(Effect.scoped, Effect.provide(makeLayer(() => valid))); + }); + it.live("refuses a channel opened with another credential", () => Effect.gen(function* () { const port = yield* serveRelay; const opened = yield* Effect.exit(connectEnvironment(port, "bad")); expect(opened._tag).toBe("Failure"); - }).pipe(Effect.scoped, Effect.provide(layer)), + }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); }); diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index 8d340b63c2c6a..1efb73c78717c 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -88,10 +88,13 @@ export const withRelayChannels = (channelHost: string) => { }; const serveChannel = Effect.fn("RelayChannels.serveChannel")(function* ( - environmentId: string, + principal: EnvironmentCredentials.EnvironmentCredentialPrincipal, + token: string, request: HttpServerRequest.HttpServerRequest, ) { - const key = yield* channelKey(environmentId); + const { environmentId } = principal; + const credentials = yield* EnvironmentCredentials.EnvironmentCredentials; + const key = yield* channelKey(environmentId, principal.environmentPublicKey); const socket = yield* request.upgrade; const writer = yield* socket.writer; const reader = yield* socket.reader; @@ -121,9 +124,25 @@ export const withRelayChannels = (channelHost: string) => { yield* Effect.logInfo("Relay channel disconnected", { environmentId }); }), ); + // The environment pings every 25 seconds; a revoked credential closes its channel. + const stillAuthorized = credentials.authenticate(token).pipe( + Effect.map((current) => + Option.exists(current, (now) => now.credentialId === principal.credentialId), + ), + Effect.orElseSucceed(() => true), + ); yield* reader.pull.pipe( Effect.flatMap((chunks) => - Effect.forEach(chunks, (chunk) => dispatch(channel, chunk), { discard: true }), + Effect.forEach( + chunks, + (chunk) => + Option.getOrNull(parseFrame(chunk))?.type === "ping" + ? stillAuthorized.pipe( + Effect.flatMap((ok) => (ok ? Effect.void : Effect.fail("revoked" as const))), + ) + : dispatch(channel, chunk), + { discard: true }, + ), ), Effect.forever, Effect.ignore, @@ -231,7 +250,7 @@ export const withRelayChannels = (channelHost: string) => { .authenticate(token) .pipe(Effect.orElseSucceed(() => Option.none())); return Option.isSome(principal) && principal.value.environmentId === decodeURIComponent(pathId) - ? principal.value.environmentId + ? { principal: principal.value, token } : null; }); @@ -240,9 +259,9 @@ export const withRelayChannels = (channelHost: string) => { const request = yield* HttpServerRequest.HttpServerRequest; const channelPath = CHANNEL_PATH.exec(request.url); if (channelPath?.[1] !== undefined && isWebSocketUpgrade(request)) { - const environmentId = yield* authenticateChannel(request, channelPath[1]); - if (environmentId === null) return HttpServerResponse.empty({ status: 401 }); - return yield* serveChannel(environmentId, request).pipe( + const authorized = yield* authenticateChannel(request, channelPath[1]); + if (authorized === null) return HttpServerResponse.empty({ status: 401 }); + return yield* serveChannel(authorized.principal, authorized.token, request).pipe( Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 })), ); } From 9084db5a95bf0785a3bc9ad7a8654b7f6a89661d Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 11:53:37 +0100 Subject: [PATCH 06/29] feat(relay): serve relay channels under /env/ instead of subdomains Channel subdomains needed wildcard DNS, a wildcard certificate and a proxy rule. The relay now serves each carried environment at RELAY_PUBLIC_ORIGIN/env// when RELAY_CHANNELS=true, strips the prefix and names it in X-Forwarded-Prefix. Clients keep an environment's base path when resolving its URLs, the server checks DPoP proofs against the prefixed URL, and the credential recheck runs on the relay's own timer. --- .../src/features/cloud/linkEnvironment.ts | 5 +- apps/server/src/auth/dpop.ts | 4 +- apps/server/src/requestUrl.test.ts | 27 +++++ apps/server/src/requestUrl.ts | 17 +++ apps/web/src/cloud/linkEnvironment.ts | 5 +- docs/user/remote-access.md | 2 +- infra/relay/selfhost/.env.example | 7 +- infra/relay/selfhost/README.md | 17 ++- infra/relay/selfhost/docker-compose.yml | 2 +- infra/relay/src/Config.ts | 3 +- infra/relay/src/composition.ts | 2 +- .../environments/EnvironmentConnector.test.ts | 37 +++--- .../src/environments/EnvironmentConnector.ts | 20 ++-- .../environments/EnvironmentLinker.test.ts | 14 +-- .../src/environments/EnvironmentLinker.ts | 10 +- .../environments/RelayChannelEndpoint.test.ts | 40 ++++--- .../src/environments/RelayChannelEndpoint.ts | 38 +++--- infra/relay/src/host/RelayChannels.test.ts | 111 ++++++++---------- infra/relay/src/host/RelayChannels.ts | 78 ++++++------ infra/relay/src/host/RelayHost.ts | 4 +- infra/relay/src/host/node.ts | 4 +- infra/relay/src/node.ts | 17 +-- .../src/connection/presentation.ts | 9 +- .../src/environment/endpoint.test.ts | 42 ++++++- .../src/environment/endpoint.ts | 5 +- packages/client-runtime/src/rpc/http.ts | 2 +- packages/client-runtime/src/state/assets.ts | 8 +- packages/client-runtime/src/webhookAddress.ts | 3 +- packages/shared/src/advertisedEndpoint.ts | 10 ++ packages/shared/src/preview.ts | 3 +- 30 files changed, 325 insertions(+), 221 deletions(-) create mode 100644 apps/server/src/requestUrl.test.ts diff --git a/apps/mobile/src/features/cloud/linkEnvironment.ts b/apps/mobile/src/features/cloud/linkEnvironment.ts index 1e16b626ca483..9e83ed719dfaf 100644 --- a/apps/mobile/src/features/cloud/linkEnvironment.ts +++ b/apps/mobile/src/features/cloud/linkEnvironment.ts @@ -137,7 +137,10 @@ function ensureLinkedEnvironmentMatches(input: { message: "Relay returned credentials for a different environment.", }); } - if (input.link.endpoint.providerKind !== input.expectedProviderKind) { + const { providerKind } = input.link.endpoint; + // A self-hosted relay may carry a publish-only environment over its own channel. + const carriedByRelay = input.expectedProviderKind === "manual" && providerKind === "t3_relay"; + if (providerKind !== input.expectedProviderKind && !carriedByRelay) { return new CloudEnvironmentLinkError({ message: "Relay returned credentials for a different endpoint provider.", }); diff --git a/apps/server/src/auth/dpop.ts b/apps/server/src/auth/dpop.ts index 7cae6bee393c9..6fc93d3e267a8 100644 --- a/apps/server/src/auth/dpop.ts +++ b/apps/server/src/auth/dpop.ts @@ -17,7 +17,7 @@ import { type ServerAuthInternalError, } from "./EnvironmentAuth.ts"; import * as ServerSecretStore from "./ServerSecretStore.ts"; -import { requestUrl } from "../requestUrl.ts"; +import { publicRequestUrl } from "../requestUrl.ts"; export const DPOP_REPLAY_MARKER_PREFIX = "dpop-proof-"; @@ -61,7 +61,7 @@ export const verifyRequestDpopProof = Effect.fn("dpop.verifyRequestDpopProof")(f readonly expectedAccessToken?: string; }) { const proof = input.proof ?? input.request.headers.dpop; - const url = requestUrl(input.request); + const url = publicRequestUrl(input.request); if (Option.isNone(url)) { return yield* new ServerAuthInvalidCredentialError({ diagnostic: "Invalid DPoP request URL.", diff --git a/apps/server/src/requestUrl.test.ts b/apps/server/src/requestUrl.test.ts new file mode 100644 index 0000000000000..1d5e7593eafc6 --- /dev/null +++ b/apps/server/src/requestUrl.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from "@effect/vitest"; +import * as Option from "effect/Option"; +import { Headers, HttpServerRequest } from "effect/http"; + +import { publicRequestUrl } from "./requestUrl.ts"; + +const relayed = (prefix: string | undefined) => + HttpServerRequest.fromWeb(new Request("http://relay.example.test/api/state?x=1")).modify({ + headers: Headers.fromInput({ + host: "relay.example.test", + "x-forwarded-proto": "https", + ...(prefix === undefined ? {} : { "x-forwarded-prefix": prefix }), + }), + }); + +describe("publicRequestUrl", () => { + it.each([ + ["a relay prefix", "/env/abc", "https://relay.example.test/env/abc/api/state?x=1"], + ["no prefix", undefined, "https://relay.example.test/api/state?x=1"], + ["a protocol-relative prefix", "//evil.test", "https://relay.example.test/api/state?x=1"], + ["a trailing slash", "/env/abc/", "https://relay.example.test/api/state?x=1"], + ] as const)("keeps %s", (_name, prefix, expected) => { + expect(Option.map(publicRequestUrl(relayed(prefix)), (url) => url.href)).toEqual( + Option.some(expected), + ); + }); +}); diff --git a/apps/server/src/requestUrl.ts b/apps/server/src/requestUrl.ts index a1a0371c5f97f..3af122589aca4 100644 --- a/apps/server/src/requestUrl.ts +++ b/apps/server/src/requestUrl.ts @@ -12,3 +12,20 @@ export const requestUrl = (request: HttpServerRequest.HttpServerRequest): Option ? request.modify({ protocol: "https" }) : request, ); + +const FORWARDED_PREFIX = /^\/(?!\/)[^?#\s]*[^/?#\s]$/u; + +/** + * The URL the client addressed. A path-routing relay serves this server under a prefix such as + * `/env/` and strips it, saying so in `X-Forwarded-Prefix`; proofs sign the full URL. + */ +export const publicRequestUrl = ( + request: HttpServerRequest.HttpServerRequest, +): Option.Option => + Option.map(requestUrl(request), (url) => { + const prefix = request.headers["x-forwarded-prefix"]; + if (prefix !== undefined && FORWARDED_PREFIX.test(prefix)) { + url.pathname = `${prefix}${url.pathname}`; + } + return url; + }); diff --git a/apps/web/src/cloud/linkEnvironment.ts b/apps/web/src/cloud/linkEnvironment.ts index c474ed0fa6c8f..e98fd47fc2eee 100644 --- a/apps/web/src/cloud/linkEnvironment.ts +++ b/apps/web/src/cloud/linkEnvironment.ts @@ -172,7 +172,10 @@ function ensureLinkedEnvironmentMatches(input: { message: "Relay returned credentials for a different environment.", }); } - if (input.link.endpoint.providerKind !== input.expectedProviderKind) { + const { providerKind } = input.link.endpoint; + // A self-hosted relay may carry a publish-only environment over its own channel. + const carriedByRelay = input.expectedProviderKind === "manual" && providerKind === "t3_relay"; + if (providerKind !== input.expectedProviderKind && !carriedByRelay) { return new CloudEnvironmentLinkError({ message: "Relay returned credentials for a different endpoint provider.", }); diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index 8eceeb234ac2e..b1b030c8a9998 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -77,7 +77,7 @@ Choose **T3 Connect** there to switch back. Environments you linked through your relay stay linked to it until you turn T3 Connect off and on again. A self-hosted relay has no managed tunnels. If the relay sets -`RELAY_CHANNEL_HOST`, a linked environment with no public address is carried +`RELAY_CHANNELS=true`, a linked environment with no public address is carried through the relay itself, so there is nothing to expose. Otherwise your other devices reach it only at an HTTPS address you expose yourself, such as a reverse proxy or [Tailscale HTTPS](#tailscale-https): start the host with that address in diff --git a/infra/relay/selfhost/.env.example b/infra/relay/selfhost/.env.example index ce67718c45a24..510884928a30f 100644 --- a/infra/relay/selfhost/.env.example +++ b/infra/relay/selfhost/.env.example @@ -15,10 +15,9 @@ OIDC_AUDIENCE=t3-code # addresses, "private" also LAN/Tailscale addresses, "off" refuses them. RELAY_MANUAL_ENDPOINTS=public -# Optional: carry environments with no public address through the relay itself. Each one gets -# the host this pattern names, with `*` replaced by a per-environment key. Needs wildcard DNS -# and a wildcard certificate at your proxy (see README). Leave empty to turn it off. -# RELAY_CHANNEL_HOST=t3-*.example.com +# Optional: carry environments with no public address through the relay itself, at +# RELAY_PUBLIC_ORIGIN/env//. No DNS or certificate beyond the relay's own. +# RELAY_CHANNELS=true POSTGRES_PASSWORD=change-me diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md index 7e4d7556c1293..b79dcbc6fd1c7 100644 --- a/infra/relay/selfhost/README.md +++ b/infra/relay/selfhost/README.md @@ -97,14 +97,13 @@ them to each app's prefix). A configured OpenID Connect provider replaces Clerk ## Reach an environment without a tunnel -**Through the relay (no address to expose).** Set `RELAY_CHANNEL_HOST` to a host pattern such as -`t3-*.example.com`. An environment linked without its own public address then keeps one outbound -WebSocket open to the relay, and the relay serves it at `https://t3-.example.com`, forwarding -HTTP and WebSocket traffic over that socket. Nothing to open on the environment's network. Route -the pattern to the relay at your proxy with a wildcard DNS record and a wildcard certificate (the -bundled Caddy serves only `RELAY_DOMAIN`; a wildcard certificate needs Caddy's DNS challenge). -All of that environment's traffic passes through the relay host, and channels live in the relay -process, so keep a single replica. +**Through the relay (no address to expose).** Set `RELAY_CHANNELS=true`. An environment linked +without its own public address then keeps one outbound WebSocket open to the relay, and the relay +serves it at `RELAY_PUBLIC_ORIGIN/env//`, forwarding HTTP and WebSocket traffic over that +socket with the prefix stripped and named in `X-Forwarded-Prefix`. Nothing to open on the +environment's network and no extra DNS or certificate. All of that environment's traffic passes +through the relay host, and channels live in the relay process, so keep a single replica. Clients +need a T3 Code version that keeps an environment's path prefix. **At an address you expose.** With `RELAY_MANUAL_ENDPOINTS=public` (the default here), an environment can publish an HTTPS address you already expose (reverse proxy, port forward) and the @@ -127,7 +126,7 @@ you control. A published address takes precedence over a relay channel. Caddy is the bundled proxy. To use your own (nginx, Traefik, a cloud load balancer), drop the `caddy` service, point your proxy at the relay's port 8080, and keep `RELAY_PUBLIC_ORIGIN` at the public HTTPS address. The relay builds no URLs from request headers, so `X-Forwarded-*` -values are ignored. It reads `Host` only to match `RELAY_CHANNEL_HOST`, so pass it through. `OIDC_ISSUER` must equal the issuer your provider reports +values are ignored. `OIDC_ISSUER` must equal the issuer your provider reports publicly, including any path prefix your proxy adds. ## What this host does not do diff --git a/infra/relay/selfhost/docker-compose.yml b/infra/relay/selfhost/docker-compose.yml index 1af20ec34c5cd..1c6d3ab6887f5 100644 --- a/infra/relay/selfhost/docker-compose.yml +++ b/infra/relay/selfhost/docker-compose.yml @@ -32,7 +32,7 @@ services: OIDC_ISSUER: ${OIDC_ISSUER:?set OIDC_ISSUER in .env} OIDC_AUDIENCE: ${OIDC_AUDIENCE:?set OIDC_AUDIENCE in .env} RELAY_MANUAL_ENDPOINTS: ${RELAY_MANUAL_ENDPOINTS:-public} - RELAY_CHANNEL_HOST: ${RELAY_CHANNEL_HOST:-} + RELAY_CHANNELS: ${RELAY_CHANNELS:-false} CLOUD_MINT_PRIVATE_KEY_FILE: /run/secrets/cloud_mint_private_key CLOUD_MINT_PUBLIC_KEY_FILE: /run/secrets/cloud_mint_public_key # Push notifications need the publisher's APNs and FCM credentials; leave them off here. diff --git a/infra/relay/src/Config.ts b/infra/relay/src/Config.ts index 55eed65a595ac..085472ac1a2c8 100644 --- a/infra/relay/src/Config.ts +++ b/infra/relay/src/Config.ts @@ -87,7 +87,8 @@ export class RelayConfiguration extends Context.Service< readonly cloudMintPrivateKey: Redacted.Redacted; readonly cloudMintPublicKey: string; readonly managedEndpointBaseDomain: string | undefined; - readonly channelHost?: string; + /** Carries publish-only environments over relay channels; self-hosted Node relays only. */ + readonly channels?: boolean; readonly managedEndpointNamespace: string | undefined; readonly managedEndpointCleanupMode?: ManagedEndpointCleanupMode; readonly legacyManagedEndpointCleanupMode?: ManagedEndpointCleanupMode; diff --git a/infra/relay/src/composition.ts b/infra/relay/src/composition.ts index ffd97fb91c180..fa4e78ce2ab00 100644 --- a/infra/relay/src/composition.ts +++ b/infra/relay/src/composition.ts @@ -183,7 +183,7 @@ export const composeRelay = Effect.fn("relay.composition.compose")(function* cloudMintPrivateKey: yield* cloudMintPrivateKey, cloudMintPublicKey: yield* cloudMintPublicKey, managedEndpointBaseDomain: host.managedEndpointBaseDomain, - ...(host.channelHost ? { channelHost: host.channelHost } : {}), + ...(host.channels ? { channels: true } : {}), managedEndpointNamespace: stage, managedEndpointCleanupMode, legacyManagedEndpointCleanupMode, diff --git a/infra/relay/src/environments/EnvironmentConnector.test.ts b/infra/relay/src/environments/EnvironmentConnector.test.ts index c55fd19c39747..6f492f98cacb9 100644 --- a/infra/relay/src/environments/EnvironmentConnector.test.ts +++ b/infra/relay/src/environments/EnvironmentConnector.test.ts @@ -798,33 +798,32 @@ describe("validateManualEndpoint", () => { }); describe("validateConnectEndpoint with relay channels", () => { - const link = (hostname: string) => - ({ - environmentId: "env-connector-test" as never, - label: "Channel Environment", - endpoint: { - httpBaseUrl: `https://${hostname}/`, - wsBaseUrl: `wss://${hostname}/ws`, - providerKind: "t3_relay", - }, - linkedAt: "2026-05-25T00:00:00.000Z", - environmentPublicKey: environmentKeyPair.publicKey, - }) satisfies EnvironmentLinks.RelayLinkedEnvironmentRecord; - const own = `t3-${"a".repeat(32)}.relay.example.test`; - const validate = (hostname: string, channelHostname: string | null) => + const endpoint = (key: string) => ({ + httpBaseUrl: `https://relay.example.test/env/${key}/`, + wsBaseUrl: `wss://relay.example.test/env/${key}/ws`, + providerKind: "t3_relay" as const, + }); + const own = endpoint("a".repeat(32)); + const validate = (linked: ReturnType, channel: typeof own | null) => EnvironmentConnector.validateConnectEndpoint({ - link: link(hostname), + link: { + environmentId: "env-connector-test" as never, + label: "Channel Environment", + endpoint: linked, + linkedAt: "2026-05-25T00:00:00.000Z", + environmentPublicKey: environmentKeyPair.publicKey, + }, allocation: null, baseDomain: undefined, manualEndpoints: "off", - channelHostname, + channelEndpoint: channel, }); it.each([ - ["another environment's channel", `t3-${"b".repeat(32)}.relay.example.test`, own], + ["another environment's channel", endpoint("b".repeat(32)), own], ["a relay without channels", own, null], - ] as const)("rejects %s", (_name, hostname, channelHostname) => { - expect(Result.isFailure(validate(hostname, channelHostname))).toBe(true); + ] as const)("rejects %s", (_name, linked, channel) => { + expect(Result.isFailure(validate(linked, channel))).toBe(true); }); it("accepts the environment's own channel", () => { diff --git a/infra/relay/src/environments/EnvironmentConnector.ts b/infra/relay/src/environments/EnvironmentConnector.ts index 8ea5315a559d6..f3e411f7a0cb6 100644 --- a/infra/relay/src/environments/EnvironmentConnector.ts +++ b/infra/relay/src/environments/EnvironmentConnector.ts @@ -46,7 +46,7 @@ import * as HttpClientError from "effect/http/HttpClientError"; import { diagnoseEndpointFailure } from "./endpointFailure.ts"; import * as EnvironmentLinks from "./EnvironmentLinks.ts"; import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts"; -import { channelEndpoint, channelHostname, channelKey } from "./RelayChannelEndpoint.ts"; +import { channelEndpoint, channelKey } from "./RelayChannelEndpoint.ts"; import * as RelayConfiguration from "../Config.ts"; import * as CloudMintSigner from "../auth/CloudMintSigner.ts"; import { isManagedEndpointHostname } from "../deploymentConfig.ts"; @@ -419,11 +419,11 @@ export function validateConnectEndpoint(input: { readonly allocation: ManagedEndpointAllocations.ManagedEndpointAllocation | null; readonly baseDomain: string | undefined; readonly manualEndpoints: RelayConfiguration.ManualEndpointsMode; - /** This environment's relay channel host, or null when the relay carries no channels. */ - readonly channelHostname?: string | null; + /** Where this relay carries this environment, or null when the relay carries no channels. */ + readonly channelEndpoint?: RelayManagedEndpoint | null; }): Result.Result { if (input.link.endpoint.providerKind === "t3_relay") { - const expected = input.channelHostname ? channelEndpoint(input.channelHostname) : null; + const expected = input.channelEndpoint ?? null; return expected !== null && input.link.endpoint.httpBaseUrl === expected.httpBaseUrl && input.link.endpoint.wsBaseUrl === expected.wsBaseUrl @@ -462,13 +462,13 @@ const make = Effect.gen(function* () { allocation: input.allocation, baseDomain: settings.managedEndpointBaseDomain, manualEndpoints: settings.manualEndpoints ?? "off", - channelHostname: - settings.channelHost === undefined - ? null - : channelHostname( - settings.channelHost, + channelEndpoint: + settings.channels === true + ? channelEndpoint( + settings.relayIssuer, yield* channelKey(input.link.environmentId, input.link.environmentPublicKey), - ), + ) + : null, }); if (Result.isSuccess(result)) { return result.success; diff --git a/infra/relay/src/environments/EnvironmentLinker.test.ts b/infra/relay/src/environments/EnvironmentLinker.test.ts index f50045f143e36..d4bdd7f408e12 100644 --- a/infra/relay/src/environments/EnvironmentLinker.test.ts +++ b/infra/relay/src/environments/EnvironmentLinker.test.ts @@ -159,7 +159,7 @@ const makePublishOnlyRequest = Effect.gen(function* () { }); function layerTest(input?: { - readonly channelHost?: string; + readonly channels?: boolean; readonly upsert?: EnvironmentLinks.EnvironmentLinks["Service"]["upsert"]; readonly consume?: DpopProofs.DpopProofReplay["Service"]["consume"]; readonly deprovision?: ManagedEndpointProvider.ManagedEndpointProvider["Service"]["deprovision"]; @@ -168,9 +168,7 @@ function layerTest(input?: { Layer.provideMerge(RelayTokens.layer.pipe(Layer.provide(CloudMintSigner.layer))), Layer.provide( Layer.mergeAll( - RelayConfiguration.layer( - input?.channelHost ? { ...config, channelHost: input.channelHost } : config, - ), + RelayConfiguration.layer(input?.channels ? { ...config, channels: true } : config), Layer.succeed(DpopProofs.DpopProofReplay, { verifyAndConsume: () => Effect.die("unexpected DPoP proof verification"), consume: input?.consume ?? (() => Effect.succeed(true)), @@ -271,9 +269,11 @@ describe("EnvironmentLinker", () => { const result = yield* linker.link({ userId: "user_123", request }); expect(result.endpoint).toEqual({ httpBaseUrl: expect.stringMatching( - /^https:\/\/t3-[0-9a-f]{32}\.relay\.example\.test\/$/u, + /^https:\/\/relay\.example\.test\/env\/[0-9a-f]{32}\/$/u, + ), + wsBaseUrl: expect.stringMatching( + /^wss:\/\/relay\.example\.test\/env\/[0-9a-f]{32}\/ws$/u, ), - wsBaseUrl: expect.stringMatching(/^wss:\/\/t3-[0-9a-f]{32}\.relay\.example\.test\/ws$/u), providerKind: "t3_relay", }); expect(persistedEndpoint).toBe(result.endpoint.httpBaseUrl); @@ -285,7 +285,7 @@ describe("EnvironmentLinker", () => { }).pipe( Effect.provide( layerTest({ - channelHost: "t3-*.relay.example.test", + channels: true, upsert: (input) => Effect.sync(() => { persistedEndpoint = input.endpoint.httpBaseUrl; diff --git a/infra/relay/src/environments/EnvironmentLinker.ts b/infra/relay/src/environments/EnvironmentLinker.ts index 361250781f16a..1c59cc73b057f 100644 --- a/infra/relay/src/environments/EnvironmentLinker.ts +++ b/infra/relay/src/environments/EnvironmentLinker.ts @@ -23,7 +23,7 @@ import { validateManualEndpoint } from "./EnvironmentConnector.ts"; import * as EnvironmentCredentials from "./EnvironmentCredentials.ts"; import * as EnvironmentLinks from "./EnvironmentLinks.ts"; import * as ManagedEndpointProvider from "./ManagedEndpointProvider.ts"; -import { channelEndpoint, channelHostname, channelKey } from "./RelayChannelEndpoint.ts"; +import { channelEndpoint, channelKey } from "./RelayChannelEndpoint.ts"; import * as RelayConfiguration from "../Config.ts"; export class EnvironmentLinkProofExpired extends Schema.TaggedError()( @@ -318,7 +318,7 @@ const make = Effect.gen(function* () { // A publish-only link with no address the relay may contact is carried over a relay channel. const channel = !input.request.managedTunnelsEnabled && - config.channelHost !== undefined && + config.channels === true && Result.isFailure( validateManualEndpoint({ endpoint: verified.endpoint, @@ -326,10 +326,8 @@ const make = Effect.gen(function* () { }), ) ? channelEndpoint( - channelHostname( - config.channelHost, - yield* channelKey(verified.environmentId, verified.environmentPublicKey), - ), + config.relayIssuer, + yield* channelKey(verified.environmentId, verified.environmentPublicKey), ) : null; const endpoint = provisioned?.endpoint ?? channel ?? verified.endpoint; diff --git a/infra/relay/src/environments/RelayChannelEndpoint.test.ts b/infra/relay/src/environments/RelayChannelEndpoint.test.ts index e931e868d136f..0c7a941e1e5ab 100644 --- a/infra/relay/src/environments/RelayChannelEndpoint.test.ts +++ b/infra/relay/src/environments/RelayChannelEndpoint.test.ts @@ -1,35 +1,45 @@ import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; -import { channelHostname, channelKey, channelKeyForHost } from "./RelayChannelEndpoint.ts"; +import { channelEndpoint, channelKey, channelPathTarget } from "./RelayChannelEndpoint.ts"; const key = "0123456789abcdef0123456789abcdef"; -describe("channelKeyForHost", () => { +describe("channelPathTarget", () => { it.each([ - ["the channel host", `t3-${key}.relay.example.test`, key], - ["a forwarded port and mixed case", `T3-${key.toUpperCase()}.Relay.Example.Test:443`, key], - ["the relay's own host", "relay.example.test", null], - ["a short key", "t3-abc.relay.example.test", null], - ["a nested label", `x.t3-${key}.relay.example.test`, null], - ["a lookalike domain", `t3-${key}.relay.example.test.evil.test`, null], - ] as const)("reads %s", (_name, host, expected) => { - expect(channelKeyForHost("t3-*.relay.example.test", host)).toBe(expected); + [ + "a nested path", + `/env/${key}/api/state?x=1`, + { key, prefix: `/env/${key}`, path: "/api/state?x=1" }, + ], + ["the bare prefix", `/env/${key}`, { key, prefix: `/env/${key}`, path: "/" }], + [ + "a query on the prefix", + `/env/${key}?ticket=t`, + { key, prefix: `/env/${key}`, path: "/?ticket=t" }, + ], + ["a relay route", "/v1/environments/env-1/connect", null], + ["a short key", "/env/abc/ws", null], + ["a longer segment", `/env/${key}0/ws`, null], + ] as const)("reads %s", (_name, url, expected) => { + expect(channelPathTarget(url)).toEqual(expected); }); }); describe("channelKey", () => { - it.effect("gives each environment its own stable host label", () => + it.effect("gives each environment key its own stable path", () => Effect.gen(function* () { const first = yield* channelKey("env-a", "key-a"); expect(first).toMatch(/^[0-9a-f]{32}$/u); expect(yield* channelKey("env-a", "key-a")).toBe(first); expect(yield* channelKey("env-b", "key-a")).not.toBe(first); - // Environment ids are public; another key claiming the same id gets another host. + // Environment ids are public; another key claiming the same id gets another path. expect(yield* channelKey("env-a", "key-b")).not.toBe(first); - expect( - channelKeyForHost("t3-*.example.test", channelHostname("t3-*.example.test", first)), - ).toBe(first); + expect(channelEndpoint("https://relay.example.test", first)).toEqual({ + httpBaseUrl: `https://relay.example.test/env/${first}/`, + wsBaseUrl: `wss://relay.example.test/env/${first}/ws`, + providerKind: "t3_relay", + }); }), ); }); diff --git a/infra/relay/src/environments/RelayChannelEndpoint.ts b/infra/relay/src/environments/RelayChannelEndpoint.ts index 71ec9e53eaa16..e5066de4a178d 100644 --- a/infra/relay/src/environments/RelayChannelEndpoint.ts +++ b/infra/relay/src/environments/RelayChannelEndpoint.ts @@ -1,10 +1,10 @@ import type { RelayManagedEndpoint } from "@t3tools/contracts/relay"; import * as Effect from "effect/Effect"; -const CHANNEL_KEY_PATTERN = /^[0-9a-f]{32}$/u; +const CHANNEL_PATH = /^\/env\/([0-9a-f]{32})(?=[/?]|$)(.*)$/su; /** - * Host label for an environment's relay channel. Environment ids are public, so the key also + * Path key for an environment's relay channel. Environment ids are public, so the key also * covers the environment's public key: only the holder of a credential for that key can serve it. */ export const channelKey = (environmentId: string, environmentPublicKey: string) => @@ -21,25 +21,27 @@ export const channelKey = (environmentId: string, environmentPublicKey: string) ), ); -export function channelHostname(template: string, key: string): string { - return template.toLowerCase().replace("*", key); -} - -export function channelEndpoint(hostname: string): RelayManagedEndpoint { +/** Where clients reach an environment carried by this relay: `/env//`. */ +export function channelEndpoint(relayOrigin: string, key: string): RelayManagedEndpoint { + const origin = new URL(relayOrigin); + const wsProtocol = origin.protocol === "https:" ? "wss:" : "ws:"; return { - httpBaseUrl: `https://${hostname}/`, - wsBaseUrl: `wss://${hostname}/ws`, + httpBaseUrl: `${origin.origin}/env/${key}/`, + wsBaseUrl: `${wsProtocol}//${origin.host}/env/${key}/ws`, providerKind: "t3_relay", }; } -/** The channel key a request's `Host` names under `template`, or null when it is not a channel host. */ -export function channelKeyForHost(template: string, host: string): string | null { - const [prefix = "", suffix] = template.toLowerCase().split("*"); - const hostname = host.toLowerCase().replace(/:\d+$/u, ""); - if (suffix === undefined || !hostname.startsWith(prefix) || !hostname.endsWith(suffix)) { - return null; - } - const key = hostname.slice(prefix.length, hostname.length - suffix.length); - return CHANNEL_KEY_PATTERN.test(key) ? key : null; +/** Splits a request URL under `/env/` into its channel key, that prefix, and the environment path. */ +export function channelPathTarget( + url: string, +): { readonly key: string; readonly prefix: string; readonly path: string } | null { + const match = CHANNEL_PATH.exec(url); + if (match?.[1] === undefined) return null; + const rest = match[2] ?? ""; + return { + key: match[1], + prefix: `/env/${match[1]}`, + path: rest.startsWith("/") ? rest : `/${rest}`, + }; } diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index 7ab7e074a10dc..2c3c01cb4e06d 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -1,4 +1,4 @@ -// @effect-diagnostics nodeBuiltinImport:off - fetch forbids the Host header these requests route by. +// @effect-diagnostics nodeBuiltinImport:off globalFetchInEffect:off - the relay under test is a real Node HTTP server. import * as NodeHttp from "node:http"; import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; @@ -8,12 +8,11 @@ import * as HttpServer from "effect/http/HttpServer"; import * as HttpServerResponse from "effect/http/HttpServerResponse"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as TestClock from "effect/testing/TestClock"; import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts"; -import { channelHostname, channelKey } from "../environments/RelayChannelEndpoint.ts"; -import { withRelayChannels } from "./RelayChannels.ts"; - -const CHANNEL_HOST = "t3-*.relay.test"; +import { channelKey } from "../environments/RelayChannelEndpoint.ts"; +import { makeRelayChannels } from "./RelayChannels.ts"; const makeLayer = (credentialValid: () => boolean = () => true) => Layer.mergeAll( @@ -32,12 +31,14 @@ const makeLayer = (credentialValid: () => boolean = () => true) => const serveRelay = Effect.gen(function* () { yield* HttpServer.serveEffect( - withRelayChannels(CHANNEL_HOST)(Effect.succeed(HttpServerResponse.text("relay api"))), + makeRelayChannels()(Effect.succeed(HttpServerResponse.text("relay api"))), ); const { address } = yield* HttpServer.HttpServer; return "port" in address ? address.port : 0; }); +const channelPrefix = Effect.map(channelKey("env-1", "k"), (key) => `/env/${key}`); + /** A fake environment that answers each forwarded request with what it received. */ const connectEnvironment = (port: number, token: string) => Effect.acquireRelease( @@ -50,13 +51,13 @@ const connectEnvironment = (port: number, token: string) => // The relay pings once the channel is registered. if (frame.type === "ping") resume(Effect.succeed(socket)); if (frame.type === "http-request") { - const echo = `${frame.method} ${frame.path} ${frame.headers.host}`; + const echo = `${frame.method} ${frame.path} ${frame.headers["x-forwarded-prefix"]}`; socket.send( JSON.stringify({ type: "http-response", id: frame.id, - status: 201, - headers: { "content-type": "text/plain", "x-environment": "yes" }, + status: 302, + headers: { "content-type": "text/plain", location: "/next" }, body: Buffer.from(echo).toString("base64"), }), ); @@ -67,74 +68,60 @@ const connectEnvironment = (port: number, token: string) => (socket) => Effect.sync(() => socket.close()), ); -const get = (port: number, host: string, path = "/") => - Effect.callback<{ readonly status: number; readonly body: string; readonly env?: string }>( - (resume) => { - const request = NodeHttp.request( - { host: "127.0.0.1", port, path, headers: { host } }, - (response) => { - let body = ""; - response.setEncoding("utf8"); - response.on("data", (chunk: string) => { - body += chunk; - }); - response.on("end", () => - resume( - Effect.succeed({ - status: response.statusCode ?? 0, - body, - ...(typeof response.headers["x-environment"] === "string" - ? { env: response.headers["x-environment"] } - : {}), - }), - ), - ); - }, - ); - request.on("error", (error) => resume(Effect.die(error))); - request.end(); - }, - ); +const get = (port: number, path: string) => + Effect.promise(async () => { + const response = await fetch(`http://127.0.0.1:${port}${path}`, { redirect: "manual" }); + return { + status: response.status, + body: await response.text(), + location: response.headers.get("location"), + }; + }); -describe("withRelayChannels", () => { - it.live("forwards a channel host to its environment and leaves other hosts to the relay", () => +describe("makeRelayChannels", () => { + it.live("forwards /env/ to its environment and leaves other paths to the relay", () => Effect.gen(function* () { const port = yield* serveRelay; - const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); + const prefix = yield* channelPrefix; yield* connectEnvironment(port, "good"); - expect(yield* get(port, host, "/api/state?x=1")).toEqual({ - status: 201, - body: `GET /api/state?x=1 ${host}`, - env: "yes", + expect(yield* get(port, `${prefix}/api/state?x=1`)).toEqual({ + status: 302, + body: `GET /api/state?x=1 ${prefix}`, + location: `${prefix}/next`, + }); + expect(yield* get(port, "/v1/metadata")).toEqual({ + status: 200, + body: "relay api", + location: null, }); - expect(yield* get(port, "relay.test")).toEqual({ status: 200, body: "relay api" }); }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); it.live("answers 502 for an environment that has no channel open", () => Effect.gen(function* () { const port = yield* serveRelay; - const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); - expect((yield* get(port, host)).status).toBe(502); + expect((yield* get(port, `${yield* channelPrefix}/`)).status).toBe(502); }).pipe(Effect.provide(makeLayer())), ); - it.live("closes a channel once its credential is revoked", () => { - let valid = true; - return Effect.gen(function* () { - const port = yield* serveRelay; - const socket = yield* connectEnvironment(port, "good"); - valid = false; - const closed = Effect.callback((resume) => - socket.addEventListener("close", () => resume(Effect.void)), - ); - socket.send(JSON.stringify({ type: "ping" })); - yield* closed; - const host = channelHostname(CHANNEL_HOST, yield* channelKey("env-1", "k")); - expect((yield* get(port, host)).status).toBe(502); - }).pipe(Effect.scoped, Effect.provide(makeLayer(() => valid))); - }); + it.effect( + "closes a channel once its credential is revoked, whatever the environment sends", + () => { + let valid = true; + return Effect.gen(function* () { + const port = yield* serveRelay; + const socket = yield* connectEnvironment(port, "good"); + const closed = Effect.callback((resume) => + socket.addEventListener("close", () => resume(Effect.void)), + ); + valid = false; + yield* TestClock.adjust("25 seconds"); + yield* closed; + expect((yield* get(port, `${yield* channelPrefix}/`)).status).toBe(502); + }).pipe(Effect.scoped, Effect.provide(makeLayer(() => valid))); + }, + ); it.live("refuses a channel opened with another credential", () => Effect.gen(function* () { diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index 1efb73c78717c..a706a1fbefe19 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -1,6 +1,7 @@ import { RelayChannelFrame } from "@t3tools/contracts/relay"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import * as HttpServerRequest from "effect/http/HttpServerRequest"; import * as HttpServerResponse from "effect/http/HttpServerResponse"; import * as Option from "effect/Option"; @@ -9,7 +10,7 @@ import * as Schema from "effect/Schema"; import * as Socket from "effect/socket/Socket"; import * as EnvironmentCredentials from "../environments/EnvironmentCredentials.ts"; -import { channelKey, channelKeyForHost } from "../environments/RelayChannelEndpoint.ts"; +import { channelKey, channelPathTarget } from "../environments/RelayChannelEndpoint.ts"; type HttpResponseFrame = Extract; type SocketFrame = Extract< @@ -23,7 +24,8 @@ interface Channel { readonly sockets: Map>; } -const CHANNEL_PATH = /^\/v1\/environments\/([^/?]+)\/channel(?:\?.*)?$/u; +const CREDENTIAL_RECHECK_INTERVAL = "25 seconds"; +const CHANNEL_SOCKET_PATH = /^\/v1\/environments\/([^/?]+)\/channel(?:\?.*)?$/u; // Per-connection headers stay on their own side of the relay. const HOP_BY_HOP_HEADERS = new Set([ "connection", @@ -62,15 +64,22 @@ function sendableCloseCode(code: number | undefined): number { : 1000; } +type ChannelTarget = NonNullable>; + +/** The client's headers, telling the environment which prefix the relay stripped. */ +function forwardedHeaders(request: HttpServerRequest.HttpServerRequest, target: ChannelTarget) { + return { ...withoutHopByHop(request.headers), "x-forwarded-prefix": target.prefix }; +} + const isWebSocketUpgrade = (request: HttpServerRequest.HttpServerRequest) => request.headers.upgrade?.toLowerCase() === "websocket"; /** * Carries environments with no public address: each one dials `/v1/environments/:id/channel`, - * and requests to its channel host (`RELAY_CHANNEL_HOST`) are forwarded over that socket. + * and requests under its `/env//` path are forwarded over that socket, prefix stripped. * Channels live in this process, so a relay serving them must run as a single replica. */ -export const withRelayChannels = (channelHost: string) => { +export const makeRelayChannels = () => { const channels = new Map(); let nextId = 0; @@ -104,8 +113,6 @@ export const withRelayChannels = (channelHost: string) => { sockets: new Map(), }; channels.set(key, channel); - // Tells the environment its channel now receives traffic. - yield* channel.send({ type: "ping" }); yield* Effect.logInfo("Relay channel connected", { environmentId }); yield* Effect.addFinalizer(() => Effect.gen(function* () { @@ -124,35 +131,33 @@ export const withRelayChannels = (channelHost: string) => { yield* Effect.logInfo("Relay channel disconnected", { environmentId }); }), ); - // The environment pings every 25 seconds; a revoked credential closes its channel. - const stillAuthorized = credentials.authenticate(token).pipe( + const readFrames = reader.pull.pipe( + Effect.flatMap((chunks) => + Effect.forEach(chunks, (chunk) => dispatch(channel, chunk), { discard: true }), + ), + Effect.forever, + ); + // Revocation ends the channel on the relay's own clock, whatever the environment sends. + // A failed lookup keeps the channel: a database hiccup should not drop every environment. + const revoked = yield* Effect.sleep(CREDENTIAL_RECHECK_INTERVAL).pipe( + Effect.andThen(credentials.authenticate(token)), Effect.map((current) => Option.exists(current, (now) => now.credentialId === principal.credentialId), ), Effect.orElseSucceed(() => true), + Effect.repeat({ while: (stillValid) => stillValid }), + Effect.forkScoped({ startImmediately: true }), ); - yield* reader.pull.pipe( - Effect.flatMap((chunks) => - Effect.forEach( - chunks, - (chunk) => - Option.getOrNull(parseFrame(chunk))?.type === "ping" - ? stillAuthorized.pipe( - Effect.flatMap((ok) => (ok ? Effect.void : Effect.fail("revoked" as const))), - ) - : dispatch(channel, chunk), - { discard: true }, - ), - ), - Effect.forever, - Effect.ignore, - ); + // Tells the environment its channel now receives traffic. + yield* channel.send({ type: "ping" }); + yield* Effect.raceFirst(readFrames, Fiber.join(revoked)).pipe(Effect.ignore); return HttpServerResponse.empty(); }, Effect.scoped); const forwardHttp = Effect.fn("RelayChannels.forwardHttp")(function* ( channel: Channel, request: HttpServerRequest.HttpServerRequest, + target: ChannelTarget, ) { const id = ++nextId; const body = new Uint8Array(yield* request.arrayBuffer); @@ -162,8 +167,8 @@ export const withRelayChannels = (channelHost: string) => { type: "http-request", id, method: request.method, - path: request.url, - headers: withoutHopByHop(request.headers), + path: target.path, + headers: forwardedHeaders(request, target), ...(body.byteLength > 0 ? { body: Buffer.from(body).toString("base64") } : {}), }); const result = yield* Deferred.await(response).pipe( @@ -174,6 +179,10 @@ export const withRelayChannels = (channelHost: string) => { return HttpServerResponse.text("The environment did not answer in time.", { status: 504 }); } const { "content-type": contentType, ...headers } = withoutHopByHop(result.value.headers); + // A root-relative redirect from the environment stays under its prefix. + if (headers.location?.startsWith("/") && !headers.location.startsWith("//")) { + headers.location = `${target.prefix}${headers.location}`; + } return HttpServerResponse.uint8Array( result.value.body ? Buffer.from(result.value.body, "base64") : new Uint8Array(), { status: result.value.status, headers, ...(contentType ? { contentType } : {}) }, @@ -183,6 +192,7 @@ export const withRelayChannels = (channelHost: string) => { const forwardWebSocket = Effect.fn("RelayChannels.forwardWebSocket")(function* ( channel: Channel, request: HttpServerRequest.HttpServerRequest, + target: ChannelTarget, ) { const id = ++nextId; const inbox = yield* Queue.unbounded(); @@ -191,8 +201,8 @@ export const withRelayChannels = (channelHost: string) => { yield* channel.send({ type: "ws-open", id, - path: request.url, - headers: withoutHopByHop(request.headers), + path: target.path, + headers: forwardedHeaders(request, target), }); const opened = yield* Queue.take(inbox).pipe(Effect.timeoutOption("15 seconds")); if (Option.isNone(opened) || opened.value.type !== "ws-opened") { @@ -257,7 +267,7 @@ export const withRelayChannels = (channelHost: string) => { return (handler: Effect.Effect) => Effect.gen(function* () { const request = yield* HttpServerRequest.HttpServerRequest; - const channelPath = CHANNEL_PATH.exec(request.url); + const channelPath = CHANNEL_SOCKET_PATH.exec(request.url); if (channelPath?.[1] !== undefined && isWebSocketUpgrade(request)) { const authorized = yield* authenticateChannel(request, channelPath[1]); if (authorized === null) return HttpServerResponse.empty({ status: 401 }); @@ -265,9 +275,9 @@ export const withRelayChannels = (channelHost: string) => { Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 })), ); } - const key = channelKeyForHost(channelHost, request.headers.host ?? ""); - if (key === null) return yield* handler; - const channel = channels.get(key); + const target = channelPathTarget(request.url); + if (target === null) return yield* handler; + const channel = channels.get(target.key); if (channel === undefined) { return HttpServerResponse.text("This environment is not connected to the relay.", { status: 502, @@ -275,8 +285,8 @@ export const withRelayChannels = (channelHost: string) => { } return yield* ( isWebSocketUpgrade(request) - ? forwardWebSocket(channel, request) - : forwardHttp(channel, request) + ? forwardWebSocket(channel, request, target) + : forwardHttp(channel, request, target) ).pipe(Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 400 }))); }); }; diff --git a/infra/relay/src/host/RelayHost.ts b/infra/relay/src/host/RelayHost.ts index 20217005ff0ba..b34e3c2a3be5b 100644 --- a/infra/relay/src/host/RelayHost.ts +++ b/infra/relay/src/host/RelayHost.ts @@ -31,8 +31,8 @@ export interface RelayHost { readonly apnsJobSigningSecret: Effect.Effect>; /** Managed tunnel base domain; undefined disables managed endpoints. */ readonly managedEndpointBaseDomain: string | undefined; - /** Host template like `t3-*.example.com` for environments reached over a relay channel; undefined disables them. */ - readonly channelHost?: string | undefined; + /** Whether environments with no public address are carried over relay channels at `/env//`. */ + readonly channels?: boolean; readonly database: RelayDb.RelayDb["Service"]; /** Tunnel and DNS clients behind managed endpoints; inert on hosts without managed tunnels. */ readonly managedEndpointClients: Layer.Layer< diff --git a/infra/relay/src/host/node.ts b/infra/relay/src/host/node.ts index 2dc155b07ca18..97b0d74f268e2 100644 --- a/infra/relay/src/host/node.ts +++ b/infra/relay/src/host/node.ts @@ -34,7 +34,7 @@ export const withPublicOrigin = (relayPublicOrigin: string) => { export interface NodeHostOptions { readonly relayPublicOrigin: string; readonly stage: string; - readonly channelHost?: string | undefined; + readonly channels?: boolean; readonly mintKeyPair: { readonly privateKey: Redacted.Redacted; readonly publicKey: string; @@ -190,7 +190,7 @@ export const makeNodeHost = Effect.fn("relay.node.host")(function* (options: Nod ) : Effect.succeed(options.apnsJobSigningSecret), managedEndpointBaseDomain: undefined, - channelHost: options.channelHost, + channels: options.channels ?? false, database: options.database, managedEndpointClients: Layer.mergeAll( Layer.succeed(ManagedEndpointProvider.ManagedEndpointTunnelClient, inertTunnelClient), diff --git a/infra/relay/src/node.ts b/infra/relay/src/node.ts index 692acc6c67293..3bccb8896562d 100644 --- a/infra/relay/src/node.ts +++ b/infra/relay/src/node.ts @@ -21,7 +21,7 @@ import * as SecureRandom from "effect/crypto/SecureRandom"; import { composeRelay } from "./composition.ts"; import { makeNodeHost, withPublicOrigin } from "./host/node.ts"; -import { withRelayChannels } from "./host/RelayChannels.ts"; +import { makeRelayChannels } from "./host/RelayChannels.ts"; const program = Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -29,12 +29,7 @@ const program = Effect.gen(function* () { fs.readFileString(path).pipe(Effect.map((text) => text.trim())); const relayPublicOrigin = yield* Config.String("RELAY_PUBLIC_ORIGIN"); - const channelHost = Option.getOrUndefined( - Option.filter( - yield* Config.option(Config.String("RELAY_CHANNEL_HOST")), - (value) => value.trim().length > 0, - ), - )?.trim(); + const channels = yield* Config.Boolean("RELAY_CHANNELS").pipe(Config.withDefault(false)); const migrationsFolder = yield* Config.String("RELAY_MIGRATIONS_FOLDER").pipe( Config.withDefault("migrations/postgres"), ); @@ -61,7 +56,7 @@ const program = Effect.gen(function* () { const host = yield* makeNodeHost({ relayPublicOrigin, stage: "selfhost", - channelHost, + channels, mintKeyPair: { privateKey: Redacted.make(mintPrivateKey), publicKey: mintPublicKey, @@ -85,12 +80,12 @@ const program = Effect.gen(function* () { yield* HttpServer.serveEffect( handler.pipe( withPublicOrigin(relayPublicOrigin), - // Outermost, so channel hosts are matched against the Host the proxy forwarded. - channelHost ? withRelayChannels(channelHost) : (effect) => effect, + // Outermost, so channel traffic skips the relay API's middleware and tracing. + channels ? makeRelayChannels() : (effect) => effect, Effect.provideContext(context), ), ); - yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin, channelHost }); + yield* Effect.logInfo("Self-hosted relay is serving", { relayPublicOrigin, channels }); // Serving is scoped to this program; keep it alive until the process is stopped. return yield* Effect.never; }); diff --git a/packages/client-runtime/src/connection/presentation.ts b/packages/client-runtime/src/connection/presentation.ts index b0e83a9d983d5..0d19879cd0947 100644 --- a/packages/client-runtime/src/connection/presentation.ts +++ b/packages/client-runtime/src/connection/presentation.ts @@ -1,6 +1,8 @@ import type { ServerConfig } from "@t3tools/contracts"; import * as Option from "effect/Option"; +import { environmentEndpointUrl } from "../environment/endpoint.ts"; + import type { ConnectionCatalogEntry } from "./catalog.ts"; import type { ConnectionTarget, SupervisorConnectionState } from "./model.ts"; import { connectionRouteId, connectionRoutes, routeHttpBaseUrl } from "./routes.ts"; @@ -120,16 +122,11 @@ export function environmentMcpUrl(input: { } function mcpUrlFromBase(httpBaseUrl: string): string | null { - let url: URL; try { - url = new URL(httpBaseUrl); + return environmentEndpointUrl(httpBaseUrl, "/mcp"); } catch { return null; } - url.pathname = "/mcp"; - url.search = ""; - url.hash = ""; - return url.toString(); } export function connectionCatalogDisplayUrl(entry: ConnectionCatalogEntry): string | null { diff --git a/packages/client-runtime/src/environment/endpoint.test.ts b/packages/client-runtime/src/environment/endpoint.test.ts index 5e716817f6a40..f15cc51bb7395 100644 --- a/packages/client-runtime/src/environment/endpoint.test.ts +++ b/packages/client-runtime/src/environment/endpoint.test.ts @@ -1,6 +1,11 @@ import { describe, expect, it } from "vite-plus/test"; -import { deriveWsBaseUrl, normalizeHttpBaseUrl } from "./endpoint.ts"; +import { + deriveWsBaseUrl, + environmentEndpointUrl, + normalizeHttpBaseUrl, + resolveEnvironmentUrl, +} from "./endpoint.ts"; describe("advertised endpoint helpers", () => { it("normalizes HTTP and WebSocket base URLs", () => { @@ -9,4 +14,39 @@ describe("advertised endpoint helpers", () => { expect(deriveWsBaseUrl("https://example.com/api")).toBe("wss://example.com/"); expect(deriveWsBaseUrl("http://127.0.0.1:3773")).toBe("ws://127.0.0.1:3773/"); }); + + it.each([ + [ + "a root base", + "https://host.test/", + "/api/assets/a.png?s=1", + "https://host.test/api/assets/a.png?s=1", + ], + [ + "a relay channel base", + "https://relay.test/env/abc/", + "/api/assets/a.png?s=1", + "https://relay.test/env/abc/api/assets/a.png?s=1", + ], + [ + "an absolute target", + "https://relay.test/env/abc/", + "https://cdn.test/x", + "https://cdn.test/x", + ], + [ + "a protocol-relative target", + "https://relay.test/env/abc/", + "//cdn.test/x", + "https://cdn.test/x", + ], + ])("resolves environment paths under %s", (_name, base, target, expected) => { + expect(resolveEnvironmentUrl(base, target).href).toBe(expected); + }); + + it("keeps a relay prefix on endpoint URLs and drops the base query", () => { + expect(environmentEndpointUrl("https://relay.test/env/abc/?x=1", "/oauth/token")).toBe( + "https://relay.test/env/abc/oauth/token", + ); + }); }); diff --git a/packages/client-runtime/src/environment/endpoint.ts b/packages/client-runtime/src/environment/endpoint.ts index 4178259361e79..578408cda27c2 100644 --- a/packages/client-runtime/src/environment/endpoint.ts +++ b/packages/client-runtime/src/environment/endpoint.ts @@ -1,8 +1,9 @@ +import { resolveEnvironmentUrl } from "@t3tools/shared/advertisedEndpoint"; + export * from "@t3tools/shared/advertisedEndpoint"; export const environmentEndpointUrl = (httpBaseUrl: string, pathname: string): string => { - const url = new URL(httpBaseUrl); - url.pathname = pathname; + const url = resolveEnvironmentUrl(httpBaseUrl, pathname); url.search = ""; url.hash = ""; return url.toString(); diff --git a/packages/client-runtime/src/rpc/http.ts b/packages/client-runtime/src/rpc/http.ts index 5f32a6c3c087d..1bf1d74b2f618 100644 --- a/packages/client-runtime/src/rpc/http.ts +++ b/packages/client-runtime/src/rpc/http.ts @@ -81,9 +81,9 @@ export const layerRemoteHttpClient = ( HttpObservability.layer, ); +// Keeps the base path: a relay channel serves the environment's API under `/env//`. const remoteApiBaseUrl = (httpBaseUrl: string): string => { const url = new URL(httpBaseUrl); - url.pathname = "/"; url.search = ""; url.hash = ""; return url.toString(); diff --git a/packages/client-runtime/src/state/assets.ts b/packages/client-runtime/src/state/assets.ts index 56b1b5bcd7d70..24ebfcd026579 100644 --- a/packages/client-runtime/src/state/assets.ts +++ b/packages/client-runtime/src/state/assets.ts @@ -7,6 +7,7 @@ import { type ProjectCloneSnapshot, WS_METHODS, } from "@t3tools/contracts"; +import { resolveEnvironmentUrl } from "@t3tools/shared/advertisedEndpoint"; import { mediaMimeTypeFromExtension } from "@t3tools/shared/mediaPreview"; import { isWindowsAbsolutePath } from "@t3tools/shared/path"; import { @@ -60,7 +61,7 @@ function parseAssetCollectionKey( export function resolveAssetUrl(httpBaseUrl: string, relativeUrl: string): string | null { try { - return new URL(relativeUrl, httpBaseUrl).toString(); + return resolveEnvironmentUrl(httpBaseUrl, relativeUrl).toString(); } catch { return null; } @@ -284,7 +285,10 @@ export function createAssetEnvironmentAtoms( const asset = yield* registry .run(local.environmentId, mint(local.environmentId, input)) .pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => error)))); - return { ...asset, relativeUrl: new URL(asset.relativeUrl, local.httpBaseUrl).href }; + return { + ...asset, + relativeUrl: resolveEnvironmentUrl(local.httpBaseUrl, asset.relativeUrl).href, + }; }); const createUrl = createEnvironmentQueryAtomFamily(runtime, { label: "environment-data:assets:create-url", diff --git a/packages/client-runtime/src/webhookAddress.ts b/packages/client-runtime/src/webhookAddress.ts index 74c07eddae915..f9bed39749ef4 100644 --- a/packages/client-runtime/src/webhookAddress.ts +++ b/packages/client-runtime/src/webhookAddress.ts @@ -1,4 +1,5 @@ import type { ScheduledTaskWebhookEndpoint } from "@t3tools/contracts"; +import { resolveEnvironmentUrl } from "@t3tools/shared/advertisedEndpoint"; import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification"; export interface WebhookAddress { @@ -21,7 +22,7 @@ export function webhookAddress( note: "Link this environment to T3 Connect for a public URL.", }; } - const url = new URL(endpoint.path, httpBaseUrl); + const url = resolveEnvironmentUrl(httpBaseUrl, endpoint.path); return { address: url.href, copyable: true, diff --git a/packages/shared/src/advertisedEndpoint.ts b/packages/shared/src/advertisedEndpoint.ts index 14537ed69c6c0..932534b84474d 100644 --- a/packages/shared/src/advertisedEndpoint.ts +++ b/packages/shared/src/advertisedEndpoint.ts @@ -75,3 +75,13 @@ export function createAdvertisedEndpoint(input: CreateAdvertisedEndpointInput): ...(input.description === undefined ? {} : { description: input.description }), }; } + +/** + * Resolves `target` against an environment's base URL. Root-relative paths stay under the base's + * own path, so environments a relay serves at `/env//` keep that prefix. + */ +export function resolveEnvironmentUrl(httpBaseUrl: string, target: string): URL { + const base = new URL(httpBaseUrl); + if (!target.startsWith("/") || target.startsWith("//")) return new URL(target, base); + return new URL(`${base.pathname.replace(/\/+$/u, "")}${target}`, base.origin); +} diff --git a/packages/shared/src/preview.ts b/packages/shared/src/preview.ts index 6740b50d26eb0..3dd3f3736f6f1 100644 --- a/packages/shared/src/preview.ts +++ b/packages/shared/src/preview.ts @@ -1,6 +1,7 @@ import type { BrowserSearchEngine } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; +import { resolveEnvironmentUrl } from "./advertisedEndpoint.ts"; import { isLocalLoopbackHost } from "./hostClassification.ts"; const TAB_ID_PREFIX = "tab_"; @@ -27,7 +28,7 @@ export function previewPdfAssetUrl(rawUrl: string, httpBaseUrl: string | null): } // The browser may run on the host's loopback address. Fetch signed bytes // through this client's environment connection, including remote routes. - return new URL(`${url.pathname}${url.search}${url.hash}`, baseUrl).href; + return resolveEnvironmentUrl(baseUrl.href, `${url.pathname}${url.search}${url.hash}`).href; } catch { return null; } From ffea460a696459016812e1a5448045ce0e9c45d2 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 11:56:22 +0100 Subject: [PATCH 07/29] fix(relay): keep relay channel responses from acting on the relay origin Carried environments share the relay's origin under /env/, and any account can link one. Forwarded responses now drop Set-Cookie and carry Content-Security-Policy: sandbox and X-Content-Type-Options: nosniff. --- infra/relay/src/host/RelayChannels.test.ts | 8 +++++++- infra/relay/src/host/RelayChannels.ts | 13 ++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index 2c3c01cb4e06d..a50c9ad9e7354 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -57,7 +57,7 @@ const connectEnvironment = (port: number, token: string) => type: "http-response", id: frame.id, status: 302, - headers: { "content-type": "text/plain", location: "/next" }, + headers: { "content-type": "text/plain", location: "/next", "set-cookie": "s=1" }, body: Buffer.from(echo).toString("base64"), }), ); @@ -75,6 +75,8 @@ const get = (port: number, path: string) => status: response.status, body: await response.text(), location: response.headers.get("location"), + cookie: response.headers.get("set-cookie"), + csp: response.headers.get("content-security-policy"), }; }); @@ -89,11 +91,15 @@ describe("makeRelayChannels", () => { status: 302, body: `GET /api/state?x=1 ${prefix}`, location: `${prefix}/next`, + cookie: null, + csp: "sandbox", }); expect(yield* get(port, "/v1/metadata")).toEqual({ status: 200, body: "relay api", location: null, + cookie: null, + csp: null, }); }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index a706a1fbefe19..afefe3a0753a3 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -178,7 +178,18 @@ export const makeRelayChannels = () => { if (Option.isNone(result)) { return HttpServerResponse.text("The environment did not answer in time.", { status: 504 }); } - const { "content-type": contentType, ...headers } = withoutHopByHop(result.value.headers); + const { + "content-type": contentType, + "set-cookie": _cookie, + ...forwarded + } = withoutHopByHop(result.value.headers); + // Every carried environment shares the relay's origin, and anyone can link one: its responses + // may not run script there or set cookies the relay and other environments would see. + const headers: Record = { + ...forwarded, + "content-security-policy": "sandbox", + "x-content-type-options": "nosniff", + }; // A root-relative redirect from the environment stays under its prefix. if (headers.location?.startsWith("/") && !headers.location.startsWith("//")) { headers.location = `${target.prefix}${headers.location}`; From 39f6878fb148205dfc8f0659b661dc36aac048df Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 11:59:45 +0100 Subject: [PATCH 08/29] fix(relay): filter relay channel response headers case-insensitively Frames from an environment need not lowercase header names, so a Set-Cookie or Content-Security-Policy in another case slipped past the filter. Names are now lowercased first, and the environment's own security headers are dropped before the relay adds its own. --- infra/relay/src/host/RelayChannels.test.ts | 7 +++++- infra/relay/src/host/RelayChannels.ts | 28 ++++++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index a50c9ad9e7354..d0577a0dea357 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -57,7 +57,12 @@ const connectEnvironment = (port: number, token: string) => type: "http-response", id: frame.id, status: 302, - headers: { "content-type": "text/plain", location: "/next", "set-cookie": "s=1" }, + headers: { + "content-type": "text/plain", + location: "/next", + "Set-Cookie": "s=1", + "Content-Security-Policy": "default-src *", + }, body: Buffer.from(echo).toString("base64"), }), ); diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index afefe3a0753a3..fea67b9e517cb 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -47,9 +47,24 @@ function parseFrame(chunk: string | Uint8Array): Option.Option>): Record { +// Headers the relay owns on a carried environment's responses; the environment's own are dropped. +const RELAY_OWNED_RESPONSE_HEADERS = new Set([ + "set-cookie", + "set-cookie2", + "content-security-policy", + "content-security-policy-report-only", + "x-content-type-options", +]); + +/** Lowercases names, which frames from an environment need not do, then drops `excluded`. */ +function withoutHopByHop( + headers: Readonly>, + excluded: ReadonlySet = HOP_BY_HOP_HEADERS, +): Record { return Object.fromEntries( - Object.entries(headers).filter(([name]) => !HOP_BY_HOP_HEADERS.has(name.toLowerCase())), + Object.entries(headers) + .map(([name, value]) => [name.toLowerCase(), value] as const) + .filter(([name]) => !HOP_BY_HOP_HEADERS.has(name) && !excluded.has(name)), ); } @@ -178,11 +193,10 @@ export const makeRelayChannels = () => { if (Option.isNone(result)) { return HttpServerResponse.text("The environment did not answer in time.", { status: 504 }); } - const { - "content-type": contentType, - "set-cookie": _cookie, - ...forwarded - } = withoutHopByHop(result.value.headers); + const { "content-type": contentType, ...forwarded } = withoutHopByHop( + result.value.headers, + RELAY_OWNED_RESPONSE_HEADERS, + ); // Every carried environment shares the relay's origin, and anyone can link one: its responses // may not run script there or set cookies the relay and other environments would see. const headers: Record = { From 23bbd876227dc95f25478e2aba7b392dea1afdad Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 12:06:30 +0100 Subject: [PATCH 09/29] fix(relay): allowlist response headers from relay channel environments A denylist left headers such as Clear-Site-Data or Service-Worker-Allowed free to act on the relay's shared origin. Environment responses now keep only content, caching, redirect, auth challenge and CORS headers. --- infra/relay/src/host/RelayChannels.test.ts | 4 ++ infra/relay/src/host/RelayChannels.ts | 50 +++++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index d0577a0dea357..749bf7c271e9b 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -62,6 +62,7 @@ const connectEnvironment = (port: number, token: string) => location: "/next", "Set-Cookie": "s=1", "Content-Security-Policy": "default-src *", + "Clear-Site-Data": '"*"', }, body: Buffer.from(echo).toString("base64"), }), @@ -82,6 +83,7 @@ const get = (port: number, path: string) => location: response.headers.get("location"), cookie: response.headers.get("set-cookie"), csp: response.headers.get("content-security-policy"), + clearSiteData: response.headers.get("clear-site-data"), }; }); @@ -98,6 +100,7 @@ describe("makeRelayChannels", () => { location: `${prefix}/next`, cookie: null, csp: "sandbox", + clearSiteData: null, }); expect(yield* get(port, "/v1/metadata")).toEqual({ status: 200, @@ -105,6 +108,7 @@ describe("makeRelayChannels", () => { location: null, cookie: null, csp: null, + clearSiteData: null, }); }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index fea67b9e517cb..c22322aa83da4 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -47,24 +47,45 @@ function parseFrame(chunk: string | Uint8Array): Option.Option>, - excluded: ReadonlySet = HOP_BY_HOP_HEADERS, -): Record { +function withoutHopByHop(headers: Readonly>): Record { + return Object.fromEntries( + Object.entries(headers).filter(([name]) => !HOP_BY_HOP_HEADERS.has(name.toLowerCase())), + ); +} + +/** Lowercases names, which frames from an environment need not do, and keeps the allowlist. */ +function allowedResponseHeaders(headers: Readonly>): Record { return Object.fromEntries( Object.entries(headers) .map(([name, value]) => [name.toLowerCase(), value] as const) - .filter(([name]) => !HOP_BY_HOP_HEADERS.has(name) && !excluded.has(name)), + .filter(([name]) => ALLOWED_RESPONSE_HEADERS.has(name)), ); } @@ -193,9 +214,8 @@ export const makeRelayChannels = () => { if (Option.isNone(result)) { return HttpServerResponse.text("The environment did not answer in time.", { status: 504 }); } - const { "content-type": contentType, ...forwarded } = withoutHopByHop( + const { "content-type": contentType, ...forwarded } = allowedResponseHeaders( result.value.headers, - RELAY_OWNED_RESPONSE_HEADERS, ); // Every carried environment shares the relay's origin, and anyone can link one: its responses // may not run script there or set cookies the relay and other environments would see. From 137386a3d14a19131d968f5d58260bb19a872440 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 14:20:32 +0100 Subject: [PATCH 10/29] fix(relay): reconnect relay channels the relay silently dropped After a laptop sleep the channel socket stayed open on the environment side for over an hour while the relay had already dropped it, so the environment was unreachable. The relay now answers each keepalive ping, and the environment reconnects when an interval passes unanswered. --- apps/server/src/cloud/RelayChannelClient.ts | 19 +++++++++++++++---- infra/relay/src/host/RelayChannels.test.ts | 12 ++++++++++++ infra/relay/src/host/RelayChannels.ts | 1 + 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/apps/server/src/cloud/RelayChannelClient.ts b/apps/server/src/cloud/RelayChannelClient.ts index 00719decfbe03..26e756461c575 100644 --- a/apps/server/src/cloud/RelayChannelClient.ts +++ b/apps/server/src/cloud/RelayChannelClient.ts @@ -125,6 +125,8 @@ function openChannel(input: RelayChannelInput) { }); const locals = new Map(); let opened = false; + let heard = true; + let end: (reason: string) => void = () => {}; const send = (frame: RelayChannelFrame) => { if (channel.readyState === WebSocket.OPEN) channel.send(JSON.stringify(frame)); }; @@ -136,6 +138,7 @@ function openChannel(input: RelayChannelInput) { opened = true; }); channel.addEventListener("message", (event) => { + heard = true; const frame = parseFrame(event.data); switch (frame?.type) { case "http-request": @@ -165,15 +168,23 @@ function openChannel(input: RelayChannelInput) { return; } }); - const closed = new Promise((resolve) => + const closed = new Promise((resolve) => { + end = resolve; channel.addEventListener("close", (event) => { closeLocals(); resolve(opened ? `closed with code ${event.code}` : "could not connect"); - }), - ); + }); + }); return { send, closed, + // After a sleep the socket can look open long after the relay dropped it; the relay answers + // every ping, so a silent interval means the channel is gone. + ping: () => { + if (!heard) return end("the relay stopped answering"); + heard = false; + send({ type: "ping" }); + }, dispose: () => { closeLocals(); channel.close(); @@ -190,7 +201,7 @@ const connectOnce = Effect.fn("RelayChannelClient.connectOnce")(function* ( (opened) => Effect.sync(opened.dispose), ); // Proxies drop idle WebSockets; a ping keeps the channel open between requests. - yield* Effect.sync(() => channel.send({ type: "ping" })).pipe( + yield* Effect.sync(channel.ping).pipe( Effect.repeat(Schedule.spaced(KEEPALIVE_INTERVAL)), Effect.forkScoped, ); diff --git a/infra/relay/src/host/RelayChannels.test.ts b/infra/relay/src/host/RelayChannels.test.ts index 749bf7c271e9b..ca644e15f79a8 100644 --- a/infra/relay/src/host/RelayChannels.test.ts +++ b/infra/relay/src/host/RelayChannels.test.ts @@ -113,6 +113,18 @@ describe("makeRelayChannels", () => { }).pipe(Effect.scoped, Effect.provide(makeLayer())), ); + it.live("answers the environment's keepalive pings", () => + Effect.gen(function* () { + const socket = yield* connectEnvironment(yield* serveRelay, "good"); + yield* Effect.callback((resume) => { + socket.addEventListener("message", (event) => { + if (JSON.parse(String(event.data)).type === "ping") resume(Effect.void); + }); + socket.send(JSON.stringify({ type: "ping" })); + }); + }).pipe(Effect.scoped, Effect.provide(makeLayer())), + ); + it.live("answers 502 for an environment that has no channel open", () => Effect.gen(function* () { const port = yield* serveRelay; diff --git a/infra/relay/src/host/RelayChannels.ts b/infra/relay/src/host/RelayChannels.ts index c22322aa83da4..bf67bf6f51ec1 100644 --- a/infra/relay/src/host/RelayChannels.ts +++ b/infra/relay/src/host/RelayChannels.ts @@ -121,6 +121,7 @@ export const makeRelayChannels = () => { const dispatch = (channel: Channel, chunk: string | Uint8Array) => { const frame = Option.getOrNull(parseFrame(chunk)); + if (frame?.type === "ping") return channel.send({ type: "ping" }); if (frame?.type === "http-response") { const response = channel.responses.get(frame.id); return response ? Deferred.succeed(response, frame).pipe(Effect.asVoid) : Effect.void; From 5bd54c6404563f7d91631936315ceeef09aaf3fc Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 16:05:54 +0100 Subject: [PATCH 11/29] feat(connect): deregister environments from a self-hosted relay account Deregistering lived only in Clerk's account pages, so with a self-hosted relay a stale environment could not leave the account and kept reappearing after it was removed from a device. The OIDC sign-in now opens the same T3 Connect page from Settings > Connections > Linked environments, and the mobile T3 Account screen lists linked environments. --- .../settings/SettingsAuthRouteScreen.tsx | 35 +++++++++++-------- .../components/oidc/OidcManagedAuthShell.tsx | 21 +++++++++-- .../settings/T3ConnectServiceSettings.tsx | 16 ++++++++- docs/user/remote-access.md | 4 ++- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx index cc6d6025f578d..60bfa9fe69fdf 100644 --- a/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsAuthRouteScreen.tsx @@ -39,7 +39,7 @@ export function SettingsAuthRouteScreen() { ); } -/** An OpenID Connect provider hosts no profile view, so this screen only signs in and out. */ +/** An OpenID Connect provider hosts no profile view, so this screen signs in and out and lists linked environments. */ function OidcSettingsAuthRouteScreen() { const { isSignedIn, accountLabel, signOut } = useCloudAuth(); const [busy, setBusy] = useState(false); @@ -61,25 +61,32 @@ function OidcSettingsAuthRouteScreen() { return ( - - - {isSignedIn ? ( - void signOut()} - /> - ) : ( + {isSignedIn ? ( + + + + void signOut()} + /> + + + + + ) : ( + + void signIn()} /> - )} - - + + + )} ); } diff --git a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx index c2e69430fc808..a5f7ba2468daf 100644 --- a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx +++ b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx @@ -1,5 +1,5 @@ import type { OidcController } from "@t3tools/client-runtime/relay"; -import { type ReactNode, useEffect, useSyncExternalStore } from "react"; +import { type ReactNode, useEffect, useState, useSyncExternalStore } from "react"; import { CloudAuthProvider, type CloudAuth } from "../../cloud/cloudAuth"; import { createManagedRelayAccountService } from "../../cloud/managedAuth"; @@ -7,6 +7,8 @@ import { getBrowserOidcController, signInThroughDesktopBrowser, } from "../../cloud/oidc/browserOidc"; +import { T3ConnectUserProfilePage } from "../clerk/T3ConnectUserProfilePage"; +import { Dialog, DialogPanel, DialogPopup } from "../ui/dialog"; import { toastManager } from "../ui/toast"; // The relay verifies the ID token, so that is what the controller hands out as the relay token. @@ -30,6 +32,7 @@ function ConfiguredOidcShell({ readonly children: ReactNode; }) { const snapshot = useSyncExternalStore(controller.subscribe, controller.getSnapshot); + const [accountOpen, setAccountOpen] = useState(false); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches the OIDC controller to the managed relay account service for the provider's lifetime useEffect(() => accountService.attach(controller), [controller]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- another tab signs in, refreshes, or signs out through shared storage @@ -56,10 +59,22 @@ function ConfiguredOidcShell({ void signedIn.catch(reportSignInFailure); }, signOut: controller.signOut, - openAccount: null, + // The relay's account page is the only one; it lists linked environments to deregister. + openAccount: () => setAccountOpen(true), }; - return {children}; + return ( + + {children} + + + + + + + + + ); } /** Lazy-loaded when the build configures an OpenID Connect provider; Clerk is not loaded then. */ diff --git a/apps/web/src/components/settings/T3ConnectServiceSettings.tsx b/apps/web/src/components/settings/T3ConnectServiceSettings.tsx index c681cf4d5ed86..4e36ab35d315f 100644 --- a/apps/web/src/components/settings/T3ConnectServiceSettings.tsx +++ b/apps/web/src/components/settings/T3ConnectServiceSettings.tsx @@ -4,6 +4,7 @@ import { } from "@t3tools/client-runtime/relay/self-hosted"; import { useState } from "react"; +import { useCloudAuth } from "~/cloud/cloudAuth"; import { readSelfHostedRelay, saveSelfHostedRelay } from "~/cloud/publicConfig"; import { Button } from "../ui/button"; import { Input } from "../ui/input"; @@ -35,6 +36,7 @@ const switchTo = (relay: SelfHostedRelay | null) => { /** Chooses between the build's T3 Connect and a relay the user runs themselves. */ export function T3ConnectServiceSettings() { const current = readSelfHostedRelay(); + const { isSignedIn, openAccount } = useCloudAuth(); const [service, setService] = useState(current ? "self-hosted" : "default"); const [address, setAddress] = useState(current?.relayUrl ?? ""); const [error, setError] = useState(null); @@ -110,7 +112,19 @@ export function T3ConnectServiceSettings() { } /> - ) : current ? ( + ) : null} + {current && isSignedIn && openAccount ? ( + openAccount()}> + Manage + + } + /> + ) : null} + {service !== "self-hosted" && current ? ( Date: Tue, 10 Nov 2026 16:08:36 +0100 Subject: [PATCH 12/29] fix(connect): label relay-carried environments on the account page A relay-carried environment showed as activity publishing only, the same as a publish-only link, so two environments with one name could not be told apart. --- apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx | 4 +++- apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx b/apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx index 03b6daaa4e715..26cb72386f1bb 100644 --- a/apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx +++ b/apps/mobile/src/features/cloud/T3ConnectProfilePage.tsx @@ -33,7 +33,9 @@ function linkedAtLabel(value: string): string { function endpointLabel(environment: RelayClientEnvironmentRecord): string { return environment.endpoint.providerKind === "cloudflare_tunnel" ? "Managed tunnel" - : "Activity publishing only"; + : environment.endpoint.providerKind === "t3_relay" + ? "Carried by the relay" + : "Activity publishing only"; } function confirmDeregister(environment: RelayClientEnvironmentRecord, onConfirm: () => void) { diff --git a/apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx b/apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx index 8a863c61b5c33..9b045067af340 100644 --- a/apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx +++ b/apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx @@ -41,7 +41,9 @@ function linkedAtLabel(value: string): string { function endpointLabel(environment: RelayClientEnvironmentRecord): string { return environment.endpoint.providerKind === "cloudflare_tunnel" ? "Managed tunnel" - : "Activity publishing only"; + : environment.endpoint.providerKind === "t3_relay" + ? "Carried by the relay" + : "Activity publishing only"; } function T3ConnectEnvironmentRow(props: { From 4e1f5c60cbffe74716ebd5c6f51f796addd052b0 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 16:18:53 +0100 Subject: [PATCH 13/29] fix(connect): self-hosted relay account page loads, and removal deregisters The account page opened from the OIDC shell sat above the app's atom registry, so it never saw the session and kept loading. On a self-hosted relay, removing a relay environment from a device now also deregisters it, so a stale one stops coming back. --- .../RemoveT3ConnectEnvironmentDialog.tsx | 55 ++++++++++++++++--- .../components/oidc/OidcManagedAuthShell.tsx | 18 +++--- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx b/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx index 35ceafe8a6365..8cf14e9e4dbf1 100644 --- a/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx +++ b/apps/web/src/components/clerk/RemoveT3ConnectEnvironmentDialog.tsx @@ -1,8 +1,12 @@ -import { Discovery } from "@t3tools/client-runtime/relay"; +import { useAtomValue } from "@effect/atom-react"; +import { Discovery, managedRelaySessionAtom } from "@t3tools/client-runtime/relay"; import type { EnvironmentId } from "@t3tools/contracts"; import { useState } from "react"; +import { deregisterManagedRelayEnvironmentCommand } from "../../cloud/managedRelayState"; import { useRelayEnvironmentDiscovery } from "../../state/environments"; +import { relayEnvironmentDiscovery } from "../../state/relay"; +import { useAtomCommand } from "../../state/use-atom-command"; import { AlertDialog, AlertDialogClose, @@ -13,7 +17,7 @@ import { AlertDialogTitle, } from "../ui/alert-dialog"; import { Button, InlineButton } from "../ui/button"; -import { useT3ConnectAccountPage } from "../../cloud/cloudAuth"; +import { useCloudAuth, useT3ConnectAccountPage } from "../../cloud/cloudAuth"; export function RemoveT3ConnectEnvironmentDialog({ environmentLabel, @@ -27,6 +31,15 @@ export function RemoveT3ConnectEnvironmentDialog({ readonly onConfirm: () => void; }) { const accountPage = useT3ConnectAccountPage(); + const { provider } = useCloudAuth(); + const session = useAtomValue(managedRelaySessionAtom); + const deregister = useAtomCommand(deregisterManagedRelayEnvironmentCommand, { + label: "deregister environment", + }); + const refreshDiscovery = useAtomCommand(relayEnvironmentDiscovery.refreshAfterMutation, { + reportFailure: false, + }); + const [isDeregistering, setIsDeregistering] = useState(false); const discovery = useRelayEnvironmentDiscovery(); const registration = environmentId ? Discovery.relayEnvironmentRegistration(discovery, environmentId) @@ -38,6 +51,18 @@ export function RemoveT3ConnectEnvironmentDialog({ ) { setShown({ label: environmentLabel, registration }); } + // A self-hosted relay frees no host space, so leaving it there only makes it come back. + const removesEverywhere = + provider === "oidc" && shown.registration === "registered" && session !== null; + const removeEverywhere = async () => { + if (session === null || environmentId === null) return; + setIsDeregistering(true); + const result = await deregister({ accountId: session.accountId, environmentId }); + setIsDeregistering(false); + if (result._tag === "Failure") return; + void refreshDiscovery(); + onConfirm(); + }; const openAccountPage = accountPage.open; const accountPageLink = openAccountPage ? ( - Remove {shown.label} from this device? + + {removesEverywhere + ? `Remove ${shown.label}?` + : `Remove ${shown.label} from this device?`} + This forgets its pairing, credentials, and cached threads here. - {shown.registration === "registered" ? ( + {removesEverywhere ? ( + "It's also removed from your relay account, so it doesn't come back on your devices." + ) : shown.registration === "registered" ? ( <> It stays on your T3 Connect account and keeps its host space. Deregister it in{" "} {accountPageLink} to free it. @@ -84,9 +115,19 @@ export function RemoveT3ConnectEnvironmentDialog({ }>Cancel - + {removesEverywhere ? ( + + ) : ( + + )} diff --git a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx index a5f7ba2468daf..46035bb702aa1 100644 --- a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx +++ b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx @@ -3,6 +3,7 @@ import { type ReactNode, useEffect, useState, useSyncExternalStore } from "react import { CloudAuthProvider, type CloudAuth } from "../../cloud/cloudAuth"; import { createManagedRelayAccountService } from "../../cloud/managedAuth"; +import { AppAtomRegistryProvider } from "../../rpc/atomRegistry"; import { getBrowserOidcController, signInThroughDesktopBrowser, @@ -66,13 +67,16 @@ function ConfiguredOidcShell({ return ( {children} - - - - - - - + {/* This shell wraps the app root, so the page needs the app's atom registry itself. */} + + + + + + + + + ); } From ff03b4cf5700e48476bbab99a5e0e1a027e2df95 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Tue, 10 Nov 2026 20:21:45 +0100 Subject: [PATCH 14/29] fix(mobile): self-hosted sign-in works on Android without Web Crypto Hermes has no global crypto, so creating the PKCE pair threw before the sign-in page opened. The OIDC controller now takes random bytes and SHA-256 as a dependency, and mobile passes expo-crypto. --- .../src/features/cloud/oidc/mobileOidc.ts | 6 +++++ .../src/relay/oidcController.test.ts | 19 +++++++++++--- .../src/relay/oidcController.ts | 8 +++--- packages/shared/src/oidc.ts | 25 +++++++++++++++---- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/apps/mobile/src/features/cloud/oidc/mobileOidc.ts b/apps/mobile/src/features/cloud/oidc/mobileOidc.ts index 928380b80b2d3..de657290fdfcd 100644 --- a/apps/mobile/src/features/cloud/oidc/mobileOidc.ts +++ b/apps/mobile/src/features/cloud/oidc/mobileOidc.ts @@ -4,6 +4,7 @@ import { type OidcController, } from "@t3tools/client-runtime/relay"; import * as AuthSession from "expo-auth-session"; +import * as ExpoCrypto from "expo-crypto"; import * as SecureStore from "expo-secure-store"; import * as WebBrowser from "expo-web-browser"; @@ -52,6 +53,11 @@ export function getMobileOidcController(): OidcController | null { redirectUri, sessions: secureSessionStore(), pending: memoryStore(), + // Hermes has no Web Crypto. + crypto: { + randomBytes: ExpoCrypto.getRandomBytes, + sha256: (data) => ExpoCrypto.digest(ExpoCrypto.CryptoDigestAlgorithm.SHA256, data), + }, navigate: () => { throw new Error("The mobile app signs in through an in-app browser."); }, diff --git a/packages/client-runtime/src/relay/oidcController.test.ts b/packages/client-runtime/src/relay/oidcController.test.ts index 1a898a546d653..7580294bad265 100644 --- a/packages/client-runtime/src/relay/oidcController.test.ts +++ b/packages/client-runtime/src/relay/oidcController.test.ts @@ -1,5 +1,5 @@ -import { base64UrlEncode } from "@t3tools/shared/oidc"; -import { describe, expect, it } from "vite-plus/test"; +import { base64UrlEncode, type OidcCrypto } from "@t3tools/shared/oidc"; +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; import { createOidcController, type KeyValueStore } from "./oidcController.ts"; @@ -36,7 +36,7 @@ interface Harness { tokenReply: () => Response; } -function makeHarness(): Harness { +function makeHarness(options: { readonly crypto?: OidcCrypto } = {}): Harness { const sessions = memoryStore(); const pending = memoryStore(); const harness: Harness = { @@ -69,6 +69,7 @@ function makeHarness(): Harness { navigate: (url) => harness.navigations.push(url), now: () => harness.clock, fetch: fetchImpl as typeof fetch, + ...options, }), }); } @@ -91,6 +92,8 @@ async function signInWith(harness: Harness, claims: Record = {} } describe("sign-in redirect", () => { + afterEach(() => vi.unstubAllGlobals()); + it("sends the user to the provider with PKCE, state and a nonce", async () => { const harness = makeHarness(); await harness.controller.signIn("/settings/connections"); @@ -126,7 +129,15 @@ describe("sign-in redirect", () => { }); it("signs in through an in-app browser that hands back the redirect URL", async () => { - const harness = makeHarness(); + // Like React Native: no global Web Crypto, so the app passes its own. + const webCrypto = globalThis.crypto; + const harness = makeHarness({ + crypto: { + randomBytes: (length) => webCrypto.getRandomValues(new Uint8Array(length)), + sha256: (data) => webCrypto.subtle.digest("SHA-256", data), + }, + }); + vi.stubGlobal("crypto", undefined); await harness.controller.signInWithPrompt(async (authorizeUrl) => { const url = new URL(authorizeUrl); diff --git a/packages/client-runtime/src/relay/oidcController.ts b/packages/client-runtime/src/relay/oidcController.ts index 4ff63bf53bd3f..45f7968534115 100644 --- a/packages/client-runtime/src/relay/oidcController.ts +++ b/packages/client-runtime/src/relay/oidcController.ts @@ -10,6 +10,7 @@ import { oidcAccountLabel, randomToken, refreshOidcTokens, + type OidcCrypto, type OidcMetadata, type OidcTokens, } from "@t3tools/shared/oidc"; @@ -51,6 +52,7 @@ export interface OidcControllerDeps { readonly navigate: (url: string) => void; readonly now?: () => number; readonly fetch?: typeof fetch; + readonly crypto?: OidcCrypto; /** Serializes refreshes across tabs: providers that rotate refresh tokens reject a stale one. */ readonly withRefreshLock?: (run: () => Promise) => Promise; } @@ -117,10 +119,10 @@ export function createOidcController(deps: OidcControllerDeps) { }; const startAuthorization = async (returnTo: string): Promise => { - const [oidc, pkce] = await Promise.all([metadata(), createPkcePair()]); + const [oidc, pkce] = await Promise.all([metadata(), createPkcePair(undefined, deps.crypto)]); const request: PendingAuthorization = { - state: randomToken(16), - nonce: randomToken(16), + state: randomToken(16, deps.crypto), + nonce: randomToken(16, deps.crypto), verifier: pkce.verifier, returnTo: safeReturnTo(returnTo), }; diff --git a/packages/shared/src/oidc.ts b/packages/shared/src/oidc.ts index d68d90b2dda1e..cbd692021c48a 100644 --- a/packages/shared/src/oidc.ts +++ b/packages/shared/src/oidc.ts @@ -142,17 +142,32 @@ const base64UrlDecodeToString = (value: string): string => { return new TextDecoder().decode(Uint8Array.from(binary, (char) => char.charCodeAt(0))); }; -export const randomToken = (byteLength = 32): string => - base64UrlEncode(crypto.getRandomValues(new Uint8Array(byteLength))); +/** What sign-in needs from Web Crypto. React Native has no global `crypto` and passes its own. */ +export interface OidcCrypto { + readonly randomBytes: (byteLength: number) => Uint8Array; + readonly sha256: (data: Uint8Array) => Promise; +} + +const webCrypto: OidcCrypto = { + randomBytes: (byteLength) => crypto.getRandomValues(new Uint8Array(byteLength)), + sha256: (data) => crypto.subtle.digest("SHA-256", data), +}; + +export const randomToken = (byteLength = 32, cryptoImpl: OidcCrypto = webCrypto): string => + base64UrlEncode(cryptoImpl.randomBytes(byteLength)); export interface PkcePair { readonly verifier: string; readonly challenge: string; } -export async function createPkcePair(verifier: string = randomToken(32)): Promise { - const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)); - return { verifier, challenge: base64UrlEncode(new Uint8Array(digest)) }; +export async function createPkcePair( + verifier?: string, + cryptoImpl: OidcCrypto = webCrypto, +): Promise { + const value = verifier ?? randomToken(32, cryptoImpl); + const digest = await cryptoImpl.sha256(new TextEncoder().encode(value)); + return { verifier: value, challenge: base64UrlEncode(new Uint8Array(digest)) }; } export function buildAuthorizeUrl(input: { From 224b5ef501e9a11bfae74e85642e2b7452ad8050 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Wed, 11 Nov 2026 01:32:54 +0100 Subject: [PATCH 15/29] fix(connect): harden relay channels and OIDC sign-in after review A review of the self-hosted relay branch found that a crafted URL could crash a carried environment, that WebSockets through a relay channel always failed their DPoP check, and that several hosted and OIDC paths broke. Relay channels: - refuse fragments on both sides and catch frame errors, so a request can no longer throw out of the channel listener and end the environment's server; - give the environment the relay's public host (rewritten before channel routing, honored through X-Forwarded-Host with the relay prefix), so DPoP URLs match for WebSockets and behind proxies that rewrite Host; - cap bodies at 64 MiB with a 413, abort work the relay stopped waiting for, and drop Expect, instead of tearing the channel down; - end replaced and silent channels, close the environment's socket however the client side ends, and back off reconnects up to 5 minutes; - keep the environment's document sandbox (scripts in an opaque origin) and let PDFs through, and route OAuth well-known URLs under /env/; - carry publish-only links when manual endpoints are off. Also: MCP OAuth metadata and voice input keep the /env/ prefix; the Clerk account pages get the app atom registry; the MCP wizard and agent approval read the provider-neutral auth instead of Clerk; OIDC users get the relay's oidc:| account id; a sign-out during a refresh sticks; Android ignores the OIDC redirect as a navigation link; the Node relay draws its APNs job secret once and sweeps rate-limit windows once a period. --- apps/mobile/src/lib/appLinking.test.ts | 13 ++- apps/mobile/src/lib/appLinking.ts | 7 ++ apps/server/src/auth/McpOAuth.ts | 9 +- .../src/cloud/RelayChannelClient.test.ts | 29 +---- apps/server/src/cloud/RelayChannelClient.ts | 88 ++++++++++++--- apps/server/src/mcp/McpHttpServer.ts | 6 +- apps/server/src/requestUrl.test.ts | 41 +++++-- apps/server/src/requestUrl.ts | 25 ++++- .../components/auth/ApproveAgentSurface.tsx | 14 +-- .../components/clerk/ClerkCloudAuthBridge.tsx | 4 +- .../components/mcp/UseInAnotherAppDialog.tsx | 10 +- infra/relay/src/auth/SubjectTokens.test.ts | 9 +- infra/relay/src/auth/SubjectTokens.ts | 9 +- .../environments/EnvironmentLinker.test.ts | 103 +++++++++--------- .../src/environments/EnvironmentLinker.ts | 11 +- .../environments/RelayChannelEndpoint.test.ts | 12 ++ .../src/environments/RelayChannelEndpoint.ts | 15 ++- infra/relay/src/host/RelayChannels.test.ts | 29 ++++- infra/relay/src/host/RelayChannels.ts | 88 ++++++++++++--- infra/relay/src/host/node.ts | 22 ++-- infra/relay/src/http/Api.ts | 3 +- infra/relay/src/node.ts | 6 +- .../src/relay/oidcController.test.ts | 36 +++++- .../src/relay/oidcController.ts | 10 +- .../src/voice-input/environment.ts | 10 +- packages/contracts/src/relay.ts | 8 ++ packages/shared/src/oidc.ts | 26 +++-- 27 files changed, 446 insertions(+), 197 deletions(-) diff --git a/apps/mobile/src/lib/appLinking.test.ts b/apps/mobile/src/lib/appLinking.test.ts index 8b873318d72aa..1f5ae58647543 100644 --- a/apps/mobile/src/lib/appLinking.test.ts +++ b/apps/mobile/src/lib/appLinking.test.ts @@ -26,12 +26,13 @@ describe("shouldHandleAppLink", () => { expect(shouldHandleAppLink(url, true)).toBe(true); }); - it.each(["t3code://expo-development-client/?url=x", "t3code://expo-sharing/anything"])( - "ignores lifecycle URL %s", - (url) => { - expect(shouldHandleAppLink(url, true)).toBe(false); - }, - ); + it.each([ + "t3code://expo-development-client/?url=x", + "t3code://expo-sharing/anything", + "t3code-preview://oidc?code=c&state=s", + ])("ignores lifecycle URL %s", (url) => { + expect(shouldHandleAppLink(url, true)).toBe(false); + }); }); describe("cloud-only deep links", () => { diff --git a/apps/mobile/src/lib/appLinking.ts b/apps/mobile/src/lib/appLinking.ts index 570c86b36abf4..db3963431ebde 100644 --- a/apps/mobile/src/lib/appLinking.ts +++ b/apps/mobile/src/lib/appLinking.ts @@ -13,10 +13,17 @@ export function shouldHandleAppLink(url: string, cloudConfigured: boolean): bool !route.includes("expo-development-client") && !route.includes("://expo-sharing") && !/^t3code(-dev|-preview)?:\/*$/.test(url) && + !OIDC_REDIRECT_PATH.test(route) && (cloudConfigured || !CLOUD_ONLY_LINK_PATH.test(route)) ); } +/** + * Where the identity provider sends a sign-in back. On Android it also arrives as an app link, + * but the sign-in's browser session consumes it; navigating would open the NotFound route. + */ +const OIDC_REDIRECT_PATH = /^[a-z][\w+.-]*:\/*(?:[^/]*\/--\/)?oidc\/*$/i; + /** * Screens that only exist with T3 Connect configured. A build without it has * no entry points to them, so a link must not open one empty. Anchored to the diff --git a/apps/server/src/auth/McpOAuth.ts b/apps/server/src/auth/McpOAuth.ts index 426cb2213f3ef..24ae3e45b11f1 100644 --- a/apps/server/src/auth/McpOAuth.ts +++ b/apps/server/src/auth/McpOAuth.ts @@ -46,7 +46,7 @@ import { deriveAuthClientMetadata, timingSafeEqualBase64Url, } from "./utils.ts"; -import { requestUrl } from "../requestUrl.ts"; +import { publicBaseUrl } from "../requestUrl.ts"; // Request-derived origins support loopback, Tailscale and tunnels. // Signed client metadata keeps unauthenticated registration stateless. @@ -69,11 +69,8 @@ export interface McpOAuthUrls { } export const requestUrls = (request: HttpServerRequest.HttpServerRequest): McpOAuthUrls => { - const origin = Option.match(requestUrl(request), { - onNone: () => "http://localhost", - onSome: (url) => url.origin, - }); - return { issuer: origin, resource: `${origin}/mcp` }; + const base = Option.getOrElse(publicBaseUrl(request), () => "http://localhost"); + return { issuer: base, resource: `${base}/mcp` }; }; export const protectedResourceMetadata = ( diff --git a/apps/server/src/cloud/RelayChannelClient.test.ts b/apps/server/src/cloud/RelayChannelClient.test.ts index 4add59e2cde38..96371da3ecbb3 100644 --- a/apps/server/src/cloud/RelayChannelClient.test.ts +++ b/apps/server/src/cloud/RelayChannelClient.test.ts @@ -1,32 +1,15 @@ import { describe, expect, it } from "@effect/vitest"; -import { forwardedRequestHeaders, localTargetUrl } from "./RelayChannelClient.ts"; +import { localTargetUrl } from "./RelayChannelClient.ts"; describe("localTargetUrl", () => { it.each([ - ["a path", "/api/state?x=1", "http://127.0.0.1:3773/api/state?x=1"], - ["an absolute URL", "http://169.254.169.254/latest", null], - ["a protocol-relative URL", "//evil.test/x", null], - ["a relative path", "api/state", null], + ["an origin-relative path", "/ws?wsTicket=a", "http://127.0.0.1:3773/ws?wsTicket=a"], + ["a protocol-relative path", "//evil.test/x", null], + ["a relative path", "ws", null], + // The WebSocket constructor throws on a fragment, inside the channel's message listener. + ["a fragment", "/ws?a#b", null], ] as const)("maps %s", (_name, path, expected) => { expect(localTargetUrl("http://127.0.0.1:3773/", path)).toBe(expected); }); }); - -describe("forwardedRequestHeaders", () => { - it("presents the public host over HTTPS and drops connection headers", () => { - expect( - forwardedRequestHeaders({ - host: "t3-abc.relay.test", - connection: "upgrade", - "sec-websocket-key": "k", - authorization: "DPoP token", - }), - ).toEqual({ - host: "t3-abc.relay.test", - authorization: "DPoP token", - "x-forwarded-proto": "https", - "x-forwarded-host": "t3-abc.relay.test", - }); - }); -}); diff --git a/apps/server/src/cloud/RelayChannelClient.ts b/apps/server/src/cloud/RelayChannelClient.ts index 26e756461c575..2abf7d55fbb82 100644 --- a/apps/server/src/cloud/RelayChannelClient.ts +++ b/apps/server/src/cloud/RelayChannelClient.ts @@ -1,6 +1,12 @@ -import { RelayChannelFrame } from "@t3tools/contracts/relay"; +import { + RELAY_CHANNEL_MAX_BODY_BYTES, + RELAY_CHANNEL_RESPONSE_TIMEOUT_MS, + RelayChannelFrame, +} from "@t3tools/contracts/relay"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; +import * as Random from "effect/Random"; import * as Schedule from "effect/Schedule"; import * as Schema from "effect/Schema"; import { request as undiciRequest, WebSocket, type Dispatcher } from "undici"; @@ -15,13 +21,16 @@ const CONNECTION_HEADERS = new Set([ "upgrade", "transfer-encoding", "content-length", + // The relay already holds the whole body, so there is nothing to wait for. + "expect", "sec-websocket-key", "sec-websocket-version", "sec-websocket-extensions", "sec-websocket-accept", ]); const KEEPALIVE_INTERVAL = "25 seconds"; -const RECONNECT_DELAY = "3 seconds"; +const RECONNECT_DELAY = Duration.seconds(3); +const MAX_RECONNECT_DELAY = Duration.minutes(5); const decodeFrame = Schema.decodeUnknownOption(RelayChannelFrame); @@ -35,7 +44,7 @@ function parseFrame(data: unknown): RelayChannelFrame | null { } /** Requests reach this server as they would through a TLS-terminating tunnel. */ -export function forwardedRequestHeaders( +function forwardedRequestHeaders( headers: Readonly>, ): Record { const forwarded = Object.fromEntries( @@ -48,9 +57,12 @@ export function forwardedRequestHeaders( }; } -/** The local URL for a forwarded path; anything but an origin-relative path is refused. */ +/** + * The local URL for a forwarded path; anything but an origin-relative path is refused, and so is + * a fragment, which a WebSocket URL may not carry. + */ export function localTargetUrl(localOrigin: string, path: string): string | null { - return /^\/(?!\/)/u.test(path) ? `${localOrigin.replace(/\/+$/u, "")}${path}` : null; + return /^\/(?!\/)[^#]*$/u.test(path) ? `${localOrigin.replace(/\/+$/u, "")}${path}` : null; } function flattenHeaders(headers: Readonly>) { @@ -71,8 +83,22 @@ async function forwardHttp(localOrigin: string, frame: HttpRequestFrame) { method: frame.method as Dispatcher.HttpMethod, headers: forwardedRequestHeaders(frame.headers), ...(frame.body ? { body: Buffer.from(frame.body, "base64") } : {}), + // The relay stops waiting then, so the work is dropped with it. + signal: AbortSignal.timeout(RELAY_CHANNEL_RESPONSE_TIMEOUT_MS), }); - const body = Buffer.from(await response.body.arrayBuffer()); + // shortcut: a response crosses the channel whole, so streams and bodies over the cap fail; + // send bodies in chunked frames if streaming or large downloads must work over a relay. + const chunks: Array = []; + let size = 0; + for await (const chunk of response.body) { + size += (chunk as Buffer).byteLength; + if (size > RELAY_CHANNEL_MAX_BODY_BYTES) { + response.body.destroy(); + return failed; + } + chunks.push(chunk as Buffer); + } + const body = Buffer.concat(chunks); return { type: "http-response", id: frame.id, @@ -119,6 +145,12 @@ function openLocalSocket( return socket; } +interface ChannelEnd { + /** Whether the relay accepted the channel before it ended. */ + readonly opened: boolean; + readonly reason: string; +} + function openChannel(input: RelayChannelInput) { const channel = new WebSocket(input.channelUrl, { headers: { authorization: `Bearer ${input.token}` }, @@ -137,9 +169,7 @@ function openChannel(input: RelayChannelInput) { channel.addEventListener("open", () => { opened = true; }); - channel.addEventListener("message", (event) => { - heard = true; - const frame = parseFrame(event.data); + const handleFrame = (frame: RelayChannelFrame | null) => { switch (frame?.type) { case "http-request": void forwardHttp(input.localOrigin, frame).then(send); @@ -167,12 +197,22 @@ function openChannel(input: RelayChannelInput) { default: return; } + }; + channel.addEventListener("message", (event) => { + heard = true; + const frame = parseFrame(event.data); + try { + handleFrame(frame); + } catch { + // A listener's exception is rethrown as an uncaught one; one frame must not end this server. + if (frame?.type === "ws-open") send({ type: "ws-close", id: frame.id, code: 1011 }); + } }); - const closed = new Promise((resolve) => { - end = resolve; + const closed = new Promise((resolve) => { + end = (reason) => resolve({ opened, reason }); channel.addEventListener("close", (event) => { closeLocals(); - resolve(opened ? `closed with code ${event.code}` : "could not connect"); + end(opened ? `closed with code ${event.code}` : "could not connect"); }); }); return { @@ -223,9 +263,29 @@ export const runRelayChannel = Effect.fn("RelayChannelClient.run")(function* ( yield* Effect.logInfo("Connecting the T3 Connect relay channel", { channelUrl: input.channelUrl, }); + let failures = 0; return yield* connectOnce(input).pipe( - Effect.tap((reason) => Effect.logWarning("T3 Connect relay channel ended", { reason })), - Effect.andThen(Effect.sleep(RECONNECT_DELAY)), + Effect.tap((ended) => { + failures = ended.opened ? 0 : failures + 1; + return Effect.logWarning("T3 Connect relay channel ended", { reason: ended.reason }); + }), + Effect.andThen(() => reconnectDelay(failures)), + Effect.flatMap(Effect.sleep), Effect.forever, ); }); + +/** + * Doubles after each attempt the relay refused, up to a cap, so a revoked or unreachable channel + * is not retried every few seconds; jitter spreads environments out after a relay restart. + */ +const reconnectDelay = (failures: number) => + Effect.map(Random.next, (jitter) => + Duration.times( + Duration.min( + Duration.times(RECONNECT_DELAY, 2 ** Math.min(failures, 10)), + MAX_RECONNECT_DELAY, + ), + 0.5 + jitter / 2, + ), + ); diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index f838412f14f8c..22790928368e5 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -78,11 +78,11 @@ import { } from "./toolkits/device/tools.ts"; import * as HtmlHandlers from "./toolkits/html/handlers.ts"; import { HtmlPreviewTool, HtmlPreviewToolkit, HtmlRenderToolkit } from "./toolkits/html/tools.ts"; -import { requestUrl } from "../requestUrl.ts"; +import { publicBaseUrl } from "../requestUrl.ts"; const mcpResourceMetadataUrl = (request: HttpServerRequest.HttpServerRequest) => - requestUrl(request).pipe( - Option.map((url) => `${url.origin}/.well-known/oauth-protected-resource/mcp`), + publicBaseUrl(request).pipe( + Option.map((base) => `${base}/.well-known/oauth-protected-resource/mcp`), ); /** Launched agents cannot open OAuth browsers; OAuth would mint credentials outliving their session. */ diff --git a/apps/server/src/requestUrl.test.ts b/apps/server/src/requestUrl.test.ts index 1d5e7593eafc6..4100985ef4a75 100644 --- a/apps/server/src/requestUrl.test.ts +++ b/apps/server/src/requestUrl.test.ts @@ -4,23 +4,48 @@ import { Headers, HttpServerRequest } from "effect/http"; import { publicRequestUrl } from "./requestUrl.ts"; -const relayed = (prefix: string | undefined) => +const relayed = (prefix: string | undefined, forwardedHost?: string) => HttpServerRequest.fromWeb(new Request("http://relay.example.test/api/state?x=1")).modify({ headers: Headers.fromInput({ - host: "relay.example.test", + // A channel's WebSocket reaches the server with its local address as Host. + host: forwardedHost === undefined ? "relay.example.test" : "127.0.0.1:3773", "x-forwarded-proto": "https", ...(prefix === undefined ? {} : { "x-forwarded-prefix": prefix }), + ...(forwardedHost === undefined ? {} : { "x-forwarded-host": forwardedHost }), }), }); describe("publicRequestUrl", () => { it.each([ - ["a relay prefix", "/env/abc", "https://relay.example.test/env/abc/api/state?x=1"], - ["no prefix", undefined, "https://relay.example.test/api/state?x=1"], - ["a protocol-relative prefix", "//evil.test", "https://relay.example.test/api/state?x=1"], - ["a trailing slash", "/env/abc/", "https://relay.example.test/api/state?x=1"], - ] as const)("keeps %s", (_name, prefix, expected) => { - expect(Option.map(publicRequestUrl(relayed(prefix)), (url) => url.href)).toEqual( + ["a relay prefix", "/env/abc", undefined, "https://relay.example.test/env/abc/api/state?x=1"], + ["no prefix", undefined, undefined, "https://relay.example.test/api/state?x=1"], + [ + "a protocol-relative prefix", + "//evil.test", + undefined, + "https://relay.example.test/api/state?x=1", + ], + ["a trailing slash", "/env/abc/", undefined, "https://relay.example.test/api/state?x=1"], + [ + "the relay's host", + "/env/abc", + "relay.example.test", + "https://relay.example.test/env/abc/api/state?x=1", + ], + [ + "a local host without a prefix", + undefined, + "evil.test", + "https://127.0.0.1:3773/api/state?x=1", + ], + [ + "a local host for a malformed host", + "/env/abc", + "evil.test/x", + "https://127.0.0.1:3773/env/abc/api/state?x=1", + ], + ] as const)("keeps %s", (_name, prefix, forwardedHost, expected) => { + expect(Option.map(publicRequestUrl(relayed(prefix, forwardedHost)), (url) => url.href)).toEqual( Option.some(expected), ); }); diff --git a/apps/server/src/requestUrl.ts b/apps/server/src/requestUrl.ts index 3af122589aca4..8fe606a144b69 100644 --- a/apps/server/src/requestUrl.ts +++ b/apps/server/src/requestUrl.ts @@ -14,18 +14,37 @@ export const requestUrl = (request: HttpServerRequest.HttpServerRequest): Option ); const FORWARDED_PREFIX = /^\/(?!\/)[^?#\s]*[^/?#\s]$/u; +const FORWARDED_HOST = /^(?:[a-z0-9.-]+|\[[0-9a-f:.]+\])(?::\d{1,5})?$/iu; /** * The URL the client addressed. A path-routing relay serves this server under a prefix such as - * `/env/` and strips it, saying so in `X-Forwarded-Prefix`; proofs sign the full URL. + * `/env/` and strips it, saying so in `X-Forwarded-Prefix`; proofs sign the full URL. Its + * WebSockets reach this server with a local Host, so the relay's host comes in `X-Forwarded-Host`. */ export const publicRequestUrl = ( request: HttpServerRequest.HttpServerRequest, ): Option.Option => Option.map(requestUrl(request), (url) => { - const prefix = request.headers["x-forwarded-prefix"]; - if (prefix !== undefined && FORWARDED_PREFIX.test(prefix)) { + const prefix = forwardedPrefix(request); + if (prefix !== undefined) { url.pathname = `${prefix}${url.pathname}`; + const host = request.headers["x-forwarded-host"]; + if (host !== undefined && FORWARDED_HOST.test(host)) { + // Setting a host without a port keeps the old port. + url.port = ""; + url.host = host; + } } return url; }); + +/** Where this server's routes start for the client: its public origin, plus a relay's prefix. */ +export const publicBaseUrl = ( + request: HttpServerRequest.HttpServerRequest, +): Option.Option => + Option.map(publicRequestUrl(request), (url) => `${url.origin}${forwardedPrefix(request) ?? ""}`); + +const forwardedPrefix = (request: HttpServerRequest.HttpServerRequest) => { + const prefix = request.headers["x-forwarded-prefix"]; + return prefix !== undefined && FORWARDED_PREFIX.test(prefix) ? prefix : undefined; +}; diff --git a/apps/web/src/components/auth/ApproveAgentSurface.tsx b/apps/web/src/components/auth/ApproveAgentSurface.tsx index 9f4863fe97d57..ee00579910bac 100644 --- a/apps/web/src/components/auth/ApproveAgentSurface.tsx +++ b/apps/web/src/components/auth/ApproveAgentSurface.tsx @@ -1,4 +1,3 @@ -import { useAuth, useClerk } from "@clerk/react"; import { useAtomValue } from "@effect/atom-react"; import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay"; import { @@ -10,9 +9,8 @@ import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react"; -import { isElectron } from "../../env"; +import { useCloudAuth } from "../../cloud/cloudAuth"; import { runConnectMcpApproval } from "../../state/connectMcpApproval"; -import { resolveClerkSignInProps } from "../clerk/authRedirect"; import { Button } from "../ui/button"; import { Spinner } from "../ui/spinner"; import { StandalonePage, StandalonePageHeader } from "../ui/standalone-page"; @@ -84,16 +82,13 @@ export function ApproveAgentSurface() { const [transport] = useState(() => environmentId === null ? null : connectTransport(environmentId, setEnvironment), ); - const clerk = useClerk(); - const { isLoaded, isSignedIn } = useAuth(); + const { isLoaded, isSignedIn, signIn: openSignIn, signOut } = useCloudAuth(); const session = useAtomValue(managedRelaySessionAtom); const signInOpened = useRef(false); - const openSignIn = () => - clerk.openSignIn(resolveClerkSignInProps(window.location.href, isElectron)); const openSignInOnLoad = useEffectEvent(openSignIn); - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- opens Clerk sign-in once Clerk has loaded and nobody is signed in + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- opens the sign-in once auth has loaded and nobody is signed in useEffect(() => { if (!isLoaded || isSignedIn || signInOpened.current || framed) return; signInOpened.current = true; @@ -170,7 +165,8 @@ export function ApproveAgentSurface() { )} diff --git a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx index 522380e5e04f9..848681ffdc4e8 100644 --- a/apps/web/src/components/oidc/OidcManagedAuthShell.tsx +++ b/apps/web/src/components/oidc/OidcManagedAuthShell.tsx @@ -1,8 +1,10 @@ import type { OidcController } from "@t3tools/client-runtime/relay"; +import { msg, useGT } from "gt-react"; import { type ReactNode, useEffect, useState, useSyncExternalStore } from "react"; import { CloudAuthProvider, type CloudAuth } from "../../cloud/cloudAuth"; import { createManagedRelayAccountService } from "../../cloud/managedAuth"; +import { m } from "../../i18n/messages"; import { AppAtomRegistryProvider } from "../../rpc/atomRegistry"; import { getBrowserOidcController, @@ -20,8 +22,9 @@ const accountService = createManagedRelayAccountService( function reportSignInFailure(cause: unknown) { toastManager.add({ type: "error", - title: "Could not start sign-in", - description: cause instanceof Error ? cause.message : "The identity provider did not respond.", + title: m(msg("Could not start sign-in")), + description: + cause instanceof Error ? cause.message : m(msg("The identity provider did not respond.")), }); } @@ -34,6 +37,7 @@ function ConfiguredOidcShell({ }) { const snapshot = useSyncExternalStore(controller.subscribe, controller.getSnapshot); const [accountOpen, setAccountOpen] = useState(false); + const gt = useGT(); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches the OIDC controller to the managed relay account service for the provider's lifetime useEffect(() => accountService.attach(controller), [controller]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- another tab signs in, refreshes, or signs out through shared storage @@ -71,7 +75,7 @@ function ConfiguredOidcShell({ {/* This shell wraps the app root, so the page needs the app's atom registry itself. */} - + diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index 648fd58859dbe..77856fb7f426d 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -495,6 +495,7 @@ "4145793647e296fe": "Das Relay hat keine aktive Verknüpfung für diese Umgebung. Falls sie bei T3 Connect abgemeldet wurde, verknüpfe sie auf diesem Rechner erneut.", "10c07be640be8c23": "Der T3-Connect-Tunnel der Umgebung wird noch eingerichtet.", "08c3ae49e073cdc8": "Diese Umgebung ist nur zum Veröffentlichen von Agentenaktivität verknüpft und kann daher keine Verbindungen annehmen. Öffne auf diesem Computer Einstellungen > Freigabe & Zugriff und aktiviere T3 Connect.", + "fc86842c4bb9d8c0": "Das Relay darf die von dieser Umgebung veröffentlichte Adresse nicht kontaktieren. Verwende eine HTTPS-Adresse, die das Relay erreichen kann und die nicht localhost ist. Private Netzwerkadressen funktionieren nur mit einem Relay, das sie erlaubt.", "a4429a85fbe329d7": "Das Relay hat die Verbindungsanfrage der Umgebung abgelehnt ({reason}).", "745e4a36c4216140": "Das Relay hat die Verbindungsanfrage der Umgebung abgelehnt.", "c406def16dbd8691": "Das Relay konnte den Endpunkt der Umgebung nicht erreichen ({reason}).", @@ -4760,8 +4761,19 @@ "3260726e13a6882b": "Installiere T3 Code auf deinem Smartphone und melde dich bei T3 Connect an, um Push-Benachrichtigungen und Live-Aktivitäten zu erhalten.", "de111af2fe5653a6": "Mobile Clients konnten nicht geladen werden", "5d138bbbce733537": "T3-Connect-Einstellungen", + "fb516055076656ee": "Sie wird auch aus deinem Relay-Konto entfernt und erscheint daher nicht wieder auf deinen Geräten.", "ff2d5256c7b57d7f": "Es ist nicht mehr in deinem T3-Connect-Konto.", + "c7902d95bd57a25f": "Wird entfernt…", + "2c5674665579a329": "Entfernen", "fba00de8e861f8c1": "T3-Connect-Einstellungen", + "a3538ba5ca802bd7": [ + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + " entfernen?" + ], "83a0e763474f61be": [ { "i": 1, @@ -4852,11 +4864,15 @@ }, " und wähle dann „Aktualisieren“." ], + "41aa06651d90940a": "Von T3 Connect abmelden", + "112e5facdfd81300": "Abmelden ({account})", + "a656dc69a38a3639": "Abmelden", "cf09d31e268af127": "T3 Connect erneut versuchen", "6fa695d3a822bb9e": "Bei T3 Connect anmelden", "b28a147f66b0e088": "Verknüpfungsdatum nicht verfügbar", "6f0589fb1a5e38a9": "Verknüpft {date}", "ba921b079703d6d5": "Verwalteter Tunnel", + "06651d35dc2f21a2": "Über das Relay", "a8716f4cb9fcf7bb": "Nur Aktivitätsveröffentlichung", "e535cf8977e2a253": "Abmeldung von {environment} bestätigen", "c36af6045c0cf0c3": "Wird abgemeldet…", @@ -5890,6 +5906,15 @@ "dbd798f9304e4743": "Dieses Bild ist zu groß oder hat keine verwendbaren Abmessungen. Speichere es stattdessen.", "9c50c483b1d97631": "Bilder können in diesem Browser nicht kopiert werden.", "a2a3fa5baf64305c": "Das Bild konnte nicht in PNG umgewandelt werden.", + "cedaf63585cfbc1c": "Für diesen Build ist kein OpenID-Connect-Anbieter konfiguriert.", + "117d1d150ac00822": "Die Anmeldung konnte nicht abgeschlossen werden.", + "5793a07d88edde04": "Du wirst angemeldet", + "fa6662dcf0452f92": "Anmeldung nicht abgeschlossen", + "46cd220e658b94db": "Anmeldung bei deinem Identitätsanbieter wird abgeschlossen…", + "4189d6c508b9c1f1": "Die Anmeldung konnte nicht gestartet werden.", + "a03de172b2fd7089": "Anmeldung konnte nicht gestartet werden", + "3fb16cb1f079c67a": "Der Identitätsanbieter hat nicht geantwortet.", + "685dbb084305a2ee": "T3 Connect-Konto", "26dca8ee72dc5d74": "Einstellungen konnten nicht gelesen werden", "52e7a660ce883609": "Verbindung wird noch hergestellt", "9a50b20569f2c9e7": "Deine gespeicherten Einstellungen konnten nicht geladen werden.", @@ -8326,7 +8351,6 @@ "8fef1ad9234284f7": "Standard", "ca384fd1a1d6e1bb": "Optionen für {name}", "8b45e296e25933cb": "Verbinde dich mit einer Umgebung, um Profildaten zu löschen", - "c7902d95bd57a25f": "Wird entfernt…", "4a42ed8f9a4c6750": "Profil entfernen", "90eae6ed93306de1": "Systemeinstellungen konnten nicht geöffnet werden", "e80b0936736a6dd8": "Öffne „Datenschutz & Sicherheit → Festplattenvollzugriff“ manuell.", @@ -9560,14 +9584,6 @@ "13d4f258e93f1dc1": "Verbinde eine Umgebung, um die Geheimnisanbieter zu verwalten, aus denen ihre Anbietervariablen lesen.", "88466f5a03be2419": "Umgebungsweite Einstellung. Wähle eine Umgebung, um sie zu ändern.", "111a44209dc6e701": "Entfernen", - "a3538ba5ca802bd7": [ - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - " entfernen?" - ], "0d5b3b39f92c04e4": "Anbieter entfernen", "c52bdbe39649742c": [ "Nichts liest aus ", @@ -10711,6 +10727,20 @@ "a899361f5deb3476": "Beliebige lokale Dateien", "8c92f29a2b29adb5": "Nicht committete Änderungen", "deb572d6e83f3818": "Bearbeitete versionierte Dateien", + "2689839de807d3d6": "Selbst gehostetes Relay", + "1b768d8eedf9b5ed": "Das Relay ist nicht erreichbar.", + "1e0a645ef3661af3": "Dienst", + "9e35c6e0d4bfda3e": "Verwendet das Relay unter {relay}, Anmeldung über {issuer}.", + "d5ac081bf01ea146": "T3 Connect verbindet deine Geräte über das Relay von T3. Wähle ein selbst gehostetes Relay, um deinen eigenen Server und Anmeldeanbieter zu nutzen.", + "08138afa321f491d": "T3 Connect-Dienst", + "b232816704c22aa2": "Relay-Adresse", + "8f7efe3ac6305217": "Die Anmeldedaten werden vom Relay gelesen. Nach dem Wechsel musst du dich erneut anmelden.", + "5f69bb7c4858a66e": "Relay verwenden", + "a7b5f0e467829859": "Mit deinem Relay-Konto verknüpfte Umgebungen. Melde eine ab, die du nicht mehr nutzt.", + "8d35d826788b135c": "Zurück zu T3 Connect wechseln", + "0e6e4b0e2ac50c21": "Du musst dich erneut anmelden, und mit deinem Relay verknüpfte Umgebungen bleiben dort, bis du sie erneut verknüpfst.", + "9f90d63bcad3e2a7": "Verwalten", + "f63e98992f71e591": "T3 Connect verwenden", "83729e5228731819": "Traces-Endpunkt", "8739ffcda2007458": "Metriken-Endpunkt", "5c90c5ba936fb908": "Logs-Endpunkt", @@ -10775,7 +10805,6 @@ "ebffe4dabf52835f": "{name} aktivieren", "b663e2e129c61493": "Textgenerator hinzufügen", "149ffa363168a419": "Schreibt Thread-Titel, Commit-Nachrichten und andere kurze Texte, ohne einen Coding-Agenten zu starten. Dein API-Schlüssel wird in dieser Umgebung gespeichert.", - "1e0a645ef3661af3": "Dienst", "bdbab71cbcc8642a": "API-Schlüssel (optional)", "f733ede3caa103f3": "z. B. Mein lokales Modell", "798d0083bcd7a233": "{count, plural, one {{name} ist verbunden und listet {count} Modell. Stelle beliebige dieser Einträge darauf um oder behalte, was sie jetzt verwenden.} other {{name} ist verbunden und listet {count} Modelle. Stelle beliebige dieser Einträge darauf um oder behalte, was sie jetzt verwenden.}}", @@ -11555,6 +11584,7 @@ "1c3e0ac06c8a1f1e": "verbindungen server backend lokal remote zugriff administrativ berechtigungen bereich kopplungslinks qr-code autorisierte clients sitzungen widerrufen endpunkt", "744fd34e8049efeb": "verknüpfen peer anderer rechner vps agenten dort arbeiten übergabe kopplungscode", "3d00c452287f50bd": "hinzufügen koppeln backend host code ssh konfiguration agent tunnel gespeichert t3 connect", + "17dc9f6a0355b392": "selbst gehostet relay eigener server oidc openid sso anmeldung anbieter", "cbf49f84e7f456e5": "Lastverteilung", "6f1a769805a28197": "automatisch rechner umgebung ressourcen cpu speicher kapazität präferenz gewichtung gemeinsame projekte", "563b1163723f019b": "Pull-Request-Freigabe", diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index 599944358b5bb..8794fdbf5547b 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -495,6 +495,7 @@ "4145793647e296fe": "Relay has no active link for this environment. If it was deregistered from T3 Connect, link it again on that machine.", "10c07be640be8c23": "The environment's T3 Connect tunnel is still being set up.", "08c3ae49e073cdc8": "This environment is linked only to publish agent activity, so it cannot accept connections. On that computer, open Settings > Sharing & access and turn T3 Connect on.", + "fc86842c4bb9d8c0": "The relay may not contact the address this environment published. Use an HTTPS address the relay can reach that is not localhost. Private network addresses work only on a relay that allows them.", "a4429a85fbe329d7": "Relay rejected the environment connection request ({reason}).", "745e4a36c4216140": "Relay rejected the environment connection request.", "c406def16dbd8691": "Relay could not reach the environment endpoint ({reason}).", @@ -4759,8 +4760,20 @@ "3260726e13a6882b": "Install T3 Code on your phone and sign in to T3 Connect to get push notifications and Live Activities.", "de111af2fe5653a6": "Could not load mobile clients", "5d138bbbce733537": "T3 Connect settings", + "fb516055076656ee": "It's also removed from your relay account, so it doesn't come back on your devices.", "ff2d5256c7b57d7f": "It's no longer on your T3 Connect account.", + "c7902d95bd57a25f": "Removing…", + "2c5674665579a329": "Remove", "fba00de8e861f8c1": "T3 Connect settings", + "a3538ba5ca802bd7": [ + "Remove ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "Remove ", { @@ -4852,11 +4865,15 @@ }, ", then select Refresh." ], + "41aa06651d90940a": "Sign out of T3 Connect", + "112e5facdfd81300": "Sign out ({account})", + "a656dc69a38a3639": "Sign out", "cf09d31e268af127": "Retry T3 Connect", "6fa695d3a822bb9e": "Sign in to T3 Connect", "b28a147f66b0e088": "Link date unavailable", "6f0589fb1a5e38a9": "Linked {date}", "ba921b079703d6d5": "Managed tunnel", + "06651d35dc2f21a2": "Carried by the relay", "a8716f4cb9fcf7bb": "Activity publishing only", "e535cf8977e2a253": "Confirm deregistration of {environment}", "c36af6045c0cf0c3": "Deregistering…", @@ -5891,6 +5908,15 @@ "dbd798f9304e4743": "This image is too large or has no usable dimensions. Try saving it instead.", "9c50c483b1d97631": "Image copying is unavailable in this browser.", "a2a3fa5baf64305c": "The image could not be converted to PNG.", + "cedaf63585cfbc1c": "This build has no OpenID Connect provider configured.", + "117d1d150ac00822": "The sign-in could not be completed.", + "5793a07d88edde04": "Signing you in", + "fa6662dcf0452f92": "Sign-in did not complete", + "46cd220e658b94db": "Finishing the sign-in with your identity provider…", + "4189d6c508b9c1f1": "Sign-in could not start.", + "a03de172b2fd7089": "Could not start sign-in", + "3fb16cb1f079c67a": "The identity provider did not respond.", + "685dbb084305a2ee": "T3 Connect account", "26dca8ee72dc5d74": "Could not read settings", "52e7a660ce883609": "Still connecting", "9a50b20569f2c9e7": "Your saved settings could not be loaded.", @@ -8334,7 +8360,6 @@ "8fef1ad9234284f7": "Default", "ca384fd1a1d6e1bb": "{name} options", "8b45e296e25933cb": "Connect to an environment to clear profile data", - "c7902d95bd57a25f": "Removing…", "4a42ed8f9a4c6750": "Remove profile", "90eae6ed93306de1": "Could not open System Settings", "e80b0936736a6dd8": "Open Privacy & Security → Full Disk Access manually.", @@ -9568,15 +9593,6 @@ "13d4f258e93f1dc1": "Connect an environment to manage the secret providers its provider variables read from.", "88466f5a03be2419": "Environment-wide setting. Select an environment to change it.", "111a44209dc6e701": "Remove", - "a3538ba5ca802bd7": [ - "Remove ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "Remove provider", "c52bdbe39649742c": [ "Nothing reads from ", @@ -10720,6 +10736,20 @@ "a899361f5deb3476": "Any local files", "8c92f29a2b29adb5": "Uncommitted changes", "deb572d6e83f3818": "Edited tracked files", + "2689839de807d3d6": "Self-hosted relay", + "1b768d8eedf9b5ed": "Could not reach the relay.", + "1e0a645ef3661af3": "Service", + "9e35c6e0d4bfda3e": "Using the relay at {relay}, signing in through {issuer}.", + "d5ac081bf01ea146": "T3 Connect links your devices through T3's relay. Choose a self-hosted relay to use your own server and sign-in provider.", + "08138afa321f491d": "T3 Connect service", + "b232816704c22aa2": "Relay address", + "8f7efe3ac6305217": "Sign-in details are read from the relay. Signing in starts over after the switch.", + "5f69bb7c4858a66e": "Use relay", + "a7b5f0e467829859": "Environments linked to your relay account. Deregister one you no longer use.", + "8d35d826788b135c": "Switch back to T3 Connect", + "0e6e4b0e2ac50c21": "Sign-in starts over, and environments linked to your relay stay there until you link them again.", + "9f90d63bcad3e2a7": "Manage", + "f63e98992f71e591": "Use T3 Connect", "83729e5228731819": "Traces endpoint", "8739ffcda2007458": "Metrics endpoint", "5c90c5ba936fb908": "Logs endpoint", @@ -10784,7 +10814,6 @@ "ebffe4dabf52835f": "Enable {name}", "b663e2e129c61493": "Add a text generator", "149ffa363168a419": "Writes thread titles, commit messages, and other short text without starting a coding agent. Your API key is stored on this environment.", - "1e0a645ef3661af3": "Service", "bdbab71cbcc8642a": "API key (optional)", "f733ede3caa103f3": "e.g. My local model", "798d0083bcd7a233": "{count, plural, one {{name} is connected and lists {count} model. Switch any of these to it, or keep what they use now.} other {{name} is connected and lists {count} models. Switch any of these to it, or keep what they use now.}}", @@ -11563,6 +11592,7 @@ "1c3e0ac06c8a1f1e": "connections server backend local remote access administrative permissions scope pairing links qr code authorized clients sessions revoke endpoint", "744fd34e8049efeb": "link peer another machine vps agents work there handoff pairing code", "3d00c452287f50bd": "add pair backend host code ssh config agent tunnel saved t3 connect", + "17dc9f6a0355b392": "self-hosted relay own server oidc openid sso sign-in provider", "cbf49f84e7f456e5": "Load balancing", "6f1a769805a28197": "automatic machine environment resources cpu memory capacity preference weight shared projects", "563b1163723f019b": "Pull request sharing", diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index 956f4e17a9617..7103236bd7df3 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -495,6 +495,7 @@ "4145793647e296fe": "El relay no tiene ningún vínculo activo para este entorno. Si se dio de baja de T3 Connect, vuelve a vincularlo en esa máquina.", "10c07be640be8c23": "El túnel de T3 Connect del entorno aún se está configurando.", "08c3ae49e073cdc8": "Este entorno solo está vinculado para publicar la actividad de los agentes, así que no puede aceptar conexiones. En ese equipo, abre Ajustes > Compartir y acceso y activa T3 Connect.", + "fc86842c4bb9d8c0": "El relay no puede contactar con la dirección que publicó este entorno. Usa una dirección HTTPS que el relay pueda alcanzar y que no sea localhost. Las direcciones de red privada solo funcionan con un relay que las permita.", "a4429a85fbe329d7": "El relay rechazó la solicitud de conexión al entorno ({reason}).", "745e4a36c4216140": "El relay rechazó la solicitud de conexión al entorno.", "c406def16dbd8691": "El relay no pudo acceder al endpoint del entorno ({reason}).", @@ -4757,8 +4758,20 @@ "3260726e13a6882b": "Instala T3 Code en tu teléfono e inicia sesión en T3 Connect para recibir notificaciones push y Actividades en directo.", "de111af2fe5653a6": "No se pudieron cargar los clientes móviles", "5d138bbbce733537": "Ajustes de T3 Connect", + "fb516055076656ee": "También se elimina de tu cuenta del relay, así que no vuelve a aparecer en tus dispositivos.", "ff2d5256c7b57d7f": "Ya no está en tu cuenta de T3 Connect.", + "c7902d95bd57a25f": "Quitando…", + "2c5674665579a329": "Eliminar", "fba00de8e861f8c1": "Ajustes de T3 Connect", + "a3538ba5ca802bd7": [ + "¿Quitar ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "¿Quitar ", { @@ -4850,11 +4863,15 @@ }, " y selecciona Actualizar." ], + "41aa06651d90940a": "Cerrar sesión en T3 Connect", + "112e5facdfd81300": "Cerrar sesión ({account})", + "a656dc69a38a3639": "Cerrar sesión", "cf09d31e268af127": "Reintentar T3 Connect", "6fa695d3a822bb9e": "Iniciar sesión en T3 Connect", "b28a147f66b0e088": "Fecha de vinculación no disponible", "6f0589fb1a5e38a9": "Vinculado el {date}", "ba921b079703d6d5": "Túnel gestionado", + "06651d35dc2f21a2": "A través del relay", "a8716f4cb9fcf7bb": "Solo publicación de actividad", "e535cf8977e2a253": "Confirmar la baja de {environment}", "c36af6045c0cf0c3": "Dando de baja…", @@ -5889,6 +5906,15 @@ "dbd798f9304e4743": "Esta imagen es demasiado grande o no tiene dimensiones utilizables. Prueba a guardarla.", "9c50c483b1d97631": "Este navegador no puede copiar imágenes.", "a2a3fa5baf64305c": "No se pudo convertir la imagen a PNG.", + "cedaf63585cfbc1c": "Esta compilación no tiene ningún proveedor de OpenID Connect configurado.", + "117d1d150ac00822": "No se pudo completar el inicio de sesión.", + "5793a07d88edde04": "Iniciando sesión", + "fa6662dcf0452f92": "El inicio de sesión no se completó", + "46cd220e658b94db": "Completando el inicio de sesión con tu proveedor de identidad…", + "4189d6c508b9c1f1": "No se pudo iniciar el inicio de sesión.", + "a03de172b2fd7089": "No se pudo iniciar sesión", + "3fb16cb1f079c67a": "El proveedor de identidad no respondió.", + "685dbb084305a2ee": "Cuenta de T3 Connect", "26dca8ee72dc5d74": "No se pudieron leer los ajustes", "52e7a660ce883609": "Aún conectando", "9a50b20569f2c9e7": "No se pudieron cargar tus ajustes guardados.", @@ -8324,7 +8350,6 @@ "8fef1ad9234284f7": "Predeterminado", "ca384fd1a1d6e1bb": "Opciones de {name}", "8b45e296e25933cb": "Conéctate a un entorno para borrar los datos del perfil", - "c7902d95bd57a25f": "Quitando…", "4a42ed8f9a4c6750": "Quitar perfil", "90eae6ed93306de1": "No se pudo abrir Ajustes del Sistema", "e80b0936736a6dd8": "Abre manualmente Privacidad y seguridad → Acceso total al disco.", @@ -9556,15 +9581,6 @@ "13d4f258e93f1dc1": "Conecta un entorno para gestionar los proveedores de secretos de los que leen sus variables de proveedor.", "88466f5a03be2419": "Ajuste de todo el entorno. Selecciona un entorno para cambiarlo.", "111a44209dc6e701": "Quitar", - "a3538ba5ca802bd7": [ - "¿Quitar ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "Quitar proveedor", "c52bdbe39649742c": [ "Nada lee de ", @@ -10705,6 +10721,20 @@ "a899361f5deb3476": "Cualquier archivo local", "8c92f29a2b29adb5": "Cambios sin commit", "deb572d6e83f3818": "Archivos con seguimiento editados", + "2689839de807d3d6": "Relay autoalojado", + "1b768d8eedf9b5ed": "No se pudo contactar con el relay.", + "1e0a645ef3661af3": "Servicio", + "9e35c6e0d4bfda3e": "Usando el relay en {relay}, con inicio de sesión a través de {issuer}.", + "d5ac081bf01ea146": "T3 Connect enlaza tus dispositivos a través del relay de T3. Elige un relay autoalojado para usar tu propio servidor y proveedor de inicio de sesión.", + "08138afa321f491d": "Servicio de T3 Connect", + "b232816704c22aa2": "Dirección del relay", + "8f7efe3ac6305217": "Los datos de inicio de sesión se leen del relay. Tras el cambio, tendrás que iniciar sesión de nuevo.", + "5f69bb7c4858a66e": "Usar relay", + "a7b5f0e467829859": "Entornos vinculados a tu cuenta del relay. Da de baja los que ya no uses.", + "8d35d826788b135c": "Volver a T3 Connect", + "0e6e4b0e2ac50c21": "Tendrás que iniciar sesión de nuevo, y los entornos vinculados a tu relay seguirán allí hasta que los vuelvas a vincular.", + "9f90d63bcad3e2a7": "Gestionar", + "f63e98992f71e591": "Usar T3 Connect", "83729e5228731819": "Endpoint de trazas", "8739ffcda2007458": "Endpoint de métricas", "5c90c5ba936fb908": "Endpoint de registros", @@ -10769,7 +10799,6 @@ "ebffe4dabf52835f": "Activar {name}", "b663e2e129c61493": "Añadir un generador de texto", "149ffa363168a419": "Escribe títulos de hilos, mensajes de commit y otros textos breves sin iniciar un agente de código. Tu clave de API se guarda en este entorno.", - "1e0a645ef3661af3": "Servicio", "bdbab71cbcc8642a": "Clave de API (opcional)", "f733ede3caa103f3": "p. ej., Mi modelo local", "798d0083bcd7a233": "{count, plural, one {{name} está conectado y ofrece {count} modelo. Cambia cualquiera de estos a él o mantén lo que usan ahora.} other {{name} está conectado y ofrece {count} modelos. Cambia cualquiera de estos a él o mantén lo que usan ahora.}}", @@ -11548,6 +11577,7 @@ "1c3e0ac06c8a1f1e": "conexiones servidor backend local remoto acceso administrativo permisos ámbito enlaces de emparejamiento código qr clientes autorizados sesiones revocar endpoint", "744fd34e8049efeb": "vincular par otra máquina vps agentes trabajar allí traspaso código de emparejamiento", "3d00c452287f50bd": "añadir emparejar backend host código ssh config agente túnel guardado t3 connect", + "17dc9f6a0355b392": "relay autoalojado servidor propio oidc openid sso inicio de sesión proveedor", "cbf49f84e7f456e5": "Equilibrio de carga", "6f1a769805a28197": "automático máquina entorno recursos cpu memoria capacidad preferencia peso proyectos compartidos", "563b1163723f019b": "Compartir pull requests", diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 1c982cc6d6f58..58792101318cb 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -495,6 +495,7 @@ "4145793647e296fe": "Le relais n’a aucune association active pour cet environnement. S’il a été désinscrit de T3 Connect, associez-le à nouveau sur cette machine.", "10c07be640be8c23": "Le tunnel T3 Connect de l’environnement est encore en cours de configuration.", "08c3ae49e073cdc8": "Cet environnement est associé uniquement pour publier l’activité des agents ; il ne peut donc pas accepter de connexions. Sur cet ordinateur, ouvrez Paramètres > Partage et accès et activez T3 Connect.", + "fc86842c4bb9d8c0": "Le relais n’a pas le droit de contacter l’adresse publiée par cet environnement. Utilisez une adresse HTTPS joignable par le relais et qui n’est pas localhost. Les adresses de réseau privé ne fonctionnent qu’avec un relais qui les autorise.", "a4429a85fbe329d7": "Le relais a rejeté la demande de connexion à l’environnement ({reason}).", "745e4a36c4216140": "Le relais a rejeté la demande de connexion à l’environnement.", "c406def16dbd8691": "Le relais n’a pas pu joindre le point de terminaison de l’environnement ({reason}).", @@ -4758,8 +4759,20 @@ "3260726e13a6882b": "Installez T3 Code sur votre téléphone et connectez-vous à T3 Connect pour recevoir les notifications push et les Activités en direct.", "de111af2fe5653a6": "Impossible de charger les clients mobiles", "5d138bbbce733537": "Paramètres T3 Connect", + "fb516055076656ee": "Il est aussi retiré de votre compte de relais, il ne revient donc pas sur vos appareils.", "ff2d5256c7b57d7f": "Il ne figure plus sur votre compte T3 Connect.", + "c7902d95bd57a25f": "Suppression…", + "2c5674665579a329": "Supprimer", "fba00de8e861f8c1": "Paramètres T3 Connect", + "a3538ba5ca802bd7": [ + "Supprimer ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + " ?" + ], "83a0e763474f61be": [ "Retirer ", { @@ -4851,11 +4864,15 @@ }, ", puis sélectionnez Actualiser." ], + "41aa06651d90940a": "Se déconnecter de T3 Connect", + "112e5facdfd81300": "Se déconnecter ({account})", + "a656dc69a38a3639": "Se déconnecter", "cf09d31e268af127": "Réessayer T3 Connect", "6fa695d3a822bb9e": "Se connecter à T3 Connect", "b28a147f66b0e088": "Date d’association indisponible", "6f0589fb1a5e38a9": "Associé le {date}", "ba921b079703d6d5": "Tunnel géré", + "06651d35dc2f21a2": "Acheminé par le relais", "a8716f4cb9fcf7bb": "Publication de l’activité uniquement", "e535cf8977e2a253": "Confirmer la désinscription de {environment}", "c36af6045c0cf0c3": "Désinscription…", @@ -5890,6 +5907,15 @@ "dbd798f9304e4743": "Cette image est trop grande ou n’a pas de dimensions exploitables. Essayez plutôt de l’enregistrer.", "9c50c483b1d97631": "La copie d’image n’est pas disponible dans ce navigateur.", "a2a3fa5baf64305c": "Impossible de convertir l’image en PNG.", + "cedaf63585cfbc1c": "Aucun fournisseur OpenID Connect n’est configuré pour ce build.", + "117d1d150ac00822": "La connexion n’a pas pu aboutir.", + "5793a07d88edde04": "Connexion en cours", + "fa6662dcf0452f92": "La connexion n’a pas abouti", + "46cd220e658b94db": "Finalisation de la connexion avec votre fournisseur d’identité…", + "4189d6c508b9c1f1": "La connexion n’a pas pu démarrer.", + "a03de172b2fd7089": "Impossible de lancer la connexion", + "3fb16cb1f079c67a": "Le fournisseur d’identité n’a pas répondu.", + "685dbb084305a2ee": "Compte T3 Connect", "26dca8ee72dc5d74": "Impossible de lire les paramètres", "52e7a660ce883609": "Connexion en cours", "9a50b20569f2c9e7": "Impossible de charger vos paramètres enregistrés.", @@ -8325,7 +8351,6 @@ "8fef1ad9234284f7": "Par défaut", "ca384fd1a1d6e1bb": "Options de {name}", "8b45e296e25933cb": "Connectez-vous à un environnement pour effacer les données du profil", - "c7902d95bd57a25f": "Suppression…", "4a42ed8f9a4c6750": "Supprimer le profil", "90eae6ed93306de1": "Impossible d’ouvrir les Réglages Système", "e80b0936736a6dd8": "Ouvrez manuellement Confidentialité et sécurité → Accès complet au disque.", @@ -9558,15 +9583,6 @@ "13d4f258e93f1dc1": "Connectez un environnement pour gérer les fournisseurs de secrets lus par ses variables de fournisseur.", "88466f5a03be2419": "Paramètre propre à l’environnement. Sélectionnez un environnement pour le modifier.", "111a44209dc6e701": "Supprimer", - "a3538ba5ca802bd7": [ - "Supprimer ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - " ?" - ], "0d5b3b39f92c04e4": "Supprimer le fournisseur", "c52bdbe39649742c": [ "Rien ne lit depuis ", @@ -10710,6 +10726,20 @@ "a899361f5deb3476": "Tous les fichiers locaux", "8c92f29a2b29adb5": "Modifications non commitées", "deb572d6e83f3818": "Fichiers suivis modifiés", + "2689839de807d3d6": "Relais auto-hébergé", + "1b768d8eedf9b5ed": "Impossible de joindre le relais.", + "1e0a645ef3661af3": "Service", + "9e35c6e0d4bfda3e": "Relais utilisé : {relay}, connexion via {issuer}.", + "d5ac081bf01ea146": "T3 Connect relie vos appareils via le relais de T3. Choisissez un relais auto-hébergé pour utiliser votre propre serveur et votre fournisseur de connexion.", + "08138afa321f491d": "Service T3 Connect", + "b232816704c22aa2": "Adresse du relais", + "8f7efe3ac6305217": "Les informations de connexion sont lues depuis le relais. Après le changement, il faut se reconnecter.", + "5f69bb7c4858a66e": "Utiliser le relais", + "a7b5f0e467829859": "Environnements associés à votre compte de relais. Désinscrivez ceux que vous n’utilisez plus.", + "8d35d826788b135c": "Revenir à T3 Connect", + "0e6e4b0e2ac50c21": "Il faut se reconnecter, et les environnements associés à votre relais y restent jusqu’à ce que vous les associiez à nouveau.", + "9f90d63bcad3e2a7": "Gérer", + "f63e98992f71e591": "Utiliser T3 Connect", "83729e5228731819": "Point de terminaison des traces", "8739ffcda2007458": "Point de terminaison des métriques", "5c90c5ba936fb908": "Point de terminaison des journaux", @@ -10774,7 +10804,6 @@ "ebffe4dabf52835f": "Activer {name}", "b663e2e129c61493": "Ajouter un générateur de texte", "149ffa363168a419": "Rédige les titres des fils, les messages de commit et d’autres textes courts sans démarrer d’agent de code. Votre clé d’API est stockée dans cet environnement.", - "1e0a645ef3661af3": "Service", "bdbab71cbcc8642a": "Clé d’API (facultative)", "f733ede3caa103f3": "ex. Mon modèle local", "798d0083bcd7a233": "{count, plural, one {{name} est connecté et propose {count} modèle. Basculez l’un de ces usages vers lui, ou gardez ce qu’ils utilisent actuellement.} other {{name} est connecté et propose {count} modèles. Basculez l’un de ces usages vers lui, ou gardez ce qu’ils utilisent actuellement.}}", @@ -11553,6 +11582,7 @@ "1c3e0ac06c8a1f1e": "connexions serveur backend local accès distant autorisations administratives portée liens d’appairage code qr clients autorisés sessions révoquer point de terminaison", "744fd34e8049efeb": "associer pair autre machine vps agents y travaillent transfert code d’appairage", "3d00c452287f50bd": "ajouter appairer backend hôte code ssh config agent tunnel enregistré t3 connect", + "17dc9f6a0355b392": "relais auto-hébergé propre serveur oidc openid sso connexion fournisseur", "cbf49f84e7f456e5": "Répartition de charge", "6f1a769805a28197": "automatique machine environnement ressources cpu mémoire capacité préférence poids projets partagés", "563b1163723f019b": "Partage des pull requests", diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index 879531a396d2a..50ed7b5e9f510 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -495,6 +495,7 @@ "4145793647e296fe": "リレーにこの環境の有効なリンクがありません。T3 Connect から登録解除された場合は、そのマシンで再度リンクしてください。", "10c07be640be8c23": "この環境の T3 Connect トンネルはまだ設定中です。", "08c3ae49e073cdc8": "この環境はエージェントのアクティビティを公開するためだけにリンクされているため、接続を受け付けられません。そのコンピューターで [設定] > [共有とアクセス] を開き、T3 Connect をオンにしてください。", + "fc86842c4bb9d8c0": "リレーはこの環境が公開したアドレスに接続できません。リレーから到達でき、localhost ではない HTTPS アドレスを使用してください。プライベートネットワークのアドレスは、それを許可するリレーでのみ使えます。", "a4429a85fbe329d7": "リレーが環境への接続リクエストを拒否しました ({reason})。", "745e4a36c4216140": "リレーが環境への接続リクエストを拒否しました。", "c406def16dbd8691": "リレーが環境のエンドポイントに到達できませんでした ({reason})。", @@ -4747,8 +4748,19 @@ "3260726e13a6882b": "プッシュ通知とライブアクティビティを受け取るには、スマートフォンに T3 Code をインストールして T3 Connect にサインインしてください。", "de111af2fe5653a6": "モバイルクライアントを読み込めませんでした", "5d138bbbce733537": "T3 Connect の設定", + "fb516055076656ee": "リレーアカウントからも削除されるため、お使いのデバイスに再び表示されることはありません。", "ff2d5256c7b57d7f": "T3 Connect アカウントにはもう登録されていません。", + "c7902d95bd57a25f": "削除中…", + "2c5674665579a329": "削除", "fba00de8e861f8c1": "T3 Connect の設定", + "a3538ba5ca802bd7": [ + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + " を削除しますか?" + ], "83a0e763474f61be": [ { "i": 1, @@ -4837,11 +4849,15 @@ }, " で確認してから、[更新] を選択してください。" ], + "41aa06651d90940a": "T3 Connect からサインアウト", + "112e5facdfd81300": "サインアウト({account})", + "a656dc69a38a3639": "サインアウト", "cf09d31e268af127": "T3 Connect を再試行", "6fa695d3a822bb9e": "T3 Connect にサインイン", "b28a147f66b0e088": "リンク日は不明です", "6f0589fb1a5e38a9": "{date} にリンク", "ba921b079703d6d5": "マネージドトンネル", + "06651d35dc2f21a2": "リレー経由", "a8716f4cb9fcf7bb": "アクティビティの公開のみ", "e535cf8977e2a253": "{environment} の登録解除を確認", "c36af6045c0cf0c3": "登録を解除しています…", @@ -5871,6 +5887,15 @@ "dbd798f9304e4743": "この画像は大きすぎるか、有効なサイズがありません。代わりに保存してみてください。", "9c50c483b1d97631": "このブラウザーでは画像をコピーできません。", "a2a3fa5baf64305c": "画像を PNG に変換できませんでした。", + "cedaf63585cfbc1c": "このビルドには OpenID Connect プロバイダーが設定されていません。", + "117d1d150ac00822": "サインインを完了できませんでした。", + "5793a07d88edde04": "サインインしています", + "fa6662dcf0452f92": "サインインが完了しませんでした", + "46cd220e658b94db": "ID プロバイダーでのサインインを完了しています…", + "4189d6c508b9c1f1": "サインインを開始できませんでした。", + "a03de172b2fd7089": "サインインを開始できませんでした", + "3fb16cb1f079c67a": "ID プロバイダーから応答がありませんでした。", + "685dbb084305a2ee": "T3 Connect アカウント", "26dca8ee72dc5d74": "設定を読み取れませんでした", "52e7a660ce883609": "接続中です", "9a50b20569f2c9e7": "保存された設定を読み込めませんでした。", @@ -8282,7 +8307,6 @@ "8fef1ad9234284f7": "デフォルト", "ca384fd1a1d6e1bb": "{name} のオプション", "8b45e296e25933cb": "プロファイルのデータを消去するには環境に接続してください", - "c7902d95bd57a25f": "削除中…", "4a42ed8f9a4c6750": "プロファイルを削除", "90eae6ed93306de1": "システム設定を開けませんでした", "e80b0936736a6dd8": "「プライバシーとセキュリティ」→「フルディスクアクセス」を手動で開いてください。", @@ -9512,14 +9536,6 @@ "13d4f258e93f1dc1": "プロバイダー変数の読み取り元となるシークレットプロバイダーを管理するには、環境に接続してください。", "88466f5a03be2419": "環境全体の設定です。変更するには環境を選択してください。", "111a44209dc6e701": "削除", - "a3538ba5ca802bd7": [ - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - " を削除しますか?" - ], "0d5b3b39f92c04e4": "プロバイダーを削除", "c52bdbe39649742c": [ { @@ -10660,6 +10676,20 @@ "a899361f5deb3476": "ローカルファイルがある場合", "8c92f29a2b29adb5": "コミットされていない変更", "deb572d6e83f3818": "編集された追跡対象ファイル", + "2689839de807d3d6": "セルフホストのリレー", + "1b768d8eedf9b5ed": "リレーに接続できませんでした。", + "1e0a645ef3661af3": "サービス", + "9e35c6e0d4bfda3e": "{relay} のリレーを使用し、{issuer} でサインインしています。", + "d5ac081bf01ea146": "T3 Connect は T3 のリレーを通じてデバイスをつなぎます。独自のサーバーとサインインプロバイダーを使うには、セルフホストのリレーを選択してください。", + "08138afa321f491d": "T3 Connect サービス", + "b232816704c22aa2": "リレーのアドレス", + "8f7efe3ac6305217": "サインイン情報はリレーから読み込まれます。切り替え後はサインインし直す必要があります。", + "5f69bb7c4858a66e": "リレーを使用", + "a7b5f0e467829859": "リレーアカウントにリンクされた環境です。使わなくなった環境は登録を解除してください。", + "8d35d826788b135c": "T3 Connect に戻す", + "0e6e4b0e2ac50c21": "サインインし直す必要があります。リレーにリンクされた環境は、再びリンクするまでそこに残ります。", + "9f90d63bcad3e2a7": "管理", + "f63e98992f71e591": "T3 Connect を使用", "83729e5228731819": "トレースのエンドポイント", "8739ffcda2007458": "メトリクスのエンドポイント", "5c90c5ba936fb908": "ログのエンドポイント", @@ -10726,7 +10756,6 @@ "ebffe4dabf52835f": "{name} を有効にする", "b663e2e129c61493": "テキスト生成を追加", "149ffa363168a419": "コーディングエージェントを起動せずに、スレッドのタイトル、コミットメッセージ、その他の短いテキストを作成します。API キーはこの環境に保存されます。", - "1e0a645ef3661af3": "サービス", "bdbab71cbcc8642a": "API キー (省略可能)", "f733ede3caa103f3": "例: ローカルモデル", "798d0083bcd7a233": "{count, plural, other {{name} に接続し、{count} 個のモデルが見つかりました。以下のいずれかをこれに切り替えるか、現在の設定のままにしてください。}}", @@ -11503,6 +11532,7 @@ "1c3e0ac06c8a1f1e": "接続 サーバー バックエンド ローカル リモート アクセス 管理者 権限 スコープ ペアリングリンク QR コード 認可済みクライアント セッション 取り消し エンドポイント connections", "744fd34e8049efeb": "リンク ピア 別のマシン VPS エージェント そこで作業 引き継ぎ ペアリングコード link machine", "3d00c452287f50bd": "追加 ペアリング バックエンド ホスト コード SSH 構成 エージェント トンネル 保存済み T3 Connect add pair", + "17dc9f6a0355b392": "セルフホスト リレー 独自サーバー oidc openid sso サインイン プロバイダー", "cbf49f84e7f456e5": "負荷分散", "6f1a769805a28197": "自動 マシン 環境 リソース CPU メモリ 容量 優先 重み 共有 プロジェクト automatic machine", "563b1163723f019b": "プルリクエストの共有", diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index fae8ce2ec6fc4..091fdd2fb8be2 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -495,6 +495,7 @@ "4145793647e296fe": "릴레이에 이 환경의 활성 연결이 없습니다. T3 Connect에서 등록 해제되었다면 해당 머신에서 다시 연결하세요.", "10c07be640be8c23": "환경의 T3 Connect 터널을 아직 설정하는 중입니다.", "08c3ae49e073cdc8": "이 환경은 에이전트 활동 게시용으로만 연결되어 있어 연결을 받을 수 없습니다. 해당 컴퓨터에서 설정 > 공유 및 액세스를 열고 T3 Connect를 켜세요.", + "fc86842c4bb9d8c0": "릴레이는 이 환경이 게시한 주소에 연결할 수 없습니다. 릴레이가 접근할 수 있고 localhost가 아닌 HTTPS 주소를 사용하세요. 사설 네트워크 주소는 이를 허용하는 릴레이에서만 작동합니다.", "a4429a85fbe329d7": "릴레이가 환경 연결 요청을 거부했습니다({reason}).", "745e4a36c4216140": "릴레이가 환경 연결 요청을 거부했습니다.", "c406def16dbd8691": "릴레이가 환경 엔드포인트에 연결할 수 없습니다({reason}).", @@ -4744,8 +4745,19 @@ "3260726e13a6882b": "휴대폰에 T3 Code를 설치하고 T3 Connect에 로그인하면 푸시 알림과 실시간 현황을 받을 수 있습니다.", "de111af2fe5653a6": "모바일 클라이언트를 불러올 수 없습니다", "5d138bbbce733537": "T3 Connect 설정", + "fb516055076656ee": "릴레이 계정에서도 제거되므로 기기에 다시 나타나지 않습니다.", "ff2d5256c7b57d7f": "더 이상 T3 Connect 계정에 없습니다.", + "c7902d95bd57a25f": "제거 중…", + "2c5674665579a329": "제거", "fba00de8e861f8c1": "T3 Connect 설정", + "a3538ba5ca802bd7": [ + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "을(를) 제거할까요?" + ], "83a0e763474f61be": [ "이 기기에서 ", { @@ -4834,11 +4846,15 @@ }, "에서 확인한 다음 새로 고침을 선택하세요." ], + "41aa06651d90940a": "T3 Connect에서 로그아웃", + "112e5facdfd81300": "로그아웃 ({account})", + "a656dc69a38a3639": "로그아웃", "cf09d31e268af127": "T3 Connect 다시 시도", "6fa695d3a822bb9e": "T3 Connect에 로그인", "b28a147f66b0e088": "연결 날짜를 알 수 없음", "6f0589fb1a5e38a9": "{date}에 연결됨", "ba921b079703d6d5": "관리형 터널", + "06651d35dc2f21a2": "릴레이 경유", "a8716f4cb9fcf7bb": "활동 게시 전용", "e535cf8977e2a253": "{environment} 등록 해제 확인", "c36af6045c0cf0c3": "등록 해제하는 중…", @@ -5869,6 +5885,15 @@ "dbd798f9304e4743": "이 이미지는 너무 크거나 사용할 수 있는 크기 정보가 없습니다. 대신 저장해 보세요.", "9c50c483b1d97631": "이 브라우저에서는 이미지를 복사할 수 없습니다.", "a2a3fa5baf64305c": "이미지를 PNG로 변환할 수 없습니다.", + "cedaf63585cfbc1c": "이 빌드에는 OpenID Connect 공급자가 구성되어 있지 않습니다.", + "117d1d150ac00822": "로그인을 완료할 수 없습니다.", + "5793a07d88edde04": "로그인하는 중", + "fa6662dcf0452f92": "로그인이 완료되지 않았습니다", + "46cd220e658b94db": "ID 공급자로 로그인을 마무리하는 중…", + "4189d6c508b9c1f1": "로그인을 시작할 수 없습니다.", + "a03de172b2fd7089": "로그인을 시작할 수 없음", + "3fb16cb1f079c67a": "ID 공급자가 응답하지 않았습니다.", + "685dbb084305a2ee": "T3 Connect 계정", "26dca8ee72dc5d74": "설정을 읽을 수 없습니다", "52e7a660ce883609": "아직 연결하는 중", "9a50b20569f2c9e7": "저장된 설정을 불러올 수 없습니다.", @@ -8285,7 +8310,6 @@ "8fef1ad9234284f7": "기본값", "ca384fd1a1d6e1bb": "{name} 옵션", "8b45e296e25933cb": "프로필 데이터를 지우려면 환경에 연결하세요", - "c7902d95bd57a25f": "제거 중…", "4a42ed8f9a4c6750": "프로필 제거", "90eae6ed93306de1": "시스템 설정을 열 수 없습니다", "e80b0936736a6dd8": "개인정보 보호 및 보안 → 전체 디스크 접근 권한을 직접 여세요.", @@ -9515,14 +9539,6 @@ "13d4f258e93f1dc1": "제공자 변수가 읽는 시크릿 제공자를 관리하려면 환경을 연결하세요.", "88466f5a03be2419": "환경 전체 설정입니다. 변경하려면 환경을 선택하세요.", "111a44209dc6e701": "제거", - "a3538ba5ca802bd7": [ - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "을(를) 제거할까요?" - ], "0d5b3b39f92c04e4": "제공자 제거", "c52bdbe39649742c": [ { @@ -10661,6 +10677,20 @@ "a899361f5deb3476": "모든 로컬 파일", "8c92f29a2b29adb5": "커밋되지 않은 변경 사항", "deb572d6e83f3818": "편집된 추적 파일", + "2689839de807d3d6": "자체 호스팅 릴레이", + "1b768d8eedf9b5ed": "릴레이에 연결할 수 없습니다.", + "1e0a645ef3661af3": "서비스", + "9e35c6e0d4bfda3e": "{relay}의 릴레이를 사용하며 {issuer}을(를) 통해 로그인합니다.", + "d5ac081bf01ea146": "T3 Connect는 T3의 릴레이를 통해 기기를 연결합니다. 자체 서버와 로그인 공급자를 사용하려면 자체 호스팅 릴레이를 선택하세요.", + "08138afa321f491d": "T3 Connect 서비스", + "b232816704c22aa2": "릴레이 주소", + "8f7efe3ac6305217": "로그인 정보는 릴레이에서 읽어옵니다. 전환 후에는 다시 로그인해야 합니다.", + "5f69bb7c4858a66e": "릴레이 사용", + "a7b5f0e467829859": "릴레이 계정에 연결된 환경입니다. 더 이상 사용하지 않는 환경은 등록 해제하세요.", + "8d35d826788b135c": "T3 Connect로 돌아가기", + "0e6e4b0e2ac50c21": "다시 로그인해야 하며, 릴레이에 연결된 환경은 다시 연결할 때까지 그대로 남습니다.", + "9f90d63bcad3e2a7": "관리", + "f63e98992f71e591": "T3 Connect 사용", "83729e5228731819": "추적 엔드포인트", "8739ffcda2007458": "메트릭 엔드포인트", "5c90c5ba936fb908": "로그 엔드포인트", @@ -10724,7 +10754,6 @@ "ebffe4dabf52835f": "{name} 사용", "b663e2e129c61493": "텍스트 생성기 추가", "149ffa363168a419": "코딩 에이전트를 시작하지 않고 스레드 제목, 커밋 메시지 등 짧은 텍스트를 작성합니다. API 키는 이 환경에 저장됩니다.", - "1e0a645ef3661af3": "서비스", "bdbab71cbcc8642a": "API 키(선택 사항)", "f733ede3caa103f3": "예: 내 로컬 모델", "798d0083bcd7a233": "{count, plural, other {{name}이(가) 연결되었으며 모델 {count}개가 있습니다. 아래 항목을 이 생성기로 전환하거나 현재 사용 중인 것을 유지하세요.}}", @@ -11500,6 +11529,7 @@ "1c3e0ac06c8a1f1e": "연결 서버 백엔드 로컬 원격 액세스 관리 권한 범위 페어링 링크 QR 코드 인증된 클라이언트 세션 취소 엔드포인트", "744fd34e8049efeb": "다른 머신 연결 피어 vps 에이전트 원격 작업 넘기기 페어링 코드", "3d00c452287f50bd": "추가 페어링 백엔드 호스트 코드 ssh 구성 에이전트 터널 저장됨 T3 Connect", + "17dc9f6a0355b392": "자체 호스팅 릴레이 자체 서버 oidc openid sso 로그인 공급자", "cbf49f84e7f456e5": "부하 분산", "6f1a769805a28197": "자동 머신 환경 리소스 cpu 메모리 용량 선호도 가중치 공유 프로젝트", "563b1163723f019b": "풀 리퀘스트 공유", diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index 7010bef288b16..04b91468e2da2 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -495,6 +495,7 @@ "4145793647e296fe": "O relay não tem vínculo ativo para este ambiente. Se ele foi removido do T3 Connect, vincule-o novamente naquela máquina.", "10c07be640be8c23": "O túnel do T3 Connect do ambiente ainda está sendo configurado.", "08c3ae49e073cdc8": "Este ambiente está vinculado apenas para publicar a atividade dos agentes, então não aceita conexões. Naquele computador, abra Configurações > Compartilhamento e acesso e ative o T3 Connect.", + "fc86842c4bb9d8c0": "O relay não pode contatar o endereço que este ambiente publicou. Use um endereço HTTPS que o relay consiga alcançar e que não seja localhost. Endereços de rede privada só funcionam em um relay que os permita.", "a4429a85fbe329d7": "O relay rejeitou a solicitação de conexão do ambiente ({reason}).", "745e4a36c4216140": "O relay rejeitou a solicitação de conexão do ambiente.", "c406def16dbd8691": "O relay não conseguiu acessar o endpoint do ambiente ({reason}).", @@ -4757,8 +4758,20 @@ "3260726e13a6882b": "Instale o T3 Code no seu celular e entre no T3 Connect para receber notificações push e Live Activities.", "de111af2fe5653a6": "Não foi possível carregar os clientes móveis", "5d138bbbce733537": "Configurações do T3 Connect", + "fb516055076656ee": "Ele também é removido da sua conta do relay, então não volta a aparecer nos seus dispositivos.", "ff2d5256c7b57d7f": "Ele não está mais na sua conta do T3 Connect.", + "c7902d95bd57a25f": "Removendo…", + "2c5674665579a329": "Remover", "fba00de8e861f8c1": "Configurações do T3 Connect", + "a3538ba5ca802bd7": [ + "Remover ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "Remover ", { @@ -4850,11 +4863,15 @@ }, " e selecione Atualizar." ], + "41aa06651d90940a": "Sair do T3 Connect", + "112e5facdfd81300": "Sair ({account})", + "a656dc69a38a3639": "Sair", "cf09d31e268af127": "Tentar o T3 Connect novamente", "6fa695d3a822bb9e": "Entrar no T3 Connect", "b28a147f66b0e088": "Data do vínculo indisponível", "6f0589fb1a5e38a9": "Vinculado em {date}", "ba921b079703d6d5": "Túnel gerenciado", + "06651d35dc2f21a2": "Pelo relay", "a8716f4cb9fcf7bb": "Só publicação de atividade", "e535cf8977e2a253": "Confirmar o cancelamento do registro de {environment}", "c36af6045c0cf0c3": "Cancelando o registro…", @@ -5889,6 +5906,15 @@ "dbd798f9304e4743": "Esta imagem é grande demais ou não tem dimensões utilizáveis. Tente salvá-la.", "9c50c483b1d97631": "Não é possível copiar imagens neste navegador.", "a2a3fa5baf64305c": "Não foi possível converter a imagem para PNG.", + "cedaf63585cfbc1c": "Esta build não tem nenhum provedor OpenID Connect configurado.", + "117d1d150ac00822": "Não foi possível concluir o login.", + "5793a07d88edde04": "Entrando", + "fa6662dcf0452f92": "O login não foi concluído", + "46cd220e658b94db": "Concluindo o login com seu provedor de identidade…", + "4189d6c508b9c1f1": "Não foi possível iniciar o login.", + "a03de172b2fd7089": "Não foi possível iniciar o login", + "3fb16cb1f079c67a": "O provedor de identidade não respondeu.", + "685dbb084305a2ee": "Conta do T3 Connect", "26dca8ee72dc5d74": "Não foi possível ler as configurações", "52e7a660ce883609": "Ainda conectando", "9a50b20569f2c9e7": "Não foi possível carregar suas configurações salvas.", @@ -8324,7 +8350,6 @@ "8fef1ad9234284f7": "Padrão", "ca384fd1a1d6e1bb": "Opções de {name}", "8b45e296e25933cb": "Conecte-se a um ambiente para limpar os dados do perfil", - "c7902d95bd57a25f": "Removendo…", "4a42ed8f9a4c6750": "Remover perfil", "90eae6ed93306de1": "Não foi possível abrir os Ajustes do Sistema", "e80b0936736a6dd8": "Abra Privacidade e Segurança → Acesso Total ao Disco manualmente.", @@ -9557,15 +9582,6 @@ "13d4f258e93f1dc1": "Conecte um ambiente para gerenciar os provedores de segredos de onde as variáveis de provedor dele leem.", "88466f5a03be2419": "Configuração do ambiente inteiro. Selecione um ambiente para alterá-la.", "111a44209dc6e701": "Remover", - "a3538ba5ca802bd7": [ - "Remover ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "Remover provedor", "c52bdbe39649742c": [ "Nada lê de ", @@ -10709,6 +10725,20 @@ "a899361f5deb3476": "Quaisquer arquivos locais", "8c92f29a2b29adb5": "Alterações sem commit", "deb572d6e83f3818": "Arquivos rastreados editados", + "2689839de807d3d6": "Relay auto-hospedado", + "1b768d8eedf9b5ed": "Não foi possível alcançar o relay.", + "1e0a645ef3661af3": "Serviço", + "9e35c6e0d4bfda3e": "Usando o relay em {relay}, com login por {issuer}.", + "d5ac081bf01ea146": "O T3 Connect conecta seus dispositivos pelo relay da T3. Escolha um relay auto-hospedado para usar seu próprio servidor e provedor de login.", + "08138afa321f491d": "Serviço do T3 Connect", + "b232816704c22aa2": "Endereço do relay", + "8f7efe3ac6305217": "Os dados de login são lidos do relay. Depois da troca, é preciso entrar de novo.", + "5f69bb7c4858a66e": "Usar relay", + "a7b5f0e467829859": "Ambientes vinculados à sua conta do relay. Cancele o registro dos que você não usa mais.", + "8d35d826788b135c": "Voltar para o T3 Connect", + "0e6e4b0e2ac50c21": "Será preciso entrar de novo, e os ambientes vinculados ao seu relay ficam lá até você vinculá-los outra vez.", + "9f90d63bcad3e2a7": "Gerenciar", + "f63e98992f71e591": "Usar o T3 Connect", "83729e5228731819": "Endpoint de traces", "8739ffcda2007458": "Endpoint de métricas", "5c90c5ba936fb908": "Endpoint de logs", @@ -10773,7 +10803,6 @@ "ebffe4dabf52835f": "Ativar {name}", "b663e2e129c61493": "Adicionar um gerador de texto", "149ffa363168a419": "Escreve títulos de threads, mensagens de commit e outros textos curtos sem iniciar um agente de código. Sua chave de API é armazenada neste ambiente.", - "1e0a645ef3661af3": "Serviço", "bdbab71cbcc8642a": "Chave de API (opcional)", "f733ede3caa103f3": "ex.: Meu modelo local", "798d0083bcd7a233": "{count, plural, one {{name} está conectado e lista {count} modelo. Mude qualquer um destes para ele ou mantenha o que usam agora.} other {{name} está conectado e lista {count} modelos. Mude qualquer um destes para ele ou mantenha o que usam agora.}}", @@ -11552,6 +11581,7 @@ "1c3e0ac06c8a1f1e": "conexões servidor backend local remoto acesso administrativo permissões escopo links de pareamento código qr clientes autorizados sessões revogar endpoint", "744fd34e8049efeb": "vincular par outra máquina vps agentes trabalham lá transferir código de pareamento", "3d00c452287f50bd": "adicionar parear backend host código ssh config agente túnel salvo t3 connect", + "17dc9f6a0355b392": "relay auto-hospedado servidor próprio oidc openid sso login provedor", "cbf49f84e7f456e5": "Balanceamento de carga", "6f1a769805a28197": "automático máquina ambiente recursos cpu memória capacidade preferência peso projetos compartilhados", "563b1163723f019b": "Compartilhamento de pull requests", diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index 0766d953dd02d..b0f8c33cfdd85 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -495,6 +495,7 @@ "4145793647e296fe": "У Relay нет активной привязки для этого окружения. Если его отвязали от T3 Connect, привяжите его снова на той машине.", "10c07be640be8c23": "Туннель T3 Connect для этого окружения ещё настраивается.", "08c3ae49e073cdc8": "Это окружение привязано только для публикации активности агентов и не принимает подключения. На том компьютере откройте «Настройки > Общий доступ» и включите T3 Connect.", + "fc86842c4bb9d8c0": "Relay не может обращаться к адресу, который опубликовало это окружение. Используйте HTTPS-адрес, доступный для relay и отличный от localhost. Адреса частной сети работают только с relay, который их разрешает.", "a4429a85fbe329d7": "Relay отклонил запрос на подключение к окружению ({reason}).", "745e4a36c4216140": "Relay отклонил запрос на подключение к окружению.", "c406def16dbd8691": "Relay не смог связаться с эндпоинтом окружения ({reason}).", @@ -4757,8 +4758,20 @@ "3260726e13a6882b": "Установите T3 Code на телефон и войдите в T3 Connect, чтобы получать push-уведомления и Live Activities.", "de111af2fe5653a6": "Не удалось загрузить мобильные клиенты", "5d138bbbce733537": "Настройки T3 Connect", + "fb516055076656ee": "Оно также удаляется из вашей учётной записи relay, поэтому не появится снова на ваших устройствах.", "ff2d5256c7b57d7f": "Его больше нет в вашем аккаунте T3 Connect.", + "c7902d95bd57a25f": "Удаление…", + "2c5674665579a329": "Удалить", "fba00de8e861f8c1": "Настройки T3 Connect", + "a3538ba5ca802bd7": [ + "Удалить ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "Удалить ", { @@ -4850,11 +4863,15 @@ }, ", затем нажмите «Обновить»." ], + "41aa06651d90940a": "Выйти из T3 Connect", + "112e5facdfd81300": "Выйти ({account})", + "a656dc69a38a3639": "Выйти", "cf09d31e268af127": "Повторить T3 Connect", "6fa695d3a822bb9e": "Войти в T3 Connect", "b28a147f66b0e088": "Дата привязки неизвестна", "6f0589fb1a5e38a9": "Привязано {date}", "ba921b079703d6d5": "Управляемый туннель", + "06651d35dc2f21a2": "Через relay", "a8716f4cb9fcf7bb": "Только публикация активности", "e535cf8977e2a253": "Подтвердите отмену регистрации {environment}", "c36af6045c0cf0c3": "Отмена регистрации…", @@ -5889,6 +5906,15 @@ "dbd798f9304e4743": "Изображение слишком большое или не имеет пригодных размеров. Попробуйте сохранить его.", "9c50c483b1d97631": "Копирование изображений недоступно в этом браузере.", "a2a3fa5baf64305c": "Не удалось преобразовать изображение в PNG.", + "cedaf63585cfbc1c": "В этой сборке не настроен провайдер OpenID Connect.", + "117d1d150ac00822": "Не удалось завершить вход.", + "5793a07d88edde04": "Выполняется вход", + "fa6662dcf0452f92": "Вход не завершён", + "46cd220e658b94db": "Завершаем вход через вашего провайдера удостоверений…", + "4189d6c508b9c1f1": "Не удалось начать вход.", + "a03de172b2fd7089": "Не удалось начать вход", + "3fb16cb1f079c67a": "Провайдер удостоверений не ответил.", + "685dbb084305a2ee": "Учётная запись T3 Connect", "26dca8ee72dc5d74": "Не удалось прочитать настройки", "52e7a660ce883609": "Подключение ещё идёт", "9a50b20569f2c9e7": "Не удалось загрузить сохранённые настройки.", @@ -8326,7 +8352,6 @@ "8fef1ad9234284f7": "По умолчанию", "ca384fd1a1d6e1bb": "Параметры {name}", "8b45e296e25933cb": "Подключитесь к окружению, чтобы очистить данные профиля", - "c7902d95bd57a25f": "Удаление…", "4a42ed8f9a4c6750": "Удалить профиль", "90eae6ed93306de1": "Не удалось открыть Системные настройки", "e80b0936736a6dd8": "Откройте «Конфиденциальность и безопасность» → «Доступ к диску» вручную.", @@ -9560,15 +9585,6 @@ "13d4f258e93f1dc1": "Подключите окружение, чтобы управлять провайдерами секретов, из которых читают его переменные провайдеров.", "88466f5a03be2419": "Настройка для всего окружения. Выберите окружение, чтобы изменить её.", "111a44209dc6e701": "Удалить", - "a3538ba5ca802bd7": [ - "Удалить ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "Удалить провайдер", "c52bdbe39649742c": [ "Из ", @@ -10710,6 +10726,20 @@ "a899361f5deb3476": "Любые локальные файлы", "8c92f29a2b29adb5": "Незакоммиченные изменения", "deb572d6e83f3818": "Изменённые отслеживаемые файлы", + "2689839de807d3d6": "Собственный relay", + "1b768d8eedf9b5ed": "Не удалось связаться с relay.", + "1e0a645ef3661af3": "Сервис", + "9e35c6e0d4bfda3e": "Используется relay {relay}, вход через {issuer}.", + "d5ac081bf01ea146": "T3 Connect связывает ваши устройства через relay T3. Выберите собственный relay, чтобы использовать свой сервер и провайдера входа.", + "08138afa321f491d": "Сервис T3 Connect", + "b232816704c22aa2": "Адрес relay", + "8f7efe3ac6305217": "Данные для входа считываются с relay. После переключения нужно войти заново.", + "5f69bb7c4858a66e": "Использовать relay", + "a7b5f0e467829859": "Окружения, привязанные к вашей учётной записи relay. Отмените регистрацию тех, что больше не используете.", + "8d35d826788b135c": "Вернуться к T3 Connect", + "0e6e4b0e2ac50c21": "Потребуется войти заново, а окружения, привязанные к вашему relay, останутся там, пока вы не привяжете их снова.", + "9f90d63bcad3e2a7": "Управлять", + "f63e98992f71e591": "Использовать T3 Connect", "83729e5228731819": "Адрес трассировок", "8739ffcda2007458": "Адрес метрик", "5c90c5ba936fb908": "Адрес логов", @@ -10774,7 +10804,6 @@ "ebffe4dabf52835f": "Включить {name}", "b663e2e129c61493": "Добавить генератор текста", "149ffa363168a419": "Пишет заголовки тредов, сообщения коммитов и другие короткие тексты без запуска агента для программирования. Ваш API-ключ хранится в этом окружении.", - "1e0a645ef3661af3": "Сервис", "bdbab71cbcc8642a": "API-ключ (необязательно)", "f733ede3caa103f3": "например, Моя локальная модель", "798d0083bcd7a233": "{count, plural, one {{name} подключён и предлагает {count} модель. Переключите на него любые из этих функций или оставьте текущие.} few {{name} подключён и предлагает {count} модели. Переключите на него любые из этих функций или оставьте текущие.} many {{name} подключён и предлагает {count} моделей. Переключите на него любые из этих функций или оставьте текущие.} other {{name} подключён и предлагает {count} модели. Переключите на него любые из этих функций или оставьте текущие.}}", @@ -11553,6 +11582,7 @@ "1c3e0ac06c8a1f1e": "подключения сервер бэкенд локальный удалённый доступ административные разрешения область ссылки сопряжения qr-код авторизованные клиенты сеансы отозвать конечная точка", "744fd34e8049efeb": "привязать другой компьютер vps агенты работают там передача код сопряжения", "3d00c452287f50bd": "добавить сопряжение бэкенд хост код ssh конфигурация агент туннель сохранённые t3 connect", + "17dc9f6a0355b392": "собственный relay свой сервер oidc openid sso вход провайдер", "cbf49f84e7f456e5": "Балансировка нагрузки", "6f1a769805a28197": "автоматически компьютер окружение ресурсы cpu память мощность предпочтение вес общие проекты", "563b1163723f019b": "Общий доступ к пул-реквестам", diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index 5e6d1607edf31..a54f3a15cb956 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -495,6 +495,7 @@ "4145793647e296fe": "Reläet har ingen aktiv länk för den här miljön. Om den avregistrerades från T3 Connect, länka den igen på den datorn.", "10c07be640be8c23": "Miljöns T3 Connect-tunnel håller fortfarande på att konfigureras.", "08c3ae49e073cdc8": "Den här miljön är bara länkad för att publicera agentaktivitet, så den kan inte ta emot anslutningar. Öppna Inställningar > Delning och åtkomst på den datorn och aktivera T3 Connect.", + "fc86842c4bb9d8c0": "Reläet får inte kontakta adressen som den här miljön publicerade. Använd en HTTPS-adress som reläet kan nå och som inte är localhost. Privata nätverksadresser fungerar bara med ett relä som tillåter dem.", "a4429a85fbe329d7": "Reläet avvisade miljöns anslutningsbegäran ({reason}).", "745e4a36c4216140": "Reläet avvisade miljöns anslutningsbegäran.", "c406def16dbd8691": "Reläet kunde inte nå miljöns slutpunkt ({reason}).", @@ -4758,8 +4759,20 @@ "3260726e13a6882b": "Installera T3 Code på telefonen och logga in på T3 Connect för att få push-aviseringar och liveaktiviteter.", "de111af2fe5653a6": "Kunde inte läsa in mobilklienter", "5d138bbbce733537": "T3 Connect-inställningar", + "fb516055076656ee": "Den tas också bort från ditt reläkonto, så den kommer inte tillbaka på dina enheter.", "ff2d5256c7b57d7f": "Den finns inte längre på ditt T3 Connect-konto.", + "c7902d95bd57a25f": "Tar bort…", + "2c5674665579a329": "Ta bort", "fba00de8e861f8c1": "T3 Connect-inställningar", + "a3538ba5ca802bd7": [ + "Ta bort ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "Ta bort ", { @@ -4851,11 +4864,15 @@ }, " och välj sedan Uppdatera." ], + "41aa06651d90940a": "Logga ut från T3 Connect", + "112e5facdfd81300": "Logga ut ({account})", + "a656dc69a38a3639": "Logga ut", "cf09d31e268af127": "Försök med T3 Connect igen", "6fa695d3a822bb9e": "Logga in på T3 Connect", "b28a147f66b0e088": "Länkdatumet är inte tillgängligt", "6f0589fb1a5e38a9": "Länkad {date}", "ba921b079703d6d5": "Hanterad tunnel", + "06651d35dc2f21a2": "Via reläet", "a8716f4cb9fcf7bb": "Endast aktivitetspublicering", "e535cf8977e2a253": "Bekräfta avregistrering av {environment}", "c36af6045c0cf0c3": "Avregistrerar…", @@ -5890,6 +5907,15 @@ "dbd798f9304e4743": "Den här bilden är för stor eller saknar användbara mått. Försök spara den i stället.", "9c50c483b1d97631": "Det går inte att kopiera bilder i den här webbläsaren.", "a2a3fa5baf64305c": "Bilden kunde inte konverteras till PNG.", + "cedaf63585cfbc1c": "Det här bygget har ingen OpenID Connect-leverantör konfigurerad.", + "117d1d150ac00822": "Inloggningen kunde inte slutföras.", + "5793a07d88edde04": "Loggar in dig", + "fa6662dcf0452f92": "Inloggningen slutfördes inte", + "46cd220e658b94db": "Slutför inloggningen hos din identitetsleverantör…", + "4189d6c508b9c1f1": "Inloggningen kunde inte starta.", + "a03de172b2fd7089": "Det gick inte att starta inloggningen", + "3fb16cb1f079c67a": "Identitetsleverantören svarade inte.", + "685dbb084305a2ee": "T3 Connect-konto", "26dca8ee72dc5d74": "Kunde inte läsa inställningarna", "52e7a660ce883609": "Ansluter fortfarande", "9a50b20569f2c9e7": "Dina sparade inställningar kunde inte läsas in.", @@ -8332,7 +8358,6 @@ "8fef1ad9234284f7": "Standard", "ca384fd1a1d6e1bb": "Alternativ för {name}", "8b45e296e25933cb": "Anslut till en miljö för att rensa profildata", - "c7902d95bd57a25f": "Tar bort…", "4a42ed8f9a4c6750": "Ta bort profil", "90eae6ed93306de1": "Kunde inte öppna Systeminställningar", "e80b0936736a6dd8": "Öppna Integritet och säkerhet → Full skivåtkomst manuellt.", @@ -9566,15 +9591,6 @@ "13d4f258e93f1dc1": "Anslut en miljö för att hantera de hemlighetsleverantörer som dess leverantörsvariabler läser från.", "88466f5a03be2419": "Inställning för hela miljön. Välj en miljö för att ändra den.", "111a44209dc6e701": "Ta bort", - "a3538ba5ca802bd7": [ - "Ta bort ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "Ta bort leverantör", "c52bdbe39649742c": [ "Inget läser från ", @@ -10718,6 +10734,20 @@ "a899361f5deb3476": "Alla lokala filer", "8c92f29a2b29adb5": "Ej committade ändringar", "deb572d6e83f3818": "Redigerade spårade filer", + "2689839de807d3d6": "Egenhostat relä", + "1b768d8eedf9b5ed": "Det gick inte att nå reläet.", + "1e0a645ef3661af3": "Tjänst", + "9e35c6e0d4bfda3e": "Använder reläet på {relay}, inloggning via {issuer}.", + "d5ac081bf01ea146": "T3 Connect länkar dina enheter via T3:s relä. Välj ett egenhostat relä för att använda din egen server och inloggningsleverantör.", + "08138afa321f491d": "T3 Connect-tjänst", + "b232816704c22aa2": "Reläadress", + "8f7efe3ac6305217": "Inloggningsuppgifterna läses från reläet. Efter bytet behöver du logga in igen.", + "5f69bb7c4858a66e": "Använd relä", + "a7b5f0e467829859": "Miljöer som är länkade till ditt reläkonto. Avregistrera de som du inte längre använder.", + "8d35d826788b135c": "Byt tillbaka till T3 Connect", + "0e6e4b0e2ac50c21": "Du behöver logga in igen, och miljöer som är länkade till ditt relä stannar kvar där tills du länkar dem igen.", + "9f90d63bcad3e2a7": "Hantera", + "f63e98992f71e591": "Använd T3 Connect", "83729e5228731819": "Slutpunkt för spårningar", "8739ffcda2007458": "Slutpunkt för mätvärden", "5c90c5ba936fb908": "Slutpunkt för loggar", @@ -10782,7 +10812,6 @@ "ebffe4dabf52835f": "Aktivera {name}", "b663e2e129c61493": "Lägg till en textgenerator", "149ffa363168a419": "Skriver trådtitlar, commit-meddelanden och annan kort text utan att starta en kodagent. Din API-nyckel lagras i den här miljön.", - "1e0a645ef3661af3": "Tjänst", "bdbab71cbcc8642a": "API-nyckel (valfritt)", "f733ede3caa103f3": "t.ex. Min lokala modell", "798d0083bcd7a233": "{count, plural, one {{name} är ansluten och listar {count} modell. Byt valfria av de här till den, eller behåll det de använder nu.} other {{name} är ansluten och listar {count} modeller. Byt valfria av de här till den, eller behåll det de använder nu.}}", @@ -11561,6 +11590,7 @@ "1c3e0ac06c8a1f1e": "anslutningar server backend lokal fjärråtkomst administrativa behörigheter omfång parkopplingslänkar qr-kod auktoriserade klienter sessioner återkalla slutpunkt", "744fd34e8049efeb": "länka peer annan dator vps agenter arbetar där överlämning parkopplingskod", "3d00c452287f50bd": "lägg till parkoppla backend värd kod ssh-konfiguration agent tunnel sparad t3 connect", + "17dc9f6a0355b392": "egenhostat relä egen server oidc openid sso inloggning leverantör", "cbf49f84e7f456e5": "Lastbalansering", "6f1a769805a28197": "automatisk dator miljö resurser cpu minne kapacitet preferens vikt delade projekt", "563b1163723f019b": "Delning av pull requests", diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index c86b9e6c55e41..60c3b81ed2613 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -495,6 +495,7 @@ "4145793647e296fe": "中继没有此环境的有效关联。如果它已从 T3 Connect 注销,请在那台机器上重新关联。", "10c07be640be8c23": "此环境的 T3 Connect 隧道仍在设置中。", "08c3ae49e073cdc8": "此环境仅为发布智能体活动而关联,因此无法接受连接。请在那台电脑上打开“设置 > 共享与访问”并开启 T3 Connect。", + "fc86842c4bb9d8c0": "中继不能连接此环境发布的地址。请使用中继可以访问且不是 localhost 的 HTTPS 地址。私有网络地址只适用于允许它们的中继。", "a4429a85fbe329d7": "中继拒绝了环境连接请求({reason})。", "745e4a36c4216140": "中继拒绝了环境连接请求。", "c406def16dbd8691": "中继无法访问环境端点({reason})。", @@ -4764,8 +4765,20 @@ "3260726e13a6882b": "在手机上安装 T3 Code 并登录 T3 Connect,即可收到推送通知和实时活动。", "de111af2fe5653a6": "无法加载移动客户端", "5d138bbbce733537": "T3 Connect 设置", + "fb516055076656ee": "它也会从你的中继账户中移除,因此不会再出现在你的设备上。", "ff2d5256c7b57d7f": "它已不在你的 T3 Connect 账户中。", + "c7902d95bd57a25f": "正在移除…", + "2c5674665579a329": "移除", "fba00de8e861f8c1": "T3 Connect 设置", + "a3538ba5ca802bd7": [ + "移除 ", + { + "i": 1, + "k": "_gt_value_1", + "v": "v" + }, + "?" + ], "83a0e763474f61be": [ "从此设备中移除 ", { @@ -4857,11 +4870,15 @@ }, " 上确认,然后选择“刷新”。" ], + "41aa06651d90940a": "退出 T3 Connect", + "112e5facdfd81300": "退出({account})", + "a656dc69a38a3639": "退出", "cf09d31e268af127": "重试 T3 Connect", "6fa695d3a822bb9e": "登录 T3 Connect", "b28a147f66b0e088": "关联日期不可用", "6f0589fb1a5e38a9": "关联于 {date}", "ba921b079703d6d5": "托管隧道", + "06651d35dc2f21a2": "经由中继", "a8716f4cb9fcf7bb": "仅发布活动", "e535cf8977e2a253": "确认注销 {environment}", "c36af6045c0cf0c3": "正在注销…", @@ -5896,6 +5913,15 @@ "dbd798f9304e4743": "这张图片太大或没有可用的尺寸,请改为保存。", "9c50c483b1d97631": "此浏览器无法复制图片。", "a2a3fa5baf64305c": "无法将图片转换为 PNG。", + "cedaf63585cfbc1c": "此版本未配置 OpenID Connect 提供方。", + "117d1d150ac00822": "无法完成登录。", + "5793a07d88edde04": "正在登录", + "fa6662dcf0452f92": "登录未完成", + "46cd220e658b94db": "正在通过你的身份提供方完成登录…", + "4189d6c508b9c1f1": "无法开始登录。", + "a03de172b2fd7089": "无法开始登录", + "3fb16cb1f079c67a": "身份提供方没有响应。", + "685dbb084305a2ee": "T3 Connect 账户", "26dca8ee72dc5d74": "无法读取设置", "52e7a660ce883609": "仍在连接", "9a50b20569f2c9e7": "无法加载你保存的设置。", @@ -8342,7 +8368,6 @@ "8fef1ad9234284f7": "默认", "ca384fd1a1d6e1bb": "{name} 的选项", "8b45e296e25933cb": "连接一个环境以清除浏览器配置数据", - "c7902d95bd57a25f": "正在移除…", "4a42ed8f9a4c6750": "移除浏览器配置", "90eae6ed93306de1": "无法打开系统设置", "e80b0936736a6dd8": "请手动打开“隐私与安全性”→“完全磁盘访问权限”。", @@ -9573,15 +9598,6 @@ "13d4f258e93f1dc1": "连接一个环境,即可管理其提供方变量使用的凭据提供方。", "88466f5a03be2419": "这是环境级设置,请先选择一个环境再修改。", "111a44209dc6e701": "移除", - "a3538ba5ca802bd7": [ - "移除 ", - { - "i": 1, - "k": "_gt_value_1", - "v": "v" - }, - "?" - ], "0d5b3b39f92c04e4": "移除提供方", "c52bdbe39649742c": [ "没有任何内容从 ", @@ -10731,6 +10747,20 @@ "a899361f5deb3476": "任何本地文件", "8c92f29a2b29adb5": "未提交的改动", "deb572d6e83f3818": "已编辑的跟踪文件", + "2689839de807d3d6": "自托管中继", + "1b768d8eedf9b5ed": "无法连接中继。", + "1e0a645ef3661af3": "服务", + "9e35c6e0d4bfda3e": "正在使用 {relay} 的中继,通过 {issuer} 登录。", + "d5ac081bf01ea146": "T3 Connect 通过 T3 的中继连接你的设备。选择自托管中继即可使用你自己的服务器和登录提供方。", + "08138afa321f491d": "T3 Connect 服务", + "b232816704c22aa2": "中继地址", + "8f7efe3ac6305217": "登录信息从中继读取。切换后需要重新登录。", + "5f69bb7c4858a66e": "使用中继", + "a7b5f0e467829859": "关联到你的中继账户的环境。注销不再使用的环境。", + "8d35d826788b135c": "切换回 T3 Connect", + "0e6e4b0e2ac50c21": "需要重新登录,关联到你的中继的环境会保留在那里,直到你重新关联它们。", + "9f90d63bcad3e2a7": "管理", + "f63e98992f71e591": "使用 T3 Connect", "83729e5228731819": "Traces 端点", "8739ffcda2007458": "Metrics 端点", "5c90c5ba936fb908": "Logs 端点", @@ -10795,7 +10825,6 @@ "ebffe4dabf52835f": "启用 {name}", "b663e2e129c61493": "添加文本生成器", "149ffa363168a419": "无需启动编程智能体即可撰写会话标题、提交信息等短文本。你的 API 密钥存储在此环境中。", - "1e0a645ef3661af3": "服务", "bdbab71cbcc8642a": "API 密钥(可选)", "f733ede3caa103f3": "例如:我的本地模型", "798d0083bcd7a233": "{count, plural, other {{name} 已连接,提供 {count} 个模型。可将以下任意项切换为使用它,或保留当前设置。}}", @@ -11577,6 +11606,7 @@ "1c3e0ac06c8a1f1e": "连接 服务器 后端 本地 远程 访问 管理员 权限 范围 配对链接 二维码 已授权的客户端 客户端连接 撤销 端点", "744fd34e8049efeb": "关联 对等 另一台机器 vps 智能体 在那里工作 移交 配对码", "3d00c452287f50bd": "添加 配对 后端 主机 代码 ssh 配置 认证代理 隧道 已保存 t3 connect", + "17dc9f6a0355b392": "自托管 中继 自有服务器 oidc openid sso 登录 提供方", "cbf49f84e7f456e5": "负载均衡", "6f1a769805a28197": "自动 机器 环境 资源 cpu 内存 容量 偏好 权重 共享项目", "563b1163723f019b": "PR 共享", From b8787c334c519451feaa52978334fa3d4dcd2855 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Wed, 18 Nov 2026 15:05:18 +0100 Subject: [PATCH 27/29] fix(relay): show the provider's reason on token errors, document Entra setup The OIDC callback only showed "The token request failed with 400". It now adds the provider's error and error_description (the AADSTS text on Entra). A relay channel reported "Tunnel connected" although it has no tunnel. It now reads "Connected". The self-host README gets the Entra settings (SPA platform for the web redirect, public client flows for the device flow, tenant-specific v2.0 issuer, 24 h refresh tokens) and a note on linking a host from the terminal when the browser app has no sign-in action. --- infra/relay/selfhost/README.md | 18 ++++++++++++++++++ packages/contracts/src/environmentHttp.ts | 3 ++- packages/shared/src/oidc.test.ts | 21 +++++++++++++++++++++ packages/shared/src/oidc.ts | 8 ++++++-- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md index 29798b2d0fbb5..15121dbcf6707 100644 --- a/infra/relay/selfhost/README.md +++ b/infra/relay/selfhost/README.md @@ -43,6 +43,19 @@ Run these from the repository root. Request the `offline_access` scope so sessions refresh without signing in again. + **Microsoft Entra ID.** Set `OIDC_ISSUER` to the tenant's v2.0 issuer, + `https://login.microsoftonline.com//v2.0`. The `/common` and `/organizations` + documents report a `{tenantid}` template, which the relay's exact issuer check rejects. + In the app registration: + + - Register the web redirect URI under **Single-page application**. A **Web** URI cannot + redeem the code from the browser. + - Register the desktop, mobile and CLI URIs under **Mobile and desktop applications**. + - Set **Allow public client flows** to **Yes** for the CLI device flow. Without it the token + poll fails with `AADSTS7000218`. + - Entra limits refresh tokens issued to a single-page application to 24 hours, so web + sessions ask for a new sign-in daily. + 4. Start it: ```sh @@ -123,6 +136,11 @@ network addresses (LAN, Tailscale) for relays only your own network uses; `off` them. Host names are checked as written, not resolved, so only point a relay you trust at names you control. A published address takes precedence over a relay channel. +## Linking a host from a browser + +The browser app has no sign-in action in its "T3 Connect service" card. Link the host from its +terminal instead with `t3 connect link --publish-only`, then restart T3 Code. + ## Behind another reverse proxy Caddy is the bundled proxy. To use your own (nginx, Traefik, a cloud load balancer), drop the diff --git a/packages/contracts/src/environmentHttp.ts b/packages/contracts/src/environmentHttp.ts index 8b8cc6b97e2c1..d23ec12e754ad 100644 --- a/packages/contracts/src/environmentHttp.ts +++ b/packages/contracts/src/environmentHttp.ts @@ -492,11 +492,12 @@ export const EnvironmentConnectReachability = Schema.Union([ ]); export type EnvironmentConnectReachability = typeof EnvironmentConnectReachability.Type; -/** "Tunnel connected · 4 connections · QUIC" for a healthy check. */ +/** "Tunnel connected · 4 connections · QUIC" for a healthy check; "Connected" for a relay channel. */ export function formatConnectReachable( result: Extract, ): string { const count = result.readyConnections; + if (count === null && result.protocol === null) return "Connected"; return [ "Tunnel connected", ...(count === null ? [] : [`${count} connection${count === 1 ? "" : "s"}`]), diff --git a/packages/shared/src/oidc.test.ts b/packages/shared/src/oidc.test.ts index 58e1bd99b5b81..1c245963bf7f0 100644 --- a/packages/shared/src/oidc.test.ts +++ b/packages/shared/src/oidc.test.ts @@ -170,6 +170,27 @@ describe("token requests", () => { }); }); + it("puts the provider's reason in a rejected token request's message", async () => { + const rejected = exchangeAuthorizationCode({ + metadata, + clientId: "t3-code", + redirectUri: "https://app.example.com/oidc/callback", + code: "code-1", + verifier: "verifier-1", + nonce: "nonce-1", + fetch: async () => + jsonResponse( + { error: "invalid_request", error_description: "AADSTS9002327: wrong client type" }, + 400, + ), + }); + await expect(rejected).rejects.toMatchObject({ + message: + "The token request failed with 400 (invalid_request: AADSTS9002327: wrong client type).", + oauthError: "invalid_request", + }); + }); + it("keeps the current refresh token when the provider does not rotate it", async () => { const tokens = await refreshOidcTokens({ metadata, diff --git a/packages/shared/src/oidc.ts b/packages/shared/src/oidc.ts index 2f289fcae1dc5..a1709ba15e1fc 100644 --- a/packages/shared/src/oidc.ts +++ b/packages/shared/src/oidc.ts @@ -253,11 +253,15 @@ async function requestTokens( }); const body = (await response.json().catch(() => ({}))) as Record; if (!response.ok) { + const oauthError = typeof body.error === "string" ? body.error : undefined; + const description = + typeof body.error_description === "string" ? body.error_description.trim() : ""; + const reason = [oauthError, description].filter(Boolean).join(": "); throw new OidcError({ code: "token_request_failed", - message: `The token request failed with ${response.status}.`, + message: `The token request failed with ${response.status}${reason ? ` (${reason})` : ""}.`, status: response.status, - ...(typeof body.error === "string" ? { oauthError: body.error } : {}), + ...(oauthError ? { oauthError } : {}), }); } if (typeof body.id_token !== "string") { From dc34ad052cc8036850e78888e7c7c3000511196b Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Wed, 18 Nov 2026 17:34:21 +0100 Subject: [PATCH 28/29] fix(server): startup log no longer prints a stale relay URL After a relay address change, "agent activity publishing enabled" printed the previous relay URL, because it is logged before the link reconciles. The startup line drops the URL. The self-host README now lists what the relay stores for an active thread. --- apps/server/src/relay/AgentAwarenessRelay.ts | 5 ++--- infra/relay/selfhost/README.md | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.ts index 75e84e7bb8c96..e903fa1f76af2 100644 --- a/apps/server/src/relay/AgentAwarenessRelay.ts +++ b/apps/server/src/relay/AgentAwarenessRelay.ts @@ -962,9 +962,8 @@ export const make = Effect.gen(function* () { yield* Effect.logInfo("agent activity publishing disabled by T3 Connect configuration"); break; case "enabled": - yield* Effect.logInfo("agent activity publishing enabled", { - relayUrl: relayConfig?.url, - }); + // No relayUrl: a relay address change only applies once the link reconciles, after this line. + yield* Effect.logInfo("agent activity publishing enabled"); break; } yield* forkParked( diff --git a/infra/relay/selfhost/README.md b/infra/relay/selfhost/README.md index 15121dbcf6707..3637c8f59dceb 100644 --- a/infra/relay/selfhost/README.md +++ b/infra/relay/selfhost/README.md @@ -149,6 +149,13 @@ the public HTTPS address. The relay builds no URLs from request headers, so `X-F values are ignored. `OIDC_ISSUER` must equal the issuer your provider reports publicly, including any path prefix your proxy adds. +## What the relay stores + +While a thread runs, the server publishes its activity so a phone can show it. The relay keeps, +per active thread, the project title, thread title, model name, phase, a short status headline +and detail line, and a deep link (`relay_agent_activity_rows.state_json`). It does not store +message content. + ## What this host does not do - **Managed tunnels.** Cloudflare-managed endpoints are not available; use a relay channel or From c417cb8eb333e353eebf7c86493bcd9946497009 Mon Sep 17 00:00:00 2001 From: Romain PINSOLLE Date: Wed, 18 Nov 2026 17:56:53 +0100 Subject: [PATCH 29/29] fix(relay): the self-host image no longer ships node_modules The relay bundle inlines all its dependencies, so the runtime image only needs the bundle. Dropping the pnpm deploy step and its node_modules copy takes the image from 1.98 GB to 191 MB. The image starts against Postgres and answers /health. --- infra/relay/selfhost/Dockerfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/infra/relay/selfhost/Dockerfile b/infra/relay/selfhost/Dockerfile index e2666c3c21e0a..1bbd8aa769f84 100644 --- a/infra/relay/selfhost/Dockerfile +++ b/infra/relay/selfhost/Dockerfile @@ -5,8 +5,7 @@ WORKDIR /repo COPY . . # Only the relay's dependency closure: the mobile and desktop apps are not needed here. RUN pnpm install --frozen-lockfile --ignore-scripts --filter "t3code-relay..." -# Production dependencies only; the bundle keeps third-party packages external. -RUN pnpm --filter t3code-relay deploy --prod /deploy +# The bundle inlines every dependency, so the runtime image needs no node_modules. RUN vp pack infra/relay/src/node.ts --format esm --platform node --root infra/relay --out-dir /out --no-dts FROM node:26-alpine @@ -14,7 +13,6 @@ WORKDIR /app ENV NODE_ENV=production \ PORT=8080 \ RELAY_MIGRATIONS_FOLDER=/app/migrations/postgres -COPY --from=build /deploy/node_modules /app/node_modules COPY --from=build /out /app COPY --from=build /repo/infra/relay/migrations/postgres /app/migrations/postgres USER node