diff --git a/.codex-tasks/template-audit-execution/raw/completion-evidence.json b/.codex-tasks/template-audit-execution/raw/completion-evidence.json index f64078c..d035a3b 100644 --- a/.codex-tasks/template-audit-execution/raw/completion-evidence.json +++ b/.codex-tasks/template-audit-execution/raw/completion-evidence.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "scope": "模板完成证据登记,保留命令、日志与产物哈希", - "generatedAt": "2026-10-23T04:57:10.362Z", + "generatedAt": "2026-10-23T04:57:19.065Z", "templates": { "acheng-campus-food": { "real-manifest-interaction": { @@ -256,5 +256,5 @@ } } }, - "sourceTreeHash": "4eae37312c6ef28bb51218923440e8e0b5b6a823875a0061519af73481a1897d" + "sourceTreeHash": "59a9701ea852e1b9240b22b85b67ef37b5bd4830f69436a94bf83066806af59d" } diff --git a/.gitignore b/.gitignore index affc127..9e50abe 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,9 @@ media/ **/.swc/ .codex-tasks/ +!.codex-tasks/template-audit-execution/ +!.codex-tasks/template-audit-execution/raw/ +!.codex-tasks/template-audit-execution/raw/completion-evidence.json docker-compose.override.yml .env.production diff --git a/deploy/install-release.mjs b/deploy/install-release.mjs index adbd5ac..6da956b 100644 --- a/deploy/install-release.mjs +++ b/deploy/install-release.mjs @@ -16,6 +16,7 @@ import { import path from "node:path"; import { spawn, spawnSync } from "node:child_process"; import { checkPackage } from "../scripts/deploy/check-package.mjs"; +import { assertReleaseEnvironment } from "./release-environment.mjs"; const root = process.env.DEPLOY_ROOT; if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute"); @@ -28,20 +29,7 @@ if (!postgresContainer || !/^[a-zA-Z0-9_.-]+$/.test(postgresContainer)) { throw new Error("POSTGRES_CONTAINER is required for the database backup"); } if (Number(process.versions.node.split(".")[0]) !== 24) throw new Error("Node.js 24 is required"); -if ( - process.env.NODE_ENV !== "production" || - !process.env.DATABASE_URL || - !process.env.SESSION_SECRET || - process.env.SESSION_SECRET.length < 32 || - !process.env.DEFAULT_BRAND_ID || - !process.env.LOCAL_MEDIA_ROOT -) { - throw new Error("Production environment is missing required values"); -} -if (!path.isAbsolute(process.env.LOCAL_MEDIA_ROOT)) - throw new Error("LOCAL_MEDIA_ROOT must be absolute"); -if (process.env.API_PREFIX) throw new Error("API_PREFIX must be empty when Traefik strips /api"); -const databaseUrl = new URL(process.env.DATABASE_URL); +const databaseUrl = assertReleaseEnvironment(process.env); const privateDatabaseHost = /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test( databaseUrl.hostname, ); diff --git a/deploy/release-environment.mjs b/deploy/release-environment.mjs new file mode 100644 index 0000000..6c2de9d --- /dev/null +++ b/deploy/release-environment.mjs @@ -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); +} diff --git a/deploy/release-environment.test.mjs b/deploy/release-environment.test.mjs new file mode 100644 index 0000000..9c29aca --- /dev/null +++ b/deploy/release-environment.test.mjs @@ -0,0 +1,55 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { assertReleaseEnvironment } from "./release-environment.mjs"; + +const validEnvironment = { + NODE_ENV: "production", + DATABASE_URL: "postgresql://user:password@127.0.0.1:5432/personal_brand", + SESSION_SECRET: "Zx9vQ2mK7pL4rT8wY3nB6cF1hJ5sD0gA", + DEFAULT_BRAND_ID: "brand-1", + LOCAL_MEDIA_ROOT: "/srv/personal-brand/media", +}; + +test("生产环境校验接受真实随机密钥", () => { + const databaseUrl = assertReleaseEnvironment(validEnvironment); + assert.equal(databaseUrl.hostname, "127.0.0.1"); +}); + +test("生产环境校验拒绝示例占位密钥", () => { + assert.throws( + () => + assertReleaseEnvironment({ + ...validEnvironment, + SESSION_SECRET: "replace-with-at-least-32-random-characters", + }), + /must not use an example placeholder/, + ); +}); + +test("生产环境校验拒绝过短密钥", () => { + assert.throws( + () => assertReleaseEnvironment({ ...validEnvironment, SESSION_SECRET: "short-secret" }), + /at least 32 characters/, + ); +}); + +test("生产环境校验拒绝缺失的必填项", () => { + assert.throws( + () => assertReleaseEnvironment({ ...validEnvironment, DEFAULT_BRAND_ID: "" }), + /missing required values/, + ); +}); + +test("生产环境校验拒绝 Traefik 之外的 API 前缀", () => { + assert.throws( + () => assertReleaseEnvironment({ ...validEnvironment, API_PREFIX: "/api" }), + /API_PREFIX must be empty/, + ); +}); + +test("生产环境校验拒绝未知的异常限流存储", () => { + assert.throws( + () => assertReleaseEnvironment({ ...validEnvironment, CLIENT_ERROR_THROTTLE_STORE: "redis" }), + /CLIENT_ERROR_THROTTLE_STORE must be memory or postgres/, + ); +});