Repository navigation
feat!: add row level security over shared tables - #303
AndrewJackson2020 wants to merge 6 commits into
Conversation
|
@AndrewJackson2020 I wonder if this is the right design 🤔 :
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? |
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.
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.
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. |
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.
Agree.
Right, looks more complex and prone to failure.
Seems acceptable to do on next major release. |
7f66c23 to
9aa4224
Compare
|
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. |
36b79e4 to
a2d966e
Compare
Looks good to not cause a breaking change 👍 |
| timeout_milliseconds int not null | ||
| timeout_milliseconds int not null, | ||
| rolename text not null default 'public' | ||
| ); |
There was a problem hiding this comment.
@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
| case | ||
| when rolename = 'public' then true | ||
| else pg_has_role(current_user, rolename, 'member') | ||
| end |
There was a problem hiding this comment.
Also correct me if I am wrong, dont we need any upgrade script ? sql/pg_net--X--Y.sql change ?
There was a problem hiding this comment.
Those changes def need to be made and would not merge without that.
|
@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. |
41dddd7 to
b0b9a82
Compare
|
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:
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.
What if we keep the 5-argument functions as thin wrappers that call the 6-argument ones with rolename => 'public'. No ambiguity, the platform |
fb08f72 to
0a916f2
Compare
9eb0b08 to
d96073d
Compare
|
@utkarash2991 good review. Very thorough and definitely some open issues to address before merging.
|
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
|
Found 1 test failure on Blacksmith runners: Failure
|
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 |
|
@utkarash2991 Thank you for picking this up and good find. Opens up some questions though:
|
@AndrewJackson2020 responses below:
|
![Fix with [code]smith](https://pr-comments-assets.blacksmith.sh/codesmith/fix-with-codesmith-light-v2-3x.png)
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: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
publicand stay visible to everyone who could read them before.