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:
13 files changed
+244
-56
No files matched your search
@@ -12,6 +12,7 @@ import {
|
||||
import { flushSync } from 'react-dom';
|
||||
import { MotionConfig } from 'motion/react';
|
||||
import { actionErrorMessage, useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||
import { webuiSessionHeaders } from '@/lib/api/session-headers';
|
||||
import type { Palette, ThemeMode, UiAppearance, UiBackground } from '@/types';
|
||||
|
||||
// Re-export the appearance value types so consumers (settings page etc.) can
|
||||
@@ -942,7 +943,7 @@ async function fetchAppearance(): Promise<UiAppearance | null> {
|
||||
// Authed: fetch the full config so we get `customCss` (which the public
|
||||
// subset strips). Pre-auth (login page): the cosmetic public subset only.
|
||||
if (token) {
|
||||
const res = await fetch('/api/ui', { headers: { Authorization: `Bearer ${token}` } });
|
||||
const res = await fetch('/api/ui', { headers: webuiSessionHeaders(token) });
|
||||
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as {
|
||||
@@ -995,7 +996,7 @@ async function persistAppearance(appearance: UiAppearance): Promise<void> {
|
||||
|
||||
const response = await fetch('/api/ui', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
headers: webuiSessionHeaders(token, { 'Content-Type': 'application/json' }),
|
||||
// Section-level save: only `appearance`; the server preserves `layout`.
|
||||
body: JSON.stringify({ appearance }),
|
||||
});
|
||||
@@ -1347,7 +1348,7 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
form.append('file', file);
|
||||
const res = await fetch('/api/ui/background', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
headers: webuiSessionHeaders(token),
|
||||
body: form,
|
||||
});
|
||||
|
||||
@@ -1388,7 +1389,7 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
const res = await fetch('/api/ui/background', {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
headers: webuiSessionHeaders(token),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
export const WEBUI_SESSION_HEADER = 'X-SnowLuma-Token';
|
||||
|
||||
/** Leave Authorization available for HTTP authentication at the reverse proxy. */
|
||||
export function webuiSessionHeaders(token: string | null, initial?: HeadersInit): Headers {
|
||||
const headers = new Headers(initial);
|
||||
|
||||
if (token) {
|
||||
headers.set(WEBUI_SESSION_HEADER, token);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
Reference in new issue
Block a user