-
Notifications
You must be signed in to change notification settings - Fork 31
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
Implement db_pre_request #9
Comments
I personally would recommend not doing this. I have supabase RLS set up to authenticate both requests coming in via postgrest, as well as requests made in the context of a user from my backend, using the same RLS policies. I do this by setting custom transaction variables with the JWT of the user request. This won't work if I believe this will also fail with Supabase GraphQL, where currently we can use the same policies for that and the REST API. |
@fnimick could you elaborate on how implementing this would affect your use-case? From what I understand, it would be possible for me to use db_pre_request as a way to ensure that the claims in the incoming JWT are still correct compared to the claims in the group_users table. Beyond that, nothing should really change operationally. Perhaps you can describe how you are anticipating the change to function and how it would break your current setup? Thanks |
@point-source ah it wasn't clear that this would only be for JWT verification, not to actually load any other information into the request context. I would still encourage caution, since it would mean that identical table RLS policies would have different levels of security and therefore different behavior based on how the table is accessed - e.g. via the supabase API (postgrest) vs supabase graphql vs a standard database connection with the JWT included in the transaction variables. |
@fnimick Yeah my plan currently is to just have the pre request hook overwrite the claims in the jwt exactly as they are. The only time this wouldn't be overwriting it with identical information is if the user's claims changed but their jwt had not yet been refreshed. In these cases, the pre request hook is fixing what is currently a vulnerability in this system, but only fixing it for REST requests, as you pointed out. It seems to me that even if I can't patch the entire hole, I should patch as much as I can. Thoughts? |
I now have this implemented but am waiting on this doc issue and this bug so I can figure out how to roll it out. |
@fnimick After further research into the way this works, it became clear to me that adding information to the request context is likely the better approach. The way I currently have this configured is as follows:
Is this an acceptable solution for you? If not, could you explain why and perhaps propose an alternative that doesn't reduce the security of the postgrest side? Thanks! |
I still am worried about potentially different behavior between postgrest and standard queries. Could you add a function to populate To give a concrete example, this is what I do to set up a RLS transaction using the JWT when accessing the database from a SQL client. drizzle-team/drizzle-orm#594 (comment) . My thought is that in my transaction setup here, I can use your new JWT lookup function rather than simply copying the JWT into the request claims config, which would duplicate the behavior of |
Well yes, there would be a chance of different behavior but in the case where it differs, it differs in favor of security. So postgrest would be sometimes more secure than standard queries. I don't think it makes sense to keep postgrest on the less secure JWT method in order to maintain consistency.
Populating
You can then access those by either checking Ultimately, I want to deprecate and remove claims from the JWT entirely now that there are better methods to get claim data for both postgrest and standard transactions.
Yes, this sounds very nearly like the right approach. The exception being that the function will populate from the |
I still like having claim data in the JWT since I parse that claim data on the frontend and use it for client-side control of routing to allowed UI elements. The data loaded for those elements is still protected, so there's no harm in gating access to a specific frontend URL behind a JWT claim, and then failing at the load step if the claim is invalid. |
That's definitely valid. I'll make sure that that remains a possibility then, if not the default. As for the rest, do those new db functions I mentioned serve your needs for the current implementation? |
@point-source I assume you're referring to the functions as implemented on the I have a few questions:
|
Good call. No reason. Will fix in the next release
Yep, good point. In the next release (1.0.0, breaking changes), I am actually removing the JWT functions in favor of having the primary functions (now marked stable as well) check the request context first (in case of db_pre_request) and then fallback to JWT. This should improve performance enough to consolidate the functions. The only time it would impact security is if the user was previously relying on the non-jwt functions AND is not using postgrest (thus skipping db_pre_request) AND does not trust the claims within the JWT.
Got me again, I mixed that up. It will be fixed in 1.0.0 (or replaced) |
This has been implemented as part of the 1.0.0 release |
If implement db_pre_request, rbac system will be fast and secure for production. I think so.
https://github.com/burggraf/postgrest-request-processing/tree/main
The text was updated successfully, but these errors were encountered: