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

Children / sub-actions accessible even if object is unauthorized #661

Open
bars0um opened this issue Nov 11, 2020 · 1 comment
Open

Children / sub-actions accessible even if object is unauthorized #661

bars0um opened this issue Nov 11, 2020 · 1 comment

Comments

@bars0um
Copy link

bars0um commented Nov 11, 2020

Steps to reproduce

This seems like a serious bug, or possibly I'm unable to find the proper documentation to do this correctly.

I have a controller for a class Order that has many OrderItems.

If I setup my controller as follows to perform authorization on the loaded instance variable, the behavior is not as expected when touching child objects or performing custom actions.

The following is the setup I have:

class OrdersController < ApplicationController
  before_action :find_order
  load_and_authorize_resource
  load_and_authorize_resource :order_item, through: :order
...
..
def find_order
    @order = if params[:order_id]
               Order.find(params[:order_id])       
    end

I have a nested route

resources :orders, shallow: true do           
              resources :order_items
              get "status"
            end

reaching the resource directly seems to work as expected when trying to access a resource not owned by the user:

GET https://example.com/orders/10000

RESULTS IN: OrdersController action=show status=403 

however, if I try to access a custom action, unexpectedly I get back the result of that action on the unauthorized object:

GET https://example.com/orders/10000/status.json

RESULT: OrdersController action=status status=200

I can only get the expected behavior if I create my own load and authorize filter:

before_action :set_and_authorize_order

 def set_and_authorize_order
    @order = if params[:order_id]
               Order.find(params[:order_id])          
             end

    authorize! action_name.to_sym, @order
  end

System configuration

Rails version: 6

Ruby version: 2.5

CanCanCan version 3.1.0

@coorasse
Copy link
Member

Hi! Thanks for pointing this out. Can you write a gist to reproduce the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants