Skip to content

fix(supabase): keep the session token on every auth event that has one - #1671

Closed
Shub3am wants to merge 3 commits into
supabase:mainfrom
Shub3am:fix/auth-events-keep-session-token
Closed

Shub3am wants to merge 3 commits into
supabase:mainfrom
Shub3am:fix/auth-events-keep-session-token

Conversation

@Shub3am

@Shub3am Shub3am commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix, closes #1670

What is the current behavior?

_listen_to_auth_events only uses the session token for SIGNED_IN / TOKEN_REFRESHED. On USER_UPDATED, MFA_CHALLENGE_VERIFIED and PASSWORD_RECOVERY it sets Authorization to the anon key, so after update_user() or an MFA verify, storage, functions, realtime and any fresh postgrest client run as anon. The aal2 token from MFA never gets applied.

What is the new behavior?

Every event uses session.access_token when there's a session and the anon key when there isn't (only SIGNED_OUT), and the cached postgrest/storage/functions clients are always reset so they pick up the new header. That's the same thing supabase-js ends up doing.

Tests: parametrized the existing test_updates_the_authorization_header_on_auth_events (async + sync) over every event that carries a session, touched postgrest before the event so the cached client gets checked too, and added a functions header check. On main the USER_UPDATED, MFA_CHALLENGE_VERIFIED and PASSWORD_RECOVERY cases fail with the anon key; with the change they all pass.

Additional context

Checked against a local supabase start stack with RLS tables, before and after:

# main
update_user -> notes=[]  client=anon/- storage=anon/- functions=anon/-
mfa verify  -> secrets=[]  client=anon/- storage=anon/- functions=anon/-

# this branch
update_user -> notes=[{'body': 'my note'}]  client=authenticated/aal1 storage=authenticated/aal1 functions=authenticated/aal1
mfa verify  -> secrets=[{'body': 'my secret'}]  client=authenticated/aal2 storage=authenticated/aal2 functions=authenticated/aal2

(same output for the sync client)

cc @olirice @silentworks for review

USER_UPDATED, MFA_CHALLENGE_VERIFIED and PASSWORD_RECOVERY all carry the
current session, but the listener only used it for SIGNED_IN and
TOKEN_REFRESHED. On any other event it set Authorization to the anon key,
so storage, functions, realtime and any postgrest client built afterwards
ran as anon, and the aal2 token from an MFA verify was never applied.
Pins the other half of the auth event change: once every event with a
session uses its token, SIGNED_OUT (no session) has to drop back to the
anon key on the client, auth, postgrest, storage and functions headers.

@Gaurav-Kanse Gaurav-Kanse left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled this branch locally and reviewed the auth event flow and tests.
I traced PASSWORD_RECOVERY through gotrue_client.py: it's emitted from the same code path as SIGNED_IN, passing the same session object (_notify_all_subscribers("SIGNED_IN", session) / _notify_all_subscribers("PASSWORD_RECOVERY", session)). So the PR's logic of treating any event with a session identically is consistent with the current auth event implementation.
Ran the updated regression tests locally:

  • sync: 6/6 passed
  • async: 6/6 passed
    I also ran the full test_client.py suites for both clients — 19 passed, 6 xfailed, with no failures.
    The fix itself looks clean: replacing the per-event allowlist with a simple session-based check means newly introduced session-bearing events won't silently fall back to the anon key like USER_UPDATED and MFA_CHALLENGE_VERIFIED did before.
    Thanks for the fix!

@Shub3am

Shub3am commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks a lot for the thorough review @Gaurav-Kanse, and for tracing PASSWORD_RECOVERY through the gotrue client!

@o-santi could you take a look when you get a chance? The branch is up to date with main, and the CI workflows are waiting on approval since it's from a fork. A couple of people have started asking to pick up #1670, so it'd be great to get this in and close that out. Happy to change anything you'd like.

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.

Client falls back to the anon key after update_user() or an MFA verify

3 participants