fix: 生产环境拒绝占位 SESSION_SECRET

This commit is contained in:
que01 committed 2026-10-23 12:26:45 +08:00
1 parent b4e3e86dcd
commit 4d9c84defc
5 files changed
+73 -2

No files matched your search

@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
"generatedAt": "2026-10-23T04:26:41.081Z",
"generatedAt": "2026-10-23T04:26:48.766Z",
"templates": {
"acheng-campus-food": {
"real-manifest-interaction": {
@@ -256,5 +256,5 @@
}
}
},
"sourceTreeHash": "753ccedf803e63f14d8eaa3e8abcac074e274f3aed2f4f2facc3a66f8022af24"
"sourceTreeHash": "4a4f5161e6da114837458987d9574d5d09f0d89f1d53e88cd838fc9b31946c9b"
}
+12
View File
@@ -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;
}
+2
View File
@@ -6,8 +6,10 @@ import multipart from "@fastify/multipart";
import fastifyStatic from "@fastify/static";
import path from "node:path";
import { AppModule, configureApp } from "./app.module.js";
import { assertProductionSecret } from "./runtime-config.js";
export async function createApp(): Promise<NestFastifyApplication> {
assertProductionSecret(process.env.SESSION_SECRET);
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
app
.getHttpAdapter()
+20
View File
@@ -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 不能使用示例占位符");
}
+37
View File
@@ -0,0 +1,37 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { assertProductionSecret, isPlaceholderSecret } from "../src/runtime-config.js";
test("production rejects missing or short session secrets", () => {
assert.throws(() => assertProductionSecret(undefined, "production"), /至少 32 位/);
assert.throws(() => assertProductionSecret("short-secret", "production"), /至少 32 位/);
});
test("production rejects example placeholder secrets", () => {
assert.throws(
() => assertProductionSecret("replace-with-at-least-32-random-characters", "production"),
/示例占位符/,
);
assert.throws(
() => assertProductionSecret("change-me-now-1234567890-abcdefghij", "production"),
/示例占位符/,
);
});
test("production accepts a strong random secret", () => {
assert.doesNotThrow(() =>
assertProductionSecret("s3cure-Random-Value-0123456789-abcdefghij", "production"),
);
});
test("development keeps working with the example placeholder", () => {
assert.doesNotThrow(() =>
assertProductionSecret("replace-with-a-long-random-value", "development"),
);
});
test("placeholder detection covers documented example values", () => {
assert.equal(isPlaceholderSecret("replace-with-a-long-random-value"), true);
assert.equal(isPlaceholderSecret(" YOur-secret-value "), true);
assert.equal(isPlaceholderSecret("s3cure-Random-Value-0123456789-abcdefghij"), false);
});