diff --git a/apps/desktop/electron/api-transport.test.ts b/apps/desktop/electron/api-transport.test.ts index 9190867bde..1ad2448565 100644 --- a/apps/desktop/electron/api-transport.test.ts +++ b/apps/desktop/electron/api-transport.test.ts @@ -407,14 +407,36 @@ describe('httpStatusError', () => { }) describe('htmlResponseError', () => { - it('names an auth redirect for 3xx HTML and keeps the endpoint-missing capability wording otherwise', () => { - const redirected = htmlResponseError('https://gateway.example.com/api/profiles', 302).message + it('names an auth redirect with its Location for 3xx and keeps the endpoint-missing capability wording for 2xx HTML', () => { + const redirected = htmlResponseError( + 'https://gateway.example.com/api/profiles', + 302, + 'https://sso.example.com/login?next=%2Fapi%2Fprofiles' + ).message expect(redirected).toContain('status 302') - expect(redirected).toMatch(/redirected/) + expect(redirected).toContain('to https://sso.example.com/login?next=%2Fapi%2Fprofiles') + expect(redirected).toMatch(/authentication proxy/) expect(redirected).not.toContain('endpoint is likely missing') - expect(htmlResponseError('https://gateway.example.com/api/missing', 404).message).toContain( + expect(htmlResponseError('https://gateway.example.com/api/profiles', 307).message).toMatch(/redirected \(status 307\)\. This is usually/) + expect(htmlResponseError('https://gateway.example.com/api/missing', 200).message).toContain( 'endpoint is likely missing' ) }) + + it('does not blame credentials when the redirect only fixes the scheme or a trailing slash', () => { + for (const [url, location] of [ + ['http://gateway.example.com/api/profiles', 'https://gateway.example.com/api/profiles'], + ['https://gateway.example.com/api/profiles', '/api/profiles/'], + ['https://gateway.example.com/hermes/api/health', 'https://gateway.example.com/hermes/api/health/'] + ]) { + const message = htmlResponseError(url, 301, location).message + + expect(message).toContain(`to ${location}`) + expect(message).toMatch(/scheme or trailing slash/) + expect(message).not.toMatch(/authentication proxy/) + } + + expect(htmlResponseError('https://gateway.example.com/api/profiles', 302, 'https://gateway.example.com/login').message).toMatch(/authentication proxy/) + }) }) diff --git a/apps/desktop/electron/api-transport.ts b/apps/desktop/electron/api-transport.ts index 181b657e1a..dd1cfa61fa 100644 --- a/apps/desktop/electron/api-transport.ts +++ b/apps/desktop/electron/api-transport.ts @@ -195,22 +195,54 @@ function readStatusCode(error: unknown): number { } /** - * Error for a JSON endpoint that answered with HTML. A 2xx/404 HTML body is - * the SPA index.html for an unregistered /api path, and downstream capability + * Error for a JSON endpoint that did not answer JSON. A 2xx HTML body is the + * SPA index.html for an unregistered /api path, and downstream capability * probes (isMissingHealthEndpointError, gateway-rpc) key on the "endpoint is - * likely missing" wording. A 3xx is an access proxy bouncing the request to - * its login page instead — the endpoint exists, the credentials never - * arrived — so it must neither carry that wording nor blame the backend. + * likely missing" wording. A 3xx (callers never follow redirects, whatever + * the body) is the request being bounced elsewhere: an access proxy sending + * it to its login page, or a scheme / trailing-slash redirect when the saved + * URL is off by that much — so it must neither carry that wording nor blame + * the backend, and it names the Location when the server sent one. */ -function htmlResponseError(url: string, statusCode: unknown) { +function htmlResponseError(url: string, statusCode: unknown, location?: unknown) { const status = Number(statusCode) - const hint = - status >= 300 && status < 400 - ? 'The request was redirected, usually by an authentication proxy in front of the gateway; check the saved token and extra gateway headers.' - : 'The endpoint is likely missing on the Hermes backend.' + if (status >= 300 && status < 400) { + const target = typeof location === 'string' && location.trim() ? location.trim() : null - return new Error(`Expected JSON from ${url} but got HTML (status ${statusCode}). ${hint}`) + const hint = isSchemeOrSlashRedirect(url, target) + ? 'The saved gateway URL differs from the server by scheme or trailing slash; update it to match.' + : 'This is usually an authentication proxy in front of the gateway; check the saved token and extra gateway headers.' + + return new Error( + `Expected JSON from ${url} but the request was redirected (status ${statusCode})${target ? ` to ${target}` : ''}. ${hint}` + ) + } + + return new Error( + `Expected JSON from ${url} but got HTML (status ${statusCode}). The endpoint is likely missing on the Hermes backend.` + ) +} + +function isSchemeOrSlashRedirect(requestUrl: string, location: null | string): boolean { + if (!location) { + return false + } + + try { + const from = new URL(requestUrl) + const to = new URL(location, requestUrl) + const strip = (value: string) => value.replace(/\/+$/, '') + + return ( + from.host === to.host && + from.search === to.search && + (from.protocol !== to.protocol || from.pathname !== to.pathname) && + strip(from.pathname) === strip(to.pathname) + ) + } catch { + return false + } } export { diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 4801a770fc..4baab2a6a6 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -5456,6 +5456,15 @@ function fetchJson(url, token, options: any = {}) { return } + // http.request never follows redirects, so any 3xx -- with an HTML + // login page, an empty body, whatever -- is the request bouncing + // off a proxy or a scheme/slash mismatch, never JSON. + if (res.statusCode >= 300) { + reject(htmlResponseError(url, res.statusCode, res.headers.location)) + + return + } + if (!text) { resolve(null) @@ -5617,6 +5626,12 @@ function fetchPublicJson(url, options: any = {}) { return } + if (res.statusCode >= 300) { + reject(htmlResponseError(url, res.statusCode, res.headers.location)) + + return + } + if (!text) { resolve(null)