feat(supabase): add bypassAuthSession option for service-role clients #2081
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🔍 Description
This PR adds a new
global.bypassAuthSessionoption toSupabaseClientto fix an issue where service-role clients would incorrectly use user session tokens instead of the service-role key when queryingsecurity_invokerviews.What changed?
bypassAuthSession?: booleanoption toSupabaseClientOptions.global_getAccessToken()method inSupabaseClientto checkbypassAuthSessionbefore retrieving session tokensWhy was this change needed?
When using a service-role key with
security_invoker = trueviews or functions, PostgREST runs queries with the caller's permissions. The issue occurred because_getAccessToken()would prioritize the user's session token over the service-role key when both existed.This caused permission errors when:
security_invokerviews that accessed restricted schemas (likeauth.users)The requests would run with the
authenticatedrole (from the session token) instead of theservice_role(from the API key), resulting in "permission denied" errors even though the client was configured with full admin privileges.Closes #2045
Test Coverage
All 4 new tests pass successfully:
✅ Uses supabaseKey when bypassAuthSession: true even with active session
✅ Uses session token when bypassAuthSession: false (default behavior)
✅ Passes correct token to fetchWithAuth wrapper
✅ Stores bypassAuthSession value correctly
Alternative approaches considered:
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.