Skip to content

修复第 4 轮(工程质量·后端/CI):测试基线接入 CI 门禁、schema 单一来源、Low 级后端加固 - #124

Merged
todosdev[bot] merged 3 commits into
mainfrom
tds/conv-01a14a9e-e0e2-7027-8d17-d72f706f13d0
Oct 18, 2026
Merged

todosdev[bot] merged 3 commits into
mainfrom
tds/conv-01a14a9e-e0e2-7027-8d17-d72f706f13d0

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • 建立 worker 单测基线(auth / links / redirect 关键路径)并接入 CI 门禁:deploy 依赖 CI 成功,pnpm test 进 CI 后即成为部署门禁。
  • 让 lib/migrations.ts 成为 schema 唯一事实来源,migrations/*.sql 改由它生成并加一致性测试,保留 wrangler 迁移原本的 fail-if-exists 语义。
  • 新增结构化错误日志(app.onError)+ 一批 Low 级后端加固与 CI/发版安全校验。

Changes

测试基线与 CI 门禁(BE-Q2 / CI-C1)

  • 新增 apps/worker/test/{auth,links,redirect,migrations,email}.test.ts,用 Node 内置 node:sqlite 建真实内存 SQLite(套用真实 DDL)+ 内存 KV mock,JWT 用真实 jose 签发/校验;覆盖登录、refresh、token_version 吊销、links 创建/更新/权限、redirect 跳转/过期/停用。
  • apps/worker 新增唯一 dev 依赖 tsx,解决 Node 类型剥离不重写 .js→.ts 相对导入的问题。
  • .github/workflows/ci.yml 增加 pnpm test 步骤(Build 之后)。

Schema 单一来源(BE-Q1)

  • getMigrations() 迁移对象新增 fileName;新增 apps/worker/scripts/generate-migrations.ts 从 getMigrations('') 生成 migrations/*.sql,渲染时剥掉幂等标记(IF NOT EXISTS/OR IGNORE),保证与原有 wrangler SQL 语义完全一致;lib/migrations.ts 的 SQL 未改动。
  • test/migrations.test.ts 校验生成内容与磁盘一致、schema_vN ↔ NNNN_*.sql 约定、以及「对象已存在即报错」语义。

Worker 加固

  • BE-Q3:app.onError 输出结构化日志(path/method/error/requestId),客户端只收通用 500;新增 per-request requestId 中间件。
  • BE-S9:forgot-password 发信移入 waitUntil 且先落库再发信,消除时间侧信道。
  • BE-S10:CORS 仅当请求为 http 且主机是 loopback(localhost/127.0.0.1/::1)时放行 localhost Origin。
  • BE-S11:SPA/静态资源回包统一加安全头 + Vite 兼容 CSP(抽到 lib/security.ts)。
  • BE-S12:GET /2fa/totp/setup 改 POST,同步改 apps/web/src/api/client.ts 的 totpSetup。
  • BE-C4:发送验证码前统一删除旧码(补齐 register 邮箱验证分支)。
  • BE-R2:Resend 失败不再把上游响应正文带进错误信息,只记脱敏摘要。

CI / 发版安全(CI-C9)

  • tag.yml:打 tag 前用 actions.listWorkflowRuns 按 workflow 文件 + push 事件校验 main HEAD 有成功的 CI 运行。
  • release.yml:校验 tag 格式且 tag 解析到 main 上可达的 commit。
  • apps/web/vite.config.ts:dev proxy target 改为 API_PROXY_TARGET 环境变量可配。

Testing

  • pnpm check(biome)、pnpm typecheck、pnpm build 均通过。
  • pnpm test:worker 27 项单测 + web 单测全部通过。
  • 部署脚本测试 17 项通过;shellcheck .github/scripts/*.sh 通过;pnpm audit --prod --audit-level=high 无已知漏洞。
  • 说明:migrations/*.sql 为生成产物且已在本次变更中重新生成,内容与生成器一致(无过期产物)。

Todos and others added 3 commits October 18, 2026 00:31
- BE-Q2/CI-C1: node:test route tests for auth/links/redirect backed by an
  in-memory SQLite D1 (node:sqlite) and tsx for .js->.ts resolution; wired
  `pnpm test` into CI so deploy is gated on it
- BE-Q1: generate migrations/*.sql from lib/migrations.ts and assert
  consistency in a test
- BE-Q3: app.onError with structured logging and a per-request id
- BE-S9: forgot-password sends email via waitUntil (timing side-channel)
- BE-S10: CORS allows localhost origins only on plain-http (dev)
- BE-S11: SPA/static responses get security headers plus a Vite-compatible CSP
- BE-S12: totp setup becomes POST (route + client.ts call)
- BE-C4: delete stale verification codes before inserting new ones
- BE-R2: redact Resend failure before logging
- CI-C9: tag.yml verifies CI on main, release.yml validates tag source/format,
  vite dev proxy target configurable via env

Checkpoint: ZsFIsGEbQRfT1JpslSW3E
…cs, forgot-password ordering

- tag.yml: query the CI workflow via listWorkflowRuns (workflow file + push
  event) instead of the repo-wide list, so the tag workflow itself is never
  mistaken for CI
- index.ts: only allow localhost CORS origins over http on a loopback host,
  not merely on http scheme
- generate-migrations: strip idempotency markers when rendering .sql so the
  wrangler migrations keep their fail-if-exists semantics; lib/migrations.ts
  SQL is unchanged; add an existing-object test
- auth.ts: persist the reset code before sending the email and wrap the whole
  forgot-password background task in a redacted catch

Checkpoint: UBfQSDzaq25VhKiTMGKwP
@todosdev
todosdev Bot merged commit 940f99b into main Oct 18, 2026
@mapxn
mapxn deleted the tds/conv-01a14a9e-e0e2-7027-8d17-d72f706f13d0 branch October 18, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant