You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prevent stale password resets from consuming replacement tokens - #633
Fixes a race where an in-progress password reset or failed reset-link send could delete a newer reset token belonging to the same user.
Password reset tokens are stored under a key derived from the user ID. Previously, reset and failed-send cleanup removed that key without checking whether its value was still the token entry the request had validated or created.
Explained Simply
Concrete gains from this correction:
Users keep working links in a real failure case. If an email send stalls and later fails, cleanup used to delete whatever link was newest — one the user may already have received. They'd click their fresh link and get "invalid token" for no visible reason. Now a failed send deletes only its own token. This is the everyday case, and it needs nobody malicious.
The one-time-use rule can't be bypassed by timing. One token can no longer authorize two password changes, and a leftover request can no longer spend someone else's newer link. The only person this ever constrained was an attacker who already held a valid token and got lucky with timing — a narrow, unproven-in-practice scenario, as we discussed.
Third-party cache stores fail safely. A custom cache backend that can't compare-and-delete atomically now refuses to process resets with a clear error, rather than silently keeping the old race.
Password reset race
A stale reset could complete this sequence:
Two requests validate token A.
One request consumes A and updates the password.
A new reset link stores token B under the same user key.
The other request resumes after hashing its password.
It deletes the key, consuming B, and updates the password using its earlier validation of A.
This allowed token A to authorize two password updates while invalidating the unused replacement token B.
A related race existed in send_reset_link_with. If one send failed after another request stored and sent a newer token, cleanup from the failed send deleted the newer token.
Atomic token consumption
Adds Store::compare_and_forget, which removes a cache entry only when its serialized value still matches the expected value.
The built-in stores implement this atomically:
The memory store compares and removes while holding its values mutex.
Redis uses a Lua script that compares and deletes in one operation.
The database store uses a conditional delete matching the key, value, and unexpired lifetime.
Lazy and failover stores delegate to the selected store.
The method fails by default because implementing it as get followed by forget would preserve the same race for custom stores. Custom stores must implement atomic comparison and removal before they can support password resets; otherwise resets fail closed without changing the password.
Password reset behavior
Passwords::reset now retains the cache entry it validated and removes that exact entry before updating the password.
If another request replaces the entry while password hashing is in progress:
the stale request receives the existing invalid-token response;
the replacement token remains stored;
the stale request does not update the password.
Failed-send cleanup also removes only the entry created by that send attempt, preserving any newer reset link.
The existing behavior remains unchanged when no replacement occurs: concurrent requests using one token still produce exactly one successful reset.
Security
A validated token cannot be consumed after its cache entry has been replaced.
A stale reset cannot delete a newer token.
A failed send cannot delete a newer successfully sent token.
Unsupported custom stores fail closed rather than falling back to a non-atomic comparison.
Wrong, expired, missing, and already consumed entries remain unusable.
The existing resend-window race between simultaneous reset-link requests is unchanged and outside the scope of this PR.
Testing
Authentication integration tests cover:
two requests racing to consume one token;
a reset paused after reading token A while another request consumes A and issues token B;
rejection of the stale reset without changing the password;
continued usability of replacement token B;
failed-send cleanup racing with a newer successfully sent link;
refusal to reset through a custom store without atomic token consumption.
Cache tests cover:
matching and mismatched values;
missing and expired entries;
exactly one winner when two Redis consumers remove the same entry;
memory, SQLite database, and Redis implementations;
failure from a custom store without compare_and_forget.
Confirmed. Passwords keeps one reset token per user under a key derived from the user's ID, and both reset and the failed-send cleanup delete that key without checking it still holds the token they read. A reset that validated token A and is still hashing the password can delete a newer token B that was issued after another request used A, and still update the password. A send that stalls for more than a minute and then fails deletes the newer link the user asked for in the meantime, so that link reports an invalid token. Both of the PR's race tests fail on 0.x exactly this way. The window is narrow in real applications: the password hash time for the first case, and a send_reset_link_with callback that blocks past the resend window for the second.
The fix is in the right place: the bad delete is the root cause, and an atomic compare-and-delete is the primitive that fixes it. Reusing compare_and_swap or a lock would need a tombstone value or lock support from the store, so neither is simpler. The memory, database, and Redis implementations pass the store contract against SQLite, PostgreSQL, MySQL, and Redis 7.
This adds public API for Taylor to decide on: a defaulted Store::compare_and_forget that custom store authors implement, and a public Repository::compare_and_forget, while Repository::compare_and_swap stays crate-private.
Breaking for custom stores: a store that doesn't implement compare_and_forget now fails every password reset with a 500, where it worked before. The custom drivers section of docs/cache.md should list it next to compare_and_swap, so store authors know resets need it.
No changes pushed.
Thanks for your contribution, a human review will follow shortly.
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
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.
Summary
Fixes a race where an in-progress password reset or failed reset-link send could delete a newer reset token belonging to the same user.
Password reset tokens are stored under a key derived from the user ID. Previously, reset and failed-send cleanup removed that key without checking whether its value was still the token entry the request had validated or created.
Explained Simply
Concrete gains from this correction:
Password reset race
A stale reset could complete this sequence:
This allowed token A to authorize two password updates while invalidating the unused replacement token B.
A related race existed in
send_reset_link_with. If one send failed after another request stored and sent a newer token, cleanup from the failed send deleted the newer token.Atomic token consumption
Adds
Store::compare_and_forget, which removes a cache entry only when its serialized value still matches the expected value.The built-in stores implement this atomically:
The method fails by default because implementing it as
getfollowed byforgetwould preserve the same race for custom stores. Custom stores must implement atomic comparison and removal before they can support password resets; otherwise resets fail closed without changing the password.Password reset behavior
Passwords::resetnow retains the cache entry it validated and removes that exact entry before updating the password.If another request replaces the entry while password hashing is in progress:
Failed-send cleanup also removes only the entry created by that send attempt, preserving any newer reset link.
The existing behavior remains unchanged when no replacement occurs: concurrent requests using one token still produce exactly one successful reset.
Security
Testing
Authentication integration tests cover:
Cache tests cover:
compare_and_forget.Validation completed:
cargo test -p laravel-rust --test http— 643 passed, 1 ignoredcargo fmt --all --checkcargo clippy --all-targets --all-features -- -D warningsgit diff --check