From a4b226f1d0fd7cc9356e0bc4e68b7b5d85385e39 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 14:43:27 +0800 Subject: [PATCH] fix(webui): write key.pem with 0600 (was world-readable) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adversarial Wave A1 pass (MAJOR): the TLS private key was written with the default umask mode (typically 0644 — world-readable). Write config/key.pem with mode 0600 + an explicit chmodSync (writeFileSync's mode is ignored for an existing file), mirroring auth.ts's webui.json handling. cert.pem stays public. --- packages/core/src/webui/server.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 5034e344..5cd7582a 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -7,7 +7,7 @@ import type { OneBotManager } from '@snowluma/onebot/manager'; import type { OneBotConfig } from '@snowluma/onebot/types'; import { readRuntimeConfig, updateRuntimeConfig, resolveRuntimeEnvOverrides } from '@snowluma/common/runtime'; import { randomBytes } from 'crypto'; -import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'; +import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'; import { createServer as createHttpsServer } from 'https'; import { Hono, type Context } from 'hono'; import os from 'os'; @@ -472,7 +472,11 @@ export async function initWebUI( try { mkdirSync('config', { recursive: true }); writeFileSync(SYSTEM_CERT_PATH, cert.endsWith('\n') ? cert : cert + '\n', 'utf8'); - writeFileSync(SYSTEM_KEY_PATH, key.endsWith('\n') ? key : key + '\n', 'utf8'); + // Private key must not be world-readable (mirrors auth.ts's webui.json + // 0600). writeFileSync's mode is ignored for an existing file, so chmod + // explicitly afterwards. + writeFileSync(SYSTEM_KEY_PATH, key.endsWith('\n') ? key : key + '\n', { encoding: 'utf8', mode: 0o600 }); + chmodSync(SYSTEM_KEY_PATH, 0o600); } catch (err) { log.warn('write cert/key failed: %s', err instanceof Error ? err.message : String(err)); return c.json({ success: false, message: '写入证书失败,请检查服务器日志' }, 500);