Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): require http(s) scheme before shell.openExternal (R4-L8a)
Diego Zacarias2d5ecac26a0e
diff --git a/desktop-app/src/__tests__/appService.sandboxUiOauthConsent.test.ts b/desktop-app/src/__tests__/appService.sandboxUiOauthConsent.test.tsindex e1177ec8b..f3cb875d9 100644--- a/desktop-app/src/__tests__/appService.sandboxUiOauthConsent.test.ts+++ b/desktop-app/src/__tests__/appService.sandboxUiOauthConsent.test.ts@@ -161,6 +161,80 @@ describe('AppService.requestSandboxUiOauthAuthorize', () => {expect(mocks.openExternal).toHaveBeenCalledWith(AUTHORIZE_URL)})+ // R4-L8a — the authorize URL comes from rpc-proxy; a compromised or+ // misconfigured upstream returning a non-`https:` scheme must never reach+ // `shell.openExternal`, which would hand `file:`/`javascript:`/`http:` to the+ // OS protocol handler. Same boundary the tests above mock (the rpc client's+ // returned `authorizeUrl` string).+ it.each([+ ['http:', 'http://accounts.google.com/o/oauth2/v2/auth'],+ ['javascript:', 'javascript:alert(document.cookie)'],+ ['file:', 'file:///etc/passwd'],+ ['data:', 'data:text/html,<script>alert(1)</script>'],+ ['clerum:', 'clerum://oauth?clientId=evil'],+ ])(+ 'rejects a non-https authorize URL (%s) and does NOT call shell.openExternal',+ async (_label, maliciousUrl) => {+ mocks.requestSandboxUiOauthAuthorizeUrl.mockResolvedValue({ authorizeUrl: maliciousUrl })++ const svc = makeService()+ await expect(+ svc.requestSandboxUiOauthAuthorize(RECIPE_NS, RECIPE_NAME, CLIENT_ID, false)+ ).rejects.toThrow(/unsupported scheme|malformed/)++ expect(mocks.openExternal).not.toHaveBeenCalled()+ }+ )++ // Exercises the `malformed` branch of the guard: `new URL()` throws, the+ // helper catches and rethrows without ever reaching `shell.openExternal`.+ it.each([+ ['unparseable', 'not-a-url'],+ ['empty', ''],+ ])(+ 'rejects a malformed authorize URL (%s) and does NOT call shell.openExternal',+ async (_label, malformedUrl) => {+ mocks.requestSandboxUiOauthAuthorizeUrl.mockResolvedValue({ authorizeUrl: malformedUrl })++ const svc = makeService()+ await expect(+ svc.requestSandboxUiOauthAuthorize(RECIPE_NS, RECIPE_NAME, CLIENT_ID, false)+ ).rejects.toThrow(/^Refusing to open a malformed external URL$/)++ expect(mocks.openExternal).not.toHaveBeenCalled()+ }+ )++ it('does not leak the malformed input in the rejection message', async () => {+ // `ht!tp://…` has an invalid scheme char → `new URL()` throws → malformed branch.+ mocks.requestSandboxUiOauthAuthorizeUrl.mockResolvedValue({+ authorizeUrl: 'ht!tp://SUPER_SECRET_ONLY_IN_MALFORMED',+ })++ const svc = makeService()+ let caught: Error | undefined+ try {+ await svc.requestSandboxUiOauthAuthorize(RECIPE_NS, RECIPE_NAME, CLIENT_ID, false)+ } catch (e) {+ caught = e as Error+ }+ expect(caught?.message).toBe('Refusing to open a malformed external URL')+ expect(caught?.message).not.toContain('SUPER_SECRET_ONLY_IN_MALFORMED')+ expect(mocks.openExternal).not.toHaveBeenCalled()+ })++ it('does not leak the authorize URL (query/secrets) in the rejection message', async () => {+ mocks.requestSandboxUiOauthAuthorizeUrl.mockResolvedValue({+ authorizeUrl: 'http://evil.example/authorize?token=SUPER_SECRET',+ })++ const svc = makeService()+ await expect(+ svc.requestSandboxUiOauthAuthorize(RECIPE_NS, RECIPE_NAME, CLIENT_ID, false)+ ).rejects.toThrow(/^Refusing to open external URL with unsupported scheme "http:"$/)+ expect(mocks.openExternal).not.toHaveBeenCalled()+ })+it('retries with a fresh RPC token when the first call gets a 401', async () => {mocks.requestSandboxUiOauthAuthorizeUrl.mockRejectedValueOnce(new ApiError('Unauthorized', 401, ''))diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex 52c9726e1..5423bd995 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -125,6 +125,40 @@ function requireProfileUiBaseUrlForBrowserAction(): string {return explicitBaseUrl}+/**+ * Guarded wrapper around `shell.openExternal` for URLs that originate in data+ * (server responses, config, deep-links) rather than fixed constants. Electron+ * hands a non-web scheme (`file:`, `javascript:`, `data:`, a custom protocol+ * such as `clerum://`) straight to the OS protocol handler, so every+ * data-derived URL is scheme-checked before it can reach the browser.+ *+ * `requireHttps` pins OAuth authorize URLs to `https:` — real providers never+ * issue a plaintext authorize endpoint. The default also permits `http:` for+ * profile-ui links, which legitimately run on `http://localhost` in local dev+ * (see `normalizeExplicitProfileUiBaseUrl`, which accepts both schemes).+ *+ * On rejection only the offending scheme is surfaced — never the full URL,+ * whose query string may carry tokens or other secrets.+ */+async function openExternalDataUrl(+ rawUrl: string,+ options: { requireHttps?: boolean } = {}+): Promise<void> {+ const value = String(rawUrl ?? '')+ let parsed: URL+ try {+ parsed = new URL(value)+ } catch {+ throw new Error('Refusing to open a malformed external URL')+ }+ const allowedProtocols = options.requireHttps ? ['https:'] : ['https:', 'http:']+ if (!allowedProtocols.includes(parsed.protocol)) {+ throw new Error(`Refusing to open external URL with unsupported scheme "${parsed.protocol}"`)+ }+ const { shell } = await import('electron')+ await shell.openExternal(value)+}+/*** Finite-operation scope matrix — the single source of truth for the exact* scope array each finite (single request/response) Host RPC operation requests,@@ -1590,8 +1624,7 @@ export class AppService {`${activation.profileUiBaseUrl.replace(/\/+$/, '')}/`)profileUiUrl.searchParams.set('email', activation.email)- const { shell } = await import('electron')- await shell.openExternal(profileUiUrl.toString())+ await openExternalDataUrl(profileUiUrl.toString())return {profileUiUrl: profileUiUrl.toString(),appName: activation.appName,@@ -1620,8 +1653,7 @@ export class AppService {const profileUiBaseUrl = requireProfileUiBaseUrlForBrowserAction()const profileUiUrl = new URL('/forgot-password', `${profileUiBaseUrl.replace(/\/+$/, '')}/`)if (normalizedEmail) profileUiUrl.searchParams.set('email', normalizedEmail)- const { shell } = await import('electron')- await shell.openExternal(profileUiUrl.toString())+ await openExternalDataUrl(profileUiUrl.toString())return { profileUiUrl: profileUiUrl.toString() }}@@ -1641,8 +1673,7 @@ export class AppService {: '/settings/profile'const profileUiUrl = new URL(socialPath, `${profileUiBaseUrl.replace(/\/+$/, '')}/`)if (options.action === 'password') profileUiUrl.searchParams.set('action', 'password')- const { shell } = await import('electron')- await shell.openExternal(profileUiUrl.toString())+ await openExternalDataUrl(profileUiUrl.toString())return { profileUiUrl: profileUiUrl.toString() }}@@ -4497,8 +4528,7 @@ export class AppService {: await dialog.showMessageBox(opts)if (result.response !== 1) return}- const { shell } = await import('electron')- await shell.openExternal(result.authorizeUrl)+ await openExternalDataUrl(result.authorizeUrl, { requireHttps: true })}/**@@ -4543,8 +4573,7 @@ export class AppService {throw error}}- const { shell } = await import('electron')- await shell.openExternal(result.authorizeUrl)+ await openExternalDataUrl(result.authorizeUrl, { requireHttps: true })}/**