Repository navigation
Conversation
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.
|
No actionable comments were generated in the recent review. Summary
Change: Bug fix Merge Risk:
|
🔍 Description
I ran into these while building sign-in for a React Native app with an async storage adapter.
signOut()with scopeglobalandupdateUser()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 scopeglobalnow reads storage again after/logoutanswers, and skips_removeSession()when the stored session belongs to another user. It compares thesubclaim of the two access tokens, because the user object can live inuserStorage. A token refresh of the same session during the wait still ends signed out.updateUser()now reads storage again afterPUT /useranswers 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 noUSER_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.
signOut()with the default scopeglobalwaits forPOST /logoutand then calls_removeSession(), which removes whatever is in storage at that point. If asignInWithPassword()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 areSIGNED_IN(A),SIGNED_IN(B),SIGNED_OUT.updateUser()waits forPUT /userand 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.updateUser()also writes the session back when asignOut({ 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
lockin the options. The default client has none, and the built-in guard (_sessionRemovalEpochand the commit guard in_callRefreshToken) only covers token refresh.To reproduce I used the real client with a
Mapas storage and a stubbedfetchthat holds the/logoutor/userresponse until the second call is done:signOut()orupdateUser()and hold the response.🔄 Breaking changes
📋 Checklist
<type>(<scope>): <description>pnpm nx formatto ensure consistent code formatting📝 Additional notes
The new file has 5 tests that use a mock
fetchand hold the/logoutor/userresponse 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
submeans a new sign-in for the same user during a global sign-out is still removed. I left it like that becauseglobalrevokes that user's sessions anyway, but I can compare thesession_idclaim instead if you prefer.