diff --git a/.github/workflows/publish-nix-pgupgrade-scripts.yml b/.github/workflows/publish-nix-pgupgrade-scripts.yml index 537ae6bb55..fafb481bc1 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 @@ -155,6 +157,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..f8d3fc32f0 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,39 @@ EOF return $rc } +# Runs the pg_net grants migration on the upgraded project: +# migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql, copied next to this +# script by ansible as pg_net_grants.sql. +# +# Why here: only new and restored projects replay migrations. A running project gets new +# migrations only through pg_upgrade, and this is the step that applies this one. The file +# creates extensions.apply_pg_net_grants() and the event trigger that calls it, then runs the +# routine once, which moves the pg_net grants of anon, authenticated and service_role from +# supabase_admin to postgres. +# +# Why at step 4.2: 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 does nothing. +# +# Why one transaction (-1): the file drops the event trigger and creates it again. A failure in +# between would leave the project with no trigger, so every later create extension pg_net would +# install pg_net with no grants at all. +# +# Fail-soft: a failure is logged as a warning and the upgrade completes with the grants the +# project had before. +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 +352,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/postgres_release.yml b/ansible/postgres_release.yml index e045de4c7d..83e6550064 100644 --- a/ansible/postgres_release.yml +++ b/ansible/postgres_release.yml @@ -1,5 +1,5 @@ --- postgres_release: - postgresorioledb-17: "17.11.0.024-orioledb" - postgres17: "17.12.0.005" - postgres15: "15.20.0.005" + postgresorioledb-17: "17.11.0.025-orioledb" + postgres17: "17.12.0.006" + postgres15: "15.20.0.006" diff --git a/ansible/tasks/internal/admin-api.yml b/ansible/tasks/internal/admin-api.yml index 1c25524e3f..47c57a31d2 100644 --- a/ansible/tasks/internal/admin-api.yml +++ b/ansible/tasks/internal/admin-api.yml @@ -83,6 +83,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..956f108774 --- /dev/null +++ b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql @@ -0,0 +1,223 @@ +-- migrate:up + +-- Goal: let postgres manage who can use pg_net. +-- +-- Until now the pg_net grants for anon, authenticated and service_role were made by +-- supabase_admin. A grant can be revoked by its grantor, a superuser, the object owner or a role +-- that inherits the grantor. The customer's postgres is none of those for a grant by +-- supabase_admin, so a customer who wanted one of those roles out of pg_net could not do it. This +-- led to a lot of support tickets. After this migration those grants come from postgres: the +-- customer can revoke them, and a role revoked by the customer stays revoked. +-- +-- extensions.apply_pg_net_grants() is the routine which does all of it. It is the only place that +-- grants pg_net access. It runs: +-- - at the end of this file, for projects that already have pg_net, +-- - from the event trigger below, on every CREATE EXTENSION and ALTER EXTENSION pg_net, +-- - from complete.sh after a pg_upgrade, once postgres is no longer a superuser. +-- +-- Different cases the routine handles: +-- - fresh install: the three roles, anon, authenticated and service_role, get USAGE on net and +-- EXECUTE on the http functions from postgres, +-- - existing project: what the three roles hold from supabase_admin is granted again by +-- postgres, then the supabase_admin grant is removed, +-- - a role that support or the customer revoked previously: the role holds no USAGE, so it gets +-- nothing, +-- - extension update that drops and recreates a function: roles that hold USAGE get EXECUTE on +-- the new function again, +-- - restore: the dump brings back the old grants, so this file runs after it and converts them +-- to the required privileges post restore. + +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; + + -- When postgres is a superuser, Postgres records its GRANTs as made by the object owner, i.e. + -- supabase_admin, so every grant below would get the wrong grantor and step 2 would then + -- remove the only grant a role has. This is the state during restore and during pg_upgrade. + -- Do nothing: the migration replay after restore and complete.sh after pg_upgrade run this + -- again once postgres is demoted. + 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; + + -- The http functions are found by name. Earlier the script found them by argument list, which + -- breaks when pg_net changes one. + 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 gets USAGE on the schema and EXECUTE on the http functions with grant option, + -- so it can grant them onward and revoke them. + 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_upgrade used to make http_get and http_post SECURITY DEFINER with search_path = net, so + -- the API roles could queue a request on a pg_net that gave PUBLIC nothing on its tables. + -- From 0.12.0 PUBLIC holds the table and sequence grants, so the functions run as the + -- caller like on every other project, and a trigger on the queue no longer runs as the + -- extension owner. + if (select string_to_array(extversion, '.')::int[] >= array[0, 12, 0] + from pg_extension where extname = 'pg_net') then + foreach http_function in array http_functions loop + execute format('alter function %s security invoker reset search_path', http_function); + end loop; + end if; + + -- pg_net 1.0.0 stops granting worker_restart to PUBLIC and leaves it to the extension owner. + -- Here the owner is supabase_admin, and the customer administers pg_net as postgres: changes + -- a setting, restarts the worker, brings it back when it died. So postgres gets it the same + -- way as the http functions, with grant option, so it can hand it to its own roles. + if to_regprocedure('net.worker_restart()') is not null then + grant execute on function net.worker_restart() to postgres with grant option; + end if; + + -- pg_net grants ALL on its tables to PUBLIC, including TRIGGER. A trigger on the queue runs + -- as the worker, which connects as postgres, so no role other than postgres may create one. + 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. The three API roles get their grants from postgres. + -- + -- This function runs as supabase_admin, and to make the grant work it should ideally be made + -- by postgres. SET ROLE postgres will not work, as SET ROLE is not allowed inside CREATE + -- EXTENSION on the Supabase platform. This restriction is added by supautils. + -- So another way is a SECURITY DEFINER function. One owned by postgres can run there, and + -- its grants are recorded as made by postgres. It is dropped and created on every run, so + -- that a superuser never calls a function body the customer may have 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; + + -- Fresh install: give the three roles USAGE as supabase_admin first, so the conversion below + -- treats a new project exactly like an old one. pg_event_trigger_ddl_commands() works only + -- inside an event trigger and raises anywhere else, which is how this block knows it runs + -- for CREATE EXTENSION pg_net. In any other place a role without USAGE should stay without + -- it, as support or the customer may have revoked it 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; + + -- Conversion. Every API role that holds USAGE on net, from supabase_admin on an old project + -- or from postgres on one already converted, gets USAGE and EXECUTE on the http functions + -- from postgres. A role that holds no USAGE gets nothing. Running this on every extension + -- update is what restores EXECUTE on a function the update recreated. + 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; + -- Remove the supabase_admin grants. The grants from postgres added above stay, so the role + -- keeps only what the customer can revoke. + 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. It keeps getting its grants from + -- supabase_admin, so a customer cannot break webhooks by revoking them. + 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 used to grant the five roles itself, with the argument lists of http_get and +-- http_post spelled out. Now it calls the routine. The routine does not catch errors, so a failure +-- aborts the CREATE or ALTER EXTENSION. +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; +$$; + +-- Fires on ALTER EXTENSION too: an extension update can drop and recreate a function, which loses +-- every grant on it, and the routine grants it again. +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(); + +-- Everything above only defines the routine and the trigger. The trigger fires on the next CREATE +-- or ALTER EXTENSION pg_net, which on a project that already has pg_net may never happen. So run +-- the routine once here, to convert the grants that project holds today. +-- +-- The exception block turns a failure into a warning on purpose. This file runs under +-- ON_ERROR_STOP: through dbmate at init and restore, and through complete.sh after pg_upgrade. An +-- error here would abort the whole run and leave the project half migrated or half upgraded. A +-- warning leaves it on its current grants, which still work, and the next CREATE or ALTER +-- EXTENSION runs the routine again. +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..a472a48888 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -163,6 +163,151 @@ 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; + + -- When postgres is a superuser, Postgres records its GRANTs as made by the object owner, i.e. + -- supabase_admin, so every grant below would get the wrong grantor and step 2 would then + -- remove the only grant a role has. This is the state during restore and during pg_upgrade. + -- Do nothing: the migration replay after restore and complete.sh after pg_upgrade run this + -- again once postgres is demoted. + 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; + + -- The http functions are found by name. Earlier the script found them by argument list, which + -- breaks when pg_net changes one. + 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 gets USAGE on the schema and EXECUTE on the http functions with grant option, + -- so it can grant them onward and revoke them. + 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_upgrade used to make http_get and http_post SECURITY DEFINER with search_path = net, so + -- the API roles could queue a request on a pg_net that gave PUBLIC nothing on its tables. + -- From 0.12.0 PUBLIC holds the table and sequence grants, so the functions run as the + -- caller like on every other project, and a trigger on the queue no longer runs as the + -- extension owner. + if (select string_to_array(extversion, '.')::int[] >= array[0, 12, 0] + from pg_extension where extname = 'pg_net') then + foreach http_function in array http_functions loop + execute format('alter function %s security invoker reset search_path', http_function); + end loop; + end if; + + -- pg_net 1.0.0 stops granting worker_restart to PUBLIC and leaves it to the extension owner. + -- Here the owner is supabase_admin, and the customer administers pg_net as postgres: changes + -- a setting, restarts the worker, brings it back when it died. So postgres gets it the same + -- way as the http functions, with grant option, so it can hand it to its own roles. + if to_regprocedure('net.worker_restart()') is not null then + grant execute on function net.worker_restart() to postgres with grant option; + end if; + + -- pg_net grants ALL on its tables to PUBLIC, including TRIGGER. A trigger on the queue runs + -- as the worker, which connects as postgres, so no role other than postgres may create one. + 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. The three API roles get their grants from postgres. + -- + -- This function runs as supabase_admin, and to make the grant work it should ideally be made + -- by postgres. SET ROLE postgres will not work, as SET ROLE is not allowed inside CREATE + -- EXTENSION on the Supabase platform. This restriction is added by supautils. + -- So another way is a SECURITY DEFINER function. One owned by postgres can run there, and + -- its grants are recorded as made by postgres. It is dropped and created on every run, so + -- that a superuser never calls a function body the customer may have 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; + + -- Fresh install: give the three roles USAGE as supabase_admin first, so the conversion below + -- treats a new project exactly like an old one. pg_event_trigger_ddl_commands() works only + -- inside an event trigger and raises anywhere else, which is how this block knows it runs + -- for CREATE EXTENSION pg_net. In any other place a role without USAGE should stay without + -- it, as support or the customer may have revoked it 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; + + -- Conversion. Every API role that holds USAGE on net, from supabase_admin on an old project + -- or from postgres on one already converted, gets USAGE and EXECUTE on the http functions + -- from postgres. A role that holds no USAGE gets nothing. Running this on every extension + -- update is what restores EXECUTE on a function the update recreated. + 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; + -- Remove the supabase_admin grants. The grants from postgres added above stay, so the role + -- keeps only what the customer can revoke. + 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. It keeps getting its grants from + -- supabase_admin, so a customer cannot break webhooks by revoking them. + 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 +417,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 +939,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..a5d5203d87 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -164,6 +164,151 @@ 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; + + -- When postgres is a superuser, Postgres records its GRANTs as made by the object owner, i.e. + -- supabase_admin, so every grant below would get the wrong grantor and step 2 would then + -- remove the only grant a role has. This is the state during restore and during pg_upgrade. + -- Do nothing: the migration replay after restore and complete.sh after pg_upgrade run this + -- again once postgres is demoted. + 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; + + -- The http functions are found by name. Earlier the script found them by argument list, which + -- breaks when pg_net changes one. + 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 gets USAGE on the schema and EXECUTE on the http functions with grant option, + -- so it can grant them onward and revoke them. + 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_upgrade used to make http_get and http_post SECURITY DEFINER with search_path = net, so + -- the API roles could queue a request on a pg_net that gave PUBLIC nothing on its tables. + -- From 0.12.0 PUBLIC holds the table and sequence grants, so the functions run as the + -- caller like on every other project, and a trigger on the queue no longer runs as the + -- extension owner. + if (select string_to_array(extversion, '.')::int[] >= array[0, 12, 0] + from pg_extension where extname = 'pg_net') then + foreach http_function in array http_functions loop + execute format('alter function %s security invoker reset search_path', http_function); + end loop; + end if; + + -- pg_net 1.0.0 stops granting worker_restart to PUBLIC and leaves it to the extension owner. + -- Here the owner is supabase_admin, and the customer administers pg_net as postgres: changes + -- a setting, restarts the worker, brings it back when it died. So postgres gets it the same + -- way as the http functions, with grant option, so it can hand it to its own roles. + if to_regprocedure('net.worker_restart()') is not null then + grant execute on function net.worker_restart() to postgres with grant option; + end if; + + -- pg_net grants ALL on its tables to PUBLIC, including TRIGGER. A trigger on the queue runs + -- as the worker, which connects as postgres, so no role other than postgres may create one. + 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. The three API roles get their grants from postgres. + -- + -- This function runs as supabase_admin, and to make the grant work it should ideally be made + -- by postgres. SET ROLE postgres will not work, as SET ROLE is not allowed inside CREATE + -- EXTENSION on the Supabase platform. This restriction is added by supautils. + -- So another way is a SECURITY DEFINER function. One owned by postgres can run there, and + -- its grants are recorded as made by postgres. It is dropped and created on every run, so + -- that a superuser never calls a function body the customer may have 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; + + -- Fresh install: give the three roles USAGE as supabase_admin first, so the conversion below + -- treats a new project exactly like an old one. pg_event_trigger_ddl_commands() works only + -- inside an event trigger and raises anywhere else, which is how this block knows it runs + -- for CREATE EXTENSION pg_net. In any other place a role without USAGE should stay without + -- it, as support or the customer may have revoked it 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; + + -- Conversion. Every API role that holds USAGE on net, from supabase_admin on an old project + -- or from postgres on one already converted, gets USAGE and EXECUTE on the http functions + -- from postgres. A role that holds no USAGE gets nothing. Running this on every extension + -- update is what restores EXECUTE on a function the update recreated. + 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; + -- Remove the supabase_admin grants. The grants from postgres added above stay, so the role + -- keeps only what the customer can revoke. + 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. It keeps getting its grants from + -- supabase_admin, so a customer cannot break webhooks by revoking them. + 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 +418,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 +940,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..1785b258da 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -178,6 +178,151 @@ 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; + + -- When postgres is a superuser, Postgres records its GRANTs as made by the object owner, i.e. + -- supabase_admin, so every grant below would get the wrong grantor and step 2 would then + -- remove the only grant a role has. This is the state during restore and during pg_upgrade. + -- Do nothing: the migration replay after restore and complete.sh after pg_upgrade run this + -- again once postgres is demoted. + 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; + + -- The http functions are found by name. Earlier the script found them by argument list, which + -- breaks when pg_net changes one. + 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 gets USAGE on the schema and EXECUTE on the http functions with grant option, + -- so it can grant them onward and revoke them. + 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_upgrade used to make http_get and http_post SECURITY DEFINER with search_path = net, so + -- the API roles could queue a request on a pg_net that gave PUBLIC nothing on its tables. + -- From 0.12.0 PUBLIC holds the table and sequence grants, so the functions run as the + -- caller like on every other project, and a trigger on the queue no longer runs as the + -- extension owner. + if (select string_to_array(extversion, '.')::int[] >= array[0, 12, 0] + from pg_extension where extname = 'pg_net') then + foreach http_function in array http_functions loop + execute format('alter function %s security invoker reset search_path', http_function); + end loop; + end if; + + -- pg_net 1.0.0 stops granting worker_restart to PUBLIC and leaves it to the extension owner. + -- Here the owner is supabase_admin, and the customer administers pg_net as postgres: changes + -- a setting, restarts the worker, brings it back when it died. So postgres gets it the same + -- way as the http functions, with grant option, so it can hand it to its own roles. + if to_regprocedure('net.worker_restart()') is not null then + grant execute on function net.worker_restart() to postgres with grant option; + end if; + + -- pg_net grants ALL on its tables to PUBLIC, including TRIGGER. A trigger on the queue runs + -- as the worker, which connects as postgres, so no role other than postgres may create one. + 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. The three API roles get their grants from postgres. + -- + -- This function runs as supabase_admin, and to make the grant work it should ideally be made + -- by postgres. SET ROLE postgres will not work, as SET ROLE is not allowed inside CREATE + -- EXTENSION on the Supabase platform. This restriction is added by supautils. + -- So another way is a SECURITY DEFINER function. One owned by postgres can run there, and + -- its grants are recorded as made by postgres. It is dropped and created on every run, so + -- that a superuser never calls a function body the customer may have 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; + + -- Fresh install: give the three roles USAGE as supabase_admin first, so the conversion below + -- treats a new project exactly like an old one. pg_event_trigger_ddl_commands() works only + -- inside an event trigger and raises anywhere else, which is how this block knows it runs + -- for CREATE EXTENSION pg_net. In any other place a role without USAGE should stay without + -- it, as support or the customer may have revoked it 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; + + -- Conversion. Every API role that holds USAGE on net, from supabase_admin on an old project + -- or from postgres on one already converted, gets USAGE and EXECUTE on the http functions + -- from postgres. A role that holds no USAGE gets nothing. Running this on every extension + -- update is what restores EXECUTE on a function the update recreated. + 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; + -- Remove the supabase_admin grants. The grants from postgres added above stay, so the role + -- keeps only what the customer can revoke. + 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. It keeps getting its grants from + -- supabase_admin, so a customer cannot break webhooks by revoking them. + 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 +432,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 +954,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..23c9e4a9a5 --- /dev/null +++ b/nix/tests/expected/pg_net_grants.out @@ -0,0 +1,463 @@ +-- Checks who holds what on pg_net after extensions.apply_pg_net_grants() runs, in each state a +-- project can be in: fresh, existing, support revoked a role, upgraded, superuser postgres. +-- +-- 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 used to make http_get and http_post SECURITY DEFINER +-- with search_path = net, revoke PUBLIC on them and grant the five roles as supabase_admin. The +-- grants move to postgres and the functions go back to SECURITY INVOKER +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 set search_path = net; +alter function net.http_post security definer set search_path = net; +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, p.proconfig +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 | proconfig +-------------+-----------+----------- + http_delete | f | + http_get | f | + http_post | f | +(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 before +-- the call, so the body planted here never raises +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. +-- +-- net.http_get only queues the request. The background worker sends it and inserts the response +-- on its own time, possibly after this file is done. pg_net_worker_privileges.sql runs next and +-- plants a trigger on net._http_response. So wait here until the response of this request has +-- been inserted, and that trigger only ever sees the one request that test adds itself +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) + +-- 10. postgres can restart the worker and grant that onward. pg_net 1.0.0 stops granting it to +-- PUBLIC, and the customer administers pg_net as postgres +select grantee, grantor, is_grantable +from ( + select 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 pg_proc p + join pg_namespace n on n.oid = p.pronamespace + cross join lateral aclexplode(p.proacl) a + where n.nspname = 'net' + and p.proname = 'worker_restart' +) x +order by grantee collate "C"; + grantee | grantor | is_grantable +----------------+----------------+-------------- + PUBLIC | supabase_admin | f + postgres | supabase_admin | t + supabase_admin | supabase_admin | f +(3 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..7f8dd169b9 --- /dev/null +++ b/nix/tests/sql/pg_net_grants.sql @@ -0,0 +1,174 @@ +-- Checks who holds what on pg_net after extensions.apply_pg_net_grants() runs, in each state a +-- project can be in: fresh, existing, support revoked a role, upgraded, superuser postgres. +-- +-- 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 used to make http_get and http_post SECURITY DEFINER +-- with search_path = net, revoke PUBLIC on them and grant the five roles as supabase_admin. The +-- grants move to postgres and the functions go back to SECURITY INVOKER +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 set search_path = net; +alter function net.http_post security definer set search_path = net; +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, p.proconfig +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 before +-- the call, so the body planted here never raises +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. +-- +-- net.http_get only queues the request. The background worker sends it and inserts the response +-- on its own time, possibly after this file is done. pg_net_worker_privileges.sql runs next and +-- plants a trigger on net._http_response. So wait here until the response of this request has +-- been inserted, and that trigger only ever sees the one request that test adds itself +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; + +-- 10. postgres can restart the worker and grant that onward. pg_net 1.0.0 stops granting it to +-- PUBLIC, and the customer administers pg_net as postgres +select grantee, grantor, is_grantable +from ( + select 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 pg_proc p + join pg_namespace n on n.oid = p.pronamespace + cross join lateral aclexplode(p.proacl) a + where n.nspname = 'net' + and p.proname = 'worker_restart' +) x +order by grantee collate "C"; 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;