From 19a564991c39220fecf5e4beb0a05413b6391c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Thu, 7 May 2026 14:36:12 +0800 Subject: [PATCH] Add WebUI dev mode auth & dev script Introduce a development-only WebUI auth mode controlled by SNOWLUMA_DEV_MODE. When enabled, a fixed in-memory password ("snowluma-dev") is used, config/webui.json is not read or written, and password changes are disabled. Expose WebuiAuth.isDevMode() and WebuiAuth.devPassword, add startup logs when dev mode is active, and wire up dev:mode npm scripts (root and packages/core) using cross-env to set the env flags. --- package.json | 2 ++ packages/core/src/webui/auth.ts | 45 ++++++++++++++++++++++++++++--- packages/core/src/webui/server.ts | 9 ++++++- pnpm-lock.yaml | 3 +++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 4a53d38f..42708386 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "build:webui": "pnpm --filter webui build", "build:all": "pnpm --filter @snowluma/core build:all && pnpm --filter webui build", "dev": "pnpm --filter @snowluma/core dev", + "dev:mode": "cd dist && cross-env SNOWLUMA_DEV_MODE=1 SNOWLUMA_DEV_WEBUI=1 node index.mjs", "dev:web": "pnpm --filter webui dev", "test": "pnpm --filter @snowluma/core test", "test:watch": "pnpm --filter @snowluma/core test:watch", @@ -17,6 +18,7 @@ "start": "node dist/index.mjs" }, "devDependencies": { + "cross-env": "^10.1.0", "typescript": "catalog:" }, "pnpm": { diff --git a/packages/core/src/webui/auth.ts b/packages/core/src/webui/auth.ts index a0d0770f..099ee3d4 100644 --- a/packages/core/src/webui/auth.ts +++ b/packages/core/src/webui/auth.ts @@ -10,6 +10,17 @@ const SCRYPT_N = 16384; // cost const SCRYPT_R = 8; const SCRYPT_P = 1; +/** + * Fixed credentials used when SNOWLUMA_DEV_MODE=1 is set. The state is kept + * entirely in memory and never written to `config/webui.json`, so toggling + * dev mode on/off never disturbs the real persisted password. + */ +const DEV_PASSWORD = 'snowluma-dev'; + +export function isDevAuthMode(): boolean { + return process.env.SNOWLUMA_DEV_MODE === '1'; +} + export interface WebuiAuthState { passwordHash: string; // hex passwordSalt: string; // hex @@ -89,20 +100,35 @@ function atomicWrite(state: WebuiAuthState): void { export class WebuiAuth { private state: WebuiAuthState; private initialPlain: string | null; + private readonly devMode: boolean; - private constructor(state: WebuiAuthState, initialPlain: string | null) { + private constructor(state: WebuiAuthState, initialPlain: string | null, devMode: boolean) { this.state = state; this.initialPlain = initialPlain; + this.devMode = devMode; } static load(): WebuiAuth { + if (isDevAuthMode()) { + const salt = randomBytes(16); + const hash = hashPassword(DEV_PASSWORD, salt); + const now = new Date().toISOString(); + const state: WebuiAuthState = { + passwordHash: hash.toString('hex'), + passwordSalt: salt.toString('hex'), + mustChangePassword: false, + generatedAt: now, + updatedAt: now, + }; + return new WebuiAuth(state, null, true); + } ensureConfigDir(); if (fs.existsSync(WEBUI_CONFIG_PATH)) { try { const raw = fs.readFileSync(WEBUI_CONFIG_PATH, 'utf8'); const parsed = JSON.parse(raw) as unknown; if (isValidState(parsed)) { - return new WebuiAuth(parsed, null); + return new WebuiAuth(parsed, null, false); } } catch { /* fallthrough — regenerate */ @@ -111,7 +137,17 @@ export class WebuiAuth { const initialPassword = randomBytes(8).toString('hex'); const state = generateInitialState(initialPassword); atomicWrite(state); - return new WebuiAuth(state, initialPassword); + return new WebuiAuth(state, initialPassword, false); + } + + /** True when SNOWLUMA_DEV_MODE was active at load time. */ + isDevMode(): boolean { + return this.devMode; + } + + /** Fixed dev password (only meaningful when {@link isDevMode} is true). */ + static get devPassword(): string { + return DEV_PASSWORD; } /** Returns the auto-generated initial password if this is a fresh install, else null. */ @@ -143,6 +179,9 @@ export class WebuiAuth { * After success, mustChangePassword=false and the file is rewritten on disk. */ setPassword(newPassword: string): void { + if (this.devMode) { + throw new Error('开发模式 (SNOWLUMA_DEV_MODE=1) 已禁用密码修改'); + } if (!isStrongPassword(newPassword)) { throw new Error('密码不符合强度要求'); } diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 9da489c2..d25450af 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -77,7 +77,14 @@ export async function initWebUI( ): Promise<{ port: number }> { const auth = WebuiAuth.load(); const initialPassword = auth.takeInitialPassword(); - if (initialPassword) { + if (auth.isDevMode()) { + log.warn('========================================='); + log.warn('WebUI 已进入开发模式 (SNOWLUMA_DEV_MODE=1)'); + log.warn('固定密码: %s', WebuiAuth.devPassword); + log.warn('config/webui.json 不会被读写,强制改密流程已跳过'); + log.warn('请勿在生产环境启用此模式'); + log.warn('========================================='); + } else if (initialPassword) { log.info('========================================='); log.info('WebUI 安全认证(首次启动,已生成临时密码)'); log.info('默认用户: admin'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66a82c78..daf6cd1e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: .: devDependencies: + cross-env: + specifier: ^10.1.0 + version: 10.1.0 typescript: specifier: 'catalog:' version: 6.0.2