fix: 生产环境拒绝占位 SESSION_SECRET
This commit is contained in:
1 parent
b4e3e86dcd
commit
4d9c84defc
5 files changed
+73
-2
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { UnauthorizedException } from "@nestjs/common";
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { isPlaceholderSecret } from "./runtime-config.js";
|
||||
|
||||
type Identity = {
|
||||
subject: string;
|
||||
@@ -9,9 +10,20 @@ type Identity = {
|
||||
exp: number;
|
||||
};
|
||||
|
||||
let warnedAboutPlaceholderSecret = false;
|
||||
|
||||
function secret(): string {
|
||||
const value = process.env.SESSION_SECRET;
|
||||
if (!value || value.length < 32) throw new UnauthorizedException("服务端会话密钥未正确配置");
|
||||
if (isPlaceholderSecret(value)) {
|
||||
// 生产环境已在启动时 fail fast;这里保留二次防线,避免绕过 createApp 的调用方签发弱 token。
|
||||
if (process.env.NODE_ENV === "production")
|
||||
throw new UnauthorizedException("服务端会话密钥未正确配置");
|
||||
if (!warnedAboutPlaceholderSecret) {
|
||||
warnedAboutPlaceholderSecret = true;
|
||||
console.warn("[api] SESSION_SECRET 仍是示例占位符,仅可用于本地开发");
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user