From 5d8d1fa4c21b3dafeba74a9645686227cb241118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E8=8D=92=E4=B8=89=E6=9C=A8?= <154658461+aozorae@users.noreply.github.com> Date: Sun, 6 Sep 2026 08:47:10 +0800 Subject: [PATCH] Fix unsafe KV namespace selection --- .github/scripts/ensure-cloudflare-resources.mjs | 13 +++---------- test/deploy-workflow-security.test.js | 6 +++--- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/.github/scripts/ensure-cloudflare-resources.mjs b/.github/scripts/ensure-cloudflare-resources.mjs index 343389f8..28beb2d0 100644 --- a/.github/scripts/ensure-cloudflare-resources.mjs +++ b/.github/scripts/ensure-cloudflare-resources.mjs @@ -17,7 +17,6 @@ const accountId = process.env.CLOUDFLARE_ACCOUNT_ID; const LEGACY_D1_DATABASE_NAME = "cfchat-db"; const LEGACY_KV_NAMESPACE_TITLE = "cfchat-sessions"; -const PRODUCTION_KV_NAMESPACE_TITLE = "SESSIONS"; const LEGACY_R2_BUCKET_NAME = "cfchat-files"; function readEnv(...names) { @@ -36,9 +35,6 @@ const d1DatabaseName = const kvNamespaceTitle = readEnv("EDGECHAT_KV_NAMESPACE_TITLE", "CFCHAT_KV_NAMESPACE_TITLE") ?? LEGACY_KV_NAMESPACE_TITLE; -const kvNamespaceTitleExplicit = Boolean( - readEnv("EDGECHAT_KV_NAMESPACE_TITLE", "CFCHAT_KV_NAMESPACE_TITLE"), -); const r2BucketName = readEnv("EDGECHAT_R2_BUCKET_NAME", "CFCHAT_R2_BUCKET_NAME") ?? LEGACY_R2_BUCKET_NAME; @@ -193,12 +189,9 @@ async function ensureD1Database() { async function ensureKvNamespace() { const namespaces = await listKvNamespaces(); - const candidateTitles = kvNamespaceTitleExplicit - ? [kvNamespaceTitle] - : [PRODUCTION_KV_NAMESPACE_TITLE, LEGACY_KV_NAMESPACE_TITLE]; - const existing = candidateTitles - .map((title) => namespaces.find((namespace) => namespace.title === title && namespace.id)) - .find(Boolean); + const existing = namespaces.find( + (namespace) => namespace.title === kvNamespaceTitle && namespace.id, + ); if (existing) { console.log(`KV namespace already exists: ${existing.title} (${existing.id})`); return { id: existing.id, title: existing.title, created: false }; diff --git a/test/deploy-workflow-security.test.js b/test/deploy-workflow-security.test.js index 27be7143..74057dee 100644 --- a/test/deploy-workflow-security.test.js +++ b/test/deploy-workflow-security.test.js @@ -69,16 +69,16 @@ test("首次部署自动创建密钥,普通部署保留密钥,手动轮换 assert.match(deployStep, /wrangler deploy --config wrangler\.ci\.toml/); }); -test("资源确认脚本优先复用生产 SESSIONS KV 且保留显式标题覆盖", () => { +test("资源确认脚本仅复用配置的 KV 标题,避免隐式共享会话存储", () => { const script = readFileSync( new URL("../.github/scripts/ensure-cloudflare-resources.mjs", import.meta.url), "utf8", ).replaceAll("\r\n", "\n"); - assert.match(script, /const PRODUCTION_KV_NAMESPACE_TITLE = "SESSIONS";/); + assert.doesNotMatch(script, /PRODUCTION_KV_NAMESPACE_TITLE/); assert.match( script, - /kvNamespaceTitleExplicit\s*\? \[kvNamespaceTitle\]\s*:\s*\[PRODUCTION_KV_NAMESPACE_TITLE, LEGACY_KV_NAMESPACE_TITLE\]/s, + /namespace\.title === kvNamespaceTitle && namespace\.id/, ); assert.match(script, /setOutput\("kv_namespace_title", kv\.title\);/); });