From a44564a165a0ec68a402e72bb9911b427cc9e94b Mon Sep 17 00:00:00 2001 From: Utkarash Singh Date: Fri, 6 Nov 2026 12:42:15 +0000 Subject: [PATCH 1/4] Make postgres the grantor of pg_net access for anon, authenticated and service_role Only the grantor can revoke a grant, and supabase_admin was the grantor, so customers could not take pg_net access away from these roles. One routine now does the grants: postgres gets them with grant option, a role that still holds the old grant gets it again from postgres, new projects get none. The event trigger calls the routine, and complete.sh runs the migration file on upgrade since migrations are not replayed there. --- .../publish-nix-pgupgrade-scripts.yml | 2 + .../pg_upgrade_scripts/complete.sh | 55 +-- ansible/tasks/internal/admin-api.yml | 10 + ...261106000000_pg_net_grants_as_postgres.sql | 116 +++++++ migrations/schema-15.sql | 129 ++++--- migrations/schema-17.sql | 129 ++++--- migrations/schema-orioledb-17.sql | 129 ++++--- nix/checks.nix | 1 + nix/tests/expected/pg_net_grants.out | 325 ++++++++++++++++++ nix/tests/expected/roles.out | 5 +- .../z_multigres-orioledb-17_roles.out | 5 +- nix/tests/sql/pg_net_grants.sql | 120 +++++++ tests/pg_upgrade/tests/01-schema.sql | 24 +- tests/pg_upgrade/tests/99-fixtures.sql | 10 + 14 files changed, 908 insertions(+), 152 deletions(-) create mode 100644 migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql create mode 100644 nix/tests/expected/pg_net_grants.out create mode 100644 nix/tests/sql/pg_net_grants.sql diff --git a/.github/workflows/publish-nix-pgupgrade-scripts.yml b/.github/workflows/publish-nix-pgupgrade-scripts.yml index dbe6f81298..c35701fce6 100644 --- a/.github/workflows/publish-nix-pgupgrade-scripts.yml +++ b/.github/workflows/publish-nix-pgupgrade-scripts.yml @@ -84,6 +84,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 +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..569bed4181 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,28 @@ 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; the tarball carries it only for the test harness. +# Fail-soft like execute_patches. +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 -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 +341,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..2fb024b8f4 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. +# The upgrade scripts tarball only delivers .sh files, 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..a6d563fd42 --- /dev/null +++ b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql @@ -0,0 +1,116 @@ +-- 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 get no grants for those three roles at all. They still +-- reach net through PUBLIC, which pg_net's own install script grants, until pg_net stops doing so. +-- +-- extensions.apply_pg_net_grants() is the one place this happens. It runs here and on every +-- CREATE EXTENSION pg_net through the event trigger below. The upgrade scripts are next, so one +-- piece of code covers init, restore and upgrade. It never raises: init and restore run with +-- ON_ERROR_STOP. + +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; + + -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now + -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by + -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside + -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + for api_role in + select g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + join pg_roles o on o.oid = a.grantor + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and o.rolname = 'supabase_admin' + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + set local role postgres; + execute format('grant usage on schema net to %I', api_role); + foreach http_function in array http_functions loop + execute format('grant execute on function %s to %I', http_function, api_role); + end loop; + reset role; + execute format('revoke usage on schema net from %I', api_role); + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from %I', http_function, api_role); + end loop; + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, + -- so a customer cannot break them. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + end if; +exception when others then + raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; +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. +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; +$$; + +select extensions.apply_pg_net_grants(); + +-- migrate:down diff --git a/migrations/schema-15.sql b/migrations/schema-15.sql index 270cf82029..711c7a64ed 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -163,6 +163,83 @@ 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; + + -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now + -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by + -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside + -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + for api_role in + select g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + join pg_roles o on o.oid = a.grantor + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and o.rolname = 'supabase_admin' + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + set local role postgres; + execute format('grant usage on schema net to %I', api_role); + foreach http_function in array http_functions loop + execute format('grant execute on function %s to %I', http_function, api_role); + end loop; + reset role; + execute format('revoke usage on schema net from %I', api_role); + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from %I', http_function, api_role); + end loop; + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, + -- so a customer cannot break them. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + end if; +exception when others then + raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; +end; +$$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -272,47 +349,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; $$; diff --git a/migrations/schema-17.sql b/migrations/schema-17.sql index 84790a4277..1d0a2354e9 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -164,6 +164,83 @@ 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; + + -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now + -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by + -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside + -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + for api_role in + select g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + join pg_roles o on o.oid = a.grantor + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and o.rolname = 'supabase_admin' + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + set local role postgres; + execute format('grant usage on schema net to %I', api_role); + foreach http_function in array http_functions loop + execute format('grant execute on function %s to %I', http_function, api_role); + end loop; + reset role; + execute format('revoke usage on schema net from %I', api_role); + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from %I', http_function, api_role); + end loop; + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, + -- so a customer cannot break them. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + end if; +exception when others then + raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; +end; +$$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -273,47 +350,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; $$; diff --git a/migrations/schema-orioledb-17.sql b/migrations/schema-orioledb-17.sql index ff82283437..cbb13d75ad 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -178,6 +178,83 @@ 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; + + -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now + -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by + -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside + -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + for api_role in + select g.rolname + from pg_namespace n + cross join lateral aclexplode(n.nspacl) a + join pg_roles g on g.oid = a.grantee + join pg_roles o on o.oid = a.grantor + where n.nspname = 'net' + and a.privilege_type = 'USAGE' + and o.rolname = 'supabase_admin' + and g.rolname in ('anon', 'authenticated', 'service_role') + order by g.rolname + loop + set local role postgres; + execute format('grant usage on schema net to %I', api_role); + foreach http_function in array http_functions loop + execute format('grant execute on function %s to %I', http_function, api_role); + end loop; + reset role; + execute format('revoke usage on schema net from %I', api_role); + foreach http_function in array http_functions loop + execute format('revoke execute on function %s from %I', http_function, api_role); + end loop; + end loop; + + -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, + -- so a customer cannot break them. + if exists (select from pg_roles where rolname = 'supabase_functions_admin') then + grant usage on schema net to supabase_functions_admin; + end if; +exception when others then + raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; +end; +$$; + + -- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: - -- @@ -287,47 +364,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; $$; diff --git a/nix/checks.nix b/nix/checks.nix index 45f0ad1127..e1bcb6b011 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -219,6 +219,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..913758473c --- /dev/null +++ b/nix/tests/expected/pg_net_grants.out @@ -0,0 +1,325 @@ +-- 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 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 have no grant of their own, postgres can +-- grant onward, supabase_functions_admin keeps its grant 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 | postgres | supabase_admin | t + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | postgres | supabase_admin | t + 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 +(14 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 | postgres | supabase_admin | t + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | postgres | supabase_admin | t + 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 +(14 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 +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_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_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 + 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 +(22 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_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_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 + 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 +(18 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; +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_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 +(26 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: back to the fresh state +drop extension pg_net; +create extension pg_net; +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 | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | postgres | supabase_admin | t + 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 +(14 rows) + +-- 7. anon can still queue a request. PUBLIC covers it until pg_net stops granting PUBLIC +select value as port from test_config where key = 'http_mock_port' \gset +set role anon; +select net.http_get('http://localhost:' || :'port' || '/get') is not null as request_queued; + request_queued +---------------- + t +(1 row) + +reset role; +-- 8. the tables and the sequence are not touched: everything 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, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + _http_response | supabase_admin | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + http_request_queue | PUBLIC | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + 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 +(6 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 | postgres | supabase_admin | t + function | http_delete | EXECUTE | supabase_admin | supabase_admin | f + function | http_get | EXECUTE | PUBLIC | supabase_admin | f + function | http_get | EXECUTE | postgres | supabase_admin | t + function | http_get | EXECUTE | supabase_admin | supabase_admin | f + function | http_post | EXECUTE | PUBLIC | supabase_admin | f + function | http_post | EXECUTE | postgres | supabase_admin | t + function | http_post | EXECUTE | supabase_admin | supabase_admin | f + schema | net | USAGE | PUBLIC | supabase_admin | f + schema | net | USAGE | postgres | supabase_admin | t + 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 +(14 rows) + diff --git a/nix/tests/expected/roles.out b/nix/tests/expected/roles.out index 78a60dc24d..0b894871d1 100644 --- a/nix/tests/expected/roles.out +++ b/nix/tests/expected/roles.out @@ -381,10 +381,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; information_schema | CREATE | supabase_admin | supabase_admin information_schema | USAGE | supabase_admin | supabase_admin net | CREATE | supabase_admin | supabase_admin - net | USAGE | anon | supabase_admin - net | USAGE | authenticated | supabase_admin net | USAGE | postgres | supabase_admin - net | USAGE | service_role | supabase_admin net | USAGE | supabase_admin | supabase_admin net | USAGE | supabase_functions_admin | supabase_admin pg_catalog | CREATE | supabase_admin | supabase_admin @@ -523,7 +520,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; vault | USAGE | postgres | supabase_admin vault | USAGE | service_role | supabase_admin vault | USAGE | supabase_admin | supabase_admin -(389 rows) +(386 rows) -- postgres can alter API roles' timeout set role postgres; diff --git a/nix/tests/expected/z_multigres-orioledb-17_roles.out b/nix/tests/expected/z_multigres-orioledb-17_roles.out index 9e530eee17..1735d03ff6 100644 --- a/nix/tests/expected/z_multigres-orioledb-17_roles.out +++ b/nix/tests/expected/z_multigres-orioledb-17_roles.out @@ -375,10 +375,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; information_schema | CREATE | supabase_admin | supabase_admin information_schema | USAGE | supabase_admin | supabase_admin net | CREATE | supabase_admin | supabase_admin - net | USAGE | anon | supabase_admin - net | USAGE | authenticated | supabase_admin net | USAGE | postgres | supabase_admin - net | USAGE | service_role | supabase_admin net | USAGE | supabase_admin | supabase_admin net | USAGE | supabase_functions_admin | supabase_admin pg_catalog | CREATE | supabase_admin | supabase_admin @@ -491,7 +488,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; vault | USAGE | postgres | supabase_admin vault | USAGE | service_role | supabase_admin vault | USAGE | supabase_admin | supabase_admin -(363 rows) +(360 rows) -- postgres can alter API roles' timeout set role postgres; diff --git a/nix/tests/sql/pg_net_grants.sql b/nix/tests/sql/pg_net_grants.sql new file mode 100644 index 0000000000..2bbd1d8d6b --- /dev/null +++ b/nix/tests/sql/pg_net_grants.sql @@ -0,0 +1,120 @@ +-- 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 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 have no grant of their own, postgres can +-- grant onward, supabase_functions_admin keeps its grant 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 +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; +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: back to the fresh state +drop extension pg_net; +create extension pg_net; +select * from net_acl; + +-- 7. anon can still queue a request. PUBLIC covers it until pg_net stops granting PUBLIC +select value as port from test_config where key = 'http_mock_port' \gset +set role anon; +select net.http_get('http://localhost:' || :'port' || '/get') is not null as request_queued; +reset role; + +-- 8. the tables and the sequence are not touched: everything 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..7ef906f7fa 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(22); select has_schema('public'); select has_schema('auth'); @@ -35,5 +35,27 @@ 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 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'); +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..b7d94d5f61 100644 --- a/tests/pg_upgrade/tests/99-fixtures.sql +++ b/tests/pg_upgrade/tests/99-fixtures.sql @@ -26,3 +26,13 @@ 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, and +-- the old upgrade script granted pg_net to anon, authenticated and service_role as supabase_admin, +-- then support revoked anon by hand. The upgrade must ship the routine, hand the grants of +-- authenticated and service_role to postgres and leave anon without one. +drop function extensions.apply_pg_net_grants(); +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; From 5c72afba67fa205520b9fe09e4bb0a4846e07a5b Mon Sep 17 00:00:00 2001 From: Utkarash Singh Date: Mon, 9 Nov 2026 15:45:19 +0000 Subject: [PATCH 2/4] Grant the webhooks role execute on the pg_net http functions supabase_functions_admin only got USAGE on net from the routine and relied on PUBLIC for EXECUTE. pg_net will stop granting PUBLIC, so the routine now grants the role EXECUTE on http_get, http_post and http_delete as supabase_admin, the same way it grants USAGE. --- ...261106000000_pg_net_grants_as_postgres.sql | 7 ++-- migrations/schema-15.sql | 7 ++-- migrations/schema-17.sql | 7 ++-- migrations/schema-orioledb-17.sql | 7 ++-- nix/tests/expected/pg_net_grants.out | 35 ++++++++++++++----- nix/tests/sql/pg_net_grants.sql | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql index a6d563fd42..05fb64158b 100644 --- a/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql +++ b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql @@ -75,10 +75,13 @@ begin end loop; end loop; - -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, - -- so a customer cannot break them. + -- 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; exception when others then raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; diff --git a/migrations/schema-15.sql b/migrations/schema-15.sql index 711c7a64ed..b64eb31b74 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -229,10 +229,13 @@ begin end loop; end loop; - -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, - -- so a customer cannot break them. + -- 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; exception when others then raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; diff --git a/migrations/schema-17.sql b/migrations/schema-17.sql index 1d0a2354e9..3bff8a24d4 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -230,10 +230,13 @@ begin end loop; end loop; - -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, - -- so a customer cannot break them. + -- 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; exception when others then raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; diff --git a/migrations/schema-orioledb-17.sql b/migrations/schema-orioledb-17.sql index cbb13d75ad..75dfdd262e 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -244,10 +244,13 @@ begin end loop; end loop; - -- 3. Database Webhooks run as supabase_functions_admin. Its grant stays with supabase_admin, - -- so a customer cannot break them. + -- 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; exception when others then raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; diff --git a/nix/tests/expected/pg_net_grants.out b/nix/tests/expected/pg_net_grants.out index 913758473c..bb0743a072 100644 --- a/nix/tests/expected/pg_net_grants.out +++ b/nix/tests/expected/pg_net_grants.out @@ -38,25 +38,28 @@ where r.rolname in ('postgres', 'anon', 'authenticated', 'service_role', 'supaba group by r.rolname order by r.rolname collate "C"; -- 1. fresh project: anon, authenticated and service_role have no grant of their own, postgres can --- grant onward, supabase_functions_admin keeps its grant from supabase_admin +-- 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 | postgres | supabase_admin | t 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 | 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 | 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 | CREATE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_functions_admin | supabase_admin | f -(14 rows) +(17 rows) select * from net_access; rolname | schema_usage | http_execute @@ -81,18 +84,21 @@ select * from net_acl; function | http_delete | EXECUTE | PUBLIC | supabase_admin | f function | http_delete | EXECUTE | postgres | supabase_admin | t 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 | 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 | 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 | CREATE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_functions_admin | supabase_admin | f -(14 rows) +(17 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 @@ -113,16 +119,19 @@ select * from net_acl; 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 @@ -130,7 +139,7 @@ select * from net_acl; 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 -(22 rows) +(25 rows) -- 4. the customer revokes authenticated. A later run does not bring it back set role postgres; @@ -150,21 +159,24 @@ select * from net_acl; 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 -(18 rows) +(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 @@ -191,6 +203,7 @@ select * from net_acl; 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 @@ -211,7 +224,7 @@ select * from net_acl; 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 -(26 rows) +(27 rows) select * from net_access; rolname | schema_usage | http_execute @@ -245,18 +258,21 @@ select * from net_acl; function | http_delete | EXECUTE | PUBLIC | supabase_admin | f function | http_delete | EXECUTE | postgres | supabase_admin | t 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 | 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 | 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 | CREATE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_functions_admin | supabase_admin | f -(14 rows) +(17 rows) -- 7. anon can still queue a request. PUBLIC covers it until pg_net stops granting PUBLIC select value as port from test_config where key = 'http_mock_port' \gset @@ -310,16 +326,19 @@ select * from net_acl; function | http_delete | EXECUTE | PUBLIC | supabase_admin | f function | http_delete | EXECUTE | postgres | supabase_admin | t 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 | 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 | 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 | CREATE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_admin | supabase_admin | f schema | net | USAGE | supabase_functions_admin | supabase_admin | f -(14 rows) +(17 rows) diff --git a/nix/tests/sql/pg_net_grants.sql b/nix/tests/sql/pg_net_grants.sql index 2bbd1d8d6b..97993152b9 100644 --- a/nix/tests/sql/pg_net_grants.sql +++ b/nix/tests/sql/pg_net_grants.sql @@ -41,7 +41,7 @@ group by r.rolname order by r.rolname collate "C"; -- 1. fresh project: anon, authenticated and service_role have no grant of their own, postgres can --- grant onward, supabase_functions_admin keeps its grant from supabase_admin +-- grant onward, supabase_functions_admin keeps its grants from supabase_admin select * from net_acl; select * from net_access; From f31939772bd8fa853b74721661ac12bf1dce9b84 Mon Sep 17 00:00:00 2001 From: Utkarash Singh Date: Wed, 18 Nov 2026 16:42:02 +0000 Subject: [PATCH 3/4] Grant the API roles through a helper owned by postgres and take TRIGGER on the net tables off PUBLIC A SECURITY DEFINER helper owned by postgres makes the grants for anon, authenticated and service_role, so they are recorded as made by postgres even inside CREATE EXTENSION where SET ROLE is refused. New projects get the same three grants from postgres. The routine no longer catches errors: the trigger path fails the CREATE or ALTER EXTENSION, the migration and complete.sh warn. The trigger also fires on ALTER EXTENSION. complete.sh runs the file in one transaction. PUBLIC loses TRIGGER, REFERENCES and TRUNCATE on the net tables. postgres keeps TRIGGER and TRUNCATE with grant option. --- .../publish-nix-pgupgrade-scripts.yml | 1 + .../pg_upgrade_scripts/complete.sh | 7 +- ansible/tasks/internal/admin-api.yml | 2 +- ...261106000000_pg_net_grants_as_postgres.sql | 106 +++++++++++++----- migrations/schema-15.sql | 75 +++++++++---- migrations/schema-17.sql | 75 +++++++++---- migrations/schema-orioledb-17.sql | 75 +++++++++---- nix/tests/expected/pg_net_grants.out | 104 +++++++++++++++-- nix/tests/expected/roles.out | 5 +- .../z_multigres-orioledb-17_roles.out | 5 +- nix/tests/sql/pg_net_grants.sql | 31 ++++- tests/pg_upgrade/tests/01-schema.sql | 12 +- tests/pg_upgrade/tests/99-fixtures.sql | 22 +++- 13 files changed, 405 insertions(+), 115 deletions(-) diff --git a/.github/workflows/publish-nix-pgupgrade-scripts.yml b/.github/workflows/publish-nix-pgupgrade-scripts.yml index beaa249d39..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: 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 569bed4181..e4dc2c8f98 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh @@ -244,14 +244,15 @@ EOF # 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; the tarball carries it only for the test harness. -# Fail-soft like execute_patches. +# 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 -f "$sql_file" || rc=1 + run_sql -v ON_ERROR_STOP=1 -1 -f "$sql_file" || rc=1 else log "WARNING: $sql_file not found" rc=1 diff --git a/ansible/tasks/internal/admin-api.yml b/ansible/tasks/internal/admin-api.yml index 2fb024b8f4..7993b253d7 100644 --- a/ansible/tasks/internal/admin-api.yml +++ b/ansible/tasks/internal/admin-api.yml @@ -77,7 +77,7 @@ - { file: "start_guard.sh" } # complete.sh runs this migration on the upgraded project, see execute_pg_net_grants there. -# The upgrade scripts tarball only delivers .sh files, so the image carries the SQL. +# 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 diff --git a/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql index 05fb64158b..9153d4ea11 100644 --- a/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql +++ b/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql @@ -4,13 +4,13 @@ -- 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 get no grants for those three roles at all. They still --- reach net through PUBLIC, which pg_net's own install script grants, until pg_net stops doing so. +-- granted again by postgres. New projects along with existing projects get the same three grants +-- from postgres. -- --- extensions.apply_pg_net_grants() is the one place this happens. It runs here and on every --- CREATE EXTENSION pg_net through the event trigger below. The upgrade scripts are next, so one --- piece of code covers init, restore and upgrade. It never raises: init and restore run with --- ON_ERROR_STOP. +-- 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 @@ -47,32 +47,69 @@ begin execute format('grant execute on function %s to postgres with grant option', http_function); end loop; - -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now - -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by - -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside - -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + -- 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 g.rolname + select distinct g.rolname from pg_namespace n cross join lateral aclexplode(n.nspacl) a join pg_roles g on g.oid = a.grantee - join pg_roles o on o.oid = a.grantor where n.nspname = 'net' and a.privilege_type = 'USAGE' - and o.rolname = 'supabase_admin' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) and g.rolname in ('anon', 'authenticated', 'service_role') order by g.rolname loop - set local role postgres; - execute format('grant usage on schema net to %I', api_role); - foreach http_function in array http_functions loop - execute format('grant execute on function %s to %I', http_function, api_role); - end loop; - reset role; - execute format('revoke usage on schema net from %I', api_role); - foreach http_function in array http_functions loop - execute format('revoke execute on function %s from %I', http_function, api_role); - end 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, @@ -83,15 +120,15 @@ begin execute format('grant execute on function %s to supabase_functions_admin', http_function); end loop; end if; -exception when others then - raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; 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. +-- 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 @@ -114,6 +151,21 @@ begin end; $$; -select extensions.apply_pg_net_grants(); +-- 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 b64eb31b74..c94ee197ee 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -201,32 +201,69 @@ begin execute format('grant execute on function %s to postgres with grant option', http_function); end loop; - -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now - -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by - -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside - -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + -- 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 g.rolname + select distinct g.rolname from pg_namespace n cross join lateral aclexplode(n.nspacl) a join pg_roles g on g.oid = a.grantee - join pg_roles o on o.oid = a.grantor where n.nspname = 'net' and a.privilege_type = 'USAGE' - and o.rolname = 'supabase_admin' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) and g.rolname in ('anon', 'authenticated', 'service_role') order by g.rolname loop - set local role postgres; - execute format('grant usage on schema net to %I', api_role); - foreach http_function in array http_functions loop - execute format('grant execute on function %s to %I', http_function, api_role); - end loop; - reset role; - execute format('revoke usage on schema net from %I', api_role); - foreach http_function in array http_functions loop - execute format('revoke execute on function %s from %I', http_function, api_role); - end 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, @@ -237,8 +274,6 @@ begin execute format('grant execute on function %s to supabase_functions_admin', http_function); end loop; end if; -exception when others then - raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; end; $$; @@ -874,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 3bff8a24d4..28cb4b3ede 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -202,32 +202,69 @@ begin execute format('grant execute on function %s to postgres with grant option', http_function); end loop; - -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now - -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by - -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside - -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + -- 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 g.rolname + select distinct g.rolname from pg_namespace n cross join lateral aclexplode(n.nspacl) a join pg_roles g on g.oid = a.grantee - join pg_roles o on o.oid = a.grantor where n.nspname = 'net' and a.privilege_type = 'USAGE' - and o.rolname = 'supabase_admin' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) and g.rolname in ('anon', 'authenticated', 'service_role') order by g.rolname loop - set local role postgres; - execute format('grant usage on schema net to %I', api_role); - foreach http_function in array http_functions loop - execute format('grant execute on function %s to %I', http_function, api_role); - end loop; - reset role; - execute format('revoke usage on schema net from %I', api_role); - foreach http_function in array http_functions loop - execute format('revoke execute on function %s from %I', http_function, api_role); - end 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, @@ -238,8 +275,6 @@ begin execute format('grant execute on function %s to supabase_functions_admin', http_function); end loop; end if; -exception when others then - raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; end; $$; @@ -875,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 75dfdd262e..67f3e5fdb0 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -216,32 +216,69 @@ begin execute format('grant execute on function %s to postgres with grant option', http_function); end loop; - -- 2. A role that still holds the old direct grant from supabase_admin keeps its access, now - -- granted by postgres, and the supabase_admin grant goes. A role support already revoked by - -- hand has no direct grant and gets nothing. Postgres does not allow SET ROLE inside - -- CREATE EXTENSION, but a fresh install has no direct grants, so this loop is empty there. + -- 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 g.rolname + select distinct g.rolname from pg_namespace n cross join lateral aclexplode(n.nspacl) a join pg_roles g on g.oid = a.grantee - join pg_roles o on o.oid = a.grantor where n.nspname = 'net' and a.privilege_type = 'USAGE' - and o.rolname = 'supabase_admin' + and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole) and g.rolname in ('anon', 'authenticated', 'service_role') order by g.rolname loop - set local role postgres; - execute format('grant usage on schema net to %I', api_role); - foreach http_function in array http_functions loop - execute format('grant execute on function %s to %I', http_function, api_role); - end loop; - reset role; - execute format('revoke usage on schema net from %I', api_role); - foreach http_function in array http_functions loop - execute format('revoke execute on function %s from %I', http_function, api_role); - end 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, @@ -252,8 +289,6 @@ begin execute format('grant execute on function %s to supabase_functions_admin', http_function); end loop; end if; -exception when others then - raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate; end; $$; @@ -889,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/tests/expected/pg_net_grants.out b/nix/tests/expected/pg_net_grants.out index bb0743a072..ecd02f40c4 100644 --- a/nix/tests/expected/pg_net_grants.out +++ b/nix/tests/expected/pg_net_grants.out @@ -1,6 +1,6 @@ -- 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 EXTENSION pg_net. +-- 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 @@ -37,29 +37,41 @@ where r.rolname in ('postgres', 'anon', 'authenticated', 'service_role', 'supaba 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 have no grant of their own, postgres can +-- 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 -(17 rows) +(29 rows) select * from net_access; rolname | schema_usage | http_execute @@ -82,27 +94,43 @@ 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 -(17 rows) +(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(); @@ -183,6 +211,10 @@ select * from net_acl; -- 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; @@ -249,32 +281,65 @@ order by p.proname; http_post | t (3 rows) --- 6. drop and recreate the extension: back to the fresh state +-- 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 -(17 rows) +(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 still queue a request. PUBLIC covers it until pg_net stops granting PUBLIC +-- 7. anon can queue a request with its grants from postgres select value as port from test_config where key = 'http_mock_port' \gset set role anon; select net.http_get('http://localhost:' || :'port' || '/get') is not null as request_queued; @@ -284,7 +349,8 @@ select net.http_get('http://localhost:' || :'port' || '/get') is not null as req (1 row) reset role; --- 8. the tables and the sequence are not touched: everything pg_net grants PUBLIC is still there +-- 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, @@ -301,13 +367,15 @@ group by relname, grantee order by relname, grantee collate "C"; relname | grantee | privileges ---------------------------+----------------+--------------------------------------------------------------- - _http_response | PUBLIC | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE + _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, 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 -(6 rows) +(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 @@ -324,21 +392,33 @@ 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 -(17 rows) +(29 rows) diff --git a/nix/tests/expected/roles.out b/nix/tests/expected/roles.out index 0b894871d1..78a60dc24d 100644 --- a/nix/tests/expected/roles.out +++ b/nix/tests/expected/roles.out @@ -381,7 +381,10 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; information_schema | CREATE | supabase_admin | supabase_admin information_schema | USAGE | supabase_admin | supabase_admin net | CREATE | supabase_admin | supabase_admin + net | USAGE | anon | supabase_admin + net | USAGE | authenticated | supabase_admin net | USAGE | postgres | supabase_admin + net | USAGE | service_role | supabase_admin net | USAGE | supabase_admin | supabase_admin net | USAGE | supabase_functions_admin | supabase_admin pg_catalog | CREATE | supabase_admin | supabase_admin @@ -520,7 +523,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; vault | USAGE | postgres | supabase_admin vault | USAGE | service_role | supabase_admin vault | USAGE | supabase_admin | supabase_admin -(386 rows) +(389 rows) -- postgres can alter API roles' timeout set role postgres; diff --git a/nix/tests/expected/z_multigres-orioledb-17_roles.out b/nix/tests/expected/z_multigres-orioledb-17_roles.out index 1735d03ff6..9e530eee17 100644 --- a/nix/tests/expected/z_multigres-orioledb-17_roles.out +++ b/nix/tests/expected/z_multigres-orioledb-17_roles.out @@ -375,7 +375,10 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; information_schema | CREATE | supabase_admin | supabase_admin information_schema | USAGE | supabase_admin | supabase_admin net | CREATE | supabase_admin | supabase_admin + net | USAGE | anon | supabase_admin + net | USAGE | authenticated | supabase_admin net | USAGE | postgres | supabase_admin + net | USAGE | service_role | supabase_admin net | USAGE | supabase_admin | supabase_admin net | USAGE | supabase_functions_admin | supabase_admin pg_catalog | CREATE | supabase_admin | supabase_admin @@ -488,7 +491,7 @@ order by schema_order, schema_name, privilege_type, grantee, default_for; vault | USAGE | postgres | supabase_admin vault | USAGE | service_role | supabase_admin vault | USAGE | supabase_admin | supabase_admin -(360 rows) +(363 rows) -- postgres can alter API roles' timeout set role postgres; diff --git a/nix/tests/sql/pg_net_grants.sql b/nix/tests/sql/pg_net_grants.sql index 97993152b9..6fd6cdd22f 100644 --- a/nix/tests/sql/pg_net_grants.sql +++ b/nix/tests/sql/pg_net_grants.sql @@ -1,6 +1,6 @@ -- 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 EXTENSION pg_net. +-- 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 @@ -40,7 +40,7 @@ where r.rolname in ('postgres', 'anon', 'authenticated', 'service_role', 'supaba group by r.rolname order by r.rolname collate "C"; --- 1. fresh project: anon, authenticated and service_role have no grant of their own, postgres can +-- 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; @@ -52,6 +52,10 @@ 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(); @@ -70,6 +74,10 @@ select * from net_acl; -- 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; @@ -85,18 +93,31 @@ where n.nspname = 'net' and p.proname in ('http_get', 'http_post', 'http_delete') order by p.proname; --- 6. drop and recreate the extension: back to the fresh state +-- 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; --- 7. anon can still queue a request. PUBLIC covers it until pg_net stops granting PUBLIC +-- 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 select value as port from test_config where key = 'http_mock_port' \gset set role anon; select net.http_get('http://localhost:' || :'port' || '/get') is not null as request_queued; reset role; --- 8. the tables and the sequence are not touched: everything pg_net grants PUBLIC is still there +-- 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, diff --git a/tests/pg_upgrade/tests/01-schema.sql b/tests/pg_upgrade/tests/01-schema.sql index 7ef906f7fa..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(22); +SELECT plan(24); select has_schema('public'); select has_schema('auth'); @@ -37,6 +37,10 @@ SELECT ok( 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 @@ -51,6 +55,12 @@ SELECT is( 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')$$, diff --git a/tests/pg_upgrade/tests/99-fixtures.sql b/tests/pg_upgrade/tests/99-fixtures.sql index b7d94d5f61..5ad213e37f 100644 --- a/tests/pg_upgrade/tests/99-fixtures.sql +++ b/tests/pg_upgrade/tests/99-fixtures.sql @@ -27,11 +27,25 @@ create event trigger issue_pg_cron_access 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, and --- the old upgrade script granted pg_net to anon, authenticated and service_role as supabase_admin, --- then support revoked anon by hand. The upgrade must ship the routine, hand the grants of --- authenticated and service_role to postgres and leave anon without one. +-- 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; From 99f8aeda960b72e48153f03ad309dce40194cc39 Mon Sep 17 00:00:00 2001 From: Utkarash Singh Date: Wed, 18 Nov 2026 21:43:02 +0000 Subject: [PATCH 4/4] Fix the schema dumps and wait for the queued request in the pg_net grants test pg_dump quotes the routine body with $_$ now that it contains $$. The test's queued request could still be in flight when the next test plants a trigger on the response table, so wait for the worker to finish it. --- migrations/schema-15.sql | 4 ++-- migrations/schema-17.sql | 4 ++-- migrations/schema-orioledb-17.sql | 4 ++-- nix/tests/expected/pg_net_grants.out | 16 +++++++++++++--- nix/tests/sql/pg_net_grants.sql | 14 ++++++++++++-- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/migrations/schema-15.sql b/migrations/schema-15.sql index cfe2cdad23..5e9ccc1bd2 100644 --- a/migrations/schema-15.sql +++ b/migrations/schema-15.sql @@ -170,7 +170,7 @@ $$; CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' - AS $$ + AS $_$ declare http_functions regprocedure[]; http_function regprocedure; @@ -275,7 +275,7 @@ begin end loop; end if; end; -$$; +$_$; -- diff --git a/migrations/schema-17.sql b/migrations/schema-17.sql index a2d4d9b039..8ddc30d9fc 100644 --- a/migrations/schema-17.sql +++ b/migrations/schema-17.sql @@ -171,7 +171,7 @@ $$; CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' - AS $$ + AS $_$ declare http_functions regprocedure[]; http_function regprocedure; @@ -276,7 +276,7 @@ begin end loop; end if; end; -$$; +$_$; -- diff --git a/migrations/schema-orioledb-17.sql b/migrations/schema-orioledb-17.sql index 67f3e5fdb0..18ee12fa47 100644 --- a/migrations/schema-orioledb-17.sql +++ b/migrations/schema-orioledb-17.sql @@ -185,7 +185,7 @@ $$; CREATE FUNCTION extensions.apply_pg_net_grants() RETURNS void LANGUAGE plpgsql SET search_path TO 'pg_catalog', 'pg_temp' - AS $$ + AS $_$ declare http_functions regprocedure[]; http_function regprocedure; @@ -290,7 +290,7 @@ begin end loop; end if; end; -$$; +$_$; -- diff --git a/nix/tests/expected/pg_net_grants.out b/nix/tests/expected/pg_net_grants.out index ecd02f40c4..4c88622bfa 100644 --- a/nix/tests/expected/pg_net_grants.out +++ b/nix/tests/expected/pg_net_grants.out @@ -339,16 +339,26 @@ select prosecdef, proowner::regrole as owner t | postgres (1 row) --- 7. anon can queue a request with its grants from postgres +-- 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') is not null as request_queued; +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) -reset role; +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 diff --git a/nix/tests/sql/pg_net_grants.sql b/nix/tests/sql/pg_net_grants.sql index 6fd6cdd22f..e3a0558e2b 100644 --- a/nix/tests/sql/pg_net_grants.sql +++ b/nix/tests/sql/pg_net_grants.sql @@ -110,11 +110,21 @@ 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 +-- 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') is not null as request_queued; +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