Account recovery
| Type | Sign-in feature; a second way into every account |
|---|---|
| Also known as | Password reset, forgotten password flow |
| Weakness | CWE-640: Weak Password Recovery Mechanism for Forgotten Password |
| OWASP Top 10 | A07:2025 Authentication Failures |
| Typical severity | High to Critical when it allows takeover |
| Affects | Apps with email or text message password reset, recovery codes or support-desk recovery |
Account recovery returns an account to a person who has lost the means of signing in to it, such as a forgotten password or a lost phone.1 On most web apps it starts at a Forgot password link that mails a one-time link to the address on file. Anyone who completes that flow can choose a new password, so it guards the account as much as the sign-in form does, and a weak recovery path cancels out the protection of strong passwords.2
The reset link
In the common design the user types an email address, and the server creates a random token, stores it against the account and emails a link with the token in the query string, such as https://example.com/reset?token=0a1b2c3d. The link opens a form for the new password, and the token is deleted after use. The whole design rests on only the account owner being able to read that inbox.3
The token stands in for a password and is handled like one. It comes from a CSPRNG, is long enough that guessing it is out of reach, belongs to one user, works once, and is stored securely.4 NIST SP 800-63B-4 caps a recovery code sent by email at 24 hours and one sent by text message or voice call at 10 minutes.1 Django's default for a reset link, the PASSWORD_RESET_TIMEOUT setting, is 259,200 seconds, or three days.5
A reset by text message sends a PIN of 6 to 12 digits instead of a link. Typing it in opens a session that can only set the password.4
Other recovery methods
An emailed link is one of four kinds of recovery method.1
| Method | How it works |
|---|---|
| Saved recovery codes | Given to the user at enrolment and kept offline, printed or written down. At least 64 random bits each, stored as a hash. A used code stops working and a new one is issued. |
| Issued recovery codes | Sent on request to an email address, phone number or postal address the user registered. At least six digits. Valid for at most 10 minutes by text or voice, 24 hours by email, 21 or 30 days by post. |
| Recovery contacts | The code goes to a trusted person the user named, who passes it on. It may stay valid 24 hours longer. |
| Repeated identity proofing | The user proves who they are again, as when the account was first verified. |
A new recovery address counts only after the user types back a code sent to it. A user with two ways to sign in, such as a two-factor authentication app and saved codes, needs recovery less often.1 Security questions ("the name of your first pet") are weak: their answers can often be guessed or looked up, and NIST forbids prompting users to set them up when they choose a password.41
Attacks on the flow
Finding which accounts exist
A form that says "no account with that email" for one address and "check your inbox" for another tells an attacker which addresses are registered: Account enumeration. A difference in response time leaks the same thing, so the server takes the same path in both cases instead of returning early.4 Supabase's resetPasswordForEmail() returns without an error and sends nothing when no user has the address.6 Django's PasswordResetView behaves the same way.7
Reset poisoning
When the server builds the link from the Host header of the request, the attacker chooses the domain in the email. They request a reset for the victim and change that header:
POST /forgot-password HTTP/1.1
Host: attacker.example
email=anna@example.comAnna gets a real email from the site with a valid token, but the link leads to attacker.example. A click hands the token to the attacker, who then uses it on the real site, and a mail scanner that follows links can hand it over without any click.3 The base URL for reset links belongs in configuration, or the Host value is checked against the site's own domains.4 In Django that list is ALLOWED_HOSTS: when the Host header matches none of its entries, request.get_host() raises SuspiciousOperation.5 Other abuses of the header are in Host header attacks.
Leaked and guessed tokens
A token in a URL travels wherever the URL goes. The reset page sends Referrer-Policy: no-referrer so that the links and files on it do not pass the address to other sites.4 Short codes need a rate limit on attempts, or they fall to a Brute-force attack.4
Flooding and lockout
With no limit per account, a script can send one person thousands of reset emails an hour.4 A reset request changes nothing on the account until a valid token comes back. Locking the account at the request stage would let anyone who knows a username shut its owner out (see Account lockout).4
GitLab, January 2024
On 11 January 2024 GitLab disclosed CVE-2023-7028, scored 10.0 on the CVSS scale. In GitLab Community and Enterprise Edition from version 16.1, password reset emails could be delivered to an email address that had never been verified.8 Version 16.1.0 had added resets through a secondary email address, and the bug sat in the verification of those addresses. The fix made the reset form refuse more than one email address. Accounts with two-factor sign-in could have their password reset but could not be taken over, because sign-in still asked for the second factor.8 On 1 May 2024 CISA added the flaw to its Known Exploited Vulnerabilities catalog, on evidence that it was being exploited.9
After the reset
The owner gets an email saying the password was changed, and that email never contains the password.4 The user then signs in through the normal page; signing them in at once adds session code where mistakes are common. Existing sessions are ended, or the user is asked whether to end them.4 Every recovery triggers a notification, so that a fraudulent one gets noticed.1
When an attacker may already hold the account, a new password is not enough: they can keep a live session, or may have changed the recovery email, phone number or two-factor method. Recently changed recovery details are not trusted alone, and afterwards every session and every unused reset link or code is cancelled.4 The steps for a lost administrator account are in Responding to an administrator account takeover.
Supabase and Django
In Supabase the reset request names the page the link opens, and that address must be on the project's list of redirect URLs:
await supabase.auth.resetPasswordForEmail('anna@example.com', {
redirectTo: 'https://example.com/account/update-password',
})That page should be open only to signed-in users, and it sets the new password with updateUser.6 Django's built-in reset views check links with PasswordResetTokenGenerator7 and take the link lifetime from PASSWORD_RESET_TIMEOUT.5
Prompt for a coding agent
Review the password reset flow in this project. Give the same message and take the same code path whether or not the email has an account. Build reset links from a base URL in configuration, never from the Host header. Make tokens random, hashed in storage, single use and valid for at most 24 hours. Rate limit reset requests and code attempts per account. After a reset, email the user and end their other sessions. List what you changed and anything you were unsure about.
In SecFlash
A reset token that leaks through the address shows up on the Security page as SF-SEC-638 (sensitive data in a URL), SF-SEC-656 (such an address sent to another site in the Referer header) and SF-SEC-107 (no Referrer-Policy). SF-EXP-208 on the Exposure page reports an API with no rate limit, which leaves short reset codes open to guessing.
See also
- Authentication
- Multi-factor authentication
- Passkeys and WebAuthn
- Account enumeration
- Host header attacks
- Account lockout
- Session management
- Cross-site request forgery
- Securing Rails, Django and Laravel applications
- Category: Authentication
References
- a b c d e f "NIST SP 800-63B-4, Digital Identity Guidelines: Authentication and Authenticator Management". NIST.
- back MITRE. "CWE-640: Weak Password Recovery Mechanism for Forgotten Password". CWE 5.0.
- a b PortSwigger. "Password reset poisoning". Web Security Academy.
- a b c d e f g h i j k l OWASP Cheat Sheet Series. "Forgot Password Cheat Sheet". OWASP.
- a b c Django Software Foundation. "Settings". Django 5.2 documentation.
- a b Supabase. "Password-based Auth". Supabase Docs.
- a b Django Software Foundation. "Using the Django authentication system". Django 5.2 documentation.
- a b GitLab (11 January 2024). "GitLab Critical Security Release: 16.7.2, 16.6.4, 16.5.6". GitLab blog.
- back CISA (1 May 2024). "CISA Adds One Known Exploited Vulnerability to Catalog". cisa.gov.
External links
- Account Recovery, SP 800-63B-4, NIST
- PasswordResetView, Django documentation