Repository navigation
fix(auth): a TOTP code is accepted once - #34617
Conversation
…e wrong-code answer A TOTP code the two-factor plugin accepts is remembered for its user and secret for as long as the verifier's window could accept it, and any later presentation at /two-factor/verify-totp is refused, on another challenge, from another client and on the session lane (RFC 6238 section 5.2). A code from a step at or before the last accepted step is refused too. The refusal is the answer, and the failure count, a wrong code gets. Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj Co-authored-by: Claude <noreply@anthropic.com>
…e; the lockout dogfood signs in with a fresh step each time The build still refuses a two-factor plugin whose routes leave out /two-factor/verify-totp. The dogfood fixture gains freshTotp(), which hands out a code from a later step than the last one it handed out for the secret, because the server accepts a TOTP code once. Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj Co-authored-by: Claude <noreply@anthropic.com>
…code never reads as an error-code stamp Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift CheckThis PR changes 1 package(s): 27 hand-written doc(s) name something this change touched — list omitted above 15 rows. Re-derive on the tree named below: ⛔ 7 release-owned page(s) also affected — read-only, see AGENTS.md Documentation Guardrails. What this run could not see
Coarse fallback — 21 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 2ca1cca06951706b2d003fb84e9aac0df288902b && git checkout 2ca1cca06951706b2d003fb84e9aac0df288902b
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 082f9c2945260fd9b29951caff1c49f6dccab4ea ef07017f682732244c1c04b2a818a0f08c219189 && git checkout -B drift-repro 082f9c2945260fd9b29951caff1c49f6dccab4ea && git merge --no-ff ef07017f682732244c1c04b2a818a0f08c219189
node scripts/docs-audit/affected-docs.mjs --json 082f9c2945260fd9b29951caff1c49f6dccab4ea
|
|
security#2 · os-justin · session_01QSeKYn5CmmdShCJifm6WYj 席位核对ACCEPT · head
|
Clause-②: no
Metadata-protocol: none
维护者速读
/two-factor/verify-totp收下的每个 TOTP 验证码按「用户 + 密钥」记住它所在的时间步,在验证窗口内同一码再出现即拒收;时间步不晚于最近一次收下的码也拒收。拒收答复与错码完全相同(401 INVALID_CODE,同样计入挑战次数与账户锁定)。sys_verification,写失败则本次验证失败(不放行)。回滚即还原本 PR:接线只有auth-manager-auth-instance.ts一行,余为新模块。packages/plugins/plugin-auth/src/totp-code-single-use.ts的refuseSpentTotpStep(预占与回填);auth-manager-auth-instance.ts的接线行;packages/qa/dogfood/test/totp.ts的freshTotp。pnpm --filter @objectstack/plugin-auth exec vitest run src/totp-code-single-use.test.ts,预期 15 个用例全部通过。What changes
Rule: a TOTP code is accepted once. A code that
/two-factor/verify-totpaccepts is remembered for its user and secret for as long as the verifier's window could still accept it. Any later presentation is refused, on another sign-in challenge, from another client, on another node and on the session lane (RFC 6238 §5.2). A code from a time step at or before the last accepted step is refused too. The refusal is the answer a wrong code gets, and it counts toward the challenge's attempt budget and the account lockout the same way. A code from a later step is accepted.packages/plugins/plugin-auth/src/totp-code-single-use.ts(new): one before-hook on the two-factor plugin, matched on/two-factor/verify-totp, the one route that takes a TOTP code. It covers the enrolment confirmation, the sign-in challenge and the session-lane check. It resolves the user in the vendor's own order (session, then the pending challenge), finds the step the code belongs to inside the vendor's window, and reserves that step insys_verificationthrough better-auth's own single-use reservation (internalAdapter.reserveVerificationValue, a primary key derived from user, secret digest and step). It also reserves the earlier steps of the window. When a step is already held, the vendor is handed a code that cannot verify, so its own wrong-code path answers. The build fails when the two-factor plugin serves routes but none at/two-factor/verify-totp.packages/plugins/plugin-auth/src/auth-manager-auth-instance.ts: one line wires the hook onto the plugin list the instance is built from.packages/plugins/plugin-auth/src/totp-code-single-use.test.ts(new): the door pins and the seam pins below.packages/qa/dogfood/test/totp.ts:freshTotp(), which hands out a code from a later step than the last one it handed out for the secret, and waits for the clock when that step is not inside the window yet.two-factor-lockout.dogfood.test.tssigns in with it, so its cases that need a later step carry longer timeouts..changeset/totp-code-accepted-once.md:@objectstack/plugin-authpatch.No spec, schema, exported signature or route change. None of the files the open draft PR #34333 holds is touched.
Tests
src/totp-code-single-use.test.ts, real stack:AuthManageroverObjectQL+@objectstack/driver-sqlon better-sqlite3:memory:, requests throughAuthManager.handleRequest, the clock pinned (Dateonly) inside a step.Door pins (8):
401 INVALID_CODE, no session cookie, no session row);Seam pins (7): HOTP equals the RFC 4226 Appendix D values; the matched step is the newest step of the window; a step is reserved per user and per secret; the build refuses a two-factor plugin whose routes leave out the door; one before-hook is appended, matched on the door, once; a two-factor plugin that serves no route gets no hook; a list with no two-factor plugin is untouched.
Ablation (the wiring line replaced on disk through
scripts/ablation-replace.mjs): door pins 7 of 8 red, then restored (blob equal toHEAD,git diff HEADempty). Pin 8 stays green by design: it pins the vendor's window.Acceptance notes
/two-factor/enable,/two-factor/disableand/two-factor/get-totp-uritake a password, and/two-factor/verify-otpand/two-factor/verify-backup-codetake codes that are not TOTP codes.auth-route-ledger.tspins the published route set, so a new code-taking route is a reviewed ledger change.auth-manager.test.tsmocks the two-factor plugin with no routes. The build accepts a plugin object that serves no route (it takes no code), so that file needed no edit.warn(Insert operation failed), as for every better-auth reservation.Verification
Head at opening:
3f95e6b9e5. The branch mergedmainonce (c173ccc489). Every run below went throughscripts/pm/os-verify-lock.sh, and each names the commit it was taken at.Door and seam pins
vitest run src/totp-code-single-use.test.tsate63319413e:Tests 14 passed (14), VERDICT command-exit 0.vitest run src/totp-code-single-use.test.ts src/auth-manager.test.tsatdbdfc4dd0e(the 15th pin added):Test Files 2 passed (2),Tests 352 passed (352), VERDICT command-exit 0.Ablation
At
e63319413e, throughscripts/ablation-replace.mjswith an EXIT/INT/TERM restore trap: the wiring line inauth-manager-auth-instance.tsreplaced on disk (anchor 1 to 0, blob changed). Door pins: 7 of 8 red (pin 8 pins the vendor's window and stays green by design), seam pins green. Then restored: blob equal toHEAD,git diff HEADempty.plugin-auth full suite, build, typecheck
e63319413e:Test Files 280 passed, 1 failed:auth-manager.test.ts(10 cases), whose two-factor mock serves no route.dbdfc4dd0ebuilds such a plugin object as it is, and that file is green above.turbo run build --filter=@objectstack/plugin-auth...thenpnpm --filter @objectstack/plugin-auth typecheckatc173ccc489: VERDICT command-exit 0 (tsc --noEmit, the examples project, andcheck:test-typecheckOK).Derived gates
node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstackatc173ccc489: 73 commands, each run with its exit code captured before any pipe;--ranreconciliation: 73 accounted, 71 run, 2 NOT MEASURED.check:dispatcher-error-vocabularyread the forced body field as an error-code stamp atc173ccc489(exit 1);3f95e6b9e5names the field, and the gate exits 0 there.check:dual-build-cjs-loads(needs every workspace package built) andcheck:type-check-debt(its re-measure build never got the lock). Both run in CI.Not measured here
two-factor-lockout,two-factor-backup-code-reveal): the verify lock was held by another worktree's full dogfood battery for over 55 minutes, and these runs were still queued at opening.Test Coreand the queue's dogfood shards run them.freshTotp()step choice was checked by a scratch simulation under a fake clock (8 uses: every code from a later step than the last, inside the verifier's window when handed out and 5 s later; waits of 0, 0, 0, 12, 28, 28, 28, 28 s).Generated by Claude Code