Skip to content

feat!: add row level security over shared tables - #303

Open
AndrewJackson2020 wants to merge 6 commits into
masterfrom
row_level_security
Open

AndrewJackson2020 wants to merge 6 commits into
masterfrom
row_level_security

Conversation

@AndrewJackson2020

@AndrewJackson2020 AndrewJackson2020 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Currently any user that has any ability to read the pg_net request or queue tables also has the ability to read every other users data on these tables. This is particularly problematic if users are using secrets (passwords, API keys, etc) in a multi tenant database environment.

@steve-chavez would appreciate any feedback on this PR in terms of the historical goals/design decisions of pg_net.

Breaking change

net.http_get, net.http_post and net.http_delete get a trailing rolename text default 'public' parameter. The upgrade script creates the new functions and drops the old ones:

drop function net.http_get(text, jsonb, jsonb, int);
drop function net.http_post(text, jsonb, jsonb, jsonb, int);
drop function net.http_delete(text, jsonb, jsonb, int, jsonb);

Calls keep working, the new parameter has a default. What breaks is anything that names the old signature: ALTER FUNCTION, GRANT, REVOKE, DROP or a regprocedure lookup on the five argument form fails with "function does not exist". Grants made on the old functions are dropped with them and have to be made again on the new ones.

The two tables also get row level security. Rows tagged with a role are only visible to members of that role. Existing rows and rows queued without a rolename are tagged public and stay visible to everyone who could read them before.

@AndrewJackson2020
AndrewJackson2020 marked this pull request as ready for review October 1, 2026 14:41
@AndrewJackson2020
AndrewJackson2020 requested a review from a team as a code owner October 1, 2026 14:41
@steve-chavez

Copy link
Copy Markdown
Member

