fix: admin Authorization header gets overwritten by user token after sign_in_with_password#1406
Conversation
…e on auth state change
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Central YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe pull request fixes a regression in the authentication module where the admin authorization header was being overwritten by user session tokens during authentication events. The fix ensures that when a user signs in, the admin headers are explicitly maintained to use the service role key (supabase_key). Changes are applied to both sync and async client implementations, with a regression test added to prevent future occurrences. Assessment against linked issues
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
|
Hey @leoortizz and @rylena tagging you as recent contributors to this file. This fixes #1404 . Happy to adjust anything if the approach doesn't fit the project direction. Thanks |
(#1406) appears to directly fix issue #1404 (#1404):
So the PR looks good to me 👍🏾 |
Fixes #1404
What was happening
After calling
sign_in_with_password()on a client, any subsequent call toclient.auth.admin.create_user()raised "User not allowed" — even when the client was created with a valid service role key.The root cause:
auth.adminwas initialised withheaders=self._headers, which is the same dict object asauth._headers. So when_listen_to_auth_eventsupdatedauth._headers["Authorization"]with theuser's session JWT,
admin._headerssaw the exact same mutation — becausethey were literally the same dict in memory. Admin endpoints then received the user token instead of the service role key, and Supabase correctly rejected the request.
The fix
One line added in
__init__of both_sync/client.pyand_async/client.py, beforeon_auth_state_changeis registered:This gives
adminits own independent copy of the headers dict at construction time, so auth state changes can never bleed into admin headers.Testing
test_admin_authorization_header_not_overwritten_on_auth_eventstotests/_sync/test_client.py