-
Notifications
You must be signed in to change notification settings - Fork 38
fix: preserve nested function calls in RLS policy expressions (#377) #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| ALTER TABLE orders ENABLE ROW LEVEL SECURITY; | ||
| CREATE POLICY orders_user_access ON orders FOR SELECT TO PUBLIC USING (user_id IN ( SELECT users.id FROM users)); | ||
| CREATE POLICY "UserPolicy" ON users TO PUBLIC USING (tenant_id = current_setting('app.current_tenant')::integer); | ||
| CREATE POLICY "UserPolicy" ON users TO PUBLIC USING (tenant_id = (current_setting('app.current_tenant'))::integer); | ||
| CREATE POLICY admin_only ON users FOR DELETE TO PUBLIC USING (is_admin()); | ||
| CREATE POLICY "my-policy" ON users FOR INSERT TO PUBLIC WITH CHECK ((role)::text = 'user'); | ||
| CREATE POLICY "select" ON users FOR SELECT TO PUBLIC USING (true); | ||
| CREATE POLICY user_tenant_isolation ON users FOR UPDATE TO PUBLIC USING (tenant_id = current_setting('app.current_tenant')::integer); | ||
| CREATE POLICY user_tenant_isolation ON users FOR UPDATE TO PUBLIC USING (tenant_id = (current_setting('app.current_tenant'))::integer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
testdata/diff/create_policy/issue_377_nested_function_in_policy/diff.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE projects ADD COLUMN description text; |
16 changes: 16 additions & 0 deletions
16
testdata/diff/create_policy/issue_377_nested_function_in_policy/new.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| CREATE FUNCTION get_user_assigned_projects() RETURNS integer[] | ||
| LANGUAGE sql STABLE AS $$ SELECT ARRAY[1, 2, 3] $$; | ||
|
|
||
| CREATE TABLE projects ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL, | ||
| description TEXT | ||
| ); | ||
|
|
||
| ALTER TABLE projects ENABLE ROW LEVEL SECURITY; | ||
|
|
||
| -- Policy unchanged, but table has new column | ||
| CREATE POLICY project_access ON projects | ||
| FOR SELECT | ||
| TO PUBLIC | ||
| USING (id IN (SELECT unnest(get_user_assigned_projects()) AS unnest)); |
14 changes: 14 additions & 0 deletions
14
testdata/diff/create_policy/issue_377_nested_function_in_policy/old.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| CREATE FUNCTION get_user_assigned_projects() RETURNS integer[] | ||
| LANGUAGE sql STABLE AS $$ SELECT ARRAY[1, 2, 3] $$; | ||
|
|
||
| CREATE TABLE projects ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL | ||
| ); | ||
|
|
||
| ALTER TABLE projects ENABLE ROW LEVEL SECURITY; | ||
|
|
||
| CREATE POLICY project_access ON projects | ||
| FOR SELECT | ||
| TO PUBLIC | ||
| USING (id IN (SELECT unnest(get_user_assigned_projects()) AS unnest)); |
20 changes: 20 additions & 0 deletions
20
testdata/diff/create_policy/issue_377_nested_function_in_policy/plan.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "version": "1.0.0", | ||
| "pgschema_version": "1.8.0", | ||
| "created_at": "1970-01-01T00:00:00Z", | ||
| "source_fingerprint": { | ||
| "hash": "37d3ebdd423c5db41ba042431c6328705f50593fe2c8f5163de497aa4eee08b8" | ||
| }, | ||
| "groups": [ | ||
| { | ||
| "steps": [ | ||
| { | ||
| "sql": "ALTER TABLE projects ADD COLUMN description text;", | ||
| "type": "table.column", | ||
| "operation": "create", | ||
| "path": "public.projects.description" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
1 change: 1 addition & 0 deletions
1
testdata/diff/create_policy/issue_377_nested_function_in_policy/plan.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE projects ADD COLUMN description text; |
13 changes: 13 additions & 0 deletions
13
testdata/diff/create_policy/issue_377_nested_function_in_policy/plan.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Plan: 1 to modify. | ||
|
|
||
| Summary by type: | ||
| tables: 1 to modify | ||
|
|
||
| Tables: | ||
| ~ projects | ||
| + description (column) | ||
|
|
||
| DDL to be executed: | ||
| -------------------------------------------------- | ||
|
|
||
| ALTER TABLE projects ADD COLUMN description text; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
TestNormalizeExpressionParenthesestests verify the fix for nested function calls and basic wrapping, but there's no test case for the expression pattern that motivated the old regex — the type-cast form(current_setting('app.current_tenant'))::integer.Adding a case like this would document that the cast suffix is preserved and guard against a future regression if someone tries to "clean up" the function by re-adding a similar regex:
{ name: "function call with type cast preserved", input: "(tenant_id = (current_setting('app.current_tenant'))::integer)", expected: "(tenant_id = (current_setting('app.current_tenant'))::integer)", },