Skip to content

Prevent stale password resets from consuming replacement tokens - #633

Open
AlextheYounga wants to merge 2 commits into
laravel:0.xfrom
AlextheYounga:fix/password-reset-token-race
Open

AlextheYounga wants to merge 2 commits into
laravel:0.xfrom
AlextheYounga:fix/password-reset-token-race

Conversation

@AlextheYounga

@AlextheYounga AlextheYounga commented Dec 3, 2026 •

Copy link
Copy Markdown

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:

  1. 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.
  2. 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.
  3. 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:

  1. Two requests validate token A.
  2. One request consumes A and updates the password.
  3. A new reset link stores token B under the same user key.
  4. The other request resumes after hashing its password.
  5. 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.

Validation completed:

  • cargo test -p laravel-rust --test http — 643 passed, 1 ignored
  • Memory and SQLite cache contracts passed
  • Redis 7 cache contract passed
  • cargo fmt --all --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • git diff --check

Copy link
Copy Markdown
Member

This is an automated, agent-powered review.

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.

@taylorotwell taylorotwell added bug p3 Priority 3: lowest labels Dec 3, 2026 — with Amp for GitHub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug p3 Priority 3: lowest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants