Skip to content

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

TypeScript

fix: validate R7-L2 discovered RPC endpoints before publication

Josué555753fb51dc
diff --git a/desktop-app/src/__tests__/appService.discoveryLifecycle.test.ts b/desktop-app/src/__tests__/appService.discoveryLifecycle.test.ts
index 0daceef3f..d327b4f6d 100644
--- a/desktop-app/src/__tests__/appService.discoveryLifecycle.test.ts
+++ b/desktop-app/src/__tests__/appService.discoveryLifecycle.test.ts
@@ -104,4 +104,34 @@ describe('RPC discovery lifecycle through native producers', () => {
expect(config.config.rpcProxyBaseUrl).toBe(rpc)
expect(discovery).toHaveBeenCalledTimes(3)
})
+
+ it.each(['javascript:alert(1)', 'file:///fixture', undefined, '', 42])(
+ 'rejects invalid discovered RPC %s and permits a later valid discovery',
+ async invalid => {
+ const { service, config, rpc, rest, store, discovery, rpcHealth } = await fixture()
+ await service.googleLogin('synthetic-google-token')
+ const key = config.getActiveEnvKey()
+ discovery.mockResolvedValueOnce({
+ externalRestApiBaseUrl: rest,
+ rpcProxyBaseUrl: invalid,
+ appName: 'A',
+ } as never)
+ const activeGeneration = service.getSessionGeneration()
+ await service.getDependenciesHealth()
+ expect(config.config.rpcProxyBaseUrl).toBe('')
+ await service.getDependenciesHealth()
+ expect(discovery).toHaveBeenCalledTimes(3)
+ expect(config.getActiveEnvKey()).toBe(key)
+ expect(service.getSessionGeneration()).toBe(activeGeneration)
+ expect(await store.getSessionToken(key)).toBe('synthetic-session-a')
+ expect(service.getCachedUserId()).toBe('user-a')
+ // The real transport provider must now supply the recovered endpoint.
+ rpcHealth.mockRestore()
+ const fetch = vi
+ .spyOn(globalThis, 'fetch')
+ .mockResolvedValue(new Response(JSON.stringify({ status: 'ok' }), { status: 200 }))
+ await service.getDependenciesHealth()
+ expect(fetch).toHaveBeenCalledWith(`${rpc}/health`, expect.any(Object))
+ }
+ )
})
diff --git a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
index c281e34bc..d17d665c7 100644
--- a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
+++ b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
@@ -114,7 +114,7 @@ vi.mock('electron', () => ({
return false
}
},
- app: { getPath: vi.fn(() => tmpdir()) },
+ app: { getPath: vi.fn(() => tmpdir()), isReady: () => false },
safeStorage: {
isEncryptionAvailable: vi.fn(() => false),
encryptString: vi.fn(),
@@ -125,35 +125,49 @@ vi.mock('electron', () => ({
shell: { openExternal: vi.fn() },
}))
-vi.mock('../config.js', () => ({
- getActiveEnvKey: () => 'test-env',
- getActiveLegacyEnvKeys: () => [],
- hydrateDesktopRuntimeConfig: () => undefined,
- isDesktopRuntimeConfigured: () => true,
- saveDesktopRuntimeConfig: vi.fn(),
- getDesktopRuntimeConfigState: () => ({
- configured: true,
- isLocalhost: false,
- selectorVisible: false,
- activeOptionId: null,
- currentConfig: {
- externalRestApiBaseUrl: 'http://rest',
+vi.mock('../config.js', async importOriginal => {
+ const validatorDirectory = await mkdtemp(path.join(tmpdir(), 'sandbox-rpc-validator-'))
+ const originalConfigPath = process.env.CLERUM_DESKTOP_CONFIG_PATH
+ let validateRpcProxyBaseUrl: typeof import('../config.js').validateRpcProxyBaseUrl
+ try {
+ process.env.CLERUM_DESKTOP_CONFIG_PATH = path.join(validatorDirectory, 'runtime-config.json')
+ ;({ validateRpcProxyBaseUrl } = await importOriginal<typeof import('../config.js')>())
+ } finally {
+ if (originalConfigPath === undefined) delete process.env.CLERUM_DESKTOP_CONFIG_PATH
+ else process.env.CLERUM_DESKTOP_CONFIG_PATH = originalConfigPath
+ await rm(validatorDirectory, { recursive: true, force: true })
+ }
+ return {
+ validateRpcProxyBaseUrl,
+ getActiveEnvKey: () => 'test-env',
+ getActiveLegacyEnvKeys: () => [],
+ hydrateDesktopRuntimeConfig: () => undefined,
+ isDesktopRuntimeConfigured: () => true,
+ saveDesktopRuntimeConfig: vi.fn(),
+ getDesktopRuntimeConfigState: () => ({
+ configured: true,
+ isLocalhost: false,
+ selectorVisible: false,
+ activeOptionId: null,
+ currentConfig: {
+ externalRestApiBaseUrl: 'http://rest',
+ rpcProxyBaseUrl: 'https://rpc.example',
+ },
+ envKey: 'test-env',
+ storagePath: '/tmp/runtime-config.json',
+ options: [],
+ }),
+ config: {
rpcProxyBaseUrl: 'https://rpc.example',
+ externalRestApiBaseUrl: 'http://rest',
+ desktopProfileUiBaseUrl: 'https://profile.example.com',
+ desktopProfileUiBaseUrlExplicit: false,
+ enableDevLoginUi: false,
+ requestTimeoutMs: 60000,
+ appName: 'test',
},
- envKey: 'test-env',
- storagePath: '/tmp/runtime-config.json',
- options: [],
- }),
- config: {
- rpcProxyBaseUrl: 'https://rpc.example',
- externalRestApiBaseUrl: 'http://rest',
- desktopProfileUiBaseUrl: 'https://profile.example.com',
- desktopProfileUiBaseUrlExplicit: false,
- enableDevLoginUi: false,
- requestTimeoutMs: 60000,
- appName: 'test',
- },
-}))
+ }
+})
vi.mock('../sandboxUiPartitionGc.js', () => ({
touchSandboxUiPartition: electronMocks.touchSandboxUiPartition,
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index db179e51a..57c46e154 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -15,6 +15,7 @@ import {
resolveEnvKey,
saveDesktopRuntimeConfig,
selectDesktopRuntimeConfigOption,
+ validateRpcProxyBaseUrl,
} from './config.js'
import {
canonicalizeDesktopRestEndpoint,
@@ -2569,6 +2570,7 @@ export class AppService {
}
}): Promise<void> {
const discovered = await this.authClient.getDesktopEnvironment()
+ const rpcProxyBaseUrl = validateRpcProxyBaseUrl(discovered.rpcProxyBaseUrl)
if (
!sameDesktopRestEndpoint(discovered.externalRestApiBaseUrl, request.externalRestApiBaseUrl)
) {
@@ -2579,7 +2581,7 @@ export class AppService {
await this.applyRuntimeConfigDiscovery({
profileId: request.environmentBinding.profileId,
externalRestApiBaseUrl: request.externalRestApiBaseUrl,
- rpcProxyBaseUrl: discovered.rpcProxyBaseUrl,
+ rpcProxyBaseUrl,
appName: discovered.appName || request.appName,
})
})
diff --git a/desktop-app/src/config.ts b/desktop-app/src/config.ts
index 4800d3629..68f1267bd 100644
--- a/desktop-app/src/config.ts
+++ b/desktop-app/src/config.ts
@@ -524,6 +524,13 @@ function isHttpUrl(value: string): boolean {
}
}
+export function validateRpcProxyBaseUrl(value: unknown): string {
+ if (typeof value !== 'string' || !value.trim() || !isHttpUrl(value.trim())) {
+ throw new Error('rpcProxyBaseUrl must be a valid http(s) URL')
+ }
+ return value.trim()
+}
+
function validateRuntimeConfig(next: DesktopRuntimeConfig): DesktopRuntimeConfig {
const normalized = {
externalRestApiBaseUrl: next.externalRestApiBaseUrl.trim(),
@@ -534,8 +541,8 @@ function validateRuntimeConfig(next: DesktopRuntimeConfig): DesktopRuntimeConfig
if (!isHttpUrl(normalized.externalRestApiBaseUrl)) {
throw new Error('externalRestApiBaseUrl must be a valid http(s) URL')
}
- if (normalized.rpcProxyBaseUrl && !isHttpUrl(normalized.rpcProxyBaseUrl)) {
- throw new Error('rpcProxyBaseUrl must be a valid http(s) URL')
+ if (normalized.rpcProxyBaseUrl) {
+ normalized.rpcProxyBaseUrl = validateRpcProxyBaseUrl(normalized.rpcProxyBaseUrl)
}
return normalized
}