@AndrewJackson2020 I wonder if this is the right design 🤔 :

  1. If we switch to callbacks (Change table queue to in-memory queue and add callbacks #62) then the table can be controlled by the user. Side-stepping the need for RLS.
  2. Another idea would be allowing the user to create the net tables on a custom schema. This would also avoid RLS.

1 would be a major redesign, 2 would keep it but seems more messy than yours.

Since we're going to do a breaking change soon, perhaps we should try 1 anyways? WDYT?

@AndrewJackson2020

Copy link
Copy Markdown
Contributor Author

I wonder if this is the right design 🤔

maybe, maybe not. hoping we can figure this out. I will say that this design is consistent with pg_cron so there is prior art to draw from. The only thing analogous in postgres contrib is probably pg_stat_statements which uses a hybrid shared memory data structure/on disk flat file. the pg_stat_statements approach is a lot more complex than an RLS table.

If we switch to callbacks

This is an interesting idea. It's a lot more true to the interface of other asynchronous frameworks. I also feel like it is harder for users to reason about. It also feels like much higher touch than this PR. I don't think I have a good opinion on this right now.

Another idea would be allowing the user to create the net tables on a custom schema

This also feels a bit higher touch than the proposed solution. Interesting idea though. What would the interface look like? You would just provide the schema/tablename of the intended request/response tables to the function calls? The user would just need to make sure that the table is set up with the correct schema and update it accordingly if we ever decide to add/remove a column, change a data type, etc? I think it would allow more granular ability to grant access though. I can definitely see the row level security change breaking applications where one user executes a requests and lets another user pick up the response and do some processing on it or whatever.

@steve-chavez

Copy link
Copy Markdown
Member

I will say that this design is consistent with pg_cron so there is prior art to draw from

TIL about that https://github.com/citusdata/pg_cron/blob/main/pg_cron.sql#L29. That does give more confidence in extensions working fine with RLS.

It also feels like much higher touch than this PR

Agree.

The user would just need to make sure that the table is set up with the correct schema and update it accordingly if we ever decide to add/remove a column, change a data type, etc?

Right, looks more complex and prone to failure.

I can definitely see the row level security change breaking applications where one user executes a requests and lets another user pick up the response and do some processing on it or whatever.

Seems acceptable to do on next major release.

Comment thread src/worker.c Resolved
@AndrewJackson2020

Copy link
Copy Markdown
Contributor Author

I went ahead and refactored this in a way that I think will solve a lot of the security issues that people have with pg_net without breaking changes.

The idea now is that instead of a username column we have a role name column which RLS is based off of. By default this column will get 'public' inserted into it. If the user wants to restrict who sees their rows they can insert their own username or a role. RLS will allow all users to access 'public' rows but will restrict based on identity/group membership if something other than public is in there. Eventually we may want to do a breaking change to force people to select who they want to see their stuff but I think we can deploy this first without breaking change and decide on that later.

@utkarash2991 @steve-chavez lmk what you guys think.

@steve-chavez

Copy link
Copy Markdown
Member

By default this column will get 'public' inserted into it. If the user wants to restrict who sees their rows they can insert their own username or a role.

Looks good to not cause a breaking change 👍

Comment thread sql/pg_net.sql Outdated
timeout_milliseconds int not null
timeout_milliseconds int not null,
rolename text not null default 'public'
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndrewJackson2020 just adding here for completeness. We need to make required changes here too : https://github.com/supabase/postgres/blob/develop/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh#L157 to handle the migration scripts correctly

Comment thread sql/pg_net.sql Outdated
case
when rolename = 'public' then true
else pg_has_role(current_user, rolename, 'member')
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also correct me if I am wrong, dont we need any upgrade script ? sql/pg_net--X--Y.sql change ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those changes def need to be made and would not merge without that.

Comment thread src/core.h Resolved
@utkarash2991

Copy link
Copy Markdown
Contributor

@AndrewJackson2020 change looks good to me . I have added some comments. Let me know what you think about it ? Approving the change to unblock you.

@AndrewJackson2020

AndrewJackson2020 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@AndrewJackson2020 change looks good to me . I have added some comments. Let me know what you think about it ? Approving the change to unblock you.

Thanks, agree with all of the above. If your sure that this is truly a non breaking change as I am then I'm good. Also was hoping to merge this one first to eliminate the possibility of spit brain in migrations as you pointed out above but it still has issues I need to fix.

#304

@AndrewJackson2020
AndrewJackson2020 force-pushed the row_level_security branch 2 times, most recently from 41dddd7 to b0b9a82 Compare November 17, 2026 18:44
@utkarash2991

Copy link
Copy Markdown
Contributor

Re-read this after 304. Design still looks good to me, no objection to the rolename approach. Three things need to be in the upgrade path before it can merge:

  1. The sixth parameter changes the signature of http_get, http_post and http_delete. Something I learnt recently, the platform runs ALTER / REVOKE / GRANT on the 5-argument signatures at two places: the webhooks service when a customer enables Database Webhooks (https://github.com/supabase/platform/blob/09925e3ae47ff029e194e85698d66e6ccbe578c6/api/apps/mgmt-api/src/common/projects/project-database-webhooks.service.ts#L114-L124), and complete.sh at every Postgres upgrade
    (https://github.com/supabase/postgres/blob/f0ae55d70b1f4a1a3085a3e002acc5fbb1e00d43/ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh#L154-L166). On a project with the 6-argument functions those statements error. Enabling webhooks would fail outright. My another PR: https://github.com/supabase/platform/pull/41518 removes the webhooks one but is not merged yet.

  2. The upgrade script has to drop the 5-argument functions, not only create the 6-argument ones. With both present, a plain net.http_get('url') fails with "function is not unique" (checked on 17, see https://www.postgresql.org/docs/current/typeconv-func.html).

Functions that have default values for parameters are considered to match any call that omits zero or more of the defaultable parameter positions. If more than one such function matches a call, the one appearing earliest in the search path is used. If there are two or more such functions in the same schema with identical parameter types in the non-defaulted positions (which is possible if they have different sets of defaultable parameters), the system will not be able to determine which to prefer, and so an “ambiguous function call” error will result if no better match to the call can be found.

Dropping them also drops every grant on them, so anon, authenticated, service_role and supabase_functions_admin lose EXECUTE at the moment of the update until something re-grants.

  1. _http_response.rolename is NOT NULL with no default. The upgrade needs a backfill to 'public' may be? for existing rows or it fails on any project with responses 🤔

What if we keep the 5-argument functions as thin wrappers that call the 6-argument ones with rolename => 'public'. No ambiguity, the platform
statements keep working, no grant gap, and the upgrade script only adds the column with a default plus the new functions. But this will required one extra function.

@AndrewJackson2020

Copy link
Copy Markdown
Contributor Author

@utkarash2991 good review. Very thorough and definitely some open issues to address before merging.

  1. I'm not sure im seeing an issue here tbh. In postgres functions are publicly permissioned by default. pg_net used to use security definer functions which should absolutely not be public. This has since been removed and the main sentinel around pg_net access is the permissions on the underlying table and not the functions themselves. Im going through this exercise in vault [0] as well. We also know from our other work that these tables are granted to public which is causing other issues.
  2. good catch, this is fixed. As I said before these functions should be available to the public so im not sure if permissions are an issue here.
  3. I added a test to assure that this gets backfilled.

What if we keep the 5-argument functions as thin wrappers that call the 6-argument ones with rolename => 'public'.
I'm not a fan of this. I feel like we should keep the number of objects in the database to an absolute bear minimum.

[0] supabase/vault#72

The library is replaced per server while the extension is updated per
database, so after an image upgrade the worker runs against the old
schema until the project runs ALTER EXTENSION pg_net UPDATE. The queue
and response queries named rolename unconditionally and would fail on
every batch there, leaving the worker in a restart loop.

The worker now checks for the column on each batch and builds its plans
with or without it, rebuilding them when the column appears. The column
is also NOT NULL with a default, because the worker treats a null
rolename as an error and a single null row would stop it for good.

Tests cover the old schema served by the new library, the null rolename,
and the response row carrying and hiding the rolename.
# Conflicts:
#	sql/pg_net--0.20.4--0.20.6.sql
#	test/test_misc.py
@blacksmith-sh

blacksmith-sh Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Found 1 test failure on Blacksmith runners:

Failure

Test View Logs
test_http_get_responses_have_different_created_times View Logs

Fix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

@utkarash2991

Copy link
Copy Markdown
Contributor

@utkarash2991 good review. Very thorough and definitely some open issues to address before merging.

  1. I'm not sure im seeing an issue here tbh. In postgres functions are publicly permissioned by default. pg_net used to use security definer functions which should absolutely not be public. This has since been removed and the main sentinel around pg_net access is the permissions on the underlying table and not the functions themselves. Im going through this exercise in vault [0] as well. We also know from our other work that these tables are granted to public which is causing other issues.
  2. good catch, this is fixed. As I said before these functions should be available to the public so im not sure if permissions are an issue here.
  3. I added a test to assure that this gets backfilled.

What if we keep the 5-argument functions as thin wrappers that call the 6-argument ones with rolename => 'public'.
I'm not a fan of this. I feel like we should keep the number of objects in the database to an absolute bear minimum.

[0] supabase/vault#72

Hey @AndrewJackson2020 , while reviewing this I came up with one corner case where the pg_net worker can go into a crash loop. On existing projects an image upgrade replaces the library, but the extension schema stays at the old version until someone runs ALTER EXTENSION pg_net UPDATE. So the new worker runs its queries against the old schema and fails every batch with column q.rolename does not exist. I added a commit where the worker checks for the column on every batch (syscache lookup, no statement) and builds its plans with or without it, rebuilding them when the column appears. I also made rolename not null default 'public' on both tables: get_request_queue_row errors on a null rolename and one null row would stop the worker. I also resolved the merge conflict so that its good to merge.
Let me know please what you think?

@AndrewJackson2020

Copy link
Copy Markdown
Contributor Author

@utkarash2991 Thank you for picking this up and good find. Opens up some questions though:

  1. Right now test_http_get_responses_have_different_created_times is failing. This is a perpetually flakey test. Any chance you (or someone else) can make this test not flakey?
  2. In the case where there is obvious incompatibility between c library and SQL extension version should we remove the ability for people to install earlier versions? My thinking as to how this could work:
  • Stop shipping with sql/pg_net--0.1.sql but keep in tree for upgrade testing. Maybe in a sql/historical/pg_net--0.1.sql, something like that.
  • Create a new sql/pg_net--2.21.0.sql so all new installs execute that directly. We can potentially base this off of the scripts/dump_extension.sh script.
  • As apart of our CI/pytest suite test all branches and compare the sql to ensure they are the same. Ie install extension 0.1, upgrade to current, validate that matches creating the extension directly for pg_net--2.21.0.sql (and any upgrade files we add in the future)
  1. Maybe we should bundle this change with your upcoming 1.0.0 bump considering the binary/sql incompatibility?
  2. Should the commit/PR message here be BREAKING CHANGE: instead of fix?
  3. Should we implement some sort of shared library compatibility testing? thinking something along the lines of running the test suite of the new sql against the previous releases binary to check for any issues. We would mark and skip any tests for behavior that tends on the changes between the old and new binary.

@utkarash2991

Copy link
Copy Markdown
Contributor

@utkarash2991 Thank you for picking this up and good find. Opens up some questions though:

  1. Right now test_http_get_responses_have_different_created_times is failing. This is a perpetually flakey test. Any chance you (or someone else) can make this test not flakey?
  2. In the case where there is obvious incompatibility between c library and SQL extension version should we remove the ability for people to install earlier versions? My thinking as to how this could work:
  • Stop shipping with sql/pg_net--0.1.sql but keep in tree for upgrade testing. Maybe in a sql/historical/pg_net--0.1.sql, something like that.
  • Create a new sql/pg_net--2.21.0.sql so all new installs execute that directly. We can potentially base this off of the scripts/dump_extension.sh script.
  • As apart of our CI/pytest suite test all branches and compare the sql to ensure they are the same. Ie install extension 0.1, upgrade to current, validate that matches creating the extension directly for pg_net--2.21.0.sql (and any upgrade files we add in the future)
  1. Maybe we should bundle this change with your upcoming 1.0.0 bump considering the binary/sql incompatibility?
  2. Should the commit/PR message here be BREAKING CHANGE: instead of fix?
  3. Should we implement some sort of shared library compatibility testing? thinking something along the lines of running the test suite of the new sql against the previous releases binary to check for any issues. We would mark and skip any tests for behavior that tends on the changes between the old and new binary.

@AndrewJackson2020 responses below:

  1. PR for resolving the test flakiness here: test: wait for each response in the created times test #342
  2. Yeah we can do it. We can take in a follow up PR as it looks to me good amount of change and its non-blocker for this PR.
  3. Yeah we can, but I think with recent change in worker it makes the new binary work against the old schema . So it should be good now. Anything is fine to me. WDYT?
  4. Yeah it should be. I am going to update it.
  5. The test I added, test_worker_serves_schema_without_rolename, covers the direction that happens on the fleet, new binary against old schema. IMO, the other direction i.e old binary with new SQL, never happens on the fleet because the library is always replaced first. So do we really need it?

@utkarash2991 utkarash2991 changed the title fix: add row level security over shared tables feat!: add row level security over shared tables Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants