Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/publish-nix-pgupgrade-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -84,6 +85,7 @@ jobs:
run: |
mkdir -p /tmp/pg_upgrade_scripts
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
cp migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql /tmp/pg_upgrade_scripts/pg_net_grants.sql
Comment thread
utkarash2991 marked this conversation as resolved.
Resolved
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts

- name: configure aws credentials - staging
Expand Down Expand Up @@ -156,6 +158,7 @@ jobs:
run: |
mkdir -p /tmp/pg_upgrade_scripts
cp -r ansible/files/admin_api_scripts/pg_upgrade_scripts/* /tmp/pg_upgrade_scripts
cp migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql /tmp/pg_upgrade_scripts/pg_net_grants.sql
tar -czvf /tmp/pg_upgrade_scripts.tar.gz -C /tmp/ pg_upgrade_scripts

- name: configure aws credentials - prod
Expand Down
56 changes: 30 additions & 26 deletions ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;

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;

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;

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;
EOF
)

run_sql -c "$PG_NET_GRANT_QUERY" || rc=1
fi

# Patching pg_cron ownership as it resets during upgrade
HAS_PG_CRON_OWNED_BY_POSTGRES=$(run_sql -A -t -c "select count(*) > 0 from pg_extension where extname = 'pg_cron' and extowner::regrole::text = 'postgres';") || rc=1

Expand Down Expand Up @@ -263,6 +238,29 @@ EOF
return $rc
}

# Runs the pg_net grants migration (migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql)
# on the upgraded project. Running projects never replay migrations, only new and restored ones do,
# so this is how they get extensions.apply_pg_net_grants() and the event trigger that calls it. The
# file ends by running the routine, which hands the pg_net grants of anon, authenticated and
# service_role to postgres. It must run after ALTER USER postgres WITH NOSUPERUSER: while postgres is
# a superuser its grants are recorded as made by supabase_admin, and the routine refuses to run.
# The file is put next to the scripts by ansible. Fail-soft like execute_patches.
# One transaction: without it a failure after the drop of the event trigger would leave the
# project with no trigger, so every later create extension pg_net would install with no grants.
function execute_pg_net_grants {
local rc=0
local sql_file="$SCRIPT_DIR/pg_net_grants.sql"

if [ -f "$sql_file" ]; then
run_sql -v ON_ERROR_STOP=1 -1 -f "$sql_file" || rc=1
else
log "WARNING: $sql_file not found"
rc=1
fi

return $rc
}

function complete_pg_upgrade {
if [ -f /tmp/pg-upgrade-status ]; then
log "Upgrade job already started. Bailing."
Expand Down Expand Up @@ -344,7 +342,13 @@ function complete_pg_upgrade {

run_sql -c "ALTER USER postgres WITH NOSUPERUSER;"

log "4.2. Applying authentication scheme updates"
log "4.2. Applying pg_net grants"
execute_pg_net_grants || {
log "WARNING: pg_net grants failed"
warnings=1
}

log "4.3. Applying authentication scheme updates"
retry 3 apply_auth_scheme_updates

sleep 5
Expand Down
10 changes: 10 additions & 0 deletions ansible/tasks/internal/admin-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
- { file: "common.sh" }
- { file: "start_guard.sh" }

# complete.sh runs this migration on the upgraded project, see execute_pg_net_grants there.
# adminapi copies only .sh files out of the upgrade scripts tarball, so the image carries the SQL.
- name: Move the pg_net grants migration to /etc/adminapi/pg_upgrade_scripts/
copy:
src: /tmp/migrations/db/migrations/20261106000000_pg_net_grants_as_postgres.sql
dest: /etc/adminapi/pg_upgrade_scripts/pg_net_grants.sql
remote_src: true
mode: "0644"
owner: adminapi

- name: adminapi - create service file
template:
src: files/adminapi.service.j2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
-- migrate:up

-- pg_net grants used to be made by supabase_admin, and only the grantor can revoke a grant. So a
-- customer who wanted anon or authenticated out of net could not do it themselves. From now on
-- postgres is the grantor: supabase_admin gives postgres USAGE on net and EXECUTE on the http
-- functions WITH GRANT OPTION, and whatever anon, authenticated and service_role already hold is
-- granted again by postgres. New projects along with existing projects get the same three grants
-- from postgres.
--
-- extensions.apply_pg_net_grants() is the only place this happens. It runs here, on every CREATE
-- or ALTER EXTENSION pg_net through the event trigger below, and from complete.sh on upgrade, so
-- one piece of code covers init, restore and upgrade. It raises on failure: the callers below
-- decide whether that is fatal.

create or replace function extensions.apply_pg_net_grants()
returns void
language plpgsql
set search_path to pg_catalog, pg_temp
as $$
declare
http_functions regprocedure[];
http_function regprocedure;
api_role name;
begin
if not exists (select from pg_extension where extname = 'pg_net') then
return;
end if;

-- Postgres records a superuser's GRANT as made by the object owner, supabase_admin. The grants
-- below would get the wrong grantor and step 2 would then drop the only grant a role has.
if exists (select from pg_roles where rolname = 'postgres' and rolsuper) then
raise warning 'apply_pg_net_grants: postgres is a superuser, nothing changed';
return;
end if;

-- Looked up by name, so a pg_net release that changes an argument list does not break this.
select coalesce(array_agg(p.oid::regprocedure order by p.proname), '{}')
into http_functions
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'net'
and p.proname in ('http_get', 'http_post', 'http_delete');

-- 1. postgres can grant onward.
grant usage on schema net to postgres with grant option;
foreach http_function in array http_functions loop
execute format('grant execute on function %s to postgres with grant option', http_function);
end loop;

-- pg_net grants ALL on its tables to PUBLIC. TRIGGER there lets any role run code as the
-- worker, which connects as postgres. postgres keeps TRIGGER for response handlers and
-- TRUNCATE for clearing the queue.
revoke trigger, references, truncate on all tables in schema net from public;
grant trigger, truncate on all tables in schema net to postgres with grant option;

-- 2. SET ROLE is not allowed inside CREATE EXTENSION, but a SECURITY DEFINER function owned by
-- postgres is, and its grants are postgres's. Dropped and created on every run, so a superuser
-- never calls a body the customer changed.
drop function if exists extensions.grant_pg_net_api_access(name, regprocedure[]);
create function extensions.grant_pg_net_api_access(api_role name, http_functions regprocedure[])
returns void
language plpgsql
security definer
set search_path to pg_catalog, pg_temp
as $helper$
begin
execute format('grant usage on schema net to %I', api_role);
if http_functions <> '{}' then
execute format('grant execute on function %s to %I', array_to_string(http_functions, ', '), api_role);
end if;
end;
$helper$;
alter function extensions.grant_pg_net_api_access(name, regprocedure[]) owner to postgres;
revoke execute on function extensions.grant_pg_net_api_access(name, regprocedure[]) from public;

-- On a fresh install, give the three roles USAGE as supabase_admin so the loop below converts
-- them like an old project. pg_event_trigger_ddl_commands() only works inside an event trigger,
-- so this runs for CREATE EXTENSION pg_net and raises everywhere else. Elsewhere a role without
-- USAGE stays without: it may have been revoked (supabase support handling etc.) on purpose.
begin
perform from pg_event_trigger_ddl_commands() ev
join pg_extension e on e.oid = ev.objid
where ev.command_tag = 'CREATE EXTENSION' and e.extname = 'pg_net';
if found then
grant usage on schema net to anon, authenticated, service_role;
end if;
exception when event_trigger_protocol_violated then
null;
end;

-- USAGE on net is the switch. A role that holds it, from supabase_admin before the migration
-- or from postgres after, gets the full set from postgres, so an extension update that drops
-- and recreates a function is repaired too. Whatever supabase_admin granted the three roles
-- goes, so a role support revoked, in full or in part, keeps nothing it cannot revoke itself.
for api_role in
select distinct g.rolname
from pg_namespace n
cross join lateral aclexplode(n.nspacl) a
join pg_roles g on g.oid = a.grantee
where n.nspname = 'net'
and a.privilege_type = 'USAGE'
and a.grantor in ('supabase_admin'::regrole, 'postgres'::regrole)
and g.rolname in ('anon', 'authenticated', 'service_role')
order by g.rolname
loop
perform extensions.grant_pg_net_api_access(api_role, http_functions);
end loop;
-- The helper added the row the customer can revoke. This removes the row they cannot, the
-- one from supabase_admin.
revoke usage on schema net from anon, authenticated, service_role;
foreach http_function in array http_functions loop
execute format('revoke execute on function %s from anon, authenticated, service_role', http_function);
end loop;
Comment thread
utkarash2991 marked this conversation as resolved.
Resolved

-- 3. Database Webhooks run as supabase_functions_admin. Its grants stay with supabase_admin,
-- so a customer cannot break them, and it gets them here rather than through PUBLIC.
if exists (select from pg_roles where rolname = 'supabase_functions_admin') then
grant usage on schema net to supabase_functions_admin;
foreach http_function in array http_functions loop
execute format('grant execute on function %s to supabase_functions_admin', http_function);
end loop;
end if;
end;
$$;

revoke execute on function extensions.apply_pg_net_grants() from public, postgres;

-- The event trigger on CREATE EXTENSION used to grant the five roles itself, with the full
-- argument lists of http_get and http_post spelled out. Now it calls the routine. The routine
-- doesn't catches errors, so one here aborts the CREATE or ALTER EXTENSION and the customer sees
-- the message instead of a server log line.
create or replace function extensions.grant_pg_net_access()
returns event_trigger
language plpgsql
set search_path to pg_catalog, pg_temp
as $$
begin
if not exists (
select from pg_event_trigger_ddl_commands() ev
join pg_extension e on ev.objid = e.oid
where e.extname = 'pg_net'
) then
return;
end if;

if not exists (select from pg_roles where rolname = 'supabase_functions_admin') then
create user supabase_functions_admin noinherit createrole login noreplication;
end if;

perform extensions.apply_pg_net_grants();
end;
$$;

-- An extension update can drop and recreate a function, which loses every grant on it.
drop event trigger if exists issue_pg_net_access;
create event trigger issue_pg_net_access
on ddl_command_end
when tag in ('CREATE EXTENSION', 'ALTER EXTENSION')
execute procedure extensions.grant_pg_net_access();

-- Init and restore run this file with ON_ERROR_STOP, and complete.sh runs it on upgrade, so a
-- failed conversion is a warning here and the project keeps its current grants.
do $$
begin
perform extensions.apply_pg_net_grants();
exception when others then
raise warning 'apply_pg_net_grants: % (%)', sqlerrm, sqlstate;
end;
$$;

-- migrate:down
Loading
Loading