Lesson 5 of 7
Password reset
Listen to an AI summary of Password reset
The five steps of a reset
A website lets a person who forgets a password choose a new password, which is called a password reset. Sam, a customer, has forgotten the password for the sports shop website. The shop stores a hash of each password. Nobody can work the password out from the hash, so the shop has no password to send to Sam.
Sam clicks Forgot your password on the sign-in page, types an email address and presses Send. The shop has to know that the person who chooses the new password can read Sam's email.
A reset token is a long random string that the shop puts in a link and emails to Sam. The token works once and proves that the shop sent the link.
1.Make
The shop makes a reset token for Sam's account.
2.Store
The shop stores a hash of the token, and the time.
3.Send
The shop emails Sam a link with the token.
4.Check
Sam clicks the link. The shop checks the token and the age of the token.
5.Use up
Sam types a new password. The shop saves the password and deletes the token.
The reset link
The email to Sam has the link in the picture.
https://shop.example/reset?token=Xk9fQ2mZ7cRt
- shop.examplethe host name
- Xk9fQ2mZ7cRtthe reset token
The host name is the name of the computer a website runs on, such as shop.example. When Sam clicks the link, the browser sends the token to that host name.
Three properties of a reset token
A reviewer checks three properties of a reset token.
- Random
- The shop makes the token from a secure random number. An attacker can guess a token made from an account number and the time.
- Used once
- The shop deletes the token in step 5. A second click on the same link does nothing.
- Soon out of date
- The token stops working after a short time, such as 30 minutes.
The shop stores a hash of the token and not the token. A thief with a copy of the shop's data gets no working links. In step 4 the shop works out the hash of the token in the link and compares the two hashes.
What is a weak password reset
A weak password reset (CWE-640) is a reset that an attacker can use to take over the account of another person.
A reset link built from the request
When Sam presses Send, the browser sends the shop's server a request for a reset. Three lines of Python then run on the server. Look at what line 2 joins into the link.
Line 1 uses secrets.token_urlsafe, Python's function for a random string that nobody can predict. Line 2 joins four strings with + to build the link. The second string is request.headers["Host"], which reads the Host header of the request.
The Host header names the website that the request is for. Sam's browser writes Host: shop.example, so line 2 builds a link to the shop.
The sender of a request writes every line of the request. An attacker can send a request from a program, with no browser, and write any Host header.
How an attacker uses the link
An attacker owns the website evil.example. The attacker sends the shop a request for a reset, with Sam's email address and the header Host: evil.example. The shop makes a real token for Sam's account, and line 2 builds the link in the picture.
https://evil.example/reset?token=Xk9fQ2mZ7cRt
- evil.examplethe host name, from the attacker's request
- Xk9fQ2mZ7cRta real token for Sam's account
The email comes from the real shop, so Sam may click the link. Sam's browser then sends the token to evil.example, the attacker's website. The attacker uses the token on the real shop and chooses a new password for Sam's account.
The safer link
The programmer writes the address of the shop in the code. No part of the request reaches the link:
SHOP_ADDRESS = "https://shop.example" link = SHOP_ADDRESS + "/reset?token=" + token
The link now starts with https://shop.example for every request.
The reply and the email
The shop shows the same reply for every email address, such as If the address has an account, we have sent an email. A different reply, or a slower one, for an unknown address tells an attacker which people have an account.
The email goes to the address stored on the account, and never to an address from the request. The email holds no password.
The shop sets a rate limit, a cap on how many resets can be asked for in a set time. With no rate limit, an attacker can make the shop send Sam thousands of emails.
After the reset
The reset does not sign Sam in. Sam signs in with the new password. The shop also ends every session on the account, so any browser still signed in as Sam is signed out.
Examples of what to look for
- A reset token built from an account number, an email address or the time.
- A reset token stored in the database as the token and not as a hash.
- A reset token with no time limit, or one that still works after the new password is saved.
- A reset link built from the
Hostheader, such asrequest.headers["Host"]in Python. - A different reply, or a slower reply, for an email address with no account.
- A reset email sent to an address read from the request, or with a password in the email.
- A page that sends a reset email with no rate limit.
- A reset that signs the person in, or leaves the other sessions on the account open.
Check your understanding
Name three properties of a reset token.
Show the answer
A reset token is random, is used once and is soon out of date.
Which line of the sports shop example builds the reset link from a header of the request?
Show the answer
Line 2, which joins
request.headers["Host"]into the link.Give one reason why the shop shows the same reply whether or not an email address has an account.
Show the answer
A different reply for an unknown address tells an attacker which people have an account.
Practise it
Challenges with a weakness this lesson teaches.
- Password resetGradebook, BeginnerOpen