Repository navigation
fix: admin Authorization header gets overwritten by user token after sign_in_with_password - #1406
AdityaMedidala wants to merge 1 commit into
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 👍🏾 |
|
Hi, I’d like to work on this issue. Is it still open |
|
Thanks for this @AdityaMedidala, and sorry it sat for so long. The bug is real and your diagnosis was right. I reproduced it against a local Supabase instance before touching anything: I also confirmed your patch fixes it end to end. I've opened #1645 with the same fix moved one layer down, into - self._headers = headers
+ self._headers = dict(headers)The reason: the aliasing is created inside Three smaller things I found while reviewing this branch, in case they're useful to you generally:
I'm leaving this open rather than closing it — that's the maintainers' call, and #1645 links back here. Your finding is credited in that PR. |
…1645) Supersedes #1406. Same bug, fixed one layer down. Credit to @AdityaMedidala for finding and diagnosing it in #1404 / #1406. ## The bug `GoTrueBaseAPI.__init__` stored the caller's dict by reference: ```python self._headers = headers ``` and `GoTrueClient.__init__` hands the admin API its own `_headers`: ```python self.admin = AsyncGoTrueAdminAPI(url=..., headers=self._headers, ...) ``` So `client._headers is client.admin._headers`. Any in-place rewrite of the client's `Authorization` header rewrote the admin one too. The `supabase` wrapper does exactly that on every auth event: ```python # supabase/_async/client.py, _listen_to_auth_events self.auth._headers["Authorization"] = auth_header # the user's token ``` Result: a client built with the `service_role` key loses admin rights the moment a user signs in. ## Reproduced against a local Supabase instance ``` admin.list_users() before sign-in : OK admin.list_users() after sign-in : AuthApiError: User not allowed admin/_client share one dict : True ``` After this change: ``` admin.list_users() before sign-in : OK admin.list_users() after sign-in : OK admin hdr is service_role : True admin/_client share one dict : False ``` ## Why here and not in the wrapper #1406 fixes it in `supabase/_async/client.py` by copying the admin dict and re-asserting the key on every auth event. That works, but it leaves the sharp edge in place: anyone constructing `GoTrueClient` or `GoTrueAdminAPI` directly still gets the aliased dict, and any future in-place header write reintroduces the bug. Copying on the way in is one line, needs no wrapper changes at all, and makes in-place mutation safe by construction. I verified the wrapper needs no edit: `_listen_to_auth_events` is the only in-place `_headers` mutation in either package, and the wrapper already updates `options.headers`, postgrest and storage explicitly, so nothing relied on the sharing. ## Tests Four new tests in `tests/_async/test_admin_headers.py`, generated to `_sync`. Both cover the changed line: the client/admin aliasing, and a caller that keeps mutating the dict it passed in. All four fail without the fix and pass with it. ## Verification Every package, locally, against real infra (gotrue v2.187, PostgREST v11.2.2 and v16.2, storage-api v1.72.1): | package | tests | mypy | |---|---|---| | auth | 168 passed (+4) | clean | | supabase | 30 passed (unchanged) | clean | | functions | 65 passed | clean | | postgrest | 306 passed | clean | | storage | 116 passed | clean | | realtime | 31 passed | clean | `ruff check` and `ruff format --check` both clean. `_sync` regenerated with `make auth.build-sync`.
|
Superseded by #1645 |
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