diff --git a/.github/workflows/publish-nix-pgupgrade-scripts.yml b/.github/workflows/publish-nix-pgupgrade-scripts.yml index 2a95fe09d5..0d47015c69 100644 --- a/.github/workflows/publish-nix-pgupgrade-scripts.yml +++ b/.github/workflows/publish-nix-pgupgrade-scripts.yml @@ -8,6 +8,7 @@ on: paths: - .github/workflows/publish-nix-pgupgrade-scripts.yml - ansible/files/admin_api_scripts/pg_upgrade_scripts/** + - migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql - ansible/postgres_release.yml - ansible/vars.yml workflow_dispatch: @@ -84,6 +85,7 @@ jobs: run: | mkdir -p /tmp/pg_upgrade_scripts cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts + cp migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql /tmp/pg_upgrade_scripts/pg_net_grants.sql tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts - name: configure aws credentials - staging @@ -156,6 +158,7 @@ jobs: run: | mkdir -p /tmp/pg_upgrade_scripts cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts + cp migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql /tmp/pg_upgrade_scripts/pg_net_grants.sql tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts - name: configure aws credentials - prod diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh index add5ac232a..e4dc2c8f98 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh @@ -145,31 +145,6 @@ EOF function execute_patches { local rc=0 - # Patch pg_net grants - PG_NET_ENABLED=$(run_sql -A -t -c "select count(*) > 0 from pg_extension where extname = 'pg_net';") || rc=1 - - if [ "$PG_NET_ENABLED" = "t" ]; then - PG_NET_GRANT_QUERY=$( - cat < 0 from pg_extension where extname = 'pg_cron' and extowner::regrole::text = 'postgres';") || rc=1 @@ -263,6 +238,29 @@ EOF return $rc } +# Runs the pg_net grants migration (migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql) +# on the upgraded project. Running projects never replay migrations, only new and restored ones do, +# so this is how they get extensions.apply_pg_net_grants() and the event trigger that calls it. The +# file ends by running the routine, which hands the pg_net grants of anon, authenticated and +# service_role to postgres. It must run after ALTER USER postgres WITH NOSUPERUSER: while postgres is +# a superuser its grants are recorded as made by supabase_admin, and the routine refuses to run. +# The file is put next to the scripts by ansible. Fail-soft like execute_patches. +# One transaction: without it a failure after the drop of the event trigger would leave the +# project with no trigger, so every later create extension pg_net would install with no grants. +function execute_pg_net_grants { + local rc=0 + local sql_file="$SCRIPT_DIR/pg_net_grants.sql" + + if [ -f "$sql_file" ]; then + run_sql -v ON_ERROR_STOP=1 -1 -f "$sql_file" || rc=1 + else + log "WARNING: $sql_file not found" + rc=1 + fi + + return $rc +} + function complete_pg_upgrade { if [ -f /tmp/pg-upgrade-status ]; then log "Upgrade job already started. Bailing." @@ -344,7 +342,13 @@ function complete_pg_upgrade { run_sql -c "ALTER USER postgres WITH NOSUPERUSER;" - log "4.2. Applying authentication scheme updates" + log "4.2. Applying pg_net grants" + execute_pg_net_grants || { + log "WARNING: pg_net grants failed" + warnings=1 + } + + log "4.3. Applying authentication scheme updates" retry 3 apply_auth_scheme_updates sleep 5 diff --git a/ansible/tasks/internal/admin-api.yml b/ansible/tasks/internal/admin-api.yml index 99120a9829..7993b253d7 100644 --- a/ansible/tasks/internal/admin-api.yml +++ b/ansible/tasks/internal/admin-api.yml @@ -76,6 +76,16 @@ - { file: "common.sh" } - { file: "start_guard.sh" } +# complete.sh runs this migration on the upgraded project, see execute_pg_net_grants there. +# adminapi copies only .sh files out of the upgrade scripts tarball, so the image carries the SQL. +- name: Move the pg_net grants migration to /etc/adminapi/pg_upgrade_scripts/ + copy: + src: /tmp/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql + dest: /etc/adminapi/pg_upgrade_scripts/pg_net_grants.sql + remote_src: true + mode: "0644" + owner: adminapi + - name: adminapi - create service file template: src: files/adminapi.service.j2 diff --git a/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql new file mode 100644 index 0000000000..9153d4ea11 --- /dev/null +++ b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql @@ -0,0 +1,171 @@ +-- migrate:up + +-- pg_net grants used to be made by supabase_admin, and only the grantor can revoke a grant. So a +-- customer who wanted anon or authenticated out of net could not do it themselves. From now on +-- postgres is the grantor: supabase_admin gives postgres USAGE on net and EXECUTE on the http +-- functions WITH GRANT OPTION, and whatever anon, authenticated and service_role already hold is +-- granted again by postgres. New projects along with existing projects get the same three grants +-- from postgres. +-- +-- extensions.apply_pg_net_grants() is the only place this happens. It runs here, on every CREATE +-- or ALTER EXTENSION pg_net through the event trigger below, and from complete.sh on upgrade, so +-- one piece of code covers init, restore and upgrade. It raises on failure: the callers below +-- decide whether that is fatal. + +create or replace function extensions.apply_pg_net_grants() + returns void + language plpgsql + set search_path to pg_catalog, pg_temp +as $$ +declare + http_functions regprocedure[]; + http_function regprocedure; + api_role name; +begin + if not exists (select from pg_extension where extname = 'pg_net') then + return; + end if; + + -- Postgres records a superuser's GRANT as made by the object owner, supabase_admin. The grants + -- below would get the wrong grantor and step 2 would then drop the only grant a role has. + if exists (select from pg_roles where rolname = 'postgres' and rolsuper) then + raise warning 'apply_pg_net_grants: postgres is a superuser, nothing changed'; + return; + end if; + + -- Looked up by name, so a pg_net release that changes an argument list does not break this. + select coalesce(array_agg(p.oid::regprocedure order by p.proname), '{}') + into http_functions + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete'); + + -- 1. postgres can grant onward. + grant usage on schema net to postgres with grant option; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to postgres with grant option', http_function); + end loop; + + -- pg_net grants ALL on its tables to PUBLIC. TRIGGER there lets any role run code as the + -- worker, which connects as postgres. postgres keeps TRIGGER for response handlers and + -- TRUNCATE for clearing the queue. + revoke trigger, references, truncate on all tables in schema net from public; + grant trigger, truncate on all tables in schema net to postgres with grant option; + + -- 2. SET ROLE is not allowed inside CREATE EXTENSION, but a SECURITY DEFINER function owned by + -- postgres is, and its grants are postgres's. Dropped and created on every run, so a superuser + -- never calls a body the customer changed. + drop function if exists extensions.grant_pg_net_api_access(name, regprocedure[]); + create function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void + language plpgsql + security definer + set search_path to pg_catalog, pg_temp + as $helper$ + begin + execute format('grant usage on schema net to %I', api_role); + if http_functions <> '{}' then + execute format('grant execute on function %s to %I', array_to_string(http_functions, ', '), api_role); + end if; + end; + $helper$; + alter function extensions.grant_pg_net_api_access(name, regprocedure[]) owner to postgres; + revoke execute on function extensions.grant_pg_net_api_access(name, regprocedure[]) from public; + + -- On a fresh install, give the three roles USAGE as supabase_admin so the loop below converts + -- them like an old project. pg_event_trigger_ddl_commands() only works inside an event trigger, + -- so this runs for CREATE EXTENSION pg_net and raises everywhere else. Elsewhere a role without + -- USAGE stays without: it may have been revoked (supabase support handling etc.) on purpose. + begin + perform from pg_event_trigger_ddl_commands() ev + join pg_extension e on e.oid = ev.objid + where ev.command_tag = 'CREATE EXTENSION' and e.extname = 'pg_net'; + if found then + grant usage on schema net to anon, authenticated, service_role; + end if; + exception when event_trigger_protocol_violated then + null; + end; + + -- USAGE on net is the switch. A role that holds it, from supabase_admin before the migration + -- or from postgres after, gets the full set from postgres, so an extension update that drops + -- and recreates a function is repaired too. Whatever supabase_admin granted the three roles + -- goes, so a role support revoked, in full or in part, keeps nothing it cannot revoke itself. + for api_role in + select distinct g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + perform extensions.grant_pg_net_api_access(api_role, http_functions); + end loop; + -- The helper added the row the customer can revoke. This removes the row they cannot, the + -- one from supabase_admin. + revoke usage on schema net from anon, authenticated, service_role; + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from anon, authenticated, service_role', http_function); + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grants stay with supabase_admin, + -- so a customer cannot break them, and it gets them here rather than through PUBLIC. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to supabase_functions_admin', http_function); + end loop; + end if; +end; +$$; + +revoke execute on function extensions.apply_pg_net_grants() from public, postgres; + +-- The event trigger on CREATE EXTENSION used to grant the five roles itself, with the full +-- argument lists of http_get and http_post spelled out. Now it calls the routine. The routine +-- doesn't catches errors, so one here aborts the CREATE or ALTER EXTENSION and the customer sees +-- the message instead of a server log line. +create or replace function extensions.grant_pg_net_access() + returns event_trigger + language plpgsql + set search_path to pg_catalog, pg_temp +as $$ +begin + if not exists ( + select from pg_event_trigger_ddl_commands() ev + join pg_extension e on ev.objid = e.oid + where e.extname = 'pg_net' + ) then + return; + end if; + + if not exists (select from pg_roles where rolname = 'supabase_functions_admin') then + create user supabase_functions_admin noinherit createrole login noreplication; + end if; + + perform extensions.apply_pg_net_grants(); +end; +$$; + +-- An extension update can drop and recreate a function, which loses every grant on it. +drop event trigger if exists issue_pg_net_access; +create event trigger issue_pg_net_access + on ddl_command_end + when tag in ('CREATE EXTENSION', 'ALTER EXTENSION') + execute procedure extensions.grant_pg_net_access(); + +-- Init and restore run this file with ON_ERROR_STOP, and complete.sh runs it on upgrade, so a +-- failed conversion is a warning here and the project keeps its current grants. +do $$ +begin + perform extensions.apply_pg_net_grants(); +exception when others then + raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; +end; +$$; + +-- migrate:down diff --git a/migrations/schema-15.sql b/migrations/schema-15.sql index dea5a4dabd..5e9ccc1bd2 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -163,6 +163,121 @@ CREATE FUNCTION auth.uid() RETURNS uuid $$; +-- +-- Name: apply_pg_net_grants(); Type: FUNCTION; Schema: extensions; Owner: - +-- + +CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void + LANGUAGE plpgsql + SET search_path TO 'pg_catalog', 'pg_temp' + AS $_$ +declare + http_functions regprocedure[]; + http_function regprocedure; + api_role name; +begin + if not exists (select from pg_extension where extname = 'pg_net') then + return; + end if; + + -- Postgres records a superuser's GRANT as made by the object owner, supabase_admin. The grants + -- below would get the wrong grantor and step 2 would then drop the only grant a role has. + if exists (select from pg_roles where rolname = 'postgres' and rolsuper) then + raise warning 'apply_pg_net_grants: postgres is a superuser, nothing changed'; + return; + end if; + + -- Looked up by name, so a pg_net release that changes an argument list does not break this. + select coalesce(array_agg(p.oid::regprocedure order by p.proname), '{}') + into http_functions + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete'); + + -- 1. postgres can grant onward. + grant usage on schema net to postgres with grant option; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to postgres with grant option', http_function); + end loop; + + -- pg_net grants ALL on its tables to PUBLIC. TRIGGER there lets any role run code as the + -- worker, which connects as postgres. postgres keeps TRIGGER for response handlers and + -- TRUNCATE for clearing the queue. + revoke trigger, references, truncate on all tables in schema net from public; + grant trigger, truncate on all tables in schema net to postgres with grant option; + + -- 2. SET ROLE is not allowed inside CREATE EXTENSION, but a SECURITY DEFINER function owned by + -- postgres is, and its grants are postgres's. Dropped and created on every run, so a superuser + -- never calls a body the customer changed. + drop function if exists extensions.grant_pg_net_api_access(name, regprocedure[]); + create function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void + language plpgsql + security definer + set search_path to pg_catalog, pg_temp + as $helper$ + begin + execute format('grant usage on schema net to %I', api_role); + if http_functions <> '{}' then + execute format('grant execute on function %s to %I', array_to_string(http_functions, ', '), api_role); + end if; + end; + $helper$; + alter function extensions.grant_pg_net_api_access(name, regprocedure[]) owner to postgres; + revoke execute on function extensions.grant_pg_net_api_access(name, regprocedure[]) from public; + + -- On a fresh install, give the three roles USAGE as supabase_admin so the loop below converts + -- them like an old project. pg_event_trigger_ddl_commands() only works inside an event trigger, + -- so this runs for CREATE EXTENSION pg_net and raises everywhere else. Elsewhere a role without + -- USAGE stays without: it may have been revoked (supabase support handling etc.) on purpose. + begin + perform from pg_event_trigger_ddl_commands() ev + join pg_extension e on e.oid = ev.objid + where ev.command_tag = 'CREATE EXTENSION' and e.extname = 'pg_net'; + if found then + grant usage on schema net to anon, authenticated, service_role; + end if; + exception when event_trigger_protocol_violated then + null; + end; + + -- USAGE on net is the switch. A role that holds it, from supabase_admin before the migration + -- or from postgres after, gets the full set from postgres, so an extension update that drops + -- and recreates a function is repaired too. Whatever supabase_admin granted the three roles + -- goes, so a role support revoked, in full or in part, keeps nothing it cannot revoke itself. + for api_role in + select distinct g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + perform extensions.grant_pg_net_api_access(api_role, http_functions); + end loop; + -- The helper added the row the customer can revoke. This removes the row they cannot, the + -- one from supabase_admin. + revoke usage on schema net from anon, authenticated, service_role; + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from anon, authenticated, service_role', http_function); + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grants stay with supabase_admin, + -- so a customer cannot break them, and it gets them here rather than through PUBLIC. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to supabase_functions_admin', http_function); + end loop; + end if; +end; +$_$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -272,47 +387,21 @@ CREATE FUNCTION extensions.grant_pg_net_access() RETURNS event_trigger LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' AS $$ -BEGIN - IF EXISTS ( - SELECT 1 - FROM pg_event_trigger_ddl_commands() AS ev - JOIN pg_extension AS ext - ON ev.objid = ext.oid - WHERE ext.extname = 'pg_net' - ) - THEN - IF NOT EXISTS ( - SELECT 1 - FROM pg_roles - WHERE rolname = 'supabase_functions_admin' - ) - THEN - CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION; - END IF; - - GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role; - - IF EXISTS ( - SELECT FROM pg_extension - WHERE extname = 'pg_net' - -- all versions in use on existing projects as of 2025-02-20 - -- version 0.12.0 onwards don't need these applied - AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8.0', '0.10.0', '0.11.0') - ) THEN - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; +begin + if not exists ( + select from pg_event_trigger_ddl_commands() ev + join pg_extension e on ev.objid = e.oid + where e.extname = 'pg_net' + ) then + return; + end if; - REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; - REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; + if not exists (select from pg_roles where rolname = 'supabase_functions_admin') then + create user supabase_functions_admin noinherit createrole login noreplication; + end if; - GRANT EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - GRANT EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - END IF; - END IF; -END; + perform extensions.apply_pg_net_grants(); +end; $$; @@ -820,7 +909,7 @@ CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end -- CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end - WHEN TAG IN ('CREATE EXTENSION') + WHEN TAG IN ('CREATE EXTENSION', 'ALTER EXTENSION') EXECUTE FUNCTION extensions.grant_pg_net_access(); diff --git a/migrations/schema-17.sql b/migrations/schema-17.sql index 1cc928912a..8ddc30d9fc 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -164,6 +164,121 @@ CREATE FUNCTION auth.uid() RETURNS uuid $$; +-- +-- Name: apply_pg_net_grants(); Type: FUNCTION; Schema: extensions; Owner: - +-- + +CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void + LANGUAGE plpgsql + SET search_path TO 'pg_catalog', 'pg_temp' + AS $_$ +declare + http_functions regprocedure[]; + http_function regprocedure; + api_role name; +begin + if not exists (select from pg_extension where extname = 'pg_net') then + return; + end if; + + -- Postgres records a superuser's GRANT as made by the object owner, supabase_admin. The grants + -- below would get the wrong grantor and step 2 would then drop the only grant a role has. + if exists (select from pg_roles where rolname = 'postgres' and rolsuper) then + raise warning 'apply_pg_net_grants: postgres is a superuser, nothing changed'; + return; + end if; + + -- Looked up by name, so a pg_net release that changes an argument list does not break this. + select coalesce(array_agg(p.oid::regprocedure order by p.proname), '{}') + into http_functions + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete'); + + -- 1. postgres can grant onward. + grant usage on schema net to postgres with grant option; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to postgres with grant option', http_function); + end loop; + + -- pg_net grants ALL on its tables to PUBLIC. TRIGGER there lets any role run code as the + -- worker, which connects as postgres. postgres keeps TRIGGER for response handlers and + -- TRUNCATE for clearing the queue. + revoke trigger, references, truncate on all tables in schema net from public; + grant trigger, truncate on all tables in schema net to postgres with grant option; + + -- 2. SET ROLE is not allowed inside CREATE EXTENSION, but a SECURITY DEFINER function owned by + -- postgres is, and its grants are postgres's. Dropped and created on every run, so a superuser + -- never calls a body the customer changed. + drop function if exists extensions.grant_pg_net_api_access(name, regprocedure[]); + create function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void + language plpgsql + security definer + set search_path to pg_catalog, pg_temp + as $helper$ + begin + execute format('grant usage on schema net to %I', api_role); + if http_functions <> '{}' then + execute format('grant execute on function %s to %I', array_to_string(http_functions, ', '), api_role); + end if; + end; + $helper$; + alter function extensions.grant_pg_net_api_access(name, regprocedure[]) owner to postgres; + revoke execute on function extensions.grant_pg_net_api_access(name, regprocedure[]) from public; + + -- On a fresh install, give the three roles USAGE as supabase_admin so the loop below converts + -- them like an old project. pg_event_trigger_ddl_commands() only works inside an event trigger, + -- so this runs for CREATE EXTENSION pg_net and raises everywhere else. Elsewhere a role without + -- USAGE stays without: it may have been revoked (supabase support handling etc.) on purpose. + begin + perform from pg_event_trigger_ddl_commands() ev + join pg_extension e on e.oid = ev.objid + where ev.command_tag = 'CREATE EXTENSION' and e.extname = 'pg_net'; + if found then + grant usage on schema net to anon, authenticated, service_role; + end if; + exception when event_trigger_protocol_violated then + null; + end; + + -- USAGE on net is the switch. A role that holds it, from supabase_admin before the migration + -- or from postgres after, gets the full set from postgres, so an extension update that drops + -- and recreates a function is repaired too. Whatever supabase_admin granted the three roles + -- goes, so a role support revoked, in full or in part, keeps nothing it cannot revoke itself. + for api_role in + select distinct g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + perform extensions.grant_pg_net_api_access(api_role, http_functions); + end loop; + -- The helper added the row the customer can revoke. This removes the row they cannot, the + -- one from supabase_admin. + revoke usage on schema net from anon, authenticated, service_role; + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from anon, authenticated, service_role', http_function); + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grants stay with supabase_admin, + -- so a customer cannot break them, and it gets them here rather than through PUBLIC. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to supabase_functions_admin', http_function); + end loop; + end if; +end; +$_$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -273,47 +388,21 @@ CREATE FUNCTION extensions.grant_pg_net_access() RETURNS event_trigger LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' AS $$ -BEGIN - IF EXISTS ( - SELECT 1 - FROM pg_event_trigger_ddl_commands() AS ev - JOIN pg_extension AS ext - ON ev.objid = ext.oid - WHERE ext.extname = 'pg_net' - ) - THEN - IF NOT EXISTS ( - SELECT 1 - FROM pg_roles - WHERE rolname = 'supabase_functions_admin' - ) - THEN - CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION; - END IF; - - GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role; - - IF EXISTS ( - SELECT FROM pg_extension - WHERE extname = 'pg_net' - -- all versions in use on existing projects as of 2025-02-20 - -- version 0.12.0 onwards don't need these applied - AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8.0', '0.10.0', '0.11.0') - ) THEN - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; +begin + if not exists ( + select from pg_event_trigger_ddl_commands() ev + join pg_extension e on ev.objid = e.oid + where e.extname = 'pg_net' + ) then + return; + end if; - REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; - REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; + if not exists (select from pg_roles where rolname = 'supabase_functions_admin') then + create user supabase_functions_admin noinherit createrole login noreplication; + end if; - GRANT EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - GRANT EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - END IF; - END IF; -END; + perform extensions.apply_pg_net_grants(); +end; $$; @@ -821,7 +910,7 @@ CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end -- CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end - WHEN TAG IN ('CREATE EXTENSION') + WHEN TAG IN ('CREATE EXTENSION', 'ALTER EXTENSION') EXECUTE FUNCTION extensions.grant_pg_net_access(); diff --git a/migrations/schema-orioledb-17.sql b/migrations/schema-orioledb-17.sql index ff82283437..18ee12fa47 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -178,6 +178,121 @@ CREATE FUNCTION auth.uid() RETURNS uuid $$; +-- +-- Name: apply_pg_net_grants(); Type: FUNCTION; Schema: extensions; Owner: - +-- + +CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void + LANGUAGE plpgsql + SET search_path TO 'pg_catalog', 'pg_temp' + AS $_$ +declare + http_functions regprocedure[]; + http_function regprocedure; + api_role name; +begin + if not exists (select from pg_extension where extname = 'pg_net') then + return; + end if; + + -- Postgres records a superuser's GRANT as made by the object owner, supabase_admin. The grants + -- below would get the wrong grantor and step 2 would then drop the only grant a role has. + if exists (select from pg_roles where rolname = 'postgres' and rolsuper) then + raise warning 'apply_pg_net_grants: postgres is a superuser, nothing changed'; + return; + end if; + + -- Looked up by name, so a pg_net release that changes an argument list does not break this. + select coalesce(array_agg(p.oid::regprocedure order by p.proname), '{}') + into http_functions + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete'); + + -- 1. postgres can grant onward. + grant usage on schema net to postgres with grant option; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to postgres with grant option', http_function); + end loop; + + -- pg_net grants ALL on its tables to PUBLIC. TRIGGER there lets any role run code as the + -- worker, which connects as postgres. postgres keeps TRIGGER for response handlers and + -- TRUNCATE for clearing the queue. + revoke trigger, references, truncate on all tables in schema net from public; + grant trigger, truncate on all tables in schema net to postgres with grant option; + + -- 2. SET ROLE is not allowed inside CREATE EXTENSION, but a SECURITY DEFINER function owned by + -- postgres is, and its grants are postgres's. Dropped and created on every run, so a superuser + -- never calls a body the customer changed. + drop function if exists extensions.grant_pg_net_api_access(name, regprocedure[]); + create function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void + language plpgsql + security definer + set search_path to pg_catalog, pg_temp + as $helper$ + begin + execute format('grant usage on schema net to %I', api_role); + if http_functions <> '{}' then + execute format('grant execute on function %s to %I', array_to_string(http_functions, ', '), api_role); + end if; + end; + $helper$; + alter function extensions.grant_pg_net_api_access(name, regprocedure[]) owner to postgres; + revoke execute on function extensions.grant_pg_net_api_access(name, regprocedure[]) from public; + + -- On a fresh install, give the three roles USAGE as supabase_admin so the loop below converts + -- them like an old project. pg_event_trigger_ddl_commands() only works inside an event trigger, + -- so this runs for CREATE EXTENSION pg_net and raises everywhere else. Elsewhere a role without + -- USAGE stays without: it may have been revoked (supabase support handling etc.) on purpose. + begin + perform from pg_event_trigger_ddl_commands() ev + join pg_extension e on e.oid = ev.objid + where ev.command_tag = 'CREATE EXTENSION' and e.extname = 'pg_net'; + if found then + grant usage on schema net to anon, authenticated, service_role; + end if; + exception when event_trigger_protocol_violated then + null; + end; + + -- USAGE on net is the switch. A role that holds it, from supabase_admin before the migration + -- or from postgres after, gets the full set from postgres, so an extension update that drops + -- and recreates a function is repaired too. Whatever supabase_admin granted the three roles + -- goes, so a role support revoked, in full or in part, keeps nothing it cannot revoke itself. + for api_role in + select distinct g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + perform extensions.grant_pg_net_api_access(api_role, http_functions); + end loop; + -- The helper added the row the customer can revoke. This removes the row they cannot, the + -- one from supabase_admin. + revoke usage on schema net from anon, authenticated, service_role; + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from anon, authenticated, service_role', http_function); + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grants stay with supabase_admin, + -- so a customer cannot break them, and it gets them here rather than through PUBLIC. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + foreach http_function in array http_functions loop + execute format('grant execute on function %s to supabase_functions_admin', http_function); + end loop; + end if; +end; +$_$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -287,47 +402,21 @@ CREATE FUNCTION extensions.grant_pg_net_access() RETURNS event_trigger LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' AS $$ -BEGIN - IF EXISTS ( - SELECT 1 - FROM pg_event_trigger_ddl_commands() AS ev - JOIN pg_extension AS ext - ON ev.objid = ext.oid - WHERE ext.extname = 'pg_net' - ) - THEN - IF NOT EXISTS ( - SELECT 1 - FROM pg_roles - WHERE rolname = 'supabase_functions_admin' - ) - THEN - CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION; - END IF; - - GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role; - - IF EXISTS ( - SELECT FROM pg_extension - WHERE extname = 'pg_net' - -- all versions in use on existing projects as of 2025-02-20 - -- version 0.12.0 onwards don't need these applied - AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8.0', '0.10.0', '0.11.0') - ) THEN - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER; - - ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; - ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net; +begin + if not exists ( + select from pg_event_trigger_ddl_commands() ev + join pg_extension e on ev.objid = e.oid + where e.extname = 'pg_net' + ) then + return; + end if; - REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; - REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC; + if not exists (select from pg_roles where rolname = 'supabase_functions_admin') then + create user supabase_functions_admin noinherit createrole login noreplication; + end if; - GRANT EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - GRANT EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role; - END IF; - END IF; -END; + perform extensions.apply_pg_net_grants(); +end; $$; @@ -835,7 +924,7 @@ CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end -- CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end - WHEN TAG IN ('CREATE EXTENSION') + WHEN TAG IN ('CREATE EXTENSION', 'ALTER EXTENSION') EXECUTE FUNCTION extensions.grant_pg_net_access(); diff --git a/nix/checks.nix b/nix/checks.nix index d2e900dc65..96c9eb5186 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -194,6 +194,7 @@ "extensions_schema" # tests extension loading "roles" # includes roles/schemas from extensions not in CLI (pgtle, pgmq, repack, topology) "pg_net_worker_privileges" # needs the authenticated/postgres roles from the full migrations, not present in the CLI prime file + "pg_net_grants" # needs the postgres/anon/authenticated/service_role roles and extensions.apply_pg_net_grants from the full migrations, not present in the CLI prime file "pg_cron_trigger_privileges" # needs pg_cron + the postgres role and cron-schema grants from the full migrations, not in the CLI prime file "supautils_restrict_versions" # needs the postgres role + primed hstore from the full migrations/prime, not present in the CLI variant "amcheck" # needs the postgres/anon/authenticated/service_role roles and the default privileges from the full migrations, plus amcheck primed by prime.sql diff --git a/nix/tests/expected/pg_net_grants.out b/nix/tests/expected/pg_net_grants.out new file mode 100644 index 0000000000..4c88622bfa --- /dev/null +++ b/nix/tests/expected/pg_net_grants.out @@ -0,0 +1,434 @@ +-- pg_net grants are made by postgres, not supabase_admin, so a customer can revoke anon, +-- authenticated and service_role themselves. extensions.apply_pg_net_grants() does that. It runs +-- from the migration and from the event trigger on CREATE and ALTER EXTENSION pg_net. +-- pg_net here was installed by prime.sql through the event trigger, the path a fresh project takes. +-- who holds what on the net schema and the http functions, and who granted it +create temp view net_acl as +select kind, name, privilege_type, grantee, grantor, is_grantable +from ( + select o.kind, o.name, a.privilege_type, + case a.grantee when 0 then 'PUBLIC' else a.grantee::regrole::text end as grantee, + a.grantor::regrole::text as grantor, + a.is_grantable + from ( + select 'schema' as kind, n.nspname::text as name, n.nspacl as acl + from pg_namespace n + where n.nspname = 'net' + union all + select 'function', p.proname::text, p.proacl + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') + ) o + cross join lateral aclexplode(o.acl) a +) x +order by kind, name, grantee collate "C", privilege_type; +-- what each role can actually do, through its own grants or through PUBLIC +create temp view net_access as +select r.rolname, + has_schema_privilege(r.rolname, 'net', 'USAGE') as schema_usage, + bool_and(has_function_privilege(r.rolname, p.oid, 'EXECUTE')) as http_execute +from pg_roles r, + pg_proc p + join pg_namespace n on n.oid = p.pronamespace +where r.rolname in ('postgres', 'anon', 'authenticated', 'service_role', 'supabase_functions_admin') + and n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') +group by r.rolname +order by r.rolname collate "C"; +-- 1. fresh project: anon, authenticated and service_role are granted by postgres, postgres can +-- grant onward, supabase_functions_admin keeps its grants from supabase_admin +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | anon | postgres | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | anon | postgres | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | anon | postgres | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | anon | postgres | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(29 rows) + +select * from net_access; + rolname | schema_usage | http_execute +--------------------------+--------------+-------------- + anon | t | t + authenticated | t | t + postgres | t | t + service_role | t | t + supabase_functions_admin | t | t +(5 rows) + +-- 2. running the routine again changes nothing +select extensions.apply_pg_net_grants(); + apply_pg_net_grants +--------------------- + +(1 row) + +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | anon | postgres | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | anon | postgres | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | anon | postgres | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | anon | postgres | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(29 rows) + +-- 3. existing project: the old event trigger granted the three roles as supabase_admin, and +-- support revoked anon by hand. authenticated and service_role keep their access, now granted +-- by postgres. anon gets nothing back +set role postgres; +revoke usage on schema net from anon, authenticated, service_role; +revoke execute on function net.http_get, net.http_post, net.http_delete from anon, authenticated, service_role; +reset role; +grant usage on schema net to anon, authenticated, service_role; +revoke usage on schema net from anon; +select extensions.apply_pg_net_grants(); + apply_pg_net_grants +--------------------- + +(1 row) + +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(25 rows) + +-- 4. the customer revokes authenticated. A later run does not bring it back +set role postgres; +revoke usage on schema net from authenticated; +revoke execute on function net.http_get, net.http_post, net.http_delete from authenticated; +reset role; +select extensions.apply_pg_net_grants(); + apply_pg_net_grants +--------------------- + +(1 row) + +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(21 rows) + +-- 5. upgraded project: the upgrade scripts make http_get and http_post SECURITY DEFINER, +-- revoke PUBLIC on them and grant the five roles as supabase_admin. The grants move to +-- postgres, function security is left alone +drop extension pg_net; +create extension pg_net; +set role postgres; +revoke usage on schema net from anon, authenticated, service_role; +revoke execute on function net.http_get, net.http_post, net.http_delete from anon, authenticated, service_role; +reset role; +alter function net.http_get security definer; +alter function net.http_post security definer; +revoke all on function net.http_get, net.http_post from public; +grant usage on schema net to supabase_functions_admin, postgres, anon, authenticated, service_role; +grant execute on function net.http_get, net.http_post to supabase_functions_admin, postgres, anon, authenticated, service_role; +select extensions.apply_pg_net_grants(); + apply_pg_net_grants +--------------------- + +(1 row) + +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | anon | postgres | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | anon | postgres | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | anon | postgres | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | anon | postgres | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(27 rows) + +select * from net_access; + rolname | schema_usage | http_execute +--------------------------+--------------+-------------- + anon | t | t + authenticated | t | t + postgres | t | t + service_role | t | t + supabase_functions_admin | t | t +(5 rows) + +select p.proname, p.prosecdef +from pg_proc p +join pg_namespace n on n.oid = p.pronamespace +where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') +order by p.proname; + proname | prosecdef +-------------+----------- + http_delete | f + http_get | t + http_post | t +(3 rows) + +-- 6. drop and recreate the extension as postgres, the way a project does it. supautils runs the +-- trigger in a security-restricted context, and the three roles still get the set from postgres. +set role postgres; +drop extension pg_net; +create extension pg_net; +reset role; +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | anon | postgres | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | anon | postgres | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | anon | postgres | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | anon | postgres | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(29 rows) + +-- 6.1 a helper the customer replaced is not what runs: the routine drops and recreates it +set role postgres; +create or replace function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void language plpgsql as $$ begin raise exception 'customer body ran as %', current_user; end $$; +reset role; +select extensions.apply_pg_net_grants(); + apply_pg_net_grants +--------------------- + +(1 row) + +select prosecdef, proowner::regrole as owner + from pg_proc where oid = 'extensions.grant_pg_net_api_access(name, regprocedure[])'::regprocedure; + prosecdef | owner +-----------+---------- + t | postgres +(1 row) + +-- 7. anon can queue a request with its grants from postgres. Then wait for the worker to finish +-- it, so it cannot fire a trigger that a later test plants on the response table +select value as port from test_config where key = 'http_mock_port' \gset +set role anon; +select net.http_get('http://localhost:' || :'port' || '/get') as request_id \gset +reset role; +select set_config('pg_net_grants.request_id', :'request_id', false) is not null as request_queued; + request_queued +---------------- + t +(1 row) + +do $$ +begin + for i in 1..40 loop + exit when exists (select from net._http_response where id = current_setting('pg_net_grants.request_id')::bigint); + perform pg_sleep(0.25); + end loop; +end; +$$; +-- 8. PUBLIC loses TRIGGER, REFERENCES and TRUNCATE on the tables, postgres gets TRIGGER and TRUNCATE. +-- The rest of what pg_net grants PUBLIC is still there +select relname, grantee, string_agg(privilege_type, ', ' order by privilege_type) as privileges +from ( + select c.relname, + case a.grantee when 0 then 'PUBLIC' else a.grantee::regrole::text end as grantee, + a.privilege_type + from pg_class c + join pg_namespace n on n.oid = c.relnamespace + cross join lateral aclexplode(c.relacl) a + where n.nspname = 'net' + and c.relkind in ('r', 'S') + and a.privilege_type <> 'MAINTAIN' -- PG17+ +) x +group by relname, grantee +order by relname, grantee collate "C"; + relname | grantee | privileges +---------------------------+----------------+--------------------------------------------------------------- + _http_response | PUBLIC | DELETE, INSERT, SELECT, UPDATE + _http_response | postgres | TRIGGER, TRUNCATE + _http_response | supabase_admin | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + http_request_queue | PUBLIC | DELETE, INSERT, SELECT, UPDATE + http_request_queue | postgres | TRIGGER, TRUNCATE + http_request_queue | supabase_admin | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + http_request_queue_id_seq | PUBLIC | SELECT, UPDATE, USAGE + http_request_queue_id_seq | supabase_admin | SELECT, UPDATE, USAGE +(8 rows) + +-- 9. postgres is still a superuser, as it is during an upgrade until complete.sh demotes it. The +-- routine warns and changes nothing: a grant made now would be recorded as made by supabase_admin +alter role postgres superuser; +select extensions.apply_pg_net_grants(); +WARNING: apply_pg_net_grants: postgres is a superuser, nothing changed + apply_pg_net_grants +--------------------- + +(1 row) + +alter role postgres nosuperuser; +select * from net_acl; + kind | name | privilege_type | grantee | grantor | is_grantable +----------+-------------+----------------+--------------------------+----------------+-------------- + function | http_delete | EXECUTE | PUBLIC | supabase_admin | f + function | http_delete | EXECUTE | anon | postgres | f + function | http_delete | EXECUTE | authenticated | postgres | f + function | http_delete | EXECUTE | postgres | supabase_admin | t + function | http_delete | EXECUTE | service_role | postgres | f + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_delete | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | anon | postgres | f + function | http_get | EXECUTE | authenticated | postgres | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | service_role | postgres | f + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | supabase_functions_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | anon | postgres | f + function | http_post | EXECUTE | authenticated | postgres | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | service_role | postgres | f + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | supabase_functions_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | anon | postgres | f + schema | net | USAGE | authenticated | postgres | f + schema | net | USAGE | postgres | supabase_admin | t + schema | net | USAGE | service_role | postgres | f + schema | net | CREATE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_admin | supabase_admin | f + schema | net | USAGE | supabase_functions_admin | supabase_admin | f +(29 rows) + diff --git a/nix/tests/sql/pg_net_grants.sql b/nix/tests/sql/pg_net_grants.sql new file mode 100644 index 0000000000..e3a0558e2b --- /dev/null +++ b/nix/tests/sql/pg_net_grants.sql @@ -0,0 +1,151 @@ +-- pg_net grants are made by postgres, not supabase_admin, so a customer can revoke anon, +-- authenticated and service_role themselves. extensions.apply_pg_net_grants() does that. It runs +-- from the migration and from the event trigger on CREATE and ALTER EXTENSION pg_net. +-- pg_net here was installed by prime.sql through the event trigger, the path a fresh project takes. + +-- who holds what on the net schema and the http functions, and who granted it +create temp view net_acl as +select kind, name, privilege_type, grantee, grantor, is_grantable +from ( + select o.kind, o.name, a.privilege_type, + case a.grantee when 0 then 'PUBLIC' else a.grantee::regrole::text end as grantee, + a.grantor::regrole::text as grantor, + a.is_grantable + from ( + select 'schema' as kind, n.nspname::text as name, n.nspacl as acl + from pg_namespace n + where n.nspname = 'net' + union all + select 'function', p.proname::text, p.proacl + from pg_proc p + join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') + ) o + cross join lateral aclexplode(o.acl) a +) x +order by kind, name, grantee collate "C", privilege_type; + +-- what each role can actually do, through its own grants or through PUBLIC +create temp view net_access as +select r.rolname, + has_schema_privilege(r.rolname, 'net', 'USAGE') as schema_usage, + bool_and(has_function_privilege(r.rolname, p.oid, 'EXECUTE')) as http_execute +from pg_roles r, + pg_proc p + join pg_namespace n on n.oid = p.pronamespace +where r.rolname in ('postgres', 'anon', 'authenticated', 'service_role', 'supabase_functions_admin') + and n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') +group by r.rolname +order by r.rolname collate "C"; + +-- 1. fresh project: anon, authenticated and service_role are granted by postgres, postgres can +-- grant onward, supabase_functions_admin keeps its grants from supabase_admin +select * from net_acl; +select * from net_access; + +-- 2. running the routine again changes nothing +select extensions.apply_pg_net_grants(); +select * from net_acl; + +-- 3. existing project: the old event trigger granted the three roles as supabase_admin, and +-- support revoked anon by hand. authenticated and service_role keep their access, now granted +-- by postgres. anon gets nothing back +set role postgres; +revoke usage on schema net from anon, authenticated, service_role; +revoke execute on function net.http_get, net.http_post, net.http_delete from anon, authenticated, service_role; +reset role; +grant usage on schema net to anon, authenticated, service_role; +revoke usage on schema net from anon; +select extensions.apply_pg_net_grants(); +select * from net_acl; + +-- 4. the customer revokes authenticated. A later run does not bring it back +set role postgres; +revoke usage on schema net from authenticated; +revoke execute on function net.http_get, net.http_post, net.http_delete from authenticated; +reset role; +select extensions.apply_pg_net_grants(); +select * from net_acl; + +-- 5. upgraded project: the upgrade scripts make http_get and http_post SECURITY DEFINER, +-- revoke PUBLIC on them and grant the five roles as supabase_admin. The grants move to +-- postgres, function security is left alone +drop extension pg_net; +create extension pg_net; +set role postgres; +revoke usage on schema net from anon, authenticated, service_role; +revoke execute on function net.http_get, net.http_post, net.http_delete from anon, authenticated, service_role; +reset role; +alter function net.http_get security definer; +alter function net.http_post security definer; +revoke all on function net.http_get, net.http_post from public; +grant usage on schema net to supabase_functions_admin, postgres, anon, authenticated, service_role; +grant execute on function net.http_get, net.http_post to supabase_functions_admin, postgres, anon, authenticated, service_role; +select extensions.apply_pg_net_grants(); +select * from net_acl; +select * from net_access; +select p.proname, p.prosecdef +from pg_proc p +join pg_namespace n on n.oid = p.pronamespace +where n.nspname = 'net' + and p.proname in ('http_get', 'http_post', 'http_delete') +order by p.proname; + +-- 6. drop and recreate the extension as postgres, the way a project does it. supautils runs the +-- trigger in a security-restricted context, and the three roles still get the set from postgres. +set role postgres; +drop extension pg_net; +create extension pg_net; +reset role; +select * from net_acl; + +-- 6.1 a helper the customer replaced is not what runs: the routine drops and recreates it +set role postgres; +create or replace function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[]) + returns void language plpgsql as $$ begin raise exception 'customer body ran as %', current_user; end $$; +reset role; +select extensions.apply_pg_net_grants(); +select prosecdef, proowner::regrole as owner + from pg_proc where oid = 'extensions.grant_pg_net_api_access(name, regprocedure[])'::regprocedure; + +-- 7. anon can queue a request with its grants from postgres. Then wait for the worker to finish +-- it, so it cannot fire a trigger that a later test plants on the response table +select value as port from test_config where key = 'http_mock_port' \gset +set role anon; +select net.http_get('http://localhost:' || :'port' || '/get') as request_id \gset +reset role; +select set_config('pg_net_grants.request_id', :'request_id', false) is not null as request_queued; +do $$ +begin + for i in 1..40 loop + exit when exists (select from net._http_response where id = current_setting('pg_net_grants.request_id')::bigint); + perform pg_sleep(0.25); + end loop; +end; +$$; + +-- 8. PUBLIC loses TRIGGER, REFERENCES and TRUNCATE on the tables, postgres gets TRIGGER and TRUNCATE. +-- The rest of what pg_net grants PUBLIC is still there +select relname, grantee, string_agg(privilege_type, ', ' order by privilege_type) as privileges +from ( + select c.relname, + case a.grantee when 0 then 'PUBLIC' else a.grantee::regrole::text end as grantee, + a.privilege_type + from pg_class c + join pg_namespace n on n.oid = c.relnamespace + cross join lateral aclexplode(c.relacl) a + where n.nspname = 'net' + and c.relkind in ('r', 'S') + and a.privilege_type <> 'MAINTAIN' -- PG17+ +) x +group by relname, grantee +order by relname, grantee collate "C"; + +-- 9. postgres is still a superuser, as it is during an upgrade until complete.sh demotes it. The +-- routine warns and changes nothing: a grant made now would be recorded as made by supabase_admin +alter role postgres superuser; +select extensions.apply_pg_net_grants(); +alter role postgres nosuperuser; +select * from net_acl; diff --git a/tests/pg_upgrade/tests/01-schema.sql b/tests/pg_upgrade/tests/01-schema.sql index 8b03dc64f8..2267748f64 100644 --- a/tests/pg_upgrade/tests/01-schema.sql +++ b/tests/pg_upgrade/tests/01-schema.sql @@ -1,7 +1,7 @@ CREATE EXTENSION IF NOT EXISTS pgtap; BEGIN; -SELECT plan(18); +SELECT plan(24); select has_schema('public'); select has_schema('auth'); @@ -35,5 +35,37 @@ SELECT ok( WHERE proname = 'graphql' AND pronamespace = 'graphql_public'::regnamespace), 'graphql_public.graphql should be the real wrapper after upgrade, not the placeholder'); +SELECT has_function('extensions', 'apply_pg_net_grants', ARRAY[]::text[], + 'the upgrade ships extensions.apply_pg_net_grants to a project that never had it'); +SELECT is( + (SELECT evttags FROM pg_event_trigger WHERE evtname = 'issue_pg_net_access'), + ARRAY['CREATE EXTENSION', 'ALTER EXTENSION']::text[], + 'issue_pg_net_access should fire on ALTER EXTENSION too after the upgrade'); +SELECT is( + (SELECT string_agg(a.grantee::regrole::text || ' by ' || a.grantor::regrole::text, ', ' ORDER BY a.grantee::regrole::text) + FROM pg_namespace n, aclexplode(n.nspacl) a + WHERE n.nspname = 'net' AND a.privilege_type = 'USAGE' + AND a.grantee::regrole::text IN ('anon', 'authenticated', 'service_role')), + 'authenticated by postgres, service_role by postgres', + 'the upgrade hands the pg_net schema grants to postgres and does not bring back the revoked anon'); +SELECT is( + (SELECT string_agg(a.grantee::regrole::text || ' by ' || a.grantor::regrole::text, ', ' ORDER BY a.grantee::regrole::text) + FROM pg_proc p, aclexplode(p.proacl) a + WHERE p.oid = 'net.http_get'::regproc AND a.privilege_type = 'EXECUTE' + AND a.grantee::regrole::text IN ('anon', 'authenticated', 'service_role')), + 'authenticated by postgres, service_role by postgres', + 'the upgrade hands the net.http_get grants to postgres and does not bring back the revoked anon'); +SELECT is( + (SELECT string_agg(g, ', ' ORDER BY g) FROM (SELECT c.relname || ': ' || CASE a.grantee WHEN 0 THEN 'PUBLIC' ELSE a.grantee::regrole::text END AS g + FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace, aclexplode(c.relacl) a + WHERE n.nspname = 'net' AND c.relkind = 'r' AND a.privilege_type = 'TRIGGER') t), + '_http_response: postgres, _http_response: supabase_admin, http_request_queue: postgres, http_request_queue: supabase_admin', + 'the upgrade takes TRIGGER on the pg_net tables away from PUBLIC and leaves it with postgres'); +SET LOCAL ROLE authenticated; +SELECT lives_ok( + $$SELECT net.http_get('http://localhost')$$, + 'authenticated can still queue a pg_net request after the upgrade'); +RESET ROLE; + SELECT * FROM finish(); ROLLBACK; diff --git a/tests/pg_upgrade/tests/99-fixtures.sql b/tests/pg_upgrade/tests/99-fixtures.sql index 5e5315ea45..5ad213e37f 100644 --- a/tests/pg_upgrade/tests/99-fixtures.sql +++ b/tests/pg_upgrade/tests/99-fixtures.sql @@ -26,3 +26,27 @@ create event trigger issue_pg_cron_access on ddl_command_end when tag in ('CREATE SCHEMA') execute procedure extensions.grant_pg_cron_access(); + +-- simulate a running project that never received the pg_net grants migration: no routine, the +-- event trigger only on CREATE EXTENSION, no grants from postgres, and the old upgrade script +-- granted pg_net to anon, authenticated and service_role as supabase_admin, then support revoked +-- anon by hand. PUBLIC still has everything pg_net grants on the tables. The upgrade must ship +-- the routine, hand the grants of authenticated and service_role to postgres, leave anon without +-- one and take TRIGGER away from PUBLIC. +drop function extensions.apply_pg_net_grants(); +drop function extensions.grant_pg_net_api_access(name, regprocedure[]); +drop event trigger issue_pg_net_access; +create event trigger issue_pg_net_access + on ddl_command_end + when tag in ('CREATE EXTENSION') + execute procedure extensions.grant_pg_net_access(); +set role postgres; +revoke usage on schema net from anon, authenticated, service_role; +revoke execute on function net.http_get, net.http_post, net.http_delete from anon, authenticated, service_role; +reset role; +revoke trigger, truncate on all tables in schema net from postgres; +grant trigger, references, truncate on all tables in schema net to public; +grant usage on schema net to anon, authenticated, service_role; +grant execute on function net.http_get, net.http_post to anon, authenticated, service_role; +revoke usage on schema net from anon; +revoke execute on function net.http_get, net.http_post from anon;