Repository navigation
fix(db): anon could execute as a superuser, and a rebuild would restore that #2707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
pmoves/supabase/migrations/20260824000000_revoke_secdef_public_anon.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| -- Revoke PUBLIC and anon EXECUTE on SECURITY DEFINER functions in `public`. | ||
| -- | ||
| -- Why this exists as a migration rather than a one-off: the grants were never | ||
| -- authored. `20260325110000_publisher_publish_state.sql` and both 20260326* | ||
| -- migrations contain ZERO GRANT statements. anon/authenticated/service_role | ||
| -- EXECUTE comes from Supabase's stock default privileges on `public`, so a | ||
| -- volume reset or a fresh node re-grants it and the hardening silently | ||
| -- disappears. Applied by hand on B850 2026-08-23; this makes it reproducible. | ||
| -- | ||
| -- What the grants actually meant: | ||
| -- claim_/complete_/fail_studio_board_publish are SECURITY DEFINER and owned | ||
| -- by supabase_admin (rolsuper), so they execute AS a superuser and bypass | ||
| -- RLS. Meanwhile the schema's own policies say: | ||
| -- publisher_audit_svc service_role USING true WITH CHECK true | ||
| -- publisher_audit_auth_read authenticated read-only | ||
| -- publisher_audit_anon_deny anon USING false WITH CHECK false | ||
| -- and detections / segments / emotions / studio_board have RLS enabled with | ||
| -- NO anon policy at all. So anon is explicitly denied everywhere it is | ||
| -- named -- and held EXECUTE on three functions that write studio_board as a | ||
| -- superuser regardless. | ||
| -- | ||
| -- Why no consumer breaks: the publisher (services/publisher + services/common/ | ||
| -- supabase.py) does direct inserts into those RLS-protected tables, which an | ||
| -- anon-key client cannot do. It must be service_role, and service_role keeps | ||
| -- its explicit grant. authenticated keeps its own. | ||
| -- | ||
| -- PUBLIC is revoked for the same reason one level up: every role inherits | ||
| -- PUBLIC, which is how the scoped juicefs_meta role -- a LOGIN role that | ||
| -- pg_hba now admits from the tailnet -- ended up holding superuser-execution | ||
| -- rights it was created specifically not to have. | ||
| -- | ||
| -- Idempotent: REVOKE on an already-revoked grantee is a no-op. | ||
|
|
||
| DO $$ | ||
| DECLARE f record; | ||
| BEGIN | ||
| FOR f IN | ||
| SELECT p.oid::regprocedure AS sig | ||
| FROM pg_proc p | ||
| JOIN pg_namespace n ON n.oid = p.pronamespace | ||
| WHERE n.nspname = 'public' AND p.prosecdef | ||
| LOOP | ||
| EXECUTE format('REVOKE EXECUTE ON FUNCTION %s FROM PUBLIC', f.sig); | ||
| EXECUTE format('REVOKE EXECUTE ON FUNCTION %s FROM anon', f.sig); | ||
| END LOOP; | ||
| END $$; | ||
|
|
||
| -- Assert the intended end state rather than trusting the loop. | ||
| DO $$ | ||
| DECLARE n int; r text; | ||
| BEGIN | ||
| SELECT count(*) INTO n | ||
| FROM pg_proc p JOIN pg_namespace ns ON ns.oid = p.pronamespace | ||
| WHERE ns.nspname = 'public' AND p.prosecdef | ||
| AND (has_function_privilege('anon', p.oid, 'EXECUTE') | ||
| OR has_function_privilege('juicefs_meta', p.oid, 'EXECUTE')); | ||
| IF n <> 0 THEN | ||
| RAISE EXCEPTION 'revoke incomplete: % secdef function(s) still reachable by anon or juicefs_meta', n; | ||
| END IF; | ||
|
|
||
| FOREACH r IN ARRAY ARRAY['service_role','authenticated'] LOOP | ||
| IF EXISTS ( | ||
| SELECT 1 FROM pg_proc p JOIN pg_namespace ns ON ns.oid = p.pronamespace | ||
| WHERE ns.nspname = 'public' AND p.prosecdef | ||
| AND NOT has_function_privilege(r, p.oid, 'EXECUTE') | ||
| ) THEN | ||
| RAISE EXCEPTION 'over-revoked: % lost EXECUTE on a secdef function it needs', r; | ||
| END IF; | ||
| END LOOP; | ||
| END $$; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.