Skip to content

fix(auth): a TOTP code is accepted once - #34617

Merged
objectstack-fleet[bot] merged 6 commits into
mainfrom
claude/sec2-j7qr
Nov 17, 2026
Merged

objectstack-fleet[bot] merged 6 commits into
mainfrom
claude/sec2-j7qr

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:/two-factor/verify-totp 收下的每个 TOTP 验证码按「用户 + 密钥」记住它所在的时间步,在验证窗口内同一码再出现即拒收;时间步不晚于最近一次收下的码也拒收。拒收答复与错码完全相同(401 INVALID_CODE,同样计入挑战次数与账户锁定)。
  • 为什么改:规则是「TOTP 码只收一次」(RFC 6238 §5.2),跨登录挑战、跨客户端、跨节点、会话通道都成立。
  • 风险与回滚:同一 30 秒内用 TOTP 登录两次的客户端,第二次要用下一时间步的码;记账写 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-totp accepts 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 in sys_verification through 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.ts signs in with it, so its cases that need a later step carry longer timeouts.
  • .changeset/totp-code-accepted-once.md: @objectstack/plugin-auth patch.

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: AuthManager over ObjectQL + @objectstack/driver-sql on better-sqlite3 :memory:, requests through AuthManager.handleRequest, the clock pinned (Date only) inside a step.

Door pins (8):

  1. a code accepted at one sign-in challenge is refused at another client's challenge (401 INVALID_CODE, no session cookie, no session row);
  2. a code spent confirming the enrolment is refused at the sign-in challenge;
  3. a code accepted on the session lane is refused on the session lane again;
  4. a code from a step before the last accepted step is refused;
  5. a code from a later step is accepted, at the challenge that refused a spent code;
  6. a spent code gets the answer, and the failure count, a wrong code gets;
  7. two presentations of one code that arrive together admit exactly one;
  8. the vendor accepts no step outside the window whose steps are reserved.

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 to HEAD, git diff HEAD empty). Pin 8 stays green by design: it pins the vendor's window.

Acceptance notes

  • /two-factor/enable, /two-factor/disable and /two-factor/get-totp-uri take a password, and /two-factor/verify-otp and /two-factor/verify-backup-code take codes that are not TOTP codes. auth-route-ledger.ts pins the published route set, so a new code-taking route is a reviewed ledger change.
  • A reservation is keyed by user and a truncated SHA-256 digest of the secret, so a re-enrolled secret's first code may share a step with the old secret's last one.
  • auth-manager.test.ts mocks 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.
  • The reservation conflict is logged by the SQL engine at warn (Insert operation failed), as for every better-auth reservation.

Verification

Head at opening: 3f95e6b9e5. The branch merged main once (c173ccc489). Every run below went through scripts/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.ts at e63319413e: Tests 14 passed (14), VERDICT command-exit 0.
  • vitest run src/totp-code-single-use.test.ts src/auth-manager.test.ts at dbdfc4dd0e (the 15th pin added): Test Files 2 passed (2), Tests 352 passed (352), VERDICT command-exit 0.
Ablation

At e63319413e, through scripts/ablation-replace.mjs with an EXIT/INT/TERM restore trap: the wiring line in auth-manager-auth-instance.ts replaced 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 to HEAD, git diff HEAD empty.

plugin-auth full suite, build, typecheck
  • Full suite at e63319413e: Test Files 280 passed, 1 failed: auth-manager.test.ts (10 cases), whose two-factor mock serves no route. dbdfc4dd0e builds such a plugin object as it is, and that file is green above.
  • turbo run build --filter=@objectstack/plugin-auth... then pnpm --filter @objectstack/plugin-auth typecheck at c173ccc489: VERDICT command-exit 0 (tsc --noEmit, the examples project, and check:test-typecheck OK).
Derived gates

node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack at c173ccc489: 73 commands, each run with its exit code captured before any pipe; --ran reconciliation: 73 accounted, 71 run, 2 NOT MEASURED.

  • 71 exit 0. check:dispatcher-error-vocabulary read the forced body field as an error-code stamp at c173ccc489 (exit 1); 3f95e6b9e5 names the field, and the gate exits 0 there.
  • NOT MEASURED (exit 3, prerequisite): check:dual-build-cjs-loads (needs every workspace package built) and check:type-check-debt (its re-measure build never got the lock). Both run in CI.
Not measured here
  • The plugin-auth full suite at the final head, and the two-factor dogfood files (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 Core and 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

…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>
…code never reads as an error-code stamp

Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added size/l documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 17, 2026
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): @objectstack/plugin-auth, touching 22 documentable anchor(s).

27 hand-written doc(s) name something this change touched — list omitted above 15 rows. Re-derive on the tree named below: node scripts/docs-audit/affected-docs.mjs --json 082f9c2945260fd9b29951caff1c49f6dccab4ea.

⛔ 7 release-owned page(s) also affected — read-only, see AGENTS.md Documentation Guardrails.

What this run could not see
  • 5 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 65 of 247 client-bound route-ledger rows — the other 182 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 182: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 123 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 21 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json 082f9c2945260fd9b29951caff1c49f6dccab4ea → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 2ca1cca06951706b2d003fb84e9aac0df288902b — the merge of head ef07017f682732244c1c04b2a818a0f08c219189 into base 082f9c2945260fd9b29951caff1c49f6dccab4ea, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# 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

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs 082f9c2945260fd9b29951caff1c49f6dccab4ea → pass the list as
args.docs, on the commit named under Which tree this was computed on.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

security#2 · os-justin · session_01QSeKYn5CmmdShCJifm6WYj

席位核对

ACCEPT · head ef07017f682732244c1c04b2a818a0f08c219189 · 2026-11-17T04:30Z

  • Shape: 6 files. Not governed (check-governed-merges --pr 34617: 0 of 6 paths). Clause-②: no, Metadata-protocol: none. Patch for @objectstack/plugin-auth.
  • Review: no contract face, so the seat read the diff itself. When a TOTP code is accepted, its step is reserved per user and secret through the auth store's primary-key reservation, and so are the earlier steps still inside the verifier's window. A later presentation inside the window gets a wrong code's answer and counts toward the attempt budget the same way. The hook is wired outside the files PR feat(auth): an invitation is accepted in one step with the proof its mail carries #34333 holds.
  • Accepted (seat's call): the memory is keyed per user and secret rather than per user alone. That is the RFC 6238 §5.2 reading, and it does not refuse a legitimate re-enrolment.
  • Tests (dev):
    • door and seam pins 14/14;
    • plugin-auth suite 281 files, 5782 passed;
    • the two touched dogfood files 9/9 at 3f95e6b9e5;
    • ablation red (7 of 8 door pins), then restored.
  • Docs drift: no hand-written page states that a code may be reused; the changeset states the one client-visible consequence.
  • Check-runs at this head (after the relay merge of main): 36 success, 16 skipped, 0 failed. K = 2.
  • Landing: ready, then the merge queue. The advisory closes on merge.

@objectstack-fleet
objectstack-fleet Bot marked this pull request as ready for review November 17, 2026 04:31
@objectstack-fleet
objectstack-fleet Bot added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit c1ec627 Nov 17, 2026
53 checks passed
@objectstack-fleet
objectstack-fleet Bot deleted the claude/sec2-j7qr branch November 17, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/l tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant