Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): canonicalize REST URLs for R5-H1

Josué55331a9eb9fc
diff --git a/desktop-app/src/__tests__/appService.bootstrap.test.ts b/desktop-app/src/__tests__/appService.bootstrap.test.ts
index e4a5f21d5..322e266a9 100644
--- a/desktop-app/src/__tests__/appService.bootstrap.test.ts
+++ b/desktop-app/src/__tests__/appService.bootstrap.test.ts
@@ -83,7 +83,7 @@ describe('AppService invitation configuration lookup', () => {
completeDesktopSetup: vi.fn().mockResolvedValue({
valid: true,
- externalRestApiBaseUrl: 'https://api.example.com',
+ externalRestApiBaseUrl: 'https://api.example.com.',
rpcProxyBaseUrl: 'https://rpc.example.com',
appName: 'Evenfire',
}),
@@ -116,6 +116,9 @@ describe('AppService invitation configuration lookup', () => {
await service.completeDesktopSetup('[email protected]', 'setup-token')
+ expect(config.externalRestApiBaseUrl).toBe('https://api.example.com')
+ expect(config.rpcProxyBaseUrl).toBe('https://rpc.example.com')
+
expect(service.memberRegistrationServiceClient.completeDesktopSetup).toHaveBeenCalledWith(
'setup-token'
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index e2da7f6e8..c1e0a1647 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -16,6 +16,10 @@ import {
saveDesktopRuntimeConfig,
selectDesktopRuntimeConfigOption,
} from './config.js'
+import {
+ canonicalizeDesktopRestEndpoint,
+ desktopRestEndpointOrigin,
+} from './desktopEnvironmentUrl.js'
import { fetchBoundedBytes } from './gfs/boundedDownload.js'
import { type DelegationAffordances, delegationAffordances } from './gfs/delegation.js'
import {
@@ -97,15 +101,6 @@ const HOST_WAKE_SCOPE: RpcScope = 'host:wake:write'
const PROFILE_UI_BASE_URL_ORIGIN_ERROR =
'PROFILE_UI_BASE_URL must be an origin URL with a root pathname and no search parameters'
-function requireHttpOrigin(rawValue: string): string {
- const value = rawValue.trim()
- const url = new URL(value)
- if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
- throw new Error('Desktop environment URLs must use http(s) without credentials')
- }
- return url.origin
-}
-
function normalizeExplicitProfileUiBaseUrl(rawValue: string): string | null {
const value = rawValue.trim()
if (!value) return null
@@ -1952,12 +1947,12 @@ export class AppService {
hydrateDesktopRuntimeConfig()
if (!isDesktopRuntimeConfigured()) return
if (config.rpcProxyBaseUrl?.trim()) return
- const externalRestApiBaseUrl = config.externalRestApiBaseUrl
- const externalRestApiOrigin = requireHttpOrigin(externalRestApiBaseUrl)
+ const externalRestApiBaseUrl = canonicalizeDesktopRestEndpoint(config.externalRestApiBaseUrl)
+ const externalRestApiOrigin = desktopRestEndpointOrigin(externalRestApiBaseUrl)
const discovered = await this.authClient.getDesktopEnvironment()
if (
discovered.externalRestApiBaseUrl?.trim() &&
- requireHttpOrigin(discovered.externalRestApiBaseUrl) !== externalRestApiOrigin
+ desktopRestEndpointOrigin(discovered.externalRestApiBaseUrl) !== externalRestApiOrigin
) {
throw new Error('Desktop environment discovery returned a different REST host')
}
diff --git a/desktop-app/src/config.ts b/desktop-app/src/config.ts
index d747c42a3..7454f9b82 100644
--- a/desktop-app/src/config.ts
+++ b/desktop-app/src/config.ts
@@ -4,6 +4,10 @@ import fs from 'node:fs'
import fsp from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
+import {
+ canonicalizeDesktopRestEndpoint,
+ sameDesktopRestEndpoint,
+} from './desktopEnvironmentUrl.js'
import type {
DesktopRuntimeConfig,
DesktopRuntimeConfigOption,
@@ -411,7 +415,8 @@ function configsMatch(a: DesktopRuntimeConfig, b: DesktopRuntimeConfig): boolean
function runtimeEndpointsMatch(a: DesktopRuntimeConfig, b: DesktopRuntimeConfig): boolean {
return (
- a.externalRestApiBaseUrl === b.externalRestApiBaseUrl && a.rpcProxyBaseUrl === b.rpcProxyBaseUrl
+ sameDesktopRestEndpoint(a.externalRestApiBaseUrl, b.externalRestApiBaseUrl) &&
+ a.rpcProxyBaseUrl === b.rpcProxyBaseUrl
)
}
@@ -733,6 +738,9 @@ export function isDesktopRuntimeConfigured(): boolean {
export async function saveDesktopRuntimeConfig(next: DesktopRuntimeConfig): Promise<void> {
hydrateDesktopRuntimeConfig()
const validated = validateRuntimeConfig(next)
+ validated.externalRestApiBaseUrl = canonicalizeDesktopRestEndpoint(
+ validated.externalRestApiBaseUrl
+ )
const explicitPath = explicitRuntimeConfigPath()
if (explicitPath) {
const timestamp = new Date().toISOString()
@@ -753,8 +761,8 @@ export async function saveDesktopRuntimeConfig(next: DesktopRuntimeConfig): Prom
return
}
- const existing = storedProfiles.find(
- profile => profile.config.externalRestApiBaseUrl === validated.externalRestApiBaseUrl
+ const existing = storedProfiles.find(profile =>
+ sameDesktopRestEndpoint(profile.config.externalRestApiBaseUrl, validated.externalRestApiBaseUrl)
)
if (existing) {
existing.updatedAt = new Date().toISOString()
diff --git a/desktop-app/src/desktopEnvironmentUrl.ts b/desktop-app/src/desktopEnvironmentUrl.ts
new file mode 100644
index 000000000..cce6ba086
--- /dev/null
+++ b/desktop-app/src/desktopEnvironmentUrl.ts
@@ -0,0 +1,30 @@
+export function canonicalizeDesktopRestEndpoint(rawValue: string): string {
+ const url = new URL(rawValue.trim())
+ if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
+ throw new Error('Desktop environment URLs must use http(s) without credentials')
+ }
+
+ const hostname = url.hostname
+ if (!hostname.startsWith('[') && hostname.endsWith('.')) {
+ const hostnameWithoutRootDot = hostname.slice(0, -1)
+ if (!hostnameWithoutRootDot || hostnameWithoutRootDot.endsWith('.')) {
+ throw new Error('Desktop environment REST host is invalid')
+ }
+ url.hostname = hostnameWithoutRootDot
+ }
+
+ const pathname = url.pathname === '/' ? '' : url.pathname
+ return `${url.origin}${pathname}${url.search}${url.hash}`
+}
+
+export function desktopRestEndpointOrigin(rawValue: string): string {
+ return new URL(canonicalizeDesktopRestEndpoint(rawValue)).origin
+}
+
+export function sameDesktopRestEndpoint(left: string, right: string): boolean {
+ try {
+ return canonicalizeDesktopRestEndpoint(left) === canonicalizeDesktopRestEndpoint(right)
+ } catch {
+ return false
+ }
+}
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
index 1205f6b31..3152d5e7d 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
+++ b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts
@@ -198,6 +198,54 @@ describe('Desktop environment origin matching', () => {
expect(setPendingDesktopEnvironmentSetup).toHaveBeenCalledWith(null)
})
+ it('selects the saved REST profile when the link host has a terminal DNS dot', async () => {
+ const state = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ const savedTarget = state.options.find(
+ option =>
+ option.externalRestApiBaseUrl === `${targetEnvironment.externalRestApiBaseUrl}/api/v1`
+ )
+ if (!savedTarget) throw new Error('The config producer did not return the saved REST profile')
+ const { handler, selectRuntimeConfig, setPendingDesktopEnvironmentSetup } = createHandler(
+ () => ({ booting: false, busy: false, authTransitioning: false, isAuthenticated: false })
+ )
+
+ await handler({
+ ...targetEnvironment,
+ externalRestApiBaseUrl: 'https://api.example.test./api/v1',
+ rpcProxyBaseUrl: '',
+ })
+
+ const finalState = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ expect(selectRuntimeConfig).toHaveBeenCalledWith(savedTarget.id)
+ expect(setPendingDesktopEnvironmentSetup).toHaveBeenCalledWith(null)
+ expect(finalState.options).toHaveLength(state.options.length)
+ expect(finalState.currentConfig?.externalRestApiBaseUrl).toBe(
+ `${targetEnvironment.externalRestApiBaseUrl}/api/v1`
+ )
+ })
+
+ it('updates a saved profile when saving its terminal-dot REST spelling', async () => {
+ const state = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ const savedTarget = state.options.find(
+ option =>
+ option.externalRestApiBaseUrl === `${targetEnvironment.externalRestApiBaseUrl}/api/v1`
+ )
+ if (!savedTarget) throw new Error('The config producer did not return the saved REST profile')
+
+ await runtimeConfigModule!.saveDesktopRuntimeConfig({
+ ...targetEnvironment,
+ externalRestApiBaseUrl: 'https://api.example.test./api/v1',
+ rpcProxyBaseUrl: `${targetEnvironment.rpcProxyBaseUrl}/rpc`,
+ })
+
+ const finalState = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ expect(finalState.options).toHaveLength(state.options.length)
+ expect(finalState.activeOptionId).toBe(savedTarget.id)
+ expect(finalState.currentConfig?.externalRestApiBaseUrl).toBe(
+ `${targetEnvironment.externalRestApiBaseUrl}/api/v1`
+ )
+ })
+
it('rejects an omitted RPC when multiple saved profiles share the REST origin', async () => {
await runtimeConfigModule!.saveDesktopRuntimeConfig({
appName: 'Second API profile',
diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
index 59c711391..00a08344c 100644
--- a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
@@ -1,3 +1,7 @@
+import {
+ canonicalizeDesktopRestEndpoint,
+ desktopRestEndpointOrigin,
+} from '../../../../src/desktopEnvironmentUrl'
import type { DesktopRuntimeConfig, DesktopRuntimeConfigState } from '../../../../src/types'
import type { SetStatusFn } from './types'
@@ -39,8 +43,8 @@ function sameDesktopEnvironment(
): boolean {
try {
return (
- environmentOrigin(left.externalRestApiBaseUrl) ===
- environmentOrigin(right.externalRestApiBaseUrl) &&
+ desktopRestEndpointOrigin(left.externalRestApiBaseUrl) ===
+ desktopRestEndpointOrigin(right.externalRestApiBaseUrl) &&
(left.rpcProxyBaseUrl?.trim() ? environmentOrigin(left.rpcProxyBaseUrl) : '') ===
(right.rpcProxyBaseUrl?.trim() ? environmentOrigin(right.rpcProxyBaseUrl) : '')
)
@@ -56,7 +60,7 @@ function savedEnvironmentsForRestOrigin(
return options.filter(option => {
if (option.source === 'localhost' || option.id === LOCALHOST_OPTION_ID) return false
try {
- return environmentOrigin(option.externalRestApiBaseUrl) === restOrigin
+ return desktopRestEndpointOrigin(option.externalRestApiBaseUrl) === restOrigin
} catch {
return false
}
@@ -71,12 +75,12 @@ export function getDesktopEnvironmentRestOriginMatches(
configState: DesktopRuntimeConfigState,
externalRestApiBaseUrl: string
) {
- const restOrigin = environmentOrigin(externalRestApiBaseUrl)
+ const restOrigin = desktopRestEndpointOrigin(externalRestApiBaseUrl)
return {
localhost: configState.options.find(option => {
if (!isLocalhostOption(option)) return false
try {
- return environmentOrigin(option.externalRestApiBaseUrl) === restOrigin
+ return desktopRestEndpointOrigin(option.externalRestApiBaseUrl) === restOrigin
} catch {
return false
}
@@ -143,7 +147,9 @@ export function createDesktopEnvironmentSetupHandler({
appName: appName?.trim() || 'Evenfire',
}
try {
- environmentOrigin(linkedConfig.externalRestApiBaseUrl)
+ linkedConfig.externalRestApiBaseUrl = canonicalizeDesktopRestEndpoint(
+ linkedConfig.externalRestApiBaseUrl
+ )
if (linkedConfig.rpcProxyBaseUrl) environmentOrigin(linkedConfig.rpcProxyBaseUrl)
} catch (error) {
setStatus(