Repository navigation
feat(worker): 未配置发件守卫——email_configured 单一真源 + 2FA/找回密码拒绝 (ONE-461) - #153
Merged
Merged
Conversation
…ONE-461) - email.ts 新增 isEmailConfigured(env) 作为全仓唯一发件能力判断(provider none / resend 缺 api_key / smtp 缺 host / 配置损坏 / 未知 provider 一律 false,不抛错) - GET /api/system/info 新增 email_configured: boolean;catch 分支 fail-closed 回 false - POST /auth/login 过滤 email_otp;唯一方式被过滤清空时 403 2FA_METHOD_UNAVAILABLE, 不发 tempToken、不签 token(不再静默降级为仅密码);/auth/refresh 的 needs2faSetup 用同一过滤口径 - /auth/2fa/otp/send、/auth/2fa/verify(email_otp) 未配置 503 EMAIL_NOT_CONFIGURED, 前者在生成验证码之前拒绝,不写 KV、不发信 - /auth/password/forgot 在冷却与用户查询之前统一 503,注册/未注册邮箱同响应, 不写 pwd_reset:*、不设冷却;已配置语义逐字不变 - /auth/2fa/email-otp/send-verify 与 /enable 未配置直接拒绝,删除「未配置可免验证码 直接启用」分支;/disable 永远放行 - 新增 worker/test/email-not-configured.test.ts(25 例),覆盖父单 §6 第 1–7 条
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
父单 ONE-461 规格 v1.0 的后端实现环节(子单 ONE-466)。未配置发件服务时:登录不再端出必然失败的邮件验证码选项,忘记密码不再假装发送成功,设置侧不再允许埋下永远用不了的方式。
本 PR 只动
worker/,未碰web/(前端由 Mary 并行负责)、未碰 schema / 迁移账本 / 表前缀、未碰线上 D1 与任何线上 setting。核心改动
单一真源(§2) —
worker/src/services/email.ts新增并导出isEmailConfigured(env):provider 为空/none→ false;resend需resend_config可解析且api_key非空;smtp需smtp_config可解析且host非空;配置 JSON 损坏、未知 provider → false,全程不抛错。全仓发件能力判断从此只走它。sendEmail()行为一行未改。公开信号(§3.1) —
GET /api/system/info新增email_configured: boolean,只回布尔,不回 provider / config 细节;catch 降级分支同样回false(fail-closed)。登录过滤(§3.2) —
getAvailable2FAMethods加可选参数{ emailConfigured }(不传时保持原有不过滤语义,无其他调用方语义漂移)。POST /auth/login过滤后email_otp在未配置时不再出现在availableMethods;POST /auth/refresh算needs2faSetup用同一口径。唯一方式不可用 fail-closed(§3.3,规格内新增决策点) — 用户原本启用了至少一种 2FA、过滤后为空 →
403 { code: "2FA_METHOD_UNAVAILABLE" },不发 tempToken、不签 token、不建会话,绝不静默降级为仅密码登录。require_2fa=true但从未启用过任何方式的既有needs2faSetup流程行为不变(有测试守护)。发码/验码守卫(§3.4) —
POST /auth/2fa/otp/send在生成验证码之前503 EMAIL_NOT_CONFIGURED(不写 KV、不调sendOTPEmail);POST /auth/2fa/verify的email_otp分支同样拒绝(旧 tempToken 也试不了码)。totp / passkey 不受影响。忘记密码(§3.5) —
POST /auth/password/forgot在冷却检查与用户查询之前判 §2,未配置对所有邮箱一律503 EMAIL_NOT_CONFIGURED,不生成码、不写pwd_reset:*/ 冷却键。已配置时现有防枚举语义逐字不变(同样有回归测试)。设置侧启用守卫(§3.6,规格内新增决策点) —
email-otp/send-verify与email-otp/enable未配置直接503 EMAIL_NOT_CONFIGURED;「未配置可免验证码直接启用」这条陷阱分支已删除,启用一律要过验证码。email-otp/disable永远放行,未配置也能关掉历史启用记录。错误码与 HTTP 状态严格按冻结契约:
2FA_METHOD_UNAVAILABLE(403)、EMAIL_NOT_CONFIGURED(503),文案沿用现有风格(英文原文 +code,前端按 code 映射)。改了哪些文件
worker/src/services/email.ts— 新增isEmailConfigured+readJsonSettingworker/src/services/twofa.ts—getAvailable2FAMethods可选emailConfigured过滤参数worker/src/index.ts—/api/system/info增加email_configured(含 catch fail-closed)worker/src/routes/auth.ts— 登录过滤 + §3.3 拦截、refresh 同口径、忘记密码前置守卫worker/src/routes/auth2fa.ts— otp/send、verify(email_otp)、email-otp/send-verify、email-otp/enable 守卫worker/test/email-not-configured.test.ts— 新增 25 例无 schema 变更、无迁移、无回滚动作需要。
自测证据
在本地按仓库基线
main ebf20f7+ 本分支执行:pnpm test→ 18 files / 238 tests passed(其中本单新增email-not-configured.test.ts25 例,覆盖父单 §6 第 1–7 条)pnpm typecheck→ 通过(webtsc -b+ workertsc --noEmit)pnpm build→ 通过(web vite build + workertsc --noEmit)biome lint/biome format对本 PR 触碰的文件零告警(worker/src/routes/items.ts的未用导入为既有问题,未在本单范围内改动)测试覆盖点:判断函数矩阵(含空 api_key / 空 host / 损坏 JSON / 未知 provider);system-info 已配置 true、未配置 false 且响应无 provider/smtp/resend 字样、settings 读取抛错时 fail-closed;登录过滤(totp+email_otp 未配置只剩 totp,已配置两者都在);§3.3 仅启用 email_otp 未配置 → 403 且响应无 token/tempToken、无 set-cookie、KV 无
2fa:*/ss:*/rt:*,已配置正常requires2fa;从未启用过方式的needs2faSetup回归;otp/send 未配置 KV 无 OTP 记录、verify email_otp 未配置被拒且 tempToken 未被消费、totp 路径不受影响、已配置发码验码成功;忘记密码注册/未注册邮箱同响应 503 且 KV 无pwd_reset*、已配置两者均 success 且已注册者 KV 有码;§3.6 send-verify/enable 未配置被拒(带码不带码都拒)且user_2fa.email_otp_enabled未置位、disable 未配置可关、已配置带正确码可启用。范围外同源问题(仅提议,未实现)
POST /auth/email/resend与注册发信在未配置时仍会静默「success」(KV 里照样写email_verify:*)。属父单 §5 明确排除项,本 PR 未捆绑;若要修,等单独单子。纪律
PR 就绪即停:未合并、未部署;合并与上线关口归用户。