Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): r3-h1 reject untrusted RPC overrides
Josué35bc456034af
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsxindex e35ee231b..e3e7b1239 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAuthController.desktopEnvironment.test.tsx@@ -62,12 +62,12 @@ function Probe() {)}-async function dispatchDesktopEnvironmentLink() {+async function dispatchDesktopEnvironmentLink(payload = targetEnvironment) {if (!desktopEnvironmentSetupListener) {throw new Error('Desktop environment listener was not registered')}await act(async () => {- await desktopEnvironmentSetupListener?.(targetEnvironment)+ await desktopEnvironmentSetupListener?.(payload)})}@@ -181,22 +181,50 @@ describe('Desktop environment handoff', () => {)})- it('prompts to add the environment when its RPC proxy origin is not saved', async () => {+ it('rejects a link that proposes a different RPC proxy for a saved REST environment', async () => {await runtimeConfigModule!.saveDesktopRuntimeConfig({...targetEnvironment,externalRestApiBaseUrl: `${targetEnvironment.externalRestApiBaseUrl}/api/v1`,- rpcProxyBaseUrl: otherEnvironment.rpcProxyBaseUrl,+ rpcProxyBaseUrl: `${targetEnvironment.rpcProxyBaseUrl}/rpc`,})render(<Probe />)await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))- await dispatchDesktopEnvironmentLink()+ await dispatchDesktopEnvironmentLink({+ ...targetEnvironment,+ rpcProxyBaseUrl: 'https://rpc.attacker.test',+ })expect(mocks.selectRuntimeConfig).not.toHaveBeenCalled()+ expect(screen.getByTestId('pending-environment')).toHaveTextContent('none')+ expect(screen.getByTestId('pending-rpc')).toHaveTextContent('none')+ expect(+ (await runtimeConfigModule!.getDesktopRuntimeConfigState()).options.find(+ option =>+ option.externalRestApiBaseUrl === `${targetEnvironment.externalRestApiBaseUrl}/api/v1`+ )?.rpcProxyBaseUrl+ ).toBe(`${targetEnvironment.rpcProxyBaseUrl}/rpc`)+ expect(mocks.setStatus).toHaveBeenCalledWith(+ expect.stringMatching(/RPC proxy.*saved environment/i),+ 'error'+ )+ })++ it('does not retain an RPC proxy supplied by a new environment link', async () => {+ const linkedEnvironment = {+ appName: 'New tenant',+ externalRestApiBaseUrl: 'https://new-api.example.test',+ rpcProxyBaseUrl: 'https://rpc.attacker.test',+ }++ render(<Probe />)+ await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))+ await dispatchDesktopEnvironmentLink(linkedEnvironment)+expect(screen.getByTestId('pending-environment')).toHaveTextContent(- targetEnvironment.externalRestApiBaseUrl+ linkedEnvironment.externalRestApiBaseUrl)- expect(screen.getByTestId('pending-rpc')).toHaveTextContent(targetEnvironment.rpcProxyBaseUrl)+ expect(screen.getByTestId('pending-rpc')).toHaveTextContent('none')})it('does not offer setup when the saved environment list cannot be verified', async () => {diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.tsnew file mode 100644index 000000000..91302dc6b--- /dev/null+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts@@ -0,0 +1,138 @@+import type { DesktopRuntimeConfig, DesktopRuntimeConfigState } from '../../../../src/types'+import type { SetStatusFn } from './types'++type DesktopEnvironmentSetupPayload = {+ externalRestApiBaseUrl: string+ rpcProxyBaseUrl?: string+ appName?: string+}++type DesktopEnvironmentSetupHandlerOptions = {+ refreshRuntimeConfigState: () => Promise<DesktopRuntimeConfigState>+ handleSelectRuntimeConfig: (optionId: string) => Promise<DesktopRuntimeConfigState | null>+ onSessionNeedsLoad: (options?: { preserveNav?: boolean }) => Promise<void>+ setPendingDesktopEnvironmentSetup: (config: DesktopRuntimeConfig | null) => void+ setStatus: SetStatusFn+}++function environmentOrigin(value: string): string {+ const url = new URL(value.trim())+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {+ throw new Error('Only http(s) desktop environment URLs are supported')+ }+ return url.origin+}++function sameDesktopEnvironment(+ left: Pick<DesktopRuntimeConfig, 'externalRestApiBaseUrl' | 'rpcProxyBaseUrl'>,+ right: Pick<DesktopRuntimeConfig, 'externalRestApiBaseUrl' | 'rpcProxyBaseUrl'>+): boolean {+ try {+ return (+ environmentOrigin(left.externalRestApiBaseUrl) ===+ environmentOrigin(right.externalRestApiBaseUrl) &&+ (left.rpcProxyBaseUrl?.trim() ? environmentOrigin(left.rpcProxyBaseUrl) : '') ===+ (right.rpcProxyBaseUrl?.trim() ? environmentOrigin(right.rpcProxyBaseUrl) : '')+ )+ } catch {+ return false+ }+}++export function createDesktopEnvironmentSetupHandler({+ refreshRuntimeConfigState,+ handleSelectRuntimeConfig,+ onSessionNeedsLoad,+ setPendingDesktopEnvironmentSetup,+ setStatus,+}: DesktopEnvironmentSetupHandlerOptions) {+ return async ({+ externalRestApiBaseUrl,+ rpcProxyBaseUrl,+ appName,+ }: DesktopEnvironmentSetupPayload) => {+ const normalizedExternalRestApiBaseUrl = externalRestApiBaseUrl.trim()+ if (!normalizedExternalRestApiBaseUrl) return+ const linkedConfig: DesktopRuntimeConfig = {+ externalRestApiBaseUrl: normalizedExternalRestApiBaseUrl,+ rpcProxyBaseUrl: rpcProxyBaseUrl?.trim() || '',+ appName: appName?.trim() || 'Evenfire',+ }+ try {+ environmentOrigin(linkedConfig.externalRestApiBaseUrl)+ if (linkedConfig.rpcProxyBaseUrl) environmentOrigin(linkedConfig.rpcProxyBaseUrl)+ } catch (error) {+ setStatus(+ `Desktop setup link rejected: ${error instanceof Error ? error.message : String(error)}`,+ 'error'+ )+ return+ }++ let configState: DesktopRuntimeConfigState+ try {+ configState = await refreshRuntimeConfigState()+ } catch {+ setStatus('Could not verify the desktop environment. Try opening it again.', 'error')+ return+ }++ const activeEnvironmentMatches = Boolean(+ configState.configured &&+ configState.currentConfig &&+ sameDesktopEnvironment(configState.currentConfig, linkedConfig)+ )+ if (activeEnvironmentMatches) {+ setPendingDesktopEnvironmentSetup(null)+ setStatus(`Opening ${linkedConfig.appName} in Evenfire Desktop.`, 'success')+ return+ }++ const savedEnvironment = configState.options.find(option =>+ sameDesktopEnvironment(option, linkedConfig)+ )+ if (savedEnvironment) {+ setPendingDesktopEnvironmentSetup(null)+ const selectedState = await handleSelectRuntimeConfig(savedEnvironment.id)+ if (!selectedState) return+ try {+ await onSessionNeedsLoad({ preserveNav: true })+ } catch (error) {+ setStatus(+ `Could not load the selected desktop environment: ${error instanceof Error ? error.message : String(error)}`,+ 'error'+ )+ }+ return+ }++ const linkedRestOrigin = environmentOrigin(linkedConfig.externalRestApiBaseUrl)+ const linkedRpcOrigin = linkedConfig.rpcProxyBaseUrl+ ? environmentOrigin(linkedConfig.rpcProxyBaseUrl)+ : ''+ const conflictingSavedEnvironment = configState.options.find(option => {+ if (!option.rpcProxyBaseUrl.trim()) return false+ try {+ return (+ environmentOrigin(option.externalRestApiBaseUrl) === linkedRestOrigin &&+ Boolean(linkedRpcOrigin) &&+ environmentOrigin(option.rpcProxyBaseUrl) !== linkedRpcOrigin+ )+ } catch {+ return false+ }+ })+ if (conflictingSavedEnvironment) {+ setPendingDesktopEnvironmentSetup(null)+ setStatus(+ 'Desktop setup link rejected because its RPC proxy does not match the saved environment.',+ 'error'+ )+ return+ }++ // The RPC URL in an external link is untrusted. Desktop discovers it from+ // the selected REST endpoint after confirmation.+ setPendingDesktopEnvironmentSetup({ ...linkedConfig, rpcProxyBaseUrl: '' })+ }+}diff --git a/desktop-app/ui/src/hooks/domain/useAuthController.ts b/desktop-app/ui/src/hooks/domain/useAuthController.tsindex 864f5690a..029ba6e58 100644--- a/desktop-app/ui/src/hooks/domain/useAuthController.ts+++ b/desktop-app/ui/src/hooks/domain/useAuthController.ts@@ -10,6 +10,7 @@ import type {SessionMe,} from '../../../../src/types'import type { Tone } from '../../uiTypes'+import { createDesktopEnvironmentSetupHandler } from './desktopEnvironmentHandoff'import type { SetStatusFn } from './types'interface UseAuthControllerParams {@@ -37,30 +38,6 @@ function isUnauthorizedError(error: unknown) {return /\b401\s+unauthorized\b/.test(message) || /:\s*401\s/.test(message)}-function environmentOrigin(value: string): string {- const url = new URL(value.trim())- if (url.protocol !== 'http:' && url.protocol !== 'https:') {- throw new Error('Only http(s) desktop environment URLs are supported')- }- return url.origin-}--function sameDesktopEnvironment(- left: Pick<DesktopRuntimeConfig, 'externalRestApiBaseUrl' | 'rpcProxyBaseUrl'>,- right: Pick<DesktopRuntimeConfig, 'externalRestApiBaseUrl' | 'rpcProxyBaseUrl'>-): boolean {- try {- return (- environmentOrigin(left.externalRestApiBaseUrl) ===- environmentOrigin(right.externalRestApiBaseUrl) &&- (left.rpcProxyBaseUrl?.trim() ? environmentOrigin(left.rpcProxyBaseUrl) : '') ===- (right.rpcProxyBaseUrl?.trim() ? environmentOrigin(right.rpcProxyBaseUrl) : '')- )- } catch {- return false- }-}-export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthControllerParams) {const [booting, setBooting] = useState(true)const [busy, setBusy] = useState(false)@@ -361,64 +338,13 @@ export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthContuseEffect(() => {return window.clerum.auth.onDesktopEnvironmentSetup(- async ({ externalRestApiBaseUrl, rpcProxyBaseUrl, appName }) => {- const normalizedExternalRestApiBaseUrl = externalRestApiBaseUrl.trim()- if (!normalizedExternalRestApiBaseUrl) return- const targetConfig: DesktopRuntimeConfig = {- externalRestApiBaseUrl: normalizedExternalRestApiBaseUrl,- rpcProxyBaseUrl: rpcProxyBaseUrl?.trim() || '',- appName: appName?.trim() || 'Evenfire',- }- try {- environmentOrigin(targetConfig.externalRestApiBaseUrl)- if (targetConfig.rpcProxyBaseUrl) environmentOrigin(targetConfig.rpcProxyBaseUrl)- } catch (error) {- setStatus(- `Desktop setup link rejected: ${error instanceof Error ? error.message : String(error)}`,- 'error'- )- return- }-- let configState: DesktopRuntimeConfigState- try {- configState = await refreshRuntimeConfigState()- } catch {- setStatus('Could not verify the desktop environment. Try opening it again.', 'error')- return- }-- const activeEnvironmentMatches = Boolean(- configState.configured &&- configState.currentConfig &&- sameDesktopEnvironment(configState.currentConfig, targetConfig)- )- if (activeEnvironmentMatches) {- setPendingDesktopEnvironmentSetup(null)- setStatus(`Opening ${targetConfig.appName} in Evenfire Desktop.`, 'success')- return- }-- const savedEnvironment = configState.options.find(option =>- sameDesktopEnvironment(option, targetConfig)- )- if (savedEnvironment) {- setPendingDesktopEnvironmentSetup(null)- const selectedState = await handleSelectRuntimeConfig(savedEnvironment.id)- if (!selectedState) return- try {- await onSessionNeedsLoad({ preserveNav: true })- } catch (error) {- setStatus(- `Could not load the selected desktop environment: ${error instanceof Error ? error.message : String(error)}`,- 'error'- )- }- return- }-- setPendingDesktopEnvironmentSetup(targetConfig)- }+ createDesktopEnvironmentSetupHandler({+ refreshRuntimeConfigState,+ handleSelectRuntimeConfig,+ onSessionNeedsLoad,+ setPendingDesktopEnvironmentSetup,+ setStatus,+ }))}, [handleSelectRuntimeConfig, onSessionNeedsLoad, refreshRuntimeConfigState, setStatus])