AleannLab

Weak Password Reset: The Side Door Into Every Account

AleannLab team3 min read

Password reset is a second way to log in, and it is usually weaker than the first. A safe flow uses a long random token that is stored hashed, expires within an hour, works once, and is tied to one account. The reset link is built from a configured URL, the response never reveals whether an email is registered, and changing the email address needs proof that the current owner agrees.

On this page
  1. How reset flows fail
  2. A flow that holds
  3. Around the flow
  4. Test yours

If you use Supabase Auth, Clerk, Auth0 or Firebase, most of this is done for you, and your job is to check the settings. If the reset flow was written by hand or generated, check every point below.

How reset flows fail

Guessable tokens. A six-digit code with unlimited attempts. A token made from Date.now(), Math.random(), the user ID or an MD5 of the email. All of these can be predicted or brute-forced.

Tokens that live forever. A link from last year's email still works. Mailboxes get compromised and old emails get forwarded.

Tokens that work twice. The link is not invalidated after use, so anyone who sees it later (browser history, a shared computer, a proxy log) can use it again.

The token is not tied to the account. The reset endpoint takes { token, email, newPassword }, checks that the token exists, and changes the password for whatever email was sent.

The link is built from the request. The classic host header attack:

TypeScript
const link = `https://${req.headers.host}/reset?token=${token}`;

An attacker requests a reset for the victim while sending Host: evil.example. Your app emails the victim a real token inside a link to the attacker's site. One click delivers the token.

The token is returned in the response. A leftover from development: return Response.json({ ok: true, token }).

User enumeration. "No account with that email" versus "Check your inbox" tells anyone which addresses are registered.

Email change without confirmation. If a logged-in session can change the account email with no password and no confirmation, a stolen session or an XSS becomes a permanent takeover: change the email, then reset the password.

A flow that holds

Create the token:

TypeScript
import { randomBytes, createHash } from 'node:crypto';

const token = randomBytes(32).toString('base64url');           // goes in the email
const tokenHash = createHash('sha256').update(token).digest('hex');

await db.passwordReset.create({
  data: { userId: user.id, tokenHash, expiresAt: new Date(Date.now() + 30 * 60 * 1000) },
});

const link = `${process.env.APP_URL}/reset-password?token=${token}`;  // configured, not from the request

Only the hash is stored. A leaked database does not contain usable reset links.

Answer the same way whether or not the account exists:

TypeScript
return Response.json({ message: 'If an account exists for that address, we sent a reset link.' });

Redeem it:

TypeScript
const tokenHash = createHash('sha256').update(token).digest('hex');
const reset = await db.passwordReset.findFirst({
  where: { tokenHash, usedAt: null, expiresAt: { gt: new Date() } },
});
if (!reset) return Response.json({ error: 'This link is invalid or has expired.' }, { status: 400 });

await db.$transaction([
  db.user.update({ where: { id: reset.userId }, data: { passwordHash: await hash(newPassword) } }),
  db.passwordReset.updateMany({ where: { userId: reset.userId }, data: { usedAt: new Date() } }),
  db.session.deleteMany({ where: { userId: reset.userId } }),      // sign out everywhere
]);

The account comes from the token record, never from the request. All outstanding tokens for the user are closed, and existing sessions end, so an attacker who was already in is thrown out.

Then send a notification: "Your password was changed. If this was not you, contact us."

Around the flow

  • Rate limit reset requests per email and per IP. See no rate limiting.
  • Keep tokens out of logs and analytics. A token in the URL is recorded by anything that logs URLs. Exchange it on page load, set Referrer-Policy: no-referrer on the reset page, and load no third-party scripts there.
  • Email change: require the current password, send a confirmation link to the new address and a notice with an undo link to the old one.
  • Hash passwords with argon2id or bcrypt. Never SHA-256 or MD5 on their own.
  • Magic links and OTP codes are the same mechanism and need the same properties: random, short-lived, single-use, attempt-limited.

Test yours

  1. Request two resets. Does the first link still work after the second is issued, or after the password is changed?
  2. Use a link twice.
  3. Wait past the expiry and try it.
  4. Request a reset with a changed Host header and look at the link in the email:
bash
curl -X POST https://yourapp.com/api/auth/forgot-password \
  -H 'Host: evil.example' -H 'Content-Type: application/json' -d '{"email":"you@example.com"}'
  1. Compare the responses for a registered and an unregistered email, including response time.
  2. After a reset, check that old sessions on another browser are signed out.

Password reset is check 4 in our security checklist for vibe-coded apps. For login problems that are bugs, not attacks, see debugging auth in a vibe-coded app. If you want your whole auth flow reviewed, that is part of our app rescue and security audit.

All articles →

Ready to discuss your project with us?

idea
*
*
*
I'm interested in...
Web Development
App Development
UX/UI Design
DevOps
Artificial Intelligence And Machine Learning
QA Testing
Mobile Development
Consultation
OutSource
Project budget (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Upload file