fix(dashboard): pin fetchJSON/authedFetch to same-origin mode

The up-front URL check only sees the URL the caller passed. A same-origin
endpoint answering with a redirect to another host, or a document <base href>
pointing elsewhere, still sent the session token there. Setting
mode: "same-origin" after the caller's init makes the browser refuse both,
and callers can no longer widen it.

Reported by the independent review of #152386; reproduced in headless Chrome
with two loopback servers (redirect and <base> rows leaked on the first head,
blocked now).
This commit is contained in:
Teknium
2026-11-09 19:02:09 -08:00
parent 8247ac3399
commit e0f07847b8
2 changed files with 10 additions and 2 deletions

View File

@@ -383,9 +383,9 @@ describe("session token stays on the dashboard origin", () => {
vi.stubGlobal("fetch", fetchMock);
const own = `${window.location.origin}/api/plugins/demo/blob`;
await authedFetch("/api/plugins/demo/blob");
await authedFetch("/api/plugins/demo/blob", { mode: "cors" });
await authedFetch(own);
await fetchJSON("/api/plugins/demo/data");
await fetchJSON("/api/plugins/demo/data", { mode: "no-cors" });
expect(fetchMock.mock.calls.map(([url]) => url)).toEqual([
"/api/plugins/demo/blob",
@@ -395,6 +395,9 @@ describe("session token stays on the dashboard origin", () => {
for (const [, init] of fetchMock.mock.calls) {
expect((init?.headers as Headers).get(SESSION_HEADER)).toBe("live-token");
expect(init?.credentials).toBe("include");
// Pinned after the caller's init: the browser then refuses cross-origin
// redirects and off-origin <base href> resolution the URL check can't see.
expect(init?.mode).toBe("same-origin");
}
});
});

View File

@@ -161,6 +161,10 @@ export async function fetchJSON<T>(
// server doesn't read cookies and the legacy session-token header is
// already attached above.
credentials: init?.credentials ?? "include",
// After ``...init`` so a caller cannot widen it: the browser then refuses a
// cross-origin redirect and any ``<base href>`` resolution off this origin,
// which the up-front URL check cannot see.
mode: "same-origin",
});
} catch (cause) {
// fetch() only rejects when the request never got a response: the
@@ -313,6 +317,7 @@ export async function authedFetch(
...init,
headers,
credentials: init?.credentials ?? "include",
mode: "same-origin",
});
}