I am developing a Rails 4 app that serves a mobile app through an API, and has a web UI for admins to manage the application. There's also a couple of web pages that users will see (successful e-mail confirmation and reset password).
I created two sets of controllers: one set inherits from APIController, and the other from AdminController. Both of these inherit from ApplicationController. The remaining controller responsible for user facing web pages also inherits from ApplicationController.
Given this scheme, I am unsure on how to properly implement CSRF protection with protect_from_forgery. I currently have the following:
class ApplicationController < ActionController::Base
# ...
end
module API
class APIController < ApplicationController
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
# ...
end
end
module Admin
class AdminController < ApplicationController
protect_from_forgery with: :exception
# ...
end
end
class UsersController < ApplicationController
protect_from_forgery with: :exception
# ...
end
So my question is: is this correct? Is there any way to improve it? Is the check in the APIController pointless since all API requests will be only JSON anyway?
Brakeman complained that there is no protect_from_forgery call in ApplicationController, but maybe it doesn't see the calls in the subclasses.
Thanks in advance!