fix(webui): preserve reverse proxy authentication

Prevent login loops when the WebUI is protected by HTTP authentication
at a reverse proxy. Apply the fix to settings, uploads, and live streams
while retaining compatibility with existing WebUI clients.

Fixes #523
This commit is contained in:
DrSmoothl committed 2026-10-14 08:13:50 +08:00
1 parent d6dd748beb
commit 5150d83403
13 files changed
+244 -56

No files matched your search

+12 -28
View File
@@ -42,6 +42,7 @@ import {
type TotpStatus,
} from './types';
import { localStorageTokenStore } from './token-store';
import { WEBUI_SESSION_HEADER, webuiSessionHeaders } from './session-headers';
const DEFAULT_TOKEN_KEY = 'snowluma_token';
const REQUEST_TIMEOUT_MS = 30_000;
@@ -314,15 +315,9 @@ class HttpApiClient implements ApiClient {
// bearer header directly — mirroring login()'s deliberate bypass.
const form = new FormData();
form.append('file', file);
const headers: Record<string, string> = {};
if (this.currentToken) {
headers['Authorization'] = `Bearer ${this.currentToken}`;
}
const res = await fetch('/api/ui/background', {
method: 'POST',
headers,
headers: webuiSessionHeaders(this.currentToken),
body: form,
});
@@ -532,16 +527,10 @@ class HttpApiClient implements ApiClient {
// ---------- HTTP helpers ----------
async request(url: string, init: RequestInit = {}): Promise<Response> {
const headers: Record<string, string> = {
...((init.headers as Record<string, string> | undefined) ?? {}),
};
const headers = webuiSessionHeaders(this.currentToken, init.headers);
if (this.currentToken) {
headers['Authorization'] = `Bearer ${this.currentToken}`;
}
if (init.body && !(init.body instanceof FormData) && !headers['Content-Type']) {
headers['Content-Type'] = 'application/json';
if (init.body && !(init.body instanceof FormData) && !headers.has('Content-Type')) {
headers.set('Content-Type', 'application/json');
}
const res = await this.fetchWithDeadline(url, { ...init, headers });
@@ -616,7 +605,7 @@ class HttpApiClient implements ApiClient {
this.tokenStore.save(token);
// Rebuild the shared stream on any change so a re-login doesn't leave
// subscribers on a stale Authorization header (#185).
// subscribers on a stale session token (#185).
if (changed) {
this.reopenSharedLogStream();
}
@@ -763,7 +752,7 @@ class HttpApiClient implements ApiClient {
* `onMessage` and surfacing transport state ('open' / 'reconnecting' /
* 'closed') via `onStatus`. The fetch stream reconnects after transport loss
* so it retains EventSource's user-visible behaviour while keeping the token
* in the Authorization header. A malformed frame (or a throw from
* in the WebUI session header. A malformed frame (or a throw from
* `onMessage`) is skipped. The returned disposer aborts the active request.
*/
private openSseChannel<T>(
@@ -807,11 +796,10 @@ class HttpApiClient implements ApiClient {
try {
const response = await fetch(path, {
cache: 'no-store',
headers: {
headers: webuiSessionHeaders(token, {
Accept: 'text/event-stream',
Authorization: `Bearer ${token}`,
'Cache-Control': 'no-cache',
},
}),
signal: controller.signal,
});
@@ -1081,15 +1069,11 @@ class HttpApiClient implements ApiClient {
onFrame: (frame: import('@/types').DebugStreamFrame) => void,
signal?: AbortSignal,
): Promise<void> {
const headers: Record<string, string> = {
const headers = webuiSessionHeaders(this.currentToken, {
'Content-Type': 'application/json',
Accept: 'text/event-stream',
'Cache-Control': 'no-cache',
};
if (this.currentToken) {
headers['Authorization'] = `Bearer ${this.currentToken}`;
}
});
const res = await fetch('/api/debug/invoke-stream', {
method: 'POST',
@@ -1203,7 +1187,7 @@ class HttpApiClient implements ApiClient {
xhr.open('POST', `/api/debug/upload?filename=${encodeURIComponent(name)}`);
if (this.currentToken) {
xhr.setRequestHeader('Authorization', `Bearer ${this.currentToken}`);
xhr.setRequestHeader(WEBUI_SESSION_HEADER, this.currentToken);
}
xhr.responseType = 'json';