Repository navigation
fix(auth): revoke a user's other reset links when the password is reset - #418
Conversation
omegascorp
left a comment
There was a problem hiding this comment.
Fix looks correct and closes a real gap in the reset lockout. The deleteMany({ userId }) matches what clearTokens in deleteUser.ts already does. A few non-blocking suggestions inline.
| // token without revoking earlier ones, so a link sent before this reset (valid for | ||
| // up to 1 hour) would otherwise let whoever holds it set a new password again — | ||
| // undoing the lockout below. | ||
| await resetPasswordTokensCollection.deleteMany({ userId: userDoc._id }); |
There was a problem hiding this comment.
Suggestion: move this up to right after the token claim (after the if (!claimedToken) check). As written it runs after the email-verify update and invalidateAllUserSessions, so if either of those throws, the password has already changed but the other reset links survive, which is the hole this PR closes. Revoking right after the claim ties "claim this link, revoke the rest" together. The behaviour on a rejected password or a lost race stays the same.
There was a problem hiding this comment.
Done in 88b0be5. It now runs right after the if (!claimedToken) check, before the password update, and there is a new test where invalidateAllUserSessions throws and the other links are still revoked.
| // Revoke the user's other outstanding reset links too. Each request issues a new | ||
| // token without revoking earlier ones, so a link sent before this reset (valid for | ||
| // up to 1 hour) would otherwise let whoever holds it set a new password again — | ||
| // undoing the lockout below. |
There was a problem hiding this comment.
Nit: "the lockout below" is slightly off. Part of the lockout (session invalidation) runs above this line and part (magic links) below. Maybe "undoing this lockout"?
There was a problem hiding this comment.
With the revocation moved up, the session invalidation and the magic link revocation both come after it, so "the lockout below" is accurate now. I kept it, but happy to change it if you prefer.
| expect(mockResetTokensDeleteMany).toHaveBeenCalledWith({ userId }); | ||
| }); | ||
|
|
||
| test('does not revoke reset tokens when the token was already claimed', async () => { |
There was a problem hiding this comment.
The PR description says a rejected password also leaves the other links alone, but only the lost-race case is tested. Could you add expect(mockResetTokensDeleteMany).not.toHaveBeenCalled() to one of the existing password-policy rejection tests to cover that too?
There was a problem hiding this comment.
Added expect(mockResetTokensDeleteMany).not.toHaveBeenCalled() to the password-policy rejection test.
Revoke them before the password and session updates, so a failure in a later step cannot leave earlier links valid after the password changed. Also cover the rejected-password case, which leaves the other links alone.
Problem
Every
sendResetPasswordTokenrequest inserts a new reset token without revoking earlier ones, and each stays valid for an hour.handleResetPasswordconsumes only the token it was given. So after a successful reset, any other reset link the user was sent in the last hour still works, and whoever holds one can set a new password again.The reset already treats itself as a lockout: it invalidates every session and revokes all outstanding magic link tokens and codes, with the comment that otherwise a link issued before the reset "would still log an attacker back in — bypassing the lockout". Other reset links are the same kind of credential and were the one thing left out. The scenario is someone who briefly had access to the inbox requesting a reset link of their own; the owner resets their password, and the earlier link still lets the other person reset it again within the hour.
Change
After the token is claimed and the password updated,
handleResetPassworddeletes the user's remaining reset tokens (resetPasswordTokensCollection.deleteMany({ userId })), next to the existing session and magic link revocation. It runs only after the commit point, so a rejected password or an already-claimed token leaves the other links alone, matching how the token itself is only consumed on success.Tests
In
src/auth/resetPassword.test.ts:npm run lint:check,npm run format:check,npm run test(116 files, 1388 tests) andtsc --noEmitpass.Note
Medium Risk
Touches the password-reset commit path in auth, but the change only tightens security by invalidating stale reset credentials and is covered by new tests.
Overview
Closes a gap in the password-reset lockout: after a reset link is successfully claimed,
handleResetPasswordnow deletes every remaining reset token for that user (resetPasswordTokensCollection.deleteMany({ userId })), in the same spirit as invalidating sessions and revoking magic links.The wipe runs immediately after the atomic token claim (before the password write), so an older link issued within the hour cannot be reused to set a new password, and other links are cleared even if a later step (e.g. session invalidation) fails. Failed validation, unclaimed tokens, and lost races do not trigger bulk deletion.
Tests in
resetPassword.test.tscover successful revocation, revocation when a later step errors, and nodeleteManywhen the token was already claimed or policy rejected the password.Reviewed by Cursor Bugbot for commit 88b0be5. Bugbot is set up for automated code reviews on this repo. Configure here.