Your 'forgot password' link: how to build it without a back door
A password reset page becomes a way to take over accounts if the link can be guessed, keeps working, or is built from the Host header. The OWASP fix: one message for everyone, a long random single-use token that expires, and a fixed domain in the link.

A user clicked "forgot password" and got an email with a link. For a while, that link is stronger than the password itself: whoever holds it changes the password and takes the account. If the link can be guessed, stays valid for a week, or points to a foreign domain, you have a back door in your site. This article builds the page in order, the way OWASP describes it.
What should the page say when someone enters an email?
A "forgot password" page should return the same message whether the account exists or not, and in about the same time. Otherwise the page becomes a tool that tells an attacker who has an account with you.
The OWASP Forgot Password Cheat Sheet asks for one message for both cases and a response that takes a consistent amount of time, because the time difference alone reveals accounts. The fix is to follow the same code path in both cases, or send the email in the background and answer right away.
It also asks for limits on automated requests, like a per-account rate limit or a CAPTCHA, so nobody can flood a user's inbox with reset emails.
What should the token in the link look like?
The token in the link should come from a cryptographically secure random generator, be long enough that guessing it does not work, be single use, and expire after a short time. The cheat sheet also asks you to store it securely, tied to one user.
import { randomBytes, createHash } from 'node:crypto';
const token = randomBytes(32).toString('hex');
const tokenHash = createHash('sha256').update(token).digest('hex');
await db.passwordResets.insert({
userId: user.id,
tokenHash,
expiresAt: new Date(Date.now() + 30 * 60 * 1000),
usedAt: null,
});The raw token goes only in the email, and the database keeps the hash. When the user opens the link, you hash the token and look it up, reject it if it expired or was used, and mark it used as soon as the password changes.
Why must the link not be built from the Host header?
The link must not be built from the Host header because an attacker can send a reset request for the victim's email with their own Host. If your site builds the link from it, the victim gets a link to the attacker's domain, and clicking it hands over the token.
The cheat sheet says: the domain in the link is hard-coded in your settings, or checked against a list of trusted domains, and the link uses HTTPS.
const link = `${process.env.APP_URL}/reset?token=${token}`;And the reset page itself sets Referrer-Policy to no-referrer, so the token in the URL does not leak to any other site the page loads something from.
What happens after the password changes?
After the user changes the password, the token is finished, and you send an email saying the password changed, without the password itself. The cheat sheet also advises against logging the user in automatically, and says to ask whether to close all their open sessions, or close them yourself.
The "your password changed" email matters more than it looks: if they did not change it, this is the first place they find out.
Where do you start now?
The lesson for you as a developer: a reset link is a key to the account, so treat it like a password: random, hidden, single use, and short-lived.
Try your page with an email that exists and one that does not, and compare the message and the timing.
Open the token code and confirm it uses a secure generator, expires, and gets marked as used.
Find where you build the email link, and make sure the domain comes from your settings, not the request.
Add a "your password changed" email if you do not have one.
How error messages give away existing accounts is covered in the account enumeration article. Referrer-Policy in detail is in the referrer leaks article. How to store the passwords themselves is in the Argon2id article. And if you want someone to test your reset page the way an attacker would, that is what Muhassan does.
How long does the reset link on your site stay valid?
FAQ
How long should a reset link stay valid?
A short time. The OWASP cheat sheet says the token should expire after an appropriate period and be single use, without a fixed number. Pick the shortest time that works for your users.
After the user changes the password, should I log them in right away?
No. The cheat sheet advises against logging the user in automatically, because it adds complexity to authentication and session handling. Let them sign in with the new password.
Do I store the token as it is in the database?
No. The cheat sheet asks you to store it securely, like passwords, and tie it to one user. The simple way is to store a hash of it, so a leaked table does not give working links.


