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,65 @@
import { bodyLimit } from 'hono/body-limit';
import { deleteCookie, getCookie, setCookie } from 'hono/cookie';
import type { Context, Hono, MiddlewareHandler } from 'hono';
export const LOGIN_BODY_LIMIT_BYTES = 16 * 1024;
const AVATAR_SESSION_COOKIE = 'snowluma_avatar_session';
const AVATAR_SESSION_MAX_AGE_SECONDS = 24 * 60 * 60;
function isJsonMediaType(value: string | undefined): boolean {
const mediaType = value?.split(';', 1)[0]?.trim().toLowerCase() ?? '';
return mediaType === 'application/json' || mediaType.endsWith('+json');
}
/** Install guards before the login route is registered. */
export function registerLoginRequestSecurity(app: Hono): void {
app.use('/api/login', bodyLimit({
maxSize: LOGIN_BODY_LIMIT_BYTES,
onError: (c) => c.json({ success: false, message: '请求体过大' }, 413),
}));
app.use('/api/login', async (c, next) => {
if (!isJsonMediaType(c.req.header('content-type'))) {
return c.json({ success: false, message: 'Content-Type 必须为 application/json' }, 415);
}
return next();
});
}
/** Minimal response policy that does not restrict the current script/style bundle. */
export const webuiSecurityHeaders: MiddlewareHandler = async (c, next) => {
await next();
c.res.headers.set('X-Frame-Options', 'DENY');
c.res.headers.set('X-Content-Type-Options', 'nosniff');
c.res.headers.set('Referrer-Policy', 'no-referrer');
c.res.headers.set(
'Content-Security-Policy',
"frame-ancestors 'none'; base-uri 'self'; object-src 'none'",
);
};
export function extractBearerToken(request: Request): string {
const authorization = request.headers.get('authorization');
return authorization?.startsWith('Bearer ') ? authorization.slice(7) : '';
}
const avatarCookieOptions = (secure: boolean) => ({
httpOnly: true,
sameSite: 'Strict' as const,
secure,
path: '/avatar',
});
export function setAvatarSessionCookie(c: Context, token: string, secure: boolean): void {
setCookie(c, AVATAR_SESSION_COOKIE, token, {
...avatarCookieOptions(secure),
maxAge: AVATAR_SESSION_MAX_AGE_SECONDS,
});
}
export function clearAvatarSessionCookie(c: Context, secure: boolean): void {
deleteCookie(c, AVATAR_SESSION_COOKIE, avatarCookieOptions(secure));
}
export function readAvatarSessionToken(c: Context): string {
return getCookie(c, AVATAR_SESSION_COOKIE) ?? '';
}
+29 -21
View File
@@ -79,6 +79,14 @@ import type { NotificationManager } from '../notifications/manager';
import { StorageManagementService } from './storage-management';
import { registerStorageRoutes } from './storage-routes';
import { LogStorageSettingsManager } from './storage-settings';
import {
clearAvatarSessionCookie,
extractBearerToken,
readAvatarSessionToken,
registerLoginRequestSecurity,
setAvatarSessionCookie,
webuiSecurityHeaders,
} from './request-security';
const log = createLogger('WebUI');
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -199,21 +207,6 @@ const CONSENT_ALLOWLIST = new Set([
'/api/logout',
]);
// Endpoints that may authenticate via `?token=` query parameter. Reserved
// for SSE streams: EventSource cannot set custom headers, so these MUST
// accept the token via query. All other endpoints use the Authorization
// header so tokens never leak into access logs / Referer / browser history.
//
// Exported so the regression test can lock the invariant — a missing entry
// here silently 401s the EventSource and the whole push channel goes dark,
// which has happened (see /api/state/stream's first cut).
export const SSE_TOKEN_QUERY_PATHS: ReadonlySet<string> = new Set([
'/api/logs/stream',
'/api/debug/stream',
'/api/state/stream',
]);
const TOKEN_QUERY_ALLOWLIST = SSE_TOKEN_QUERY_PATHS;
// uin = QQ number; 5–10 digits (real QQ UINs fit in uint32, max 4294967295).
// Used to construct config file paths, so we MUST refuse anything else (path
// traversal, NUL bytes, etc.) — and the 10-digit cap also rejects the garbage
@@ -533,6 +526,8 @@ export async function initWebUI(
}
const app = new Hono();
app.use('*', webuiSecurityHeaders);
registerLoginRequestSecurity(app);
const storageManagement = new StorageManagementService({
dataDir: 'data',
getLogStatus: getLogStorageStatus,
@@ -583,10 +578,7 @@ export async function initWebUI(
// nothing sensitive (no layout, no secrets) — see ui-config.ts.
if (reqPath === '/api/login' || reqPath === '/api/ui/public') return next();
const authHeader = c.req.header('Authorization');
const bearerToken = authHeader?.startsWith('Bearer ') ? authHeader.slice(7) : '';
const queryToken = TOKEN_QUERY_ALLOWLIST.has(reqPath) ? (c.req.query('token') ?? '') : '';
const token = bearerToken || queryToken;
const token = extractBearerToken(c.req.raw);
if (!token) return c.json({ status: 'failed', message: 'Unauthorized' }, 401);
const info = sessionTokens.get(token);
@@ -650,12 +642,14 @@ export async function initWebUI(
const token = randomBytes(32).toString('hex');
const mustChange = auth.mustChangePassword();
sessionTokens.set(token, { expiresAt: now + TOKEN_TTL_MS, mustChangePassword: mustChange });
setAvatarSessionCookie(c, token, listener.tlsEnabled === true);
return c.json({ success: true, token, mustChangePassword: mustChange });
});
app.post('/api/logout', (c) => {
const token = c.get('sessionToken' as never) as string | undefined;
if (token) sessionTokens.delete(token);
clearAvatarSessionCookie(c, listener.tlsEnabled === true);
return c.json({ success: true });
});
@@ -709,6 +703,7 @@ export async function initWebUI(
// password was compromised, the attacker may already hold the current
// token; rotating credentials must rotate sessions too.
sessionTokens.clear();
clearAvatarSessionCookie(c, listener.tlsEnabled === true);
log.info('password updated; all sessions invalidated');
return c.json({ success: true, requireRelogin: true });
});
@@ -750,6 +745,12 @@ export async function initWebUI(
// ─── Avatar proxy (uin validated) ────────────────────────────────────────
app.get('/avatar/:uin', async (c) => {
const sessionToken = readAvatarSessionToken(c);
const session = sessionTokens.get(sessionToken);
if (!session || Date.now() > session.expiresAt) {
if (sessionToken) sessionTokens.delete(sessionToken);
return c.text('unauthorized', 401);
}
const uin = c.req.param('uin');
if (!UIN_REGEX.test(uin)) return c.text('invalid uin', 400);
@@ -768,7 +769,8 @@ export async function initWebUI(
return new Response(cached.body, {
headers: {
'Content-Type': cached.contentType,
'Cache-Control': `public, max-age=${AVATAR_BROWSER_CACHE_SECONDS}, immutable`,
'Cache-Control': `private, max-age=${AVATAR_BROWSER_CACHE_SECONDS}, immutable`,
Vary: 'Cookie',
},
});
});
@@ -793,7 +795,13 @@ export async function initWebUI(
});
// ─── Read-only API ───────────────────────────────────────────────────────
app.get('/api/status', (c) => c.json({ status: 'running' }));
app.get('/api/status', (c) => {
// Refresh the path-scoped avatar cookie for a still-valid localStorage
// session (for example after upgrading from a version without the cookie).
const token = c.get('sessionToken' as never) as string;
setAvatarSessionCookie(c, token, listener.tlsEnabled === true);
return c.json({ status: 'running' });
});
// Advisory update check — compares the running build against the latest
// GitHub stable release and links the user to it. Read-only: SnowLuma