Skip to content

setup 接口对齐 Zeta:状态接口、JSON 密钥、限流与管理员免密钥升级 - #127

Merged
todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a14db1-e95a-7f7c-a87c-d05df644823b
Oct 18, 2026
Merged

todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a14db1-e95a-7f7c-a87c-d05df644823b

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • 将 shortlink 的 setup 接口对齐 Zeta 当前实现:新增公开状态接口、JSON body 密钥、明确的错误码,以及管理员的免密钥升级入口。
  • 抽出共享迁移执行函数 runMigrations(),让 POST /api/setup 与新的 POST /api/admin/setup 走同一套迁移逻辑。
  • 限流收紧为每 IP 每 10 分钟 3 次(计数先于密钥校验),并返回 Retry-After。

Changes

接口(apps/worker/src/routes/setup.ts)

  • GET /api/setup/state(公开):返回 { enabled },只读 setup_page_enabled('0' → false,缺省/表不存在 → true),不泄露库初始化/schema 状态。
  • GET /api/setup/*:返回 405 + Allow: POST,指引改用 JSON body。
  • POST /api/setup:接受 {"secret": "<SETUP_SECRET>"},body 为空时回退到旧 X-Setup-Secret 头;错误码 500(未配置)/400(body 非法)/403(密钥错误,常量时间比较);不因 setup_page_enabled=0 拒绝。

迁移(apps/worker/src/lib/migrations-runner.ts 新增)

  • runMigrations(env) 抽出共享,保持 TABLE_PREFIX 前缀、createMigrationsTableSql、错误 break 语义,返回 { migrations: [...] }。

管理端(apps/worker/src/routes/admin.ts)

  • POST /api/admin/setup:requireAdmin 覆盖,免密钥走同一 runMigrations,写 run_setup 审计日志(details 记 applied/skipped/error 数),不纳入 IP 限流。
  • settings 白名单加 setup_page_enabled,仅接受 '0'/'1'(非法 400),不做敏感值掩码。

限流(apps/worker/src/lib/kv.ts)

  • checkSetupRateLimit 改返回 { allowed, retryAfter },窗口 3 次/600s;fixedWindowIncrement 拆出带 retryAfter 的详细版,原布尔版作为薄封装保留给登录/注册/忘记密码。

挂载(apps/worker/src/index.ts)

  • 主路径迁到 /api/setup,保留 /setup 作为旧别名,旧 curl POST /setup -H "X-Setup-Secret: ..." 继续可用。

测试与文档

  • 新增 apps/worker/test/setup.test.ts(state 三态、405、JSON/header、错误码、限流、admin 鉴权与审计、settings 白名单),D1Mock 加 applyMigrations=false 以构造空库;覆盖 /setup 旧别名路径。
  • 更新 README.md、README.zh-CN.md、CLAUDE.md 的 setup 段落(JSON body、405、state、3 次/10 分钟、管理员按钮、setup_page_enabled 只隐藏页面)。

Testing

  • pnpm check(Biome)、pnpm typecheck、pnpm build 全绿。
  • pnpm test:61/61 通过(含新增 setup 测试)。

Todos added 2 commits October 18, 2026 14:40
Add GET /api/setup/state (public), a 405 for GET /api/setup/*, a JSON-body
POST /api/setup with a legacy X-Setup-Secret header fallback, and a keyless
POST /api/admin/setup for admins.

- Extract shared runMigrations() into lib/migrations-runner.ts, used by both
  the setup and admin endpoints
- Return 500/400/403 for unconfigured/invalid/wrong secret (constant-time
  comparison); rate-limit to 3 per IP per 10 min counted before the secret
  check (429 + Retry-After)
- Mount setup at /api/setup, keep /setup as a backward-compatible alias
- Whitelist setup_page_enabled ('0'/'1') in admin settings; state defaults to
  enabled when the setting is absent
- Add worker tests; update README/README.zh-CN/CLAUDE docs

Checkpoint: WzVRNGv2ju_UNM93bGHLI
Add two assertions so the backward-compatible /setup mount in index.ts is
exercised: POST /setup with the X-Setup-Secret header returns 200, and
GET /setup/* returns 405 + Allow: POST. Guards against silently breaking the
alias in future changes.

Checkpoint: fQy51T3T8GYCOUnLU97of
@todosdev
todosdev Bot merged commit 30abb05 into main Oct 18, 2026
4 checks passed
@mapxn
mapxn deleted the tds/conv-01a14db1-e95a-7f7c-a87c-d05df644823b 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.

0 participants