Skip to content

Sign-in and refresh paths destroy sessions they shouldn't #396

Closed

Description

@fresh55

A few more session bugs I hit while working on #394. Kept them out of that PR to keep it small. All on current master.

  1. The 2-arg RefreshToken destroys whatever session is current (Client.cs L710-L743)

    It takes any tokens you hand it, but on InvalidRefreshToken it destroys the current session and its persisted copy, whoever the tokens belonged to. On success it overwrites CurrentSession the same way. fix(gotrue,supabase): session persistence end to end #394 guards the parameterless overload against this, not this one. It's the path SignIn(SignInType.RefreshToken, ...) takes.

  2. SetSession destroys the session before it validates (Client.cs L542-L549)

    SetSession("", "") or a malformed JWT signs the user out and wipes the stored session on its way to throwing. The destroy should come after validation.

  3. The refresh timer can re-arm at zero after a success (TokenRefresh.cs L144-L163)

    fix(gotrue,supabase): session persistence end to end #394 adds a 30 second floor after a failed refresh, but the success path has no floor, so a 200 with expires_in of 0 or missing (or MaximumRefreshWaitTime = 0) reschedules immediately and never stops. About 24k requests in 1.2 seconds against a mock server.

  4. Most sign-in entry points destroy the session up front (Client.cs L174)

    SignUp, SignIn(Provider), SignInAnonymously, both SignInWithOtp, both SignInWithSSO, SignInWithIdToken, both VerifyOTP, VerifyTokenHash, and SetSession above. Password sign-in, ExchangeCodeForSession and GetSessionFromUrl don't do this. On a cold start that's a SignedOut and a persistence DestroySession for a session that was never there. If the sign-in then fails, the session the user already had is gone too.

  5. SignOut fires SignedOut twice (Client.cs L462-L473)

    Once from UpdateSession(null) and once on its own, so a persistence handler gets two DestroySession calls.

  6. A rejected refresh can still sign out the user that replaced it (Client.cs L831-L844)

    fix(gotrue,supabase): session persistence end to end #394 makes the destroy conditional on the rejected token still being the current one, but the check, the destroy and the SignedOut run one after another with nothing holding them together. A sign-in landing in between gets destroyed, and even after the clear the late SignedOut still reaches the persistence listener, which wipes whatever session is stored by then and stops its refresh timer. The listeners react to the event, not to which session it was about, so the fix is ordering the notification against replacement, not a bigger lock.

One caveat: for 1, 2 and 4 the destroy is what the interface docs promise (IGotrueClient spells it out for SetSession and the 2-arg refresh, and the failed-signup tests assert it), so fixing them is a contract change, not just a bug fix.

The fixes for 1, 4 and 6 lean on the replaced-session guard from #394

edit: added 6, found in a later review pass on #394

Activity

  1. changed the title [-]sign-in and refresh paths destroy sessions they shouldn't[/-] [+]Sign-in and refresh paths destroy sessions they shouldn't[/+] on Aug 30, 2026
  2. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions