Skip to content
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

hash_condition_match returns nil when subject is ActiveRecord Collection Proxy #536

Open
BookOfGreg opened this issue Oct 17, 2018 · 7 comments
Assignees

Comments

@BookOfGreg
Copy link
Contributor

@BookOfGreg BookOfGreg commented Oct 17, 2018

When the subject is an ActiveRecord_Associations_CollectionProxy(ActiveRecord::Relation), the inner_foo value is true, but the outer_foo value is nil as seen below;

This is because it calls this method instead of array's any?:
https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-any-3F

screen shot 2018-10-17 at 17 10 56

System configuration

Rails version:
5.2.0

Ruby version:
2.5.1

CanCanCan version
2.3.0

@BookOfGreg
Copy link
Contributor Author

@BookOfGreg BookOfGreg commented Oct 17, 2018

I'm still struggling to isolate exactly why any? is returning nil, but while debugging it, got a casual segfault from the gem which makes no sense to me so far.

Edit: Turns out using return inside the block in ActiveRecord is causing the segfault.
Edit2: See @laurawatson 's reproduction below.

@coorasse
Copy link
Member

@coorasse coorasse commented Oct 18, 2018

can you provide a gist to reproduce your issue? Is not clear to me, sorry.

@laurawatson
Copy link

@laurawatson laurawatson commented Oct 19, 2018

Here is a reproduction of the issue: https://github.com/laurawatson/can_can_check.

In this reproduction, the issue occurs when you first login as an adviser user and try to access the foo show page. It will redirect you to the home page despite the adviser user having access to foo. If you then head back to the foo show page you will be allowed in (more info in the readme).

@BookOfGreg
Copy link
Contributor Author

@BookOfGreg BookOfGreg commented Oct 29, 2018

@coorasse We're currently effectively having to inline our ActiveRecord conditions to avoid this bug. We suspect it's some unusual interaction between ActiveRecord lazy loading and this gem but I have been unable to diagnose it further than that any? method mentioned previously.

Would you be so kind as to confirm if you see the same issue as us in the example repo linked by @laurawatson above? If it is at least a confirmed bug then we may be able to put some time aside to help fix it, until then all we can do is assume it's our mistake and not use this gem.

@coorasse
Copy link
Member

@coorasse coorasse commented Feb 21, 2019

I confirm that I see the same behaviour.

@coorasse coorasse self-assigned this Feb 21, 2019
@coorasse
Copy link
Member

@coorasse coorasse commented Feb 21, 2019

changing the rule
can :read, Foo, user_roles: { user: user } #wrong
into
can :read, Foo, user_roles: { user_id: user.id } #correct
fixes the issue.
But I still don't understand why.

@coorasse
Copy link
Member

@coorasse coorasse commented Feb 21, 2019

And the user can also see http://cancan_error.localhost:3000/bars/1 which should be protected. By using the id instead of the association name user (which is not supported) fixes it

@coorasse coorasse added the bug label Feb 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.