Skip to content

fix(account): make TOTP codes single-use - #15154

Merged
ChiragAgg5k merged 2 commits into
appwrite:mainfrom
ChiragAgg5k:fix/totp-single-use
Nov 1, 2026
Merged

ChiragAgg5k merged 2 commits into
appwrite:mainfrom
ChiragAgg5k:fix/totp-single-use

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

A TOTP code was accepted on any number of challenges while its 30s step was current, so one observed code could complete MFA on a second password-only session. RFC 6238 §5.2 requires a verifier to refuse a step it has already accepted.

session A: PUT /account/mfa/challenges {otp: 793177} → 200
session B: PUT /account/mfa/challenges {otp: 793177} → 200   (before)
                                                      → 401 user_invalid_token (after)
  • Challenge\TOTP::step() returns the step a code belongs to, or null when it is stale or already spent (data.step on the authenticator).
  • Challenge\TOTP::spend() re-reads the authenticator forUpdate and records the step, inside the transaction that already claims the challenge, so concurrent redemptions of one code cannot both win.
  • Enrolment (PUT /account/mfa/authenticators/totp) spends its step too, so the enrolment code cannot also complete a sign-in.

No schema change: the step is a new key in the existing encrypted data JSON.

Test Plan

  • New testMFATOTPCodeIsSingleUse: the enrolment code is refused on a challenge; a code that completed session A's challenge is refused on session B's fresh challenge, B stays user_more_factors_required, and B's challenge still accepts the next code.
  • The three tests proving a password change invalidates a pending challenge replayed the enrolment code, which is now refused for the wrong reason; they use the next code instead.

Related PRs and Issues

  • None

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Related: appwrite-labs/cloud#6666

A TOTP code was checked only against the current time step, and the
only state a successful verification consumed was the challenge. The
same code could therefore complete a fresh challenge on any other
session of the account for the rest of its step, and an enrolment code
could also complete a sign-in.

Record the spent step on the authenticator and refuse any code whose
step is not newer, as RFC 6238 section 5.2 requires. The step is
claimed under a row lock inside the transaction that already claims the
challenge, so two concurrent redemptions of one code cannot both win.
Enrolment spends its step the same way.

The tests that prove a password change invalidates a pending challenge
replayed the enrolment code, which would now be refused for the wrong
reason; they use the next code instead.
@ghost

ghost commented Nov 1, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental change accurately documents the helper's returned enrollment OTP and introduces no behavior change.

TOTP verification now tracks accepted authenticator steps and rejects code reuse, spending steps transactionally during enrollment and MFA challenge completion. An end-to-end test covers rejection of the enrollment code and replay across sessions while confirming a later code can still complete the challenge.

Latest changes: The latest commit updates the test helper's return-shape annotation to include its enrollment OTP value.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
src/Appwrite/Auth/MFA/Challenge/TOTP.php Identifies valid TOTP steps and records spent steps under an authenticator row lock.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php Spends the TOTP step used to verify authenticator enrollment.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php Checks and spends TOTP steps when completing MFA challenges.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers TOTP code reuse and updates the test helper return annotation to include its enrollment OTP.

Reviewed the commits since 8ca0c18 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit a509b9d into appwrite:main Nov 1, 2026
34 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/totp-single-use branch November 1, 2026 09:39
@ChiragAgg5k ChiragAgg5k self-assigned this Nov 2, 2026
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