Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

johnsca
Copy link

@johnsca johnsca commented Aug 11, 2023

Login with per-user PG database role, if available, to ensure that RLS policies are applied to user queries. Also changes how the redash.query_results table is filtered, since setting the role for the entire session broke saving queries (and probably some other things) due to the rest of the entire request session losing access to all of the other Redash tables.

Depends on: https://github.com/stacklet/platform/pull/1732

Tested on my sandbox with my non-admin SSO user and manually injected account mappings. Confirmed that the user could only get query results from the resources table that they had an account mapping for, and that they could neither see query results generated by other users, nor directly query the redash.query_results table and see anything other than their own records.

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.
@johnsca johnsca force-pushed the johnsca/feat/rls/ENG-2474/per-user-role-redash-query-executions branch 4 times, most recently from 9656e5f to bc1e229 Compare August 14, 2023 15:55
@johnsca johnsca force-pushed the johnsca/feat/rls/ENG-2474/per-user-role-redash-query-executions branch 3 times, most recently from a46c1d5 to f5f217b Compare August 14, 2023 18:17
@johnsca johnsca marked this pull request as ready for review August 14, 2023 20:00
Comment on lines 413 to 416
@listens_for(BaseQuery, "before_compile", retval=True)
def prefilter_query_results(query):
for desc in query.column_descriptions:
if desc['type'] is QueryResult:
Copy link
Author

Choose a reason for hiding this comment

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

This approach was the best I could come up with. It does ensure that the user only sees the QueryResult records that they "own" but doesn't actually use the role / RLS policy on that table because I couldn't figure out a way to actually wrap the query in a set / reset role, since there is no corresponding "after" event for queries. I'm open to suggestions on how to make it actually use the role, but it's worth noting that the RLS policy on the redash.query_results table is still important to prevent the user from issuing queries directly against that table to see results that they shouldn't.

Choose a reason for hiding this comment

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

Can we add a comment in the code to this affect?

Copy link

@howbazaar howbazaar left a comment

Choose a reason for hiding this comment

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

Looks good. Only ask is for a comment.

Comment on lines 413 to 416
@listens_for(BaseQuery, "before_compile", retval=True)
def prefilter_query_results(query):
for desc in query.column_descriptions:
if desc['type'] is QueryResult:

Choose a reason for hiding this comment

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

Can we add a comment in the code to this affect?

@johnsca johnsca merged commit af5cbd7 into stacklet/integration Aug 16, 2023
@johnsca johnsca deleted the johnsca/feat/rls/ENG-2474/per-user-role-redash-query-executions branch August 16, 2023 20:00
thisisshi pushed a commit that referenced this pull request Mar 1, 2024
* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
…8) (#47)

If the current user has a `db_role`, they should only see query results
that they have generated, so that they don't see results which contain
info about resources they don't have permission to view.

feat: use per-user db role for query exec (ENG-2474) (#48)

* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
…8) (#47)

If the current user has a `db_role`, they should only see query results
that they have generated, so that they don't see results which contain
info about resources they don't have permission to view.

feat: use per-user db role for query exec (ENG-2474) (#48)

* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 22, 2025
…8) (#47)

If the current user has a `db_role`, they should only see query results
that they have generated, so that they don't see results which contain
info about resources they don't have permission to view.

feat: use per-user db role for query exec (ENG-2474) (#48)

* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request May 28, 2025
…8) (#47)

If the current user has a `db_role`, they should only see query results
that they have generated, so that they don't see results which contain
info about resources they don't have permission to view.

feat: use per-user db role for query exec (ENG-2474) (#48)

* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
wgrant pushed a commit that referenced this pull request Jul 1, 2025
…8) (#47)

If the current user has a `db_role`, they should only see query results
that they have generated, so that they don't see results which contain
info about resources they don't have permission to view.

feat: use per-user db role for query exec (ENG-2474) (#48)

* feat: use per-user db role for query exec (ENG-2474)

Login with per-user PG database role, if available, to ensure that RLS
policies are applied to user queries.

* Reject login from unknown SSO users

* Use pre-filtered query for QueryResults rather than session-level `set role`

* Add docstring with non-obvious context
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.

2 participants