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

+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);
});