Skip to content

Clear pending recovery tokens when the account email changes - #14347

Merged
abnegate merged 12 commits into
mainfrom
fix/clear-recovery-tokens-on-email-change
Oct 22, 2026
Merged

abnegate merged 12 commits into
mainfrom
fix/clear-recovery-tokens-on-email-change

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

When an account email is updated (PATCH /v1/account/email or PATCH /v1/users/:userId/email), outstanding recovery tokens stay attached to the user. Those tokens were created for the previous address, so they no longer match the current account email.

This change deletes the user's pending recovery tokens after a successful email update, along with other email-bound tokens (recovery OTP, email verification, magic URL, and email OTP). Sessions are left as they are. The user document cache is purged the same way other token deletions already do.

Test Plan

  • E2E: create a recovery token, change the account email, confirm completing recovery with the previous secret fails, then confirm a new recovery for the updated email still works.
  • Same flow through the Users API email update.
  • Existing session remains valid after the email change.

Related PRs and Issues

  • N/A

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?
Open in Web Open in Cursor 

Account and Users email updates now drop outstanding recovery,
verification, magic-URL, and email OTP tokens for that user, then
purge the user document cache. Sessions are left unchanged.

Co-authored-by: Jake Barnby <[email protected]>
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/clear-recovery-tokens-on-email-change (after).

Metric Before After Change
🚀 Requests/sec 202.84 208.47 ⚪ +2.8%
⏱️ Latency P50 83.8 ms 83.62 ms ⚪ -0.2%
⏱️ Latency P95 195.79 ms 191.21 ms ⚪ -2.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 83.62 191.21 12,996 208.47 -4.57
Account 156.69 287.72 684 11.34 +0.97
TablesDB 81.25 149.71 7,068 115.67 -2.67
Storage 76.44 180.91 3,420 58.16 -16.81
Functions 112.44 218.79 1,824 31.62 -9.81

Top API waits (after)

API request Max wait (ms)
account.name.update 457.76
tablesdb.rows.create 397.26
functions.delete 382.44
functions.variables.update 368.47
account.prefs.update 360.57

@abnegate
abnegate marked this pull request as ready for review October 22, 2026 00:48
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 #[Retry] attribute never re-runs failed tests (AccountCustomClientTest.php:1504) — The new tests rely on #[Retry(count: 1)] as their flakiness mitigation, but the retry mechanism never re-executes a failed test.

Both email-update endpoints now delete the user's recovery, OTP, email-verification, magic-url, and email-type tokens when the email changes, then purge the cached user document, so stale recovery links stop working after an address change. New e2e tests assert that a previously issued recovery secret returns 401 after the change and that a fresh recovery still completes with 200.

Files Change
app/controllers/api/account.php, src/Appwrite/Platform/Modules/Users/Http/Users/Email/Update.php Add token-clearing (deleteDocuments over email-anchored token types) plus cache purge to both email-update flows
tests/e2e/Services/Account/AccountCustomClientTest.php, tests/e2e/Services/Users/UsersBase.php Add e2e tests verifying stale recovery secrets fail (401) and fresh recovery still succeeds (200)

Reviewed commit: a5f1efe

@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The token cleanup and concurrent token-creation guards are consistent across both email-update paths, with E2E coverage for the observable flows.

Email updates now remove outstanding recovery, verification, magic-URL, and email OTP tokens for the user. Token creation and email updates are serialized around the user record so a token for an outdated address cannot survive a concurrent change. E2E coverage exercises both email-update routes and verifies stale tokens fail while sessions remain valid.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
app/controllers/api/account.php Make account email updates transactional, clear email-bound tokens, and guard token issuance against concurrent email changes.
src/Appwrite/Platform/Modules/Users/Http/Users/Email/Update.php Make Users API email updates transactional and clear the same email-bound token types.
tests/e2e/Services/Account/AccountCustomClientTest.php Add E2E coverage for recovery invalidation, other email-bound tokens, and session continuity.
tests/e2e/Services/Users/UsersBase.php Add E2E coverage for recovery invalidation through the Users API.

Reviewed 94b70fb · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

ghost
ghost previously requested changes Oct 22, 2026

@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 B · 1 blocking finding to address. Summary

Comment thread app/controllers/api/account.php Resolved
Comment thread app/controllers/api/account.php Resolved
cursoragent and others added 2 commits October 22, 2026 05:20
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@ghost
ghost dismissed their stale review October 22, 2026 05:24

The findings that requested changes are resolved. See the summary comment for what is still open.

@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 A · Looks good to merge. Summary

@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 A · See the inline comments. Summary

Comment thread app/controllers/api/account.php Resolved
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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 B · See the inline comments. Summary

Comment thread app/controllers/api/account.php Resolved
Comment thread app/controllers/api/account.php Resolved
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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 B · See the inline comments. Summary

@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 B · See the inline comments. Summary

Co-authored-by: Jake Barnby <[email protected]>
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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 B · See the inline comments. Summary

@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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 B · See the inline comments. Summary

Comment thread app/controllers/api/account.php Resolved
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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 A · See the inline comments. Summary

Verification, verification OTP, magic URL, and email OTP now re-read the user under the same row lock as email updates before inserting a token. A token is stored only when the address still matches.

Co-authored-by: Jake Barnby <[email protected]>
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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

@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

Co-authored-by: Jake Barnby <[email protected]>
@cursor

cursor Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@hansi-codes review

@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

@abnegate
abnegate merged commit e7dea57 into main Oct 22, 2026
36 of 37 checks passed
@abnegate
abnegate deleted the fix/clear-recovery-tokens-on-email-change branch October 22, 2026 06:57
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…kens-on-email-change

Clear pending recovery tokens when the account email changes

(cherry picked from commit e7dea57)
sarbojitrana pushed a commit to sarbojitrana/appwrite that referenced this pull request 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.

2 participants