feat(core): validate backup restores before mutation

Route selected backup entries through owner-owned config parsers before any filesystem write. Reject unsupported paths, mismatched encodings, malformed base64, lossy normalization, invalid effective overlays, and decoded payloads larger than 32 MiB.

Treat WebUI state, private keys, and every OneBot config as a single sensitive group. Skip unselected sensitive entries without inspecting their contents, preserve recognized migrations, and assign restrictive modes to credential files.
This commit is contained in:
DrSmoothl committed 2026-07-15 16:11:44 +08:00
1 parent 6a278721b2
commit 2fea3ca4d9
5 files changed
+1442 -86

No files matched your search

+45 -9
View File
@@ -64,16 +64,52 @@ function ensureConfigDir(): void {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
function isValidState(value: unknown): value is WebuiAuthState {
if (!value || typeof value !== 'object') return false;
/**
* Validate credential state before it crosses a persistence boundary.
*
* The hash/salt lengths are part of the on-disk contract: accepting any hex
* string here only postpones failure until password verification, where the
* operator would be locked out after a restore.
*/
export function prepareWebuiAuthStateForRestore(value: unknown): WebuiAuthState {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
throw new Error('credential state must be an object');
}
const v = value as Record<string, unknown>;
return (
typeof v.passwordHash === 'string' &&
typeof v.passwordSalt === 'string' &&
typeof v.mustChangePassword === 'boolean' &&
/^[0-9a-f]+$/i.test(v.passwordHash) &&
/^[0-9a-f]+$/i.test(v.passwordSalt)
);
if (typeof v.passwordHash !== 'string' || !/^[0-9a-f]{128}$/i.test(v.passwordHash)) {
throw new Error('passwordHash must be exactly 128 hexadecimal characters');
}
if (typeof v.passwordSalt !== 'string' || !/^[0-9a-f]{32}$/i.test(v.passwordSalt)) {
throw new Error('passwordSalt must be exactly 32 hexadecimal characters');
}
if (typeof v.mustChangePassword !== 'boolean') {
throw new Error('mustChangePassword must be a boolean');
}
if (typeof v.generatedAt !== 'string' || !Number.isFinite(Date.parse(v.generatedAt))) {
throw new Error('generatedAt must be a valid timestamp');
}
if (typeof v.updatedAt !== 'string' || !Number.isFinite(Date.parse(v.updatedAt))) {
throw new Error('updatedAt must be a valid timestamp');
}
const allowed = new Set(['passwordHash', 'passwordSalt', 'mustChangePassword', 'generatedAt', 'updatedAt']);
const unknown = Object.keys(v).find((key) => !allowed.has(key));
if (unknown) throw new Error(`unknown field $.${unknown}`);
return {
passwordHash: v.passwordHash,
passwordSalt: v.passwordSalt,
mustChangePassword: v.mustChangePassword,
generatedAt: v.generatedAt,
updatedAt: v.updatedAt,
};
}
function isValidState(value: unknown): value is WebuiAuthState {
try {
prepareWebuiAuthStateForRestore(value);
return true;
} catch {
return false;
}
}
function generateInitialState(initialPassword: string): WebuiAuthState {
+452 -11
View File
@@ -9,11 +9,23 @@
// OneBot access token, so a no-credentials backup must NOT include them.
// cert.pem is public so it always travels.
//
// Pure functions here (read/list/decode); the fs glue (atomic two-phase write,
// snapshot) lives in server.ts. Restore is restart-to-apply, like A1.
// Pure functions here own bundle validation and semantic preflight. Filesystem
// transaction choreography lives in restore.ts. A non-empty restore remains
// restart-to-apply, like A1.
import { normalizeRuntimeConfig } from '@snowluma/common/runtime';
import { isRealUin } from '@snowluma/common/uin';
import { normalizeGlobalSettings } from '@snowluma/onebot/global-config';
import { prepareOneBotConfigForRestore } from '@snowluma/onebot/config';
import { isIP } from 'node:net';
import { normalizeNotificationsConfig } from '../notifications/config';
import { prepareWebuiAuthStateForRestore } from './auth';
import { MAX_BACKGROUND_BYTES, normalizeStoredUiConfig, sniffImageMime } from './ui-config';
import { validateTlsPair } from './tls';
export const BACKUP_VERSION = 1;
export const BACKUP_APP = 'snowluma';
export const MAX_BACKUP_DECODED_BYTES = 32 * 1024 * 1024;
export interface BackupFileSpec {
/** Path relative to the config dir; also the bundle key. */
@@ -40,13 +52,18 @@ export const BACKUP_FILES: readonly BackupFileSpec[] = [
];
const SPEC_BY_NAME = new Map(BACKUP_FILES.map((f) => [f.name, f]));
const PER_UIN_ONEBOT = /^onebot_\d+\.json$/;
const PER_UIN_ONEBOT = /^onebot_(\d+)\.json$/;
function isPerUinOneBotName(name: string): boolean {
const match = PER_UIN_ONEBOT.exec(name);
return match !== null && isRealUin(match[1]);
}
/** Resolve a file name (static or per-uin onebot pattern) to its spec, or null. */
export function specFor(name: string): BackupFileSpec | null {
const s = SPEC_BY_NAME.get(name);
if (s) return s;
if (PER_UIN_ONEBOT.test(name)) return { name, binary: false, credential: true };
if (isPerUinOneBotName(name)) return { name, binary: false, credential: true };
return null;
}
@@ -71,7 +88,7 @@ export function buildBackup(
const files: Record<string, BackupEntry> = {};
const all: BackupFileSpec[] = [
...BACKUP_FILES,
...perUinOnebotNames.filter((n) => PER_UIN_ONEBOT.test(n)).map((n) => ({ name: n, binary: false, credential: true })),
...perUinOnebotNames.filter(isPerUinOneBotName).map((n) => ({ name: n, binary: false, credential: true })),
];
for (const spec of all) {
if (spec.credential && !opts.includeCredentials) continue;
@@ -86,6 +103,13 @@ export function buildBackup(
/** Validate a parsed bundle wholesale — any defect rejects the whole import. */
export function validateBackup(parsed: unknown): { ok: true; backup: Backup } | { ok: false; error: string } {
return validateBackupEntries(parsed, () => false);
}
function validateBackupEntries(
parsed: unknown,
skipEntryValidation: (spec: BackupFileSpec) => boolean,
): { ok: true; backup: Backup } | { ok: false; error: string } {
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
return { ok: false, error: 'backup must be an object' };
}
@@ -97,7 +121,9 @@ export function validateBackup(parsed: unknown): { ok: true; backup: Backup } |
}
const files = b.files as Record<string, unknown>;
for (const [name, entry] of Object.entries(files)) {
if (!specFor(name)) return { ok: false, error: `unknown file in backup: ${name}` };
const spec = specFor(name);
if (!spec) return { ok: false, error: `unknown file in backup: ${name}` };
if (skipEntryValidation(spec)) continue;
if (typeof entry !== 'object' || entry === null) return { ok: false, error: `malformed entry: ${name}` };
const e = entry as Record<string, unknown>;
if (e.encoding !== 'utf8' && e.encoding !== 'base64') return { ok: false, error: `bad encoding for ${name}` };
@@ -107,10 +133,8 @@ export function validateBackup(parsed: unknown): { ok: true; backup: Backup } |
}
/**
* Decide what a (validated) bundle would restore: decode each non-skipped file
* to bytes. Pure — the caller (server.ts) does the atomic two-phase fs write so
* a mid-restore failure can't leave a half-applied live config. Credential
* files are skipped unless `restoreCredentials`.
* Decode a validated bundle for the legacy HTTP restore path. The production
* route switches to restoreBackup once the transaction module is introduced.
*/
export function planRestore(
backup: Backup,
@@ -120,9 +144,426 @@ export function planRestore(
const skipped: string[] = [];
for (const [name, entry] of Object.entries(backup.files)) {
const spec = specFor(name);
if (!spec) { skipped.push(name); continue; } // validated, but be defensive
if (!spec) { skipped.push(name); continue; }
if (spec.credential && !opts.restoreCredentials) { skipped.push(name); continue; }
restore.push({ name, data: Buffer.from(entry.data, entry.encoding) });
}
return { restore, skipped };
}
export interface PreparedRestoreFile {
name: string;
data: Buffer;
mode: 0o600 | 0o644;
}
export interface RestoreMigration {
name: string;
fields: string[];
}
export interface PreparedRestorePlan {
restore: PreparedRestoreFile[];
skipped: string[];
migrated: RestoreMigration[];
}
export interface PrepareRestoreOptions {
restoreCredentials: boolean;
/** Read an existing live file for overlay-wide validation (TLS/UI). */
readCurrent: (name: string) => Buffer | null;
/** Current per-account configs affected when onebot.json is replaced. */
listCurrentOneBotNames?: () => string[];
}
export class RestorePreflightError extends Error {
readonly file: string;
constructor(file: string, message: string, options?: { cause?: unknown }) {
super(`${file}: ${message}`, options);
this.name = 'RestorePreflightError';
this.file = file;
}
}
/**
* Validate every selected file semantically and materialize canonical bytes
* before the transaction is allowed to touch live configuration.
*/
export function prepareRestorePlan(parsed: unknown, opts: PrepareRestoreOptions): PreparedRestorePlan {
const checked = validateBackupEntries(parsed, (spec) => spec.credential && !opts.restoreCredentials);
if (!checked.ok) throw new RestorePreflightError('<backup>', checked.error);
const restore: PreparedRestoreFile[] = [];
const skipped: string[] = [];
const migrated: RestoreMigration[] = [];
const selected: Array<{ name: string; data: Buffer; spec: BackupFileSpec }> = [];
let decodedBytes = 0;
for (const [name, entry] of Object.entries(checked.backup.files)) {
const spec = specFor(name);
if (!spec) throw new RestorePreflightError(name, 'file is not in the restore allowlist');
if (spec.credential && !opts.restoreCredentials) {
skipped.push(name);
continue;
}
if (entry.encoding !== (spec.binary ? 'base64' : 'utf8')) {
throw new RestorePreflightError(name, `encoding must be ${spec.binary ? 'base64' : 'utf8'}`);
}
const decoded = decodeEntryStrict(name, entry);
decodedBytes += decoded.length;
if (decodedBytes > MAX_BACKUP_DECODED_BYTES) {
throw new RestorePreflightError(name, `selected files exceed ${String(MAX_BACKUP_DECODED_BYTES)} decoded bytes`);
}
selected.push({ name, data: decoded, spec });
}
const selectedGlobal = selected.find((file) => file.name === 'onebot.json');
const selectedPerUin = selected.filter((file) => isPerUinOneBotName(file.name));
const selectedPerUinRaw = new Map(selectedPerUin.map((file) => [file.name, parseJson(file.name, file.data)]));
const perUinNeedsGlobal = [...selectedPerUinRaw.values()].some((raw) => !isPlainObject(raw) || raw.mode !== 'snapshot');
let effectiveGlobalOneBot: unknown;
let preparedGlobalOneBot: ReturnType<typeof prepareOneBotConfigForRestore> | null = null;
if (selectedGlobal) {
if (!opts.listCurrentOneBotNames) {
throw new RestorePreflightError('onebot.json', 'current per-account config listing is required to validate the effective overlay');
}
const raw = parseJson(selectedGlobal.name, selectedGlobal.data);
preparedGlobalOneBot = prepareOneBotOrThrow(selectedGlobal.name, raw, 'global');
effectiveGlobalOneBot = preparedGlobalOneBot.value;
} else if (perUinNeedsGlobal) {
const current = readCurrentForPreflight(opts, 'onebot.json');
if (current) {
const raw = parseJson('onebot.json', current);
effectiveGlobalOneBot = prepareOneBotOrThrow('onebot.json', raw, 'global').value;
}
}
for (const { name, data, spec } of selected) {
let prepared: { data: Buffer; migratedFields: string[] };
if (name === 'onebot.json') {
const global = preparedGlobalOneBot!;
prepared = { data: serializeJson(global.value), migratedFields: global.migratedFields };
} else if (isPerUinOneBotName(name)) {
const raw = selectedPerUinRaw.get(name);
const onebot = prepareOneBotOrThrow(name, raw, 'per-uin', effectiveGlobalOneBot);
prepared = { data: serializeJson(onebot.value), migratedFields: onebot.migratedFields };
} else {
prepared = prepareFile(name, data);
}
restore.push({ name, data: prepared.data, mode: spec.credential ? 0o600 : 0o644 });
if (prepared.migratedFields.length > 0) {
migrated.push({ name, fields: [...prepared.migratedFields].sort() });
}
}
if (selectedGlobal) {
const selectedNames = new Set(selected.map((file) => file.name));
let currentNames: string[];
try {
currentNames = opts.listCurrentOneBotNames!();
} catch (error) {
throw new RestorePreflightError('onebot.json', `failed to list current per-account configs: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
}
for (const name of currentNames) {
if (!isPerUinOneBotName(name) || selectedNames.has(name)) continue;
const current = readCurrentForPreflight(opts, name);
if (!current) continue;
const raw = parseJson(name, current);
prepareOneBotOrThrow(name, raw, 'per-uin', effectiveGlobalOneBot);
}
}
validateEffectiveTls(restore, opts);
validateEffectiveUi(restore, opts);
return { restore, skipped, migrated };
}
function decodeEntryStrict(name: string, entry: BackupEntry): Buffer {
if (entry.encoding === 'utf8') return Buffer.from(entry.data, 'utf8');
if (!isCanonicalBase64(entry.data)) {
throw new RestorePreflightError(name, 'data is not canonical base64');
}
return Buffer.from(entry.data, 'base64');
}
function isCanonicalBase64(value: string): boolean {
if (value.length === 0) return true;
if (value.length % 4 !== 0 || !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)) {
return false;
}
return Buffer.from(value, 'base64').toString('base64') === value;
}
function prepareFile(name: string, data: Buffer): { data: Buffer; migratedFields: string[] } {
try {
switch (name) {
case 'runtime.json':
return prepareRuntimeJson(name, data);
case 'ui.json':
return prepareNormalizedJson(name, data, normalizeStoredUiConfig);
case 'notifications.json':
return prepareNormalizedJson(name, data, normalizeNotificationsConfig);
case 'snowluma.json':
return prepareGlobalSettingsJson(name, data);
case 'webui.json': {
const value = parseJson(name, data);
const canonical = prepareWebuiAuthStateForRestore(value);
return { data: serializeJson(canonical), migratedFields: [] };
}
case 'ui-assets/background':
if (data.length > MAX_BACKGROUND_BYTES) {
throw new Error(`image exceeds ${String(MAX_BACKGROUND_BYTES)} bytes`);
}
if (!sniffImageMime(data)) throw new Error('image must be PNG, JPEG, or WebP');
return { data, migratedFields: [] };
case 'cert.pem':
case 'key.pem':
if (!data.toString('utf8').trim()) throw new Error('PEM file must not be empty');
return { data, migratedFields: [] };
default:
throw new Error('file is not in the restore allowlist');
}
} catch (error) {
if (error instanceof RestorePreflightError) throw error;
throw new RestorePreflightError(name, error instanceof Error ? error.message : String(error), { cause: error });
}
}
function prepareOneBotOrThrow(
name: string,
value: unknown,
scope: 'global' | 'per-uin',
inheritedGlobal?: unknown,
): ReturnType<typeof prepareOneBotConfigForRestore> {
try {
return prepareOneBotConfigForRestore(value, scope, inheritedGlobal);
} catch (error) {
throw new RestorePreflightError(name, error instanceof Error ? error.message : String(error), { cause: error });
}
}
function readCurrentForPreflight(opts: PrepareRestoreOptions, name: string): Buffer | null {
try {
return opts.readCurrent(name);
} catch (error) {
throw new RestorePreflightError(name, `failed to read current file: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
}
}
function parseJson(name: string, data: Buffer): unknown {
try {
return JSON.parse(data.toString('utf8')) as unknown;
} catch (error) {
// V8 may quote the offending input in SyntaxError.message. Some restored
// JSON files contain credentials, so expose only a fixed diagnostic while
// retaining the original exception as the in-process cause.
throw new RestorePreflightError(name, 'invalid JSON', { cause: error });
}
}
function prepareNormalizedJson<T>(name: string, data: Buffer, normalize: (value: unknown) => T): {
data: Buffer;
migratedFields: string[];
} {
const raw = parseJson(name, data);
const canonical = normalize(raw);
const migratedFields: string[] = [];
assertNoLossyNormalization(name, raw, canonical, '$', migratedFields);
return { data: serializeJson(canonical), migratedFields };
}
function prepareRuntimeJson(name: string, data: Buffer): {
data: Buffer;
migratedFields: string[];
} {
const prepared = prepareNormalizedJson(name, data, normalizeRuntimeConfig);
const runtime = JSON.parse(prepared.data.toString('utf8')) as ReturnType<typeof normalizeRuntimeConfig>;
if (!runtime.webuiHost || !isValidBindHost(runtime.webuiHost)) {
throw new RestorePreflightError(name, '$.webuiHost is not a valid TCP bind host');
}
return prepared;
}
function prepareGlobalSettingsJson(name: string, data: Buffer): {
data: Buffer;
migratedFields: string[];
} {
const raw = parseJson(name, data);
const normalized = normalizeGlobalSettings(raw);
const canonical: Record<string, unknown> = { rkey: normalized.rkey };
// Missing is the existing one-shot migration sentinel: startup scans legacy
// onebot*.json only until snowluma.json first carries this key.
if (isPlainObject(raw) && Object.prototype.hasOwnProperty.call(raw, 'musicSignUrl')) {
canonical.musicSignUrl = normalized.musicSignUrl;
}
const migratedFields: string[] = [];
assertNoLossyNormalization(name, raw, canonical, '$', migratedFields);
return { data: serializeJson(canonical), migratedFields };
}
function assertNoLossyNormalization(
name: string,
raw: unknown,
canonical: unknown,
at: string,
additions: string[],
): void {
if (isPlainObject(raw)) {
if (!isPlainObject(canonical)) throw new RestorePreflightError(name, `${at} has an invalid type`);
for (const [key, rawValue] of Object.entries(raw)) {
const child = `${at}.${key}`;
if (!Object.prototype.hasOwnProperty.call(canonical, key)) {
throw new RestorePreflightError(name, `${child} is unsupported or would be discarded`);
}
assertNoLossyNormalization(name, rawValue, canonical[key], child, additions);
}
for (const key of Object.keys(canonical)) {
if (!Object.prototype.hasOwnProperty.call(raw, key)) additions.push(`${at}.${key}`);
}
return;
}
if (Array.isArray(raw)) {
if (!Array.isArray(canonical) || raw.length !== canonical.length) {
throw new RestorePreflightError(name, `${at} contains an invalid, duplicate, or discarded item`);
}
raw.forEach((value, index) => {
assertNoLossyNormalization(name, value, canonical[index], `${at}[${String(index)}]`, additions);
});
return;
}
if (!Object.is(raw, canonical)) {
if (isKnownLosslessScalarCoercion(name, at, raw, canonical)) {
additions.push(at);
return;
}
throw new RestorePreflightError(name, `${at} is invalid or would be changed`);
}
}
function isKnownLosslessScalarCoercion(name: string, at: string, raw: unknown, canonical: unknown): boolean {
if (typeof raw === 'string' && typeof canonical === 'string' && raw.trim() === canonical) {
return true;
}
if (typeof raw === 'string' && typeof canonical === 'number') {
const parsed = Number(raw.trim());
return raw.trim().length > 0 && Number.isFinite(parsed) && Object.is(parsed, canonical);
}
if (
name === 'runtime.json' &&
(at === '$.hookAutoLoad' || at === '$.webuiTls.enabled') &&
typeof canonical === 'boolean'
) {
if (raw === 1 || raw === 0) return canonical === (raw === 1);
if (typeof raw !== 'string') return false;
const token = raw.trim().toLowerCase();
const truthy = new Set(['true', '1', 'yes', 'on']);
const falsy = new Set(['false', '0', 'no', 'off', '']);
return (canonical && truthy.has(token)) || (!canonical && falsy.has(token));
}
return false;
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function serializeJson(value: unknown): Buffer {
return Buffer.from(JSON.stringify(value, null, 2), 'utf8');
}
function isValidBindHost(value: string): boolean {
const host = value.trim();
if (!host || host !== value || host.length > 253 || /[\s/?#@]/u.test(host)) return false;
if (host.includes(':')) return isIP(host) === 6;
if (/^[\d.]+$/.test(host)) {
return isIP(host) === 4;
}
const normalized = host.endsWith('.') ? host.slice(0, -1) : host;
return normalized.split('.').every((label) => /^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$/.test(label));
}
function validateEffectiveTls(restore: PreparedRestoreFile[], opts: PrepareRestoreOptions): void {
const byName = new Map(restore.map((file) => [file.name, file.data]));
const pairTouched = byName.has('cert.pem') || byName.has('key.pem');
const tlsRelated = pairTouched || byName.has('runtime.json');
if (!tlsRelated) return;
const readEffective = (name: string): Buffer | null => {
const prepared = byName.get(name);
if (prepared) return prepared;
try {
return opts.readCurrent(name);
} catch (error) {
throw new RestorePreflightError(name, `failed to read current file: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
}
};
const runtime = readEffective('runtime.json');
let tlsEnabled = false;
if (runtime) {
try {
tlsEnabled = normalizeRuntimeConfig(JSON.parse(runtime.toString('utf8')) as unknown).webuiTls?.enabled === true;
} catch (error) {
throw new RestorePreflightError('runtime.json', 'effective runtime config is invalid JSON', { cause: error });
}
}
if (!tlsEnabled && !pairTouched) return;
const cert = readEffective('cert.pem');
const key = readEffective('key.pem');
if (cert && key) {
const valid = validateTlsPair(cert, key);
if (!valid.ok) throw new RestorePreflightError('cert.pem + key.pem', valid.reason ?? 'TLS pair is invalid');
return;
}
if (tlsEnabled) {
throw new RestorePreflightError('cert.pem + key.pem', 'TLS is enabled but the effective certificate/private-key pair is incomplete');
}
}
function validateEffectiveUi(restore: PreparedRestoreFile[], opts: PrepareRestoreOptions): void {
const byName = new Map(restore.map((file) => [file.name, file.data]));
if (!byName.has('ui.json') && !byName.has('ui-assets/background')) return;
const readEffective = (name: string): Buffer | null => {
const prepared = byName.get(name);
if (prepared) return prepared;
try {
return opts.readCurrent(name);
} catch (error) {
throw new RestorePreflightError(name, `failed to read current file: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
}
};
const uiBytes = readEffective('ui.json');
if (!uiBytes) return;
let ui;
try {
ui = normalizeStoredUiConfig(JSON.parse(uiBytes.toString('utf8')) as unknown);
} catch (error) {
throw new RestorePreflightError('ui.json', 'effective UI config is invalid JSON', { cause: error });
}
const background = ui.appearance.background;
if (!background.hasImage) return;
const image = readEffective('ui-assets/background');
if (!image) {
throw new RestorePreflightError('ui.json + ui-assets/background', 'background metadata says an image exists, but the effective image is missing');
}
if (image.length > MAX_BACKGROUND_BYTES) {
throw new RestorePreflightError('ui.json + ui-assets/background', `effective background exceeds ${String(MAX_BACKGROUND_BYTES)} bytes`);
}
const mime = sniffImageMime(image);
if (!mime) {
throw new RestorePreflightError('ui.json + ui-assets/background', 'effective background is not PNG, JPEG, or WebP');
}
if (mime !== background.imageMime) {
throw new RestorePreflightError(
'ui.json + ui-assets/background',
`background metadata MIME ${background.imageMime} does not match image bytes ${mime}`,
);
}
}
+6 -1
View File
@@ -474,6 +474,11 @@ function imageStateFromParsed(parsed: unknown): ServerImageState {
return sanitizeImageState(parsed.appearance.background);
}
/** Normalize a complete ui.json read from disk or a backup bundle. */
export function normalizeStoredUiConfig(value: unknown): UiConfig {
return normalizeUiConfig(value, imageStateFromParsed(value));
}
function normalizeBackground(value: unknown, imageState: ServerImageState): UiBackground {
const v = isObject(value) ? value : {};
let type = oneOf<BackgroundType>(v.type, ['none', 'solid', 'gradient', 'image'], DEFAULT_BACKGROUND.type);
@@ -615,7 +620,7 @@ export function loadUiConfig(): UiConfig {
const parsed = JSON.parse(raw) as unknown;
// On load, the file itself is the source of truth for the server-managed
// image fields — so they survive restarts (don't reset to defaults).
const normalized = normalizeUiConfig(parsed, imageStateFromParsed(parsed));
const normalized = normalizeStoredUiConfig(parsed);
// Re-persist only if normalization changed something (corrupt/old file),
// so we self-heal on disk without rewriting on every boot.
if (JSON.stringify(parsed) !== JSON.stringify(normalized)) {