Skip to content

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

TypeScript

fix(desktop): r3-m1 block signed-in deep-link switches

Josuéb5e04b376ea8
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.tsx
index e3e7b1239..f7057249a 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
@@ -39,6 +39,8 @@ type DesktopEnvironmentSetupPayload = typeof targetEnvironment
let desktopEnvironmentSetupListener:
| ((payload: DesktopEnvironmentSetupPayload) => void | Promise<void>)
| null = null
+let setBootingForTest: ((value: boolean) => void) | null = null
+let setAuthenticatedForTest: ((value: boolean) => void) | null = null
let runtimeConfigModule: typeof import('../../../../../src/config') | null = null
let runtimeConfigDirectory = ''
const originalOnboardingPreview = process.env.EVENFIRE_ONBOARDING_PREVIEW
@@ -48,6 +50,8 @@ function Probe() {
setStatus: mocks.setStatus,
onSessionNeedsLoad: mocks.loadSession,
})
+ setBootingForTest = auth.setBooting
+ setAuthenticatedForTest = auth.setIsAuthenticated
return (
<>
@@ -85,6 +89,8 @@ async function savedTargetOptionId(): Promise<string> {
beforeEach(async () => {
vi.clearAllMocks()
desktopEnvironmentSetupListener = null
+ setBootingForTest = null
+ setAuthenticatedForTest = null
runtimeConfigModule = null
delete process.env.EVENFIRE_ONBOARDING_PREVIEW
runtimeConfigDirectory = await fsp.mkdtemp(path.join(os.tmpdir(), 'evenfire-desktop-env-test-'))
@@ -113,7 +119,7 @@ beforeEach(async () => {
await runtimeConfigModule!.selectDesktopRuntimeConfigOption(optionId)
return runtimeConfigModule!.getDesktopRuntimeConfigState()
})
- mocks.loadSession.mockResolvedValue(undefined)
+ mocks.loadSession.mockImplementation(async () => setBootingForTest?.(false))
mocks.onDesktopSetupToken.mockReturnValue(() => undefined)
mocks.onExternalLogout.mockReturnValue(() => undefined)
mocks.onDesktopEnvironmentSetup.mockImplementation(
@@ -227,6 +233,51 @@ describe('Desktop environment handoff', () => {
expect(screen.getByTestId('pending-rpc')).toHaveTextContent('none')
})
+ it('does not switch away from another saved environment while signed in', async () => {
+ const state = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ const otherOption = state.options.find(
+ option => option.externalRestApiBaseUrl === otherEnvironment.externalRestApiBaseUrl
+ )
+ if (!otherOption) throw new Error('The runtime config producer did not return the other target')
+ await runtimeConfigModule!.selectDesktopRuntimeConfigOption(otherOption.id)
+
+ render(<Probe />)
+ await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))
+ await act(async () => setAuthenticatedForTest?.(true))
+ mocks.selectRuntimeConfig.mockClear()
+ mocks.setStatus.mockClear()
+
+ await dispatchDesktopEnvironmentLink()
+
+ expect(mocks.selectRuntimeConfig).not.toHaveBeenCalled()
+ expect(screen.getByTestId('pending-environment')).toHaveTextContent('none')
+ expect(mocks.setStatus).toHaveBeenCalledWith(
+ expect.stringMatching(/sign out.*before opening another/i),
+ 'info'
+ )
+ })
+
+ it('does not let a desktop link select the built-in Localhost environment', async () => {
+ const state = await runtimeConfigModule!.getDesktopRuntimeConfigState()
+ const localhostOption = state.options.find(option => option.id === '__localhost__')
+ if (!localhostOption) throw new Error('The runtime config producer did not return Localhost')
+
+ render(<Probe />)
+ await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))
+ await dispatchDesktopEnvironmentLink({
+ appName: localhostOption.appName,
+ externalRestApiBaseUrl: localhostOption.externalRestApiBaseUrl,
+ rpcProxyBaseUrl: localhostOption.rpcProxyBaseUrl,
+ })
+
+ expect(mocks.selectRuntimeConfig).not.toHaveBeenCalled()
+ expect(screen.getByTestId('pending-environment')).toHaveTextContent('none')
+ expect(mocks.setStatus).toHaveBeenCalledWith(
+ expect.stringMatching(/Localhost.*cannot be opened from a link/i),
+ 'error'
+ )
+ })
+
it('does not offer setup when the saved environment list cannot be verified', async () => {
mocks.getRuntimeConfigState.mockImplementationOnce(async () =>
runtimeConfigModule!.getDesktopRuntimeConfigState()
diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
index 91302dc6b..eca050cee 100644
--- a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts
@@ -1,6 +1,15 @@
import type { DesktopRuntimeConfig, DesktopRuntimeConfigState } from '../../../../src/types'
import type { SetStatusFn } from './types'
+const LOCALHOST_OPTION_ID = '__localhost__'
+
+type DesktopEnvironmentHandoffState = {
+ booting: boolean
+ busy: boolean
+ authTransitioning: boolean
+ isAuthenticated: boolean
+}
+
type DesktopEnvironmentSetupPayload = {
externalRestApiBaseUrl: string
rpcProxyBaseUrl?: string
@@ -8,6 +17,7 @@ type DesktopEnvironmentSetupPayload = {
}
type DesktopEnvironmentSetupHandlerOptions = {
+ getAuthState: () => DesktopEnvironmentHandoffState
refreshRuntimeConfigState: () => Promise<DesktopRuntimeConfigState>
handleSelectRuntimeConfig: (optionId: string) => Promise<DesktopRuntimeConfigState | null>
onSessionNeedsLoad: (options?: { preserveNav?: boolean }) => Promise<void>
@@ -40,6 +50,7 @@ function sameDesktopEnvironment(
}
export function createDesktopEnvironmentSetupHandler({
+ getAuthState,
refreshRuntimeConfigState,
handleSelectRuntimeConfig,
onSessionNeedsLoad,
@@ -77,6 +88,16 @@ export function createDesktopEnvironmentSetupHandler({
return
}
+ const localhostOption = configState.options.find(option => option.id === LOCALHOST_OPTION_ID)
+ if (localhostOption && sameDesktopEnvironment(localhostOption, linkedConfig)) {
+ setPendingDesktopEnvironmentSetup(null)
+ setStatus(
+ 'Desktop setup link rejected: the Localhost environment cannot be opened from a link.',
+ 'error'
+ )
+ return
+ }
+
const activeEnvironmentMatches = Boolean(
configState.configured &&
configState.currentConfig &&
@@ -88,6 +109,12 @@ export function createDesktopEnvironmentSetupHandler({
return
}
+ if (getAuthState().isAuthenticated) {
+ setPendingDesktopEnvironmentSetup(null)
+ setStatus('Sign out before opening another desktop environment.', 'info')
+ return
+ }
+
const savedEnvironment = configState.options.find(option =>
sameDesktopEnvironment(option, linkedConfig)
)
diff --git a/desktop-app/ui/src/hooks/domain/useAuthController.ts b/desktop-app/ui/src/hooks/domain/useAuthController.ts
index 029ba6e58..5b650fb3c 100644
--- a/desktop-app/ui/src/hooks/domain/useAuthController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAuthController.ts
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useState } from 'react'
+import { useCallback, useEffect, useRef, useState } from 'react'
import { DEFAULT_TOAST_DURATION_MS } from '@constants/toasts'
import { desktopQueryClient } from '@lib/queryClient'
import type {
@@ -39,11 +39,26 @@ function isUnauthorizedError(error: unknown) {
}
export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthControllerParams) {
- const [booting, setBooting] = useState(true)
- const [busy, setBusy] = useState(false)
+ const [booting, setBootingState] = useState(true)
+ const bootingRef = useRef(true)
+ const setBooting = useCallback((next: boolean) => {
+ bootingRef.current = next
+ setBootingState(next)
+ }, [])
+ const [busy, setBusyState] = useState(false)
+ const busyRef = useRef(false)
+ const setBusy = useCallback((next: boolean) => {
+ busyRef.current = next
+ setBusyState(next)
+ }, [])
const [statusText, setStatusText] = useState('Ready.')
const [statusTone, setStatusTone] = useState<Tone>('info')
- const [isAuthenticated, setIsAuthenticated] = useState(false)
+ const [isAuthenticated, setIsAuthenticatedState] = useState(false)
+ const isAuthenticatedRef = useRef(false)
+ const setIsAuthenticated = useCallback((next: boolean) => {
+ isAuthenticatedRef.current = next
+ setIsAuthenticatedState(next)
+ }, [])
const [me, setMe] = useState<SessionMe | null>(null)
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
@@ -53,7 +68,12 @@ export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthCont
const [runtimeConfigSetupExternalRestApiBaseUrl, setRuntimeConfigSetupExternalRestApiBaseUrl] =
useState('')
const [runtimeConfigSetupRpcProxyBaseUrl, setRuntimeConfigSetupRpcProxyBaseUrl] = useState('')
- const [authTransitioning, setAuthTransitioning] = useState(false)
+ const [authTransitioning, setAuthTransitioningState] = useState(false)
+ const authTransitioningRef = useRef(false)
+ const setAuthTransitioning = useCallback((next: boolean) => {
+ authTransitioningRef.current = next
+ setAuthTransitioningState(next)
+ }, [])
const [runtimeConfigState, setRuntimeConfigState] = useState<DesktopRuntimeConfigState | null>(
null
)
@@ -78,6 +98,16 @@ export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthCont
(!dependencyHealth.externalRestApi.ok || !dependencyHealth.rpcProxy.ok)
)
+ const getDesktopEnvironmentHandoffAuthState = useCallback(
+ () => ({
+ booting: bootingRef.current,
+ busy: busyRef.current,
+ authTransitioning: authTransitioningRef.current,
+ isAuthenticated: isAuthenticatedRef.current,
+ }),
+ []
+ )
+
const refreshRuntimeConfigState = useCallback(async () => {
const state = await window.clerum.auth.getRuntimeConfigState()
setRuntimeConfigState(state)
@@ -339,6 +369,7 @@ export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthCont
useEffect(() => {
return window.clerum.auth.onDesktopEnvironmentSetup(
createDesktopEnvironmentSetupHandler({
+ getAuthState: getDesktopEnvironmentHandoffAuthState,
refreshRuntimeConfigState,
handleSelectRuntimeConfig,
onSessionNeedsLoad,
@@ -346,7 +377,13 @@ export function useAuthController({ setStatus, onSessionNeedsLoad }: UseAuthCont
setStatus,
})
)
- }, [handleSelectRuntimeConfig, onSessionNeedsLoad, refreshRuntimeConfigState, setStatus])
+ }, [
+ getDesktopEnvironmentHandoffAuthState,
+ handleSelectRuntimeConfig,
+ onSessionNeedsLoad,
+ refreshRuntimeConfigState,
+ setStatus,
+ ])
const handleClearRuntimeConfigSelection = async (): Promise<DesktopRuntimeConfigState | null> => {
try {