-
Notifications
You must be signed in to change notification settings - Fork 633
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
Pundit caching isn't sensitive to current user changing #811
Comments
Hi @danielfone this is amazing feedback! I'll ponder what Pundit should do here. The context cache might benefit from being current user-sensitive, to avoid accidents. It also might be worth having a recommended approach documented in the README. Not sure how much Pundit itself should help with the multi-user case, mostly because it's a relatively rare use-case. Also, one very important thing to consider, is that previous releases of Pundit did also have issues with caching and changing the current user. We've been caching policy lookups for years, and that cache is also not sensitive to the current user changing. Effectively this won't be good: def show?
post = Post.find(...)
# current_user = post.author here
authorize post
@current_user = User::Guest.new
authorize post # => WARNING! cached post policy from earlier
end So if you do switch users like this, you need to be mindful of all the caches: |
Thanks @Burgestrand. Yes, I don't blame you if you don't want to support this use-case, I should really implement it as a high-order concern. In my specific case, the other caches never affected things since the impersonation was very early on in the request. I suppose the more salient issue is that, as naive user of the gem, there's a bit of hidden 'magic' happening between the Appreciate your time! |
Very much agree. Pundit should strive to minimise surprise 🙂 If nothing else, documenting how to safetly deal with user-switching mid-request is something we could do! |
@Burgestrand Hi there! Firstly, thanks for all the hard work maintaining pundit, it's a brilliant gem and I'm a long time fan.
Re #797
Unfortunately, 2.3.2 has broken a key piece of authorization functionality in one of my apps. I'm not sure if it's because I was doing it wrong in the first place, or if this is an unintentional regression. I have a controller concern that allows admins to impersonate/become another user, to view the app from another user's perspective. The way we implement this is approximately:
Unfortunately, since
Pundit::Context
is initialised on the first policy look up (when the current user is the admin) and memoizes the current user at that point, all subsequent policies are checked against the admin user, and not the impersonated user.I can work around it in a variety of ways, but wanted to let you know and see if this is something you're concerned about. It was unfortunate that it (a) happened in a patch release, and (b) my integration testing wasn't sufficient to catch this change, since it slipped into production and I had a very panicked phone call from an admin who thought users could now see everything. 😂 You live and learn!
Thanks again.
The text was updated successfully, but these errors were encountered: