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

CanCan gotcha for abilities with blocks #5144

Closed
tf opened this issue Aug 17, 2017 · 0 comments
Closed

CanCan gotcha for abilities with blocks #5144

tf opened this issue Aug 17, 2017 · 0 comments

Comments

@tf
Copy link
Contributor

tf commented Aug 17, 2017

When using the CanCan adapter, ActiveAdmin normally passes a resource object to determine if a specific action is allowed. There are at least three cases, though, where ActiveAdmin passes the resource class as subject:

These work great for simple cases:

  # ability.rb
  if user.admin?
    can(:manage, Resource)
  end

When using the CanCan block API they present a potential security risk, though:

  # ability.rb
  can(:manage, Resource) do
    user.admin?
  end

As described in CanCanCommunity/cancancan#200, by design, this results in:

  ability = Ability.new(User.new(admin: false))
  ability.can?(:manage, resource) # => false
  ability.can?(:manage, Resource) # => true (!!)

This causes links to forbidden pages to be displayed in our app.

For complex authorization flows, it is not always possible to stick to the first Ability style. And even if a reformulation is easily possible, this behavior looks like a gotcha that should be mentioned in the docs.

The issue could be circumvented by passing symbols instead of classes (i.e. can(:manage, :accounts)). That would be a breaking change, though, and make the simple case harder.

@javierjulio javierjulio closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2023
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

No branches or pull requests

2 participants