Pattern: Forgot password, password change and email change
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Numbers are example values.
- Core ideas: reset codes are short-lived and single-use, the answer never reveals whether an account exists, every change notifies the owner, and an email change can be undone from the old address.
- Related: sign-up, passwords and login, sessions.
1. Problem
Recovery flows are the usual way into accounts that have strong passwords. Attackers abuse them to learn which accounts exist, to guess codes, and to quietly change the email so the real owner is locked out.
2. Options
| Method | Upside | Downside |
|---|---|---|
| Reset link (URL token) | Fastest and simplest (OWASP) | Link scanners and mail previews can touch it (practitioner experience); the reset page needs a no-referrer policy (OWASP) |
| Reset code (6 digits) | Works across apps; scanners cannot use it | Needs attempt limits |
| Security questions | None worth the risk | Guessable or researchable; OWASP advises against relying on them (see its security-questions sheet) |
| Support-assisted recovery | Handles lost email access | Slow; social-engineering target; needs a strict identity process |
3. Recommended design
Forgot password
- Send a single-use code to the registered email: example 6 digits, 15 minutes, limited attempts. OWASP requires tokens that are random from a cryptographically safe source, long enough against brute force, stored securely, single use and expiring.
- Consume the code with a conditional update (“mark used only if unused”), so two simultaneous requests cannot both use it. See PostgreSQL locking documentation for the pattern.
- Same answer for unregistered addresses. Pad the response time to a fixed minimum so timing does not reveal the difference (OWASP: consistent message and uniform time; use asynchronous sending or the same logic path).
- Limits: per address; how often mail goes to a registered address; total attempts per account.
- A user who forgot which email they used may type a username: the code goes to that account’s registered address, and the screen does not show the address.
- After a successful reset: all sessions end, all outstanding codes for that account become invalid, and the new password follows the normal rules. The new-password screen does not display the email. Notify the owner, as the OWASP sheet advises (notify through registered addresses with instructions for reporting unauthorized recovery).
- Do not change the account (for example lock it) before a valid token is presented; otherwise anyone can lock someone else by typing their email.
Password change (user is signed in)
- Require the current password; limit re-verification attempts like login.
- Write conditionally. Other devices’ sessions end.
- Send a “your password was changed” email. A missing notification is a common gap; add it.
Email change
- Current password, then a code to the new address, change only after the code is verified. OWASP email guidance: re-authenticate, notify the existing address, confirm the new one.
- Block disposable domains here too; recheck canonical uniqueness under lock; daily request limit and a slow rhythm for actual changes (example: once per 14 days); end other sessions.
- Notify the old address with an undo link (example valid 7 days). If an attacker took the account and changed the email, the real owner can reverse it. Opening the link (GET) changes nothing; a confirmation (POST) does. Undo restores the old email, sets the password to a random value (the attacker cannot sign in with the password they know; the owner uses “forgot password”), and ends all sessions. This design is practitioner experience; OWASP only requires notifying the old address.
- If the new address already belongs to someone else, the verification step behaves like any other (no existence leak).
4. Scenarios and edge cases
- Attacker types a victim’s email many times: per-address and per-account limits stop mail flooding; the answer stays identical.
- Owner no longer has the old email: support process outside the app with strict identity checks, logged and approved by two people.
- User changes email, attacker uses the undo link later: link expires; undo only works from the original address’s mail.
- Reset requested twice: the newer code replaces the older one.
5. Common mistakes
- Different message for known and unknown email.
- Locking the account when someone merely requests a reset.
- Reset codes that never expire or can be reused.
- No notification after password or email changes.
- Undo link that changes data on GET.
- Showing the full email on the reset screen.
6. How to audit
- What: uniform answer for unknown emails.
- How to check: request a reset for a real and a made-up email; compare body, status and time.
- Evidence: both responses and timings.
- What: codes are single use and expire.
- How to check: use a code twice; use one after its lifetime.
- Evidence: rejection responses.
- What: sessions end after reset and change.
- How to check: keep a session open, reset the password, call the API.
- Evidence: 401 response.
- What: notifications are sent.
- How to check: change password and email in a test account.
- Evidence: the received emails.
Sources
- Forgot Password Cheat Sheet — OWASP Cheat Sheet Series
- Authentication Cheat Sheet — OWASP Cheat Sheet Series
- Email Validation and Verification Cheat Sheet — OWASP Cheat Sheet Series
- NIST SP 800-63B-4: Authentication and Authenticator Management — NIST
- Session Management Cheat Sheet — OWASP Cheat Sheet Series
- PostgreSQL: Explicit Locking — PostgreSQL Global Development Group
- Application Security Verification Standard (ASVS) — OWASP