feat(webui): custom CSS + config export/import/reset (phase 6)

A "高级" Settings tab adds:
- Custom CSS (appearance.customCss, <=50KB) applied post-auth only, as the
  last <style> so it overrides everything. It never reaches the login page:
  the server strips it from /api/ui/public, applyAppearance gates injection on
  a bearer token (so the localStorage cache can't leak it pre-auth), and the
  login page clears any lingering <style> on mount. `?safe-mode=1` (sticky for
  the tab session) disables custom CSS so a broken rule can't lock the
  operator out.
- Export / import the whole UI config as JSON (import is normalized
  server-side, then reload); reset-all behind a confirm dialog.

POST /api/ui and the import file are size-capped (256KB) on top of the
per-field caps. Verified across 2 adversarial review rounds — the pre-auth
cached-CSS leak/lockout vector found in round 1 is closed across the boot /
logout / 401-expiry / re-login paths.
This commit is contained in:
DrSmoothl committed 2026-06-13 21:17:23 +08:00
1 parent 3258300539
commit 7c2923dd04
7 files changed
+237 -8

No files matched your search

+13 -2
View File
@@ -87,6 +87,13 @@ export interface UiAppearance {
timeFormat: TimeFormat;
/** Dashboard poll interval (ms); 0 = paused. */
pollInterval: number;
/**
* Operator custom CSS, applied AFTER auth only (never on the login page —
* `publicAppearance` strips it). Capped at 50 KB. The client injects it as
* the last <style> and skips it under `?safe-mode=1` so a broken rule can't
* lock the operator out.
*/
customCss: string;
}
export interface UiLayoutItem {
@@ -157,6 +164,7 @@ const DEFAULT_APPEARANCE: UiAppearance = {
sidebarDefaultCollapsed: false,
timeFormat: '24h',
pollInterval: 3000,
customCss: '',
};
const DEFAULT_OVERVIEW_BLOCKS: UiLayoutItem[] = [
@@ -342,6 +350,7 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState
sidebarDefaultCollapsed: boolOr(v.sidebarDefaultCollapsed, DEFAULT_APPEARANCE.sidebarDefaultCollapsed),
timeFormat: oneOf<TimeFormat>(v.timeFormat, ['12h', '24h'], DEFAULT_APPEARANCE.timeFormat),
pollInterval: clampNum(v.pollInterval, 0, 60_000, DEFAULT_APPEARANCE.pollInterval),
customCss: typeof v.customCss === 'string' ? v.customCss.slice(0, 50_000) : DEFAULT_APPEARANCE.customCss,
};
}
@@ -443,9 +452,11 @@ export function saveUiConfig(incoming: unknown): UiConfig {
return next;
}
/** The appearance-only subset served unauthenticated to the login page. */
/** The appearance subset served unauthenticated to the login page. Strips
* `customCss` so a broken/hostile rule can never reach the pre-auth page
* (and so the operator can always log in to fix it). */
export function publicAppearance(): UiAppearance {
return loadUiConfig().appearance;
return { ...loadUiConfig().appearance, customCss: '' };
}
// ─── Background image lifecycle ─────────────────────────────────────────────