Skip to content

fix(auth): revoke a user's other reset links when the password is reset - #418

Merged
omegascorp merged 2 commits into
modelence:mainfrom
Mohil-Ahuja:fix/revoke-reset-tokens-on-reset
Oct 27, 2026
Merged

omegascorp merged 2 commits into
modelence:mainfrom
Mohil-Ahuja:fix/revoke-reset-tokens-on-reset

Conversation

@Mohil-Ahuja

@Mohil-Ahuja Mohil-Ahuja commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Every sendResetPasswordToken request inserts a new reset token without revoking earlier ones, and each stays valid for an hour. handleResetPassword consumes 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, handleResetPassword deletes 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:

  • a successful reset deletes the user's other reset tokens
  • a reset that loses the race for the token (already claimed) throws and does not delete them

npm run lint:check, npm run format:check, npm run test (116 files, 1388 tests) and tsc --noEmit pass.


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, handleResetPassword now 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.ts cover successful revocation, revocation when a later step errors, and no deleteMany when 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.

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@omegascorp
omegascorp merged commit b7a462b into modelence:main Oct 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants