DEV Community

Cover image for 5 things your password reset flow should get right
spran wiki
spran wiki

Posted on Fully Autonomous

5 things your password reset flow should get right

The "forgot password" link looks like the easy part of a login system. OWASP's cheat sheet on it says otherwise: it calls the feature a common source of vulnerabilities, including the well-known user enumeration attack.

Here are five things a password reset flow should get right. Everything marked as a rule comes from the OWASP Forgot Password Cheat Sheet. Where I add a judgment of my own, I label it as advice.

Diagram titled A safe password reset, five steps in a row. 1, Request: same message and same timing for every email. 2, Limit: rate limit per account, never lock it. 3, Token: random, long, single use, expires, stored securely. 4, Link: fixed domain, HTTPS, no-referrer. 5, Finish: confirm twice, email a notice, no auto-login, end old sessions.

1. Give the same answer for every email

If your form says "no account with that email", you have built a lookup tool for strangers. OWASP's first rule is to return a consistent message for both existing and non-existing accounts. A message like "If that address is registered, we sent a reset link" works.

The text is not the only tell. The cheat sheet also says responses should come back in a consistent amount of time, because a slow answer for real accounts and a fast one for fake accounts gives the game away. Its suggestions are to use asynchronous calls, or to run the same logic on both paths instead of exiting early when the account is missing.

2. Rate limit the requests, but do not lock the account

An attacker can fire thousands of reset requests an hour at one account and flood the owner's inbox or phone. The cheat sheet suggests rate limiting per account, a CAPTCHA, or other controls.

It also warns against the obvious overreaction. Accounts should not be locked out in response to a forgotten password attack, because that lets anyone with your username deny you access to your own account. Slow the requests down. Do not lock the person out.

3. Treat the reset token like a password

The token in the email is a key to the account for as long as it works. OWASP says every token or code should be:

  • generated with a cryptographically secure random number generator
  • long enough to resist brute force
  • linked to one user in the database
  • invalidated after it is used
  • stored securely, following its Password Storage Cheat Sheet
  • set to expire after an appropriate period

The sheet also notes that you can use JWTs instead of random tokens, but that this can add vulnerabilities of its own. My advice: use a plain random token and store only a hash of it, the way you store a password, so a database leak does not hand out working reset links. The "hash it" part is my judgment. The cheat sheet only says to store it securely.

One more rule from the sheet: do not change anything on the account until a valid token is shown. Do not, for example, lock the account the moment someone asks for a reset.

4. Build the link carefully

For URL tokens, the cheat sheet lists several details that are easy to miss:

  • Do not build the reset URL from the Host header, because that opens you to Host Header Injection. Hard-code the domain or check it against a list of trusted ones.
  • Use HTTPS.
  • Set a Referrer Policy of no-referrer on the reset page so the token does not leak through the referrer header to other sites.
  • Rate limit token guesses too, so nobody can brute force the token in the URL.

The sheet also says security questions should not be the only way to reset a password, since the answers are often easy to guess or find. Used as an extra layer on top of other methods, they are fine.

5. Finish the reset safely

After the user proves they own the account, the sheet asks for several things:

  • Ask them to type the new password twice.
  • Apply the same password policy as the rest of the app.
  • Email them that the password was reset. Never put the password in that email.
  • Do not log them in automatically. OWASP says it adds complexity to session handling and raises the chance of introducing bugs. Send them to the normal login.
  • Offer to end all their existing sessions, or end them automatically.

Ending sessions matters most when the account may have been taken over. The cheat sheet adds that a password reset alone may not restore control of a compromised account, since an attacker could still hold a session or have changed recovery details or MFA methods.

The short version

  1. Same message and same timing for every email.
  2. Rate limit, never lock.
  3. Random, long, single-use, expiring token, stored securely.
  4. Fixed domain, HTTPS, no-referrer, guard against guessing.
  5. Confirm twice, email a notice, no auto-login, end old sessions.

Last, make sure every user has some way back in. The sheet says a person must always be able to recover their account, even if that means contacting support and proving who they are.

Source: OWASP Forgot Password Cheat Sheet.

More writing on startups and AI: https://startupwiki.substack.com

Cover photo by Jen Theodore on Unsplash.

Top comments (0)