Repository navigation
fix(account): make TOTP codes single-use - #15154
Merged
Merged
Conversation
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.
🟢 Tier S · Ready to merge
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.
📂 Walkthrough · 4
Reviewed the commits since |
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.
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.
Challenge\TOTP::step()returns the step a code belongs to, or null when it is stale or already spent (data.stepon the authenticator).Challenge\TOTP::spend()re-reads the authenticatorforUpdateand records the step, inside the transaction that already claims the challenge, so concurrent redemptions of one code cannot both win.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
dataJSON.Test Plan
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 staysuser_more_factors_required, and B's challenge still accepts the next code.Related PRs and Issues
Checklist
Related: appwrite-labs/cloud#6666