Repository navigation
fix(account): claim an email verification token under the user row lock [appwrite:appwrite:alert#4642] - #15278
Conversation
Both email confirmation handlers validated the secret against an unlocked user snapshot and then wrote emailVerification outside any transaction, discarding the token delete's return value. An email change takes the user row lock, stores the new address with emailVerification false and deletes every verification token precisely so a token sent to the old address cannot prove the new one; committing in that window left the new, unproven address marked verified, which defeats the users/verified role and the account-claim guard that only revokes credentials on unverified addresses. Mirror the phone handler: lock the row with forUpdate inside withTransaction, require deleteDocument on the token to succeed, and only then set the flag. Cover both routes in e2e with concurrent confirmations of one token, which previously all returned 200. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Review complete. No blocking issues — approved ✅; 3 nitpicks below. 🧹 Nitpicks (3) — 🟢 3 low
This PR hardens email verification (both token-link and OTP flows) against concurrent token reuse by claiming the verification token inside a database transaction before it is validated, throwing
Reviewed commit: a9ff51e |
🟢 Tier S · Ready to merge
The email-link and email-OTP confirmation handlers now claim their verification token inside a transaction while holding the user-row lock, matching the email-change locking path. Added end-to-end tests verify that concurrent attempts to consume either kind of token yield exactly one success.
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…user-lock fix(account): claim an email verification token under the user row lock [appwrite:appwrite:alert#4642] (cherry picked from commit 6150d5b)
Closes code scanning alert #4642.
Problem
Both email verification confirmation handlers validated the submitted secret against an unlocked user snapshot and then wrote
emailVerification => trueoutside any transaction, discarding the token delete's return value:Verifications/Email/Update.php— read at:104withauthorization->skip(getDocument(...))(noforUpdate, no transaction),tokenVerify()against that snapshot at:110, sparse flag write at:118,deleteDocumentreturn value ignored at:128.Verifications/Email/OTP/Update.php— the identical sequence.Account/Email/Update.phpis the writer they race. InsidewithTransactionit locks the row withforUpdate, persists the newemailwithemailVerificationfalse, and deletes everyTOKEN_TYPE_VERIFICATION/TOKEN_TYPE_VERIFICATION_OTPtoken — precisely so a token sent to the old address cannot prove the new one. Because the confirmation side held no lock, that transaction committing between the confirmation's read and its write madeDatabase::updateDocument()merge the sparse{'emailVerification': true}onto the row's current email, marking the new, unproven address verified.A verified flag on an address the account holder does not own defeats every feature that trusts it: the
users/verifiedpermission role, and the account-claim guard —Account/Base.php:203,Sessions/IdToken/Create.php:437andSessions/OAuth2/Redirect/Get.php:449only callUser::revokeCredentials()whenemailVerificationis false, so the real owner later signing in with a verified OAuth/ID-token identity lands in the pre-existing account with the attacker's password, MFA and sessions intact.The sibling handlers already do this correctly:
Verifications/Phone/Update.php:99-107andRecovery/OTP/Update.php. The two email handlers were the outliers.Fix
Mirror the phone handler in both: inside
withTransaction, lock the user row withforUpdate, requiredeleteDocument('tokens', ...)to succeed (otherwiseUSER_INVALID_TOKEN), and only then write the sparseemailVerificationupdate. An email change now either commits first — leaving no token to claim, so the confirmation is rejected — or waits behind the confirmation and resets the flag to false with the new address. The phone handler is unchanged.The now-contradictory "we don't need it anymore" docblock above the delete is dropped: the delete's result is exactly what the handler needs.
Tests
Two e2e regressions in
AccountCustomClientTest, modelled on the existingtestUpdateAccountRecoveryConcurrently: fire 8 concurrent confirmations of one verification token (link route and OTP route) and assert exactly one 200 with seven 401s.Before the fix:
After the fix:
OK (2 tests, 41 assertions).🤖 Generated with Claude Code