Repository navigation
Make postgres the grantor of pg_net access for anon, authenticated and service_role - #2628
utkarash2991 wants to merge 10 commits into
Conversation
PostgreSQL Extension Dependency Analysis: PR #2628
SummaryNo extensions had dependencies with MAJOR version updates. Full Analysis ResultsPostgreSQL 15 Extension DependenciesPostgreSQL 17 Extension DependenciesOrioleDB 17 Extension Dependencies |
PostgreSQL Package Dependency Analysis: PR #2628
SummaryNo packages had MAJOR version updates. Full Analysis ResultsPostgreSQL 15 Dependency ChangesExtracting PostgreSQL 15 dependencies...
Runtime Closure Size
Raw Dependency ClosurePostgreSQL 17 Dependency ChangesExtracting PostgreSQL 17 dependencies...
Runtime Closure Size
Raw Dependency Closure |
Package version diffCompares built package versions in legacyPackages between this PR and the tip of the base branch, per system. aarch64-linux: Closure size: 372 -> 372 (2 paths added, 2 paths removed, delta +0, disk usage +11.8KiB).x86_64-linux: Closure size: 370 -> 370 (2 paths added, 2 paths removed, delta +0, disk usage +11.8KiB). |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
brainrake
left a comment
There was a problem hiding this comment.
Grants: Changes needed
New projects get no API role grants: supautils runs create extension with both security flags set, so set role postgres fails inside the trigger and step 2 has nothing to convert. The grantor is whoever runs the GRANT, and only a SECURITY DEFINER function owned by postgres changes that here:
SET ROLE/SET SESSION AUTHORIZATION: blocked by both supautils flags.GRANT ... GRANTED BY postgres: PG requires the grantor to be the current user.ALTER SCHEMA net OWNER TO postgres: not dumped for extension members, and hands postgres the extension's objects.- Grant as supabase_admin in the trigger and convert later: new projects differ until their next pg_upgrade.
The inline suggestions add that helper, dropped and recreated on every run so a superuser never calls a body the customer changed, and reduce step 2 to one rule: a role holding USAGE on net from supabase_admin or postgres gets the full set from postgres, and whatever supabase_admin gave the three roles goes. A fresh install plants USAGE as supabase_admin and the same loop converts it, so the trigger body and signature stay. Firing the trigger on ALTER EXTENSION too lets that loop repair an update that recreates a function.
Restore: OK
Restore checks out as written: the multi-file restore runs with postgres as a superuser, so the routine stays out of the way and the migration replay converts the dump's grants afterwards. The superuser branch is what makes that work, so it should stay.
Tested: OK
Verified on this repo's PG 17 with supautils: fresh install, re-run after a customer revoke, migration with anon revoked by support, USAGE-only revoke, update 0.19.5 to 0.20.4, full revoke then update, and the restore sequence.
Smaller changes needed
The publish workflow's paths filter misses the migration, complete.sh should run the file in one transaction, and the nix test should recreate pg_net as postgres so it goes through supautils.
Merge after supabase/platform#41518 is deployed, per the TODO.
|
@brainrake Thanks for reviewing the PR. I think I have addressed all your review comments. Can you take a look please ? |
brainrake
left a comment
There was a problem hiding this comment.
Thanks, all comments addressed.
However, I had to change perspective to see some of this could have been fixed in pg_net. Please create a followup cleanup issue and link me if you agree.
| -- 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); |
There was a problem hiding this comment.
Question: why we need to grant execute on these functions? Functions should be granted to public by default in postgres which is fine for security invoker functions.
There was a problem hiding this comment.
On the fleet PUBLIC mostly does not have EXECUTE on these. complete.sh revokes it from PUBLIC on every pg_upgrade of a project that has pg_net, please see related code here:
postgres/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh
Lines 162 to 166 in c854223
So without an explicit grant anon, authenticated and service_role lose the functions on any project that has been upgraded. Also, granting from postgres also lets postgres revoke it for one role, which the PUBLIC default never allows. And the next proposed pg_net release, supabase/pg_net#340, grants nothing to PUBLIC at all, so this grant is what every role will rely on.
| end; | ||
| $$; | ||
|
|
||
| -- migrate:down |
There was a problem hiding this comment.
I'm finding the code in this file quite hard to review. It's not very clear what is being attempted and which all use cases are handled. Looks like the AI generated code did a lot of clever stuff but that doesn't come through to the reviewer. It's probably doing the right thing but I'm not confident I understand that. I think if at least the comments in the file were re-written by a human to clarify the intent, it'll become easier to parse.
There was a problem hiding this comment.
@imor Thanks for giving it a look. I tried rewriting the comments making it more explanatory . Can you please have a look and see if it serves the purpose?
…d 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.
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.
…ER 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.
…ants 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.
Rewrote the comments in the migration and in complete.sh: what the routine is for, where it runs, which project states it handles, and one plain comment per step. The schema dumps carry the function body, so they change with it. No code change.
5bbe57a to
c4ed5bb
Compare
This comment has been minimized.
This comment has been minimized.
pg_net 1.0.0 stops granting it to PUBLIC and leaves it to the extension owner. The customer administers pg_net as postgres, so postgres gets it the same way as the http functions.
| -- Goal: let postgres manage who can use pg_net. | ||
| -- | ||
| -- Until now the pg_net grants for anon, authenticated and service_role were made by | ||
| -- supabase_admin. In Postgres only the grantor can revoke a grant, so a customer who wanted one of |
There was a problem hiding this comment.
It's not strictly true that only a grantor can revoke a grant, superuser, object owners, and roles that are members of (and inherit privileges from) the grantor can also revoke a grant.
There was a problem hiding this comment.
Makes sense. Updated the comment in recent revision
| schema | net | USAGE | supabase_functions_admin | supabase_admin | f | ||
| (21 rows) | ||
|
|
||
| -- 5. upgraded project: the upgrade scripts make http_get and http_post SECURITY DEFINER, |
There was a problem hiding this comment.
We should seriously consider not making these methods security definers due to security implications.
There was a problem hiding this comment.
yes, I added the change to make it security definer now
| 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 |
There was a problem hiding this comment.
Why is public getting the grant again after an extension recreation? Is it because the extension is old?
There was a problem hiding this comment.
IIUC it is not because the extension is old or because someone grants it. It is Postgres default behaviour: PUBLIC EXECUTE is the built-in default for every function. A new function in schema net has proacl = NULL, which means "use the default": owner ALL plus PUBLIC EXECUTE. The first explicit GRANT on the function makes Postgres write that default out as real entries, with the owner as grantor. Please see below:
psql (17.12)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
Type "help" for help.
postgres=# create function f() returns int language sql as 'select 1';
CREATE FUNCTION
postgres=# select proacl from pg_proc where proname = 'f';
proacl
------------------------------------------------------------------------------------------------------------------------------------------------------------------
{=X/supabase_admin,supabase_admin=X/supabase_admin,postgres=X/supabase_admin,anon=X/supabase_admin,authenticated=X/supabase_admin,service_role=X/supabase_admin}
(1 row)
postgres=# create extension pg_net;
CREATE EXTENSION
postgres=# create function net.f() returns int language sql as 'select 1';
CREATE FUNCTION
postgres=# select pronamespace::regnamespace as schema, proacl from pg_proc where proname = 'f';
schema | proacl
--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------
public | {=X/supabase_admin,supabase_admin=X/supabase_admin,postgres=X/supabase_admin,anon=X/supabase_admin,authenticated=X/supabase_admin,service_role=X/supabase_admin}
net |
(2 rows)
postgres=# grant execute on function net.f() to postgres with grant option;
GRANT
postgres=# select pronamespace::regnamespace as schema, proacl from pg_proc where proname = 'f';
schema | proacl
--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------
public | {=X/supabase_admin,supabase_admin=X/supabase_admin,postgres=X/supabase_admin,anon=X/supabase_admin,authenticated=X/supabase_admin,service_role=X/supabase_admin}
net | {=X/supabase_admin,supabase_admin=X/supabase_admin,postgres=X*/supabase_admin}
(2 rows)
postgres=#
I hope that answers
| prosecdef | owner | ||
| -----------+---------- | ||
| t | postgres | ||
| (1 row) |
There was a problem hiding this comment.
Shouldn't a stronger proof would have been actually running the function and showing that the text "customer body ran as ..." did not appear?
There was a problem hiding this comment.
Yeah correct. Ideally it should have raised exception if the customer modified function is ran, but the test run for function extensions.apply_pg_net_grants() is clean. Adding lines before 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)
Also updated the comment. It should be good now
| -- 7. anon can queue a request with its grants from postgres. Then wait for the worker to finish | ||
| -- it, so it cannot fire a trigger that a later test plants on the response table | ||
| select value as port from test_config where key = 'http_mock_port' \gset | ||
| set role anon; | ||
| select net.http_get('http://localhost:' || :'port' || '/get') as request_id \gset | ||
| reset role; | ||
| select set_config('pg_net_grants.request_id', :'request_id', false) is not null as request_queued; | ||
| request_queued | ||
| ---------------- | ||
| t | ||
| (1 row) | ||
|
|
||
| do $$ | ||
| begin | ||
| for i in 1..40 loop | ||
| exit when exists (select from net._http_response where id = current_setting('pg_net_grants.request_id')::bigint); | ||
| perform pg_sleep(0.25); | ||
| end loop; | ||
| end; | ||
| $$; |
There was a problem hiding this comment.
This step is not clear to me what it's trying to do. Can you clarify please.
There was a problem hiding this comment.
Cool, added clarification comment to clarify it more
…et-grants-as-postgres # Conflicts: # ansible/postgres_release.yml
pg_upgrade used to make http_get and http_post SECURITY DEFINER so the API roles could queue requests on a pg_net that gave PUBLIC nothing on its tables. From pg_net 0.12.0 PUBLIC holds those grants, and the migration from February 2025 already made the functions INVOKER fleet wide, so the routine now resets them on 0.12.0 and up. Review follow-ups: the header says who can revoke a grant, the test comments explain the planted helper and the wait for the worker, and the worker_restart check orders by the text alias through a subquery, since collate on the oid column failed.
In postgres, only the grantor can revoke a grant. Today supabase_admin grants anon, authenticated and service_role access to schema net and the http functions, so a customer cannot take that access away.
What changes
One routine,
extensions.apply_pg_net_grants(), replaces the grant code in the event trigger and in complete.sh.postgres also gets execute on
net.worker_restart()with grant option. pg_net 1.0.0 stops granting it to PUBLIC and leaves it to the extension owner, and the customer administers pg_net as postgres.How it reaches a project
The routine must run after postgres loses superuser. While postgres is a superuser, Postgres records its grants as made by supabase_admin. The routine checks for that and refuses with a warning.
Linear: PSQLEXT-1795, PSQLEXT-1796, PSQLEXT-1797, PSQLEXT-1798