fix: correct password reset endpoint query logic#250
Merged
Conversation
The original code tried to use a compound where clause with usedAt: null which Prisma cannot handle reliably. Changed to: 1. Find token by tokenHash alone 2. Verify usedAt is null explicitly 3. Use a transaction to atomically mark token as used and update password This fixes the 'Invalid or expired reset token' error that was preventing password resets from working. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Password reset was broken—users received 'Invalid or expired reset token' errors even with valid tokens.
Root Cause
prisma.passwordResetToken.update({ where: { tokenHash, usedAt: null } })which fails silently because Prisma cannot filter onnullin composite unique clauses.usedAt === nullcheck still happened outside the transaction. Two concurrent requests with the same token would both pass the guard, both enter the transaction, and both successfully reset the password (TOCTOU vulnerability).Solution
usedAt: nullguard inside the transaction usingupdateMany(which supports arbitrary WHERE clauses, not just unique constraints)updateManyto atomically mark token as used AND validate it hasn't been used yetcount > 0(i.e., token was successfully marked used by this request)Changed Files
server/src/routes/auth.ts(lines 129–166)Fixes #233
Code Review
This PR passed code review by Sonnet 4.6, which identified and approved the race condition fix. The
updateManypattern is the correct way to handle conditional atomic writes in Prisma when using non-unique WHERE clauses.Testing