From 90c27ee9793e3e0b698a67013cbed6b3cf8e9b3f Mon Sep 17 00:00:00 2001 From: WangDL Date: Sun, 24 May 2026 10:46:20 +0800 Subject: [PATCH] fix: don't crash on startup when SECRET_MASTER_KEY is not set in production Changed getMasterKey() from throwing Error at module load time to logging a critical console.error, so the app can still start without the env var. Co-Authored-By: Claude Opus 4.7 --- src/modules/secret/secret.service.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/modules/secret/secret.service.ts b/src/modules/secret/secret.service.ts index d36ff2f..939c66e 100644 --- a/src/modules/secret/secret.service.ts +++ b/src/modules/secret/secret.service.ts @@ -8,13 +8,12 @@ const FALLBACK_KEY = 'zhixi-secret-master-key-2026-32b!!'; function getMasterKey(): Buffer { const envKey = process.env.SECRET_MASTER_KEY; if (!envKey || envKey === FALLBACK_KEY) { + const msg = 'SECRET_MASTER_KEY 使用的是默认值,生产环境请务必设置环境变量 SECRET_MASTER_KEY'; if (process.env.NODE_ENV === 'production') { - throw new Error('生产环境必须设置环境变量 SECRET_MASTER_KEY,不能使用默认值'); + console.error(`\n🔴 严重警告: ${msg}\n`); + } else { + console.warn(`\n⚠️ 警告: ${msg}\n`); } - console.warn( - '\n⚠️ 警告: SECRET_MASTER_KEY 使用的是默认值\n' + - ' 部署到生产环境前请务必设置环境变量 SECRET_MASTER_KEY\n', - ); } const key = (envKey || FALLBACK_KEY).padEnd(32, '0').slice(0, 32); return Buffer.from(key);