Password reset tokens
A reset token is a password that expires. It has to be unguessable, stored only as a hash, bound to one account, single-use, and delivered on a channel you already trust.
Diagram
From a reset request to a single-use token
Step 1 of 4
Request
Someone asks to reset an account. You do not reveal whether the account exists.
# Someone asked to reset one account. # The request is not itself the reset.
A short or sequential value is stored in the clear.
It works until someone notices.
The request chooses where it is sent.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Unguessable and hashed
- What fails
- A reset token is a bearer secret. A timestamp, a user id, or a short number is not a token. The database should store a hash so a copied table is not a pile of live resets.
- Who is affected
- The account holder, if anyone who can read the table can reset them.
- What you would notice
- Reset rows that look like sequential integers or plaintext tokens.
- What you change
- Draw at least 32 random bytes. Store only the hash. Compare the hash of what the person presents.
Bound, brief, and single-use
- What fails
- A token that works for any account, or twice, or next week, is a second password with worse storage.
- Who is affected
- Whoever can be signed in if the token leaks from a log or a mailbox.
- What you would notice
- Expiry measured in days, no account id on the row, or a token that still works after a successful reset.
- What you change
- Bind the hash to the account, expire in minutes, and delete the row on use and on a new request.
Where it is sent
- What fails
- The request must not choose the destination. Otherwise the reset is an account takeover with extra steps.
- Who is affected
- The person who loses the account, and the new mailbox that receives the token.
- What you would notice
- A form field for “send the reset here” that overrides the stored address.
- What you change
- Send only to the address or device already on the account. Changing that address is a separate authenticated flow.
After the reset
- What fails
- A reset that does not revoke existing sessions leaves the previous holder signed in.
- Who is affected
- The person who reset, if a stolen session survives, and the previous holder of a stolen account if you revoke.
- What you would notice
- Sessions and refresh tokens that are not touched by the reset handler.
- What you change
- Revoke sessions and refresh tokens for the account when the password changes. Regenerate the id on the new login.
A reset token is a bearer secret
Whoever presents the token gets to set the password. That is the definition of a bearer secret, so the rules are the same as a session: unguessable, not stored in the clear, not written into logs, not placed in a referrer. Use a cryptographic random generator. Thirty-two bytes encoded for a URL is a reasonable size. The account id plus a timestamp is not a token. It is a username with a clock.
Store a hash of the token. When the person submits it, hash what they submitted and look up that hash. A database copy then does not contain live tokens. Delete the row when it is used, when it expires, and when a newer reset is requested so old mail is dead.
Bind it to the account and to a few minutes
The row has to say which account it resets. A token that only proves “someone requested a reset” and then trusts an account id in the form is an account takeover with a coupon code. Check the hash and the account together. Expire in minutes. A week-long reset link is a week-long password sitting in a mailbox and in every system that archives mail.
Use the same response whether or not the account exists, so the form is not a directory. Send the message only when the account exists, and send it only to the address you already stored. A field on the form that says where to send the reset is the bug. Changing the address is an authenticated action with its own token and its own delay.
Revoke what the old password could do
After a successful reset, revoke existing sessions and refresh tokens. The person who knew the old password, or who stole a session, should not remain inside. Then regenerate the session id for the person who just proved they hold the mailbox. Do not log the token, the new password, or the reset URL. Support does not need to “read the link back.” Support uses the recovery flow you designed, which should not be a shared inbox of live tokens.
You would notice sequential tokens, plaintext columns, expiry in days, a destination taken from the request, and a reset handler that does not touch the session table. The person affected is the account holder. The finding lists those properties. It does not include a working token.
What you change
Implement the hashed, bound, short-lived, single-use token. Point delivery at the stored address only. Revoke sessions on success. Add tests for expiry, reuse, the wrong account, and a request that tries to choose a new address. None of those tests need a real mailbox. A fake sender that records the destination is enough.
If you also reset by SMS or by a support chat, the same properties apply, and the channel has to be one the account already enrolled. A chat that accepts a new phone number and a story is the help-desk failure, not a backup for a weak token. Pair this page with the callback-verification lesson.
Check yourself
Recognition and the fix. Nothing here is a procedure.
Reset tokens are the account id plus the current hour, stored in the clear, and they work for a week. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.