fix(webui): tighten auth handling and remove read-side write effects

Stack of WebUI fixes prompted by an internal security/correctness pass:

* getClientIp now resolves the real socket peer via @hono/node-server's
  getConnInfo helper instead of trusting `X-Forwarded-For`. The previous
  behaviour let any client rotate that header to bypass the 5-attempts/
  15-minute login lockout.
* `?token=` is restricted to /api/logs/stream (the only endpoint that
  can't use the Authorization header, because EventSource lacks custom
  headers). Other routes used to leak tokens via access logs / Referer
  / browser history.
* change-password now invalidates every session (including the current
  one) and signals requireRelogin. Keeping the current token alive made
  the rotation pointless if the old credentials were already in an
  attacker's hands.
* GET /api/config/:uin no longer writes defaults to disk and no longer
  doubles up the payload (`{config}` only, dropping the {…config, config}
  legacy shape). Frontend's `data?.config ?? data` fallback still works.
* webui.json is moved to .bak.<ts> instead of being silently overwritten
  when it fails to parse — losing the prior hash silently was rotating
  operator credentials with no warning.
* Process load/unload/refresh routes are deduped into a single helper;
  internal error messages are logged but no longer surfaced verbatim to
  the client.
* SSE log stream tolerates a closed controller (try/catch around enqueue
  + central teardown) and runs an unref'd janitor to GC expired session
  tokens + login attempt counters every minute.
This commit is contained in:
时瑾 authored and DrSmoothl committed 2026-05-16 21:31:28 +08:00
1 parent 2c80e1a267
commit 917f08ce4d
2 files changed
+118 -84

No files matched your search

+21 -2
View File
@@ -1,6 +1,9 @@
import fs from 'fs';
import path from 'path';
import { randomBytes, scryptSync, timingSafeEqual } from 'crypto';
import { createLogger } from '../utils/logger';
const log = createLogger('WebUI.Auth');
const CONFIG_DIR = 'config';
const WEBUI_CONFIG_PATH = path.join(CONFIG_DIR, 'webui.json');
@@ -84,6 +87,16 @@ function generateInitialState(initialPassword: string): WebuiAuthState {
};
}
function backupCorruptConfig(): void {
try {
const dest = `${WEBUI_CONFIG_PATH}.bak.${Date.now()}`;
fs.renameSync(WEBUI_CONFIG_PATH, dest);
log.warn('previous webui.json moved to %s', dest);
} catch (err) {
log.warn('failed to back up corrupt webui.json: %s', err instanceof Error ? err.message : String(err));
}
}
function atomicWrite(state: WebuiAuthState): void {
ensureConfigDir();
const tmp = WEBUI_CONFIG_PATH + '.tmp';
@@ -130,8 +143,14 @@ export class WebuiAuth {
if (isValidState(parsed)) {
return new WebuiAuth(parsed, null, false);
}
} catch {
/* fallthrough — regenerate */
log.error('webui.json schema invalid; backing up and regenerating credentials');
backupCorruptConfig();
} catch (err) {
log.error(
'webui.json is corrupt and will be regenerated; the previous file is backed up: %s',
err instanceof Error ? err.message : String(err),
);
backupCorruptConfig();
}
}
const initialPassword = randomBytes(8).toString('hex');