fix(webui): strengthen public session handling

Bound unauthenticated sign-in requests and require active sessions for browser-served account images.

Keep live updates authenticated without exposing session material in request URLs, and apply conservative response protections without restricting existing WebUI assets.
This commit is contained in:
DrSmoothl committed 2026-08-01 16:36:33 +08:00
1 parent 958d17ecc0
commit e4179ffb47
7 files changed
+391 -53

No files matched your search

@@ -0,0 +1,97 @@
import { Hono } from 'hono';
import { describe, expect, it } from 'vitest';
import {
clearAvatarSessionCookie,
extractBearerToken,
LOGIN_BODY_LIMIT_BYTES,
readAvatarSessionToken,
registerLoginRequestSecurity,
setAvatarSessionCookie,
webuiSecurityHeaders,
} from '../src/webui/request-security';
describe('WebUI public request security', () => {
it('rejects an oversized login body before JSON parsing', async () => {
const app = new Hono();
app.use('*', webuiSecurityHeaders);
registerLoginRequestSecurity(app);
app.post('/api/login', async (c) => c.json(await c.req.json()));
const response = await app.request('/api/login', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ password: 'x'.repeat(LOGIN_BODY_LIMIT_BYTES) }),
});
expect(response.status).toBe(413);
expect(response.headers.get('x-frame-options')).toBe('DENY');
await expect(response.json()).resolves.toMatchObject({ success: false });
});
it('accepts normal JSON login requests and rejects misleading content types', async () => {
const app = new Hono();
registerLoginRequestSecurity(app);
app.post('/api/login', async (c) => c.json(await c.req.json()));
const accepted = await app.request('/api/login', {
method: 'POST',
headers: { 'content-type': 'application/problem+json' },
body: JSON.stringify({ password: 'normal-password' }),
});
const rejected = await app.request('/api/login', {
method: 'POST',
headers: { 'content-type': 'text/plain' },
body: JSON.stringify({ password: 'normal-password' }),
});
expect(accepted.status).toBe(200);
expect(rejected.status).toBe(415);
});
it('never treats a query parameter as a bearer credential', () => {
expect(extractBearerToken(new Request('http://localhost/api/state/stream?token=leaked'))).toBe('');
expect(extractBearerToken(new Request('http://localhost/api/state/stream', {
headers: { authorization: 'Bearer header-token' },
}))).toBe('header-token');
});
it('round-trips an HttpOnly avatar session cookie and clears it on logout', async () => {
const app = new Hono();
app.get('/set', (c) => {
setAvatarSessionCookie(c, 'session-token', false);
return c.text('ok');
});
app.get('/read', (c) => c.text(readAvatarSessionToken(c)));
app.get('/clear', (c) => {
clearAvatarSessionCookie(c, false);
return c.text('ok');
});
const setResponse = await app.request('/set');
const setCookie = setResponse.headers.get('set-cookie') ?? '';
expect(setCookie).toContain('HttpOnly');
expect(setCookie).toContain('SameSite=Strict');
expect(setCookie).toContain('Path=/avatar');
const cookie = setCookie.split(';', 1)[0];
const readResponse = await app.request('/read', { headers: { cookie } });
expect(await readResponse.text()).toBe('session-token');
const clearResponse = await app.request('/clear');
expect(clearResponse.headers.get('set-cookie')).toMatch(/Max-Age=0/i);
});
it('adds security headers without imposing a script or style policy', async () => {
const app = new Hono();
app.use('*', webuiSecurityHeaders);
app.get('/', (c) => c.html('<main>SnowLuma</main>'));
const response = await app.request('/');
expect(response.headers.get('x-frame-options')).toBe('DENY');
expect(response.headers.get('x-content-type-options')).toBe('nosniff');
expect(response.headers.get('referrer-policy')).toBe('no-referrer');
expect(response.headers.get('content-security-policy')).toBe(
"frame-ancestors 'none'; base-uri 'self'; object-src 'none'",
);
});
});
@@ -1,24 +1,19 @@
import { describe, expect, it } from 'vitest';
import { SSE_TOKEN_QUERY_PATHS } from '../src/webui/server';
import { extractBearerToken } from '../src/webui/request-security';
// Regression lock for a class of bug that has bricked an SSE endpoint
// before: EventSource cannot set Authorization headers, so every SSE
// path MUST appear in this allowlist or the auth middleware computes
// `queryToken = ''` and 401s every connect. A missing entry is silent —
// the only symptom is "live updates don't work, refresh fixes it" — so
// keep this list short and verified.
describe('SSE_TOKEN_QUERY_PATHS', () => {
it('contains every SSE stream the WebUI subscribes to', () => {
expect(SSE_TOKEN_QUERY_PATHS.has('/api/logs/stream')).toBe(true);
expect(SSE_TOKEN_QUERY_PATHS.has('/api/debug/stream')).toBe(true);
expect(SSE_TOKEN_QUERY_PATHS.has('/api/state/stream')).toBe(true);
describe('WebUI stream authentication', () => {
it('accepts bearer credentials for every live stream', () => {
for (const path of ['/api/logs/stream', '/api/debug/stream', '/api/state/stream']) {
const request = new Request(`http://localhost${path}`, {
headers: { authorization: 'Bearer stream-token' },
});
expect(extractBearerToken(request)).toBe('stream-token');
}
});
it('does NOT carry any non-stream endpoint — token-via-query is reserved for SSE', () => {
// The auth middleware accepts `?token=...` for these paths only; any
// bearer-only endpoint listed here would leak tokens via access logs.
for (const path of SSE_TOKEN_QUERY_PATHS) {
expect(path.endsWith('/stream')).toBe(true);
it('does not accept tokens from stream query parameters', () => {
for (const path of ['/api/logs/stream', '/api/debug/stream', '/api/state/stream']) {
expect(extractBearerToken(new Request(`http://localhost${path}?token=leaked`))).toBe('');
}
});
});