Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): reject saved same-origin REST endpoint collisions (R13-L5)
Josuébd84ce9c3374
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.tsindex c42785a0c..d74577b4e 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts+++ b/desktop-app/ui/src/hooks/domain/__tests__/desktopEnvironmentHandoff.test.ts@@ -162,7 +162,7 @@ describe('Desktop environment handoff concurrency', () => {},{outcome: 'prompts to add the linked environment',- externalRestApiBaseUrl: `${targetEnvironment.externalRestApiBaseUrl}/other`,+ externalRestApiBaseUrl: 'https://unconfigured-api.example.test/api/v1',action: 'prompt',},])('waits for logout to finish, then $outcome', async ({ externalRestApiBaseUrl, action }) => {@@ -563,7 +563,7 @@ describe('Desktop environment REST endpoint matching', () => {})it.each(['one saved profile', 'multiple saved profiles'])(- 'offers to add an unmatched REST path with %s on the same origin',+ 'rejects a different REST API path when %s already uses the same origin',async profileCount => {if (profileCount === 'multiple saved profiles') {await runtimeConfigModule!.saveDesktopRuntimeConfig({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 c4eaec318..25b266f78 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@@ -213,6 +213,35 @@ afterEach(async () => {})describe('Desktop environment handoff', () => {+ it('rejects a same-origin REST path conflict added before setup confirmation', async () => {+ const linkedEnvironment = {+ appName: 'New tenant',+ externalRestApiBaseUrl: 'https://new-api.example.test/api/v1',+ }+ render(<Probe />)+ await waitFor(() => expect(screen.getByTestId('configuration-loaded')).toHaveTextContent('yes'))+ await dispatchDesktopEnvironmentLink(linkedEnvironment)+ expect(screen.getByTestId('pending-environment')).toHaveTextContent(+ linkedEnvironment.externalRestApiBaseUrl+ )++ await runtimeConfigModule!.saveDesktopRuntimeConfig({+ appName: 'Other API path',+ externalRestApiBaseUrl: 'https://new-api.example.test/api/v2',+ rpcProxyBaseUrl: 'https://rpc.new-api.example.test',+ })+ mocks.setStatus.mockClear()++ await act(async () => confirmDesktopEnvironmentSetupForTest?.())++ expect(mocks.saveRuntimeConfig).not.toHaveBeenCalled()+ expect(screen.getByTestId('pending-environment')).toHaveTextContent('none')+ expect(mocks.setStatus).toHaveBeenCalledWith(+ 'Desktop setup link rejected because this REST host is already saved with a different API endpoint.',+ 'error'+ )+ })+it('asks the user to sign out when native desktop setup rejects an active session', async () => {mocks.completeDesktopSetup.mockRejectedValue(wrapLikeElectronIpc('auth:completeDesktopSetup', new Error('desktop_setup_requires_signout'))diff --git a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.tsindex c2dbbe84a..b5a8d2b12 100644--- a/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts+++ b/desktop-app/ui/src/hooks/domain/desktopEnvironmentHandoff.ts@@ -51,6 +51,24 @@ function savedEnvironmentsForRestEndpoint(})}+function savedEnvironmentsWithDifferentEndpointOnSameOrigin(+ options: DesktopRuntimeConfigState['options'],+ externalRestApiBaseUrl: string+) {+ const restOrigin = desktopRestEndpointOrigin(externalRestApiBaseUrl)+ return options.filter(option => {+ if (option.source === 'localhost' || option.id === LOCALHOST_OPTION_ID) return false+ try {+ return (+ desktopRestEndpointOrigin(option.externalRestApiBaseUrl) === restOrigin &&+ !sameDesktopRestEndpoint(option.externalRestApiBaseUrl, externalRestApiBaseUrl)+ )+ } catch {+ return false+ }+ })+}+function isLocalhostOption(option: DesktopRuntimeConfigState['options'][number]): boolean {return option.source === 'localhost' || option.id === LOCALHOST_OPTION_ID}@@ -70,6 +88,10 @@ export function getDesktopEnvironmentRestMatches(}}),saved: savedEnvironmentsForRestEndpoint(configState.options, externalRestApiBaseUrl),+ sameOriginDifferentEndpoint: savedEnvironmentsWithDifferentEndpointOnSameOrigin(+ configState.options,+ externalRestApiBaseUrl+ ),}}@@ -77,6 +99,27 @@ function isAuthenticationOperationInProgress(state: DesktopEnvironmentHandoffStareturn state.booting || state.busy || state.authTransitioning}+function isActiveRestEndpointMatch(+ configState: DesktopRuntimeConfigState,+ externalRestApiBaseUrl: string+): boolean {+ return Boolean(+ configState.configured &&+ configState.currentConfig &&+ sameDesktopRestEndpoint(+ configState.currentConfig.externalRestApiBaseUrl,+ externalRestApiBaseUrl+ )+ )+}++function rejectSameOriginPathConflict(setStatus: SetStatusFn): void {+ setStatus(+ 'Desktop setup link rejected because this REST host is already saved with a different API endpoint.',+ 'error'+ )+}+export function createDesktopEnvironmentSetupHandler({getAuthState,getSessionGeneration,@@ -162,14 +205,16 @@ export function createDesktopEnvironmentSetupHandler({return}+ if (restMatches.sameOriginDifferentEndpoint.length > 0) {+ setPendingDesktopEnvironmentSetup(null)+ rejectSameOriginPathConflict(setStatus)+ return+ }+let authState = getAuthState()- const activeRestEndpointMatches = Boolean(- configState.configured &&- configState.currentConfig &&- sameDesktopRestEndpoint(- configState.currentConfig.externalRestApiBaseUrl,- linkedConfig.externalRestApiBaseUrl- )+ let activeRestEndpointMatches = isActiveRestEndpointMatch(+ configState,+ linkedConfig.externalRestApiBaseUrl)if (restMatches.saved.length > 1 && !activeRestEndpointMatches) {setPendingDesktopEnvironmentSetup(null)@@ -256,6 +301,11 @@ export function createDesktopEnvironmentSetupHandler({)return}+ if (restMatches.sameOriginDifferentEndpoint.length > 0) {+ setPendingDesktopEnvironmentSetup(null)+ rejectSameOriginPathConflict(setStatus)+ return+ }}if (authState.isAuthenticated) {diff --git a/desktop-app/ui/src/hooks/domain/useAuthController.ts b/desktop-app/ui/src/hooks/domain/useAuthController.tsindex 45b08b9d1..46fd73d8b 100644--- a/desktop-app/ui/src/hooks/domain/useAuthController.ts+++ b/desktop-app/ui/src/hooks/domain/useAuthController.ts@@ -555,6 +555,15 @@ export function useAuthController({return}+ if (restMatches.sameOriginDifferentEndpoint.length > 0) {+ setPendingDesktopEnvironmentSetup(null)+ setStatus(+ 'Desktop setup link rejected because this REST host is already saved with a different API endpoint.',+ 'error'+ )+ return+ }+if (restMatches.saved.length > 0) {if (restMatches.saved.length > 1) {setPendingDesktopEnvironmentSetup(null)