【发布时间】:2016-03-15 11:17:45
【问题描述】:
我正在开发一个 Rails 4 应用程序,该应用程序通过 API 为移动应用程序提供服务,并有一个 Web UI 供管理员管理应用程序。用户还将看到几个网页(成功的电子邮件确认和重置密码)。
我创建了两组控制器:一组继承自 APIController,另一组继承自 AdminController。这两个都继承自 ApplicationController。负责面向用户的网页的其余控制器也继承自 ApplicationController。
鉴于此方案,我不确定如何使用protect_from_forgery 正确实施CSRF 保护。我目前有以下:
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
所以我的问题是:这是正确的吗?有什么办法可以改善吗? APIController 中的检查是否毫无意义,因为所有 API 请求都只是 JSON?
Brakeman 抱怨 ApplicationController 中没有protect_from_forgery 调用,但可能在子类中没有看到调用。
提前致谢!
【问题讨论】:
标签: ruby-on-rails-4 csrf-protection