Highlights

Contents

Part V
05

Password reset

We design the account recovery flow that bypasses the password entirely. Because reset grants access without knowing the password, it is the easiest path into an account when implemented carelessly. We will build the safe eight-step flow and then study the ways real reset implementations are exploited.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Session authentication2Logout, devices andrevocation3Password storage4Login attacks anddefences5Password reset6JSON Web Tokens7Access and refresh tokens8JWT attacks and mistakes9OAuth 2.010OpenID Connect and thefull login

#Section 14The reset flow

A reset link lets someone into an account without the password, so it is a password substitute that travels by email, and it needs the same care as a password. Wren’s flow has eight steps.

First, the user submits an email address. Wren always replies “if an account exists for that address, we have sent a link”, whatever the outcome, so the form cannot be used to find out who has an account. Second, if the account exists, Wren generates a token of 32 random bytes (256 bits) from a cryptographic generator. Third, it stores only sha256(token), together with the user id. Fourth, it records an expiry 30 minutes ahead and a “used” flag. Fifth, it emails a link containing the raw token, built from a configured canonical host name, https://app.wren.example/reset?t=....

When the link is opened, the sixth step validates it. Wren hashes the presented token, finds the matching row, and checks that it is unexpired and unused. Seventh, the user chooses a new password, which Wren checks against breached-password lists and hashes with Argon2id. Eighth, Wren marks the token used, deletes any other outstanding reset tokens for that user, revokes every existing session and refresh token, and emails the user that their password changed, with a link to report it if they did not.

A SAFE PASSWORD RESET IN EIGHT STEPSA SAFE PASSWORD RESET IN EIGHT STEPS1 requestsame reply always2 random token32 bytes3 store sha256(token)4 expires in 30 min5 email linkcanonical host only6 validate: hash, expiry, unused7 set the new password hash8 burn token, end all sessions
Figure 3.23Eight steps. The stored hash means a database leak does not leak working reset links.

Storing a hash of the token follows the same logic as password hashing. If the reset table leaks, its contents cannot be turned into working links. A fast hash such as SHA-256 is fine here, unlike for passwords, because the token is 256 random bits rather than something a human chose, so there is nothing to brute-force. The 30-minute expiry limits the damage of a link sitting in a mailbox. The single-use flag stops a link found later in browser history or a forwarded email from working a second time.

Revoking sessions in the last step matters more than it looks. Users often reset their password because something is wrong, such as a strange login notification or a suspicious order. An attacker who already holds a session would otherwise stay logged in after the reset, and the user would believe they had fixed the problem.

#Section 15How reset implementations become exploitable

Four mistakes recur, and each turns the reset email into someone else’s login. The first is host header poisoning. If the application builds the link from the incoming Host header, an attacker submits a reset request for user 42’s email with Host: evil.example. User 42 receives a genuine email from Wren, correctly signed and from the right sender, whose link points at https://evil.example/reset?t=.... One click sends the token to the attacker, who then uses it on the real site. Several popular frameworks and applications have shipped exactly this bug. The fix is to build links from configuration, never from request headers, and to reject requests whose Host is not one you serve.

The second is token leakage. The token sits in a URL, so it lands in the web server’s access logs, in browser history and in any analytics tool that records page URLs. If the reset page loads third-party scripts, images or fonts, the browser may send the URL to those third parties in the Referer header. Set Referrer-Policy: no-referrer on reset pages, keep third-party content off them, scrub the t parameter from logs, and have the page exchange the URL token for a short-lived form token as soon as it loads, so the URL becomes useless.

FOUR WAYS RESET FLOWS LEAK ACCOUNTSFOUR WAYS RESET FLOWS LEAK ACCOUNTSHost header poisoninglink built from Host: evil.exampleevil.example/reset?t=…token leaksURL token reaches logs and Referert=…no expiry or reusean old email in a breached inbox workssessions surviveattacker stays logged in after reset
Figure 3.24Four common holes. Each one turns the reset email into someone else’s login.

The third is missing expiry or reuse checks. A reset email left in a mailbox that is breached months later still works, and so does a link that was already used once. The fourth is leaving existing sessions alive after a reset, which keeps the attacker logged in, as the previous section explained.

Other variants show up in security reviews. Tokens built from a timestamp, the user id or a weak random generator can be predicted. Six-digit numeric reset codes sent by SMS have only 1,000,000 values, so without strict attempt limits they can be brute-forced within their lifetime. Flows that let a user change the email address and the password in one step let an attacker lock the real owner out of recovery. Security questions such as “your first pet’s name” are guessable from social media and should not exist.