Every application with passwords has a second authentication system hiding behind the “forgot password” link. It skips the password check and usually the MFA check, and it is guarded by whatever the developer wrote on a Thursday afternoon. When I review an app, the reset flow is the first place I look, because it is almost always weaker than the login page next to it.
What the reset flow really is

The diagram shows the reset flow described below and where each safeguard sits.
A reset flow is a way to prove you control a mailbox, and then trade that proof for a credential. That means the email account is effectively a password for every account that resets through it. You cannot make the reset stronger than the inbox. What you can do is make sure the flow itself adds no new weaknesses, and there are four places where it usually does: the token, the request page, the link, and what happens after the password changes.
Token design
The token has to be unguessable, short-lived and single use. Unguessable means output from a CSPRNG with at least 128 bits of entropy. Not a timestamp, not a hash of the user ID and the time, not rand(). I have seen all three. Sequential or time-seeded tokens are the classic path to takeover, because an attacker can request a reset for their own account, observe the pattern, then request one for the victim and predict the value.
Store only a hash of the token. If the database leaks, live reset tokens in plain text are as good as live passwords. Because the token is already 128 bits of random data, a fast hash like SHA-256 is fine here. You do not need bcrypt or Argon2id for high-entropy secrets, and a salt adds nothing.
import hashlib, secrets, time
def issue_reset_token(db, user_id):
raw = secrets.token_urlsafe(32) # 256 bits from os.urandom
digest = hashlib.sha256(raw.encode()).hexdigest()
db.execute(
"INSERT INTO reset_tokens (user_id, token_hash, expires_at) VALUES (?, ?, ?)",
(user_id, digest, int(time.time()) + 30 * 60),
)
return raw # goes in the email, never in the DB
def redeem_reset_token(db, raw):
digest = hashlib.sha256(raw.encode()).hexdigest()
row = db.execute(
"DELETE FROM reset_tokens WHERE token_hash = ? AND expires_at > ? RETURNING user_id",
(digest, int(time.time())),
).fetchone()
return row[0] if row else None # one atomic step: single use
The DELETE with RETURNING matters. If you SELECT, then check, then UPDATE, two concurrent requests can both redeem the same token. Do it in one statement. Also invalidate older tokens when a new one is issued, otherwise a user who clicked “send again” three times has three live tokens.
Thirty minutes is a reasonable lifetime. OWASP’s Forgot Password Cheat Sheet recommends short expiry without picking one number, and I would rather have a user request a second email than leave a valid token sitting in an inbox for a day.
Enumeration on the request page
The request form asks for an email address. If the response says “no account with that address”, you have given anyone a free account-existence oracle. The fix is to return the same message for both cases: “If that address has an account, we have sent a link.”
Writing the message is the easy part. Timing is where people slip. If the known-user path queries the database, builds an email and talks to an SMTP server, while the unknown-user path returns immediately, the difference shows up clearly over the network. Queue the email send in the background for both cases, and keep the synchronous work identical. Rate limit per address and per source IP, since the form is also an email bombing tool aimed at your users.
The limit of this is worth stating. If your sign-up form says “that email is already registered”, the enumeration protection on the reset page does nothing useful. Either fix both or accept that the account list is not secret and put your effort elsewhere.
The link itself
The classic bug here is password reset poisoning. The app builds the link from the request’s Host header, or from X-Forwarded-Host, so an attacker submits a reset for the victim’s address with a Host header they control. The victim gets a genuine email from your domain, with a link to the attacker’s server, and the token arrives in the attacker’s access log when the victim clicks. WordPress had exactly this as CVE-2017-8295, where versions through 4.7.4 used the SERVER_NAME value when building the reset email. The fix is boring: take the base URL from configuration, never from the request.
The token in the URL has other leaks. It lands in browser history, in proxy logs, and in the Referer header if the reset page loads any third party resource. Two habits cover most of it. Send Referrer-Policy: no-referrer on the reset page, and have the page exchange the token for a short-lived server-side session on first load, then redirect to a clean URL. Do not load analytics scripts on that page at all.
Also check that the token is bound to the user it was issued to. I have found flows where the reset form carried a hidden user_id field next to the token, and changing it reset someone else’s password with my own valid token.
After the password changes
Most teams never write this step. A reset should do four things, in the same transaction:
1. delete every outstanding reset token for the user
2. revoke all existing sessions and remember-me cookies
3. revoke refresh tokens and API tokens that depend on the password
4. send a "your password was changed" email to the old address
Session revocation is the part that actually evicts the attacker. If an attacker has a live session and the owner resets the password, the attacker should be thrown out. Without it, the reset fixes nothing for a user who is already compromised. Do not log the user in automatically after a reset either, unless you also enforce MFA at that moment. If the reset skips the second factor, the reset flow has become the MFA bypass. NIST SP 800-63B makes the same point from the other side: recovery must not be a weaker route than the authenticator it replaces.
The notification email matters because it is the only signal the real owner gets. Make sure it also goes out when the account email changes, with a link that can undo the change for a few days. Attackers who get a session will often change the email first and the password second.
What I test
My checklist for any reset flow is short. Request ten tokens and look at them for structure. Redeem one twice. Redeem one after requesting a newer one. Send a poisoned Host header and an X-Forwarded-Host header and read the email. Compare response times for real and fake addresses over a few hundred requests. Reset a password while a second session is open and see whether that session survives. Most apps fail at least two of these.
None of it is clever and none of it needs a framework. What it needs is someone who treats the reset path as the authentication system it is.

Leave a Reply
You must be logged in to post a comment.