Skip to content

fix(auth): re-read session after the request in signOut and updateUser - #2866

Open
snwchd71 wants to merge 1 commit into
supabase:masterfrom
snwchd71:fix/auth-keep-newer-session
Open

snwchd71 wants to merge 1 commit into
supabase:masterfrom
snwchd71:fix/auth-keep-newer-session

Conversation

@snwchd71

Copy link
Copy Markdown

🔍 Description

I ran into these while building sign-in for a React Native app with an async storage adapter. signOut() with scope global and updateUser() both read the session, wait for their request, and then write to storage without checking that storage still holds the session they started with. It looks like the same class as #2731, just in other calls.

What changed?

signOut() with scope global now reads storage again after /logout answers, and skips _removeSession() when the stored session belongs to another user. It compares the sub claim of the two access tokens, because the user object can live in userStorage. A token refresh of the same session during the wait still ends signed out.

updateUser() now reads storage again after PUT /user answers and puts the new user onto the session that storage holds at that point, so a refresh that finished during the wait keeps its tokens. When storage is empty or holds another user's session, it saves nothing and fires no USER_UPDATED, but it still returns the updated user.

That is 40 lines added and 5 removed in src/GoTrueClient.ts, plus one new test file, test/GoTrueClient.sessionReplaced.test.ts.

Why was this change needed?

I found three cases. The first two I see on both 2.117.2 and 2.119.0, the third one I found while writing the tests.

  1. signOut() with the default scope global waits for POST /logout and then calls _removeSession(), which removes whatever is in storage at that point. If a signInWithPassword() for another account finishes while the logout request is still open, the new session gets removed and the user ends up signed out. The events I see are SIGNED_IN (A), SIGNED_IN (B), SIGNED_OUT.
  2. updateUser() waits for PUT /user and then saves the session object it read at the start. If a token refresh happened in between, the old refresh token is written back over the new one. If another account signed in in between, the first account's session is written over the second.
  3. updateUser() also writes the session back when a signOut({ scope: 'local' }) finished during the wait, so the user is signed in again.

As far as I can tell the lock does not cover this, because the client only takes a lock when the caller passes lock in the options. The default client has none, and the built-in guard (_sessionRemovalEpoch and the commit guard in _callRefreshToken) only covers token refresh.

To reproduce I used the real client with a Map as storage and a stubbed fetch that holds the /logout or /user response until the second call is done:

  1. Sign in as A.
  2. Call signOut() or updateUser() and hold the response.
  3. Sign in as B, or let a token refresh finish, or sign out locally.
  4. Release the response and read the session from storage.

🔄 Breaking changes

  • This PR contains no breaking changes

📋 Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run pnpm nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

📝 Additional notes

The new file has 5 tests that use a mock fetch and hold the /logout or /user response open. 4 of them fail without the change. The fifth one (a refresh during a global sign-out still signs out) passes with and without it, and is there to pin that behaviour. I ran these and the other auth suites that don't need the test server locally and they pass (300 tests). I did not run the suites that need the Docker test server.

This does not close the window completely. Storage is async, so there is still a gap between the new read and the write. It went from the length of a network request to the length of one storage read.

#2759 is open in the same area for #2731. This change does not touch the code that one rewrites, and its diff still applies on top of this branch.

One thing I was not sure about: comparing sub means a new sign-in for the same user during a global sign-out is still removed. I left it like that because global revokes that user's sessions anyway, but I can compare the session_id claim instead if you prefer.

signOut (scope global) and updateUser wrote to storage after their request
returned, without checking that storage still held the session they started
with. A sign-in for another user during the wait was removed by signOut, and
updateUser wrote its old session back over a refreshed one, over another
user's session, or after a local sign-out.

signOut now skips the removal when the stored session has another sub.
updateUser puts the updated user onto the session that storage holds, and
saves nothing when storage is empty or holds another user's session.
@snwchd71
snwchd71 requested review from a team as code owners November 16, 2026 08:10
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Updated user details now preserve refreshed authentication tokens and no longer overwrite a newer sign-in or restore a session that has been signed out.
    • Signing out no longer removes a different user’s active session when account changes occur while the request is in progress. Existing behavior for the same session remains unchanged.

Global sign-out now re-reads storage after the server request and removes the session only if it does not belong to another user. User updates also re-read storage after the request. They update the stored session and emit USER_UPDATED only when storage contains a valid session for the same user. Tests cover concurrent sign-in, refresh, and local sign-out cases.

Possibly related PRs

  • supabase/supabase-js#2786: Both changes define when global sign-out removes local session data relative to the server response.

Suggested labels: auth-js

Change: Bug fix

Merge Risk: Minimal · up to a6b44

This change stops a delayed sign-out or user-update response from overwriting a newer session in storage, for example a different user who signed in while the request was pending. No merge-blocking issue was found. A small race between the storage read and write remains, and it was already present before this change.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: de89bf41-ccf4-409c-911b-2435489ae6ac
Commits

Reviewing files that changed from the base of the PR and between 13211d9 and a6b44dc.

Files selected for processing: 2

  • packages/core/auth-js/src/GoTrueClient.ts
  • packages/core/auth-js/test/GoTrueClient.sessionReplaced.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant