fix: 生产环境拒绝占位 SESSION_SECRET
This commit is contained in:
1 parent
b4e3e86dcd
commit
4d9c84defc
5 files changed
+73
-2
No files matched your search
@@ -0,0 +1,20 @@
|
||||
const PLACEHOLDER_PATTERN = /(replace-with|change-me|changeme|^your[-_ ]|example|placeholder)/i;
|
||||
|
||||
/** 判断值是否仍是仓库示例中的占位符,而不是真实随机密钥。 */
|
||||
export function isPlaceholderSecret(value: string): boolean {
|
||||
return PLACEHOLDER_PATTERN.test(value.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生产环境启动前的快速失败校验。
|
||||
* 本地开发允许占位符,避免示例 `.env` 阻塞调试;生产环境必须使用真实随机值。
|
||||
*/
|
||||
export function assertProductionSecret(
|
||||
value: string | undefined,
|
||||
nodeEnv: string | undefined = process.env.NODE_ENV,
|
||||
): void {
|
||||
if (nodeEnv !== "production") return;
|
||||
const secret = value?.trim() ?? "";
|
||||
if (secret.length < 32) throw new Error("SESSION_SECRET 必须是至少 32 位的随机值");
|
||||
if (isPlaceholderSecret(secret)) throw new Error("SESSION_SECRET 不能使用示例占位符");
|
||||
}
|
||||
Reference in new issue
Block a user