fix: 收紧发布环境与任务文件边界
This commit is contained in:
1 parent
2fe108d66a
commit
3af5611da5
5 files changed
+103
-16
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import path from "node:path";
|
||||
|
||||
const PLACEHOLDER_PATTERN = /(replace-with|change-me|changeme|^your[-_ ]|example|placeholder)/i;
|
||||
|
||||
/** 判断值是否仍是示例占位符,而不是真实随机密钥。 */
|
||||
export function isPlaceholderSecret(value) {
|
||||
return PLACEHOLDER_PATTERN.test(value.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* 切流前校验共享生产环境文件。
|
||||
* 除必填项外,必须拒绝示例密钥,避免上线后才在 API 启动时失败。
|
||||
*/
|
||||
export function assertReleaseEnvironment(environment) {
|
||||
if (
|
||||
environment.NODE_ENV !== "production" ||
|
||||
!environment.DATABASE_URL ||
|
||||
!environment.SESSION_SECRET ||
|
||||
!environment.DEFAULT_BRAND_ID ||
|
||||
!environment.LOCAL_MEDIA_ROOT
|
||||
) {
|
||||
throw new Error("Production environment is missing required values");
|
||||
}
|
||||
if (environment.SESSION_SECRET.trim().length < 32) {
|
||||
throw new Error("SESSION_SECRET must contain at least 32 characters");
|
||||
}
|
||||
if (isPlaceholderSecret(environment.SESSION_SECRET)) {
|
||||
throw new Error("SESSION_SECRET must not use an example placeholder");
|
||||
}
|
||||
if (!path.isAbsolute(environment.LOCAL_MEDIA_ROOT)) {
|
||||
throw new Error("LOCAL_MEDIA_ROOT must be absolute");
|
||||
}
|
||||
if (environment.API_PREFIX) {
|
||||
throw new Error("API_PREFIX must be empty when Traefik strips /api");
|
||||
}
|
||||
const throttleStore = environment.CLIENT_ERROR_THROTTLE_STORE ?? "memory";
|
||||
if (!["memory", "postgres"].includes(throttleStore)) {
|
||||
throw new Error("CLIENT_ERROR_THROTTLE_STORE must be memory or postgres");
|
||||
}
|
||||
return new URL(environment.DATABASE_URL);
|
||||
}
|
||||
Reference in new issue
Block a user