fix(desktop): classify any 3xx as a redirect and name its Location

fetchJson/fetchPublicJson only reached the redirect diagnostic when the 3xx
carried an HTML body or text/html content-type; an empty-body 302/307 (the
common reverse-proxy / forward-auth shape) still resolved null through the
earlier empty-body check. http.request never follows redirects, so classify
on status first and reject every 3xx with the redirect error regardless of
body.

Pass res.headers.location through so the message says where the request was
sent, and stop blaming credentials when the Location differs from the
requested URL only by scheme or trailing slash -- that is a saved-URL
mismatch, not an authentication proxy. Drop the 404 mention from the
docstring/test: both callers reject >= 400 before this branch, so only the
2xx leg carries the endpoint-missing capability wording.

Part of #112072
This commit is contained in:
teknium1
2026-09-15 14:31:02 -07:00
committed by Teknium
parent c902f50efb
commit 10652c9345
3 changed files with 84 additions and 15 deletions
+26 -4
View File
@@ -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/)
})
})
+43 -11
View File
@@ -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 {
+15
View File
@@ -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)