【发布时间】:2018-01-09 00:11:30
【问题描述】:
我已经阅读了这篇https://nvisium.com/blog/2014/09/10/understanding-protectfromforgery/ 的帖子,如果我理解正确,默认情况下在 Rails 3 中,如果我们有一个看起来像这样的控制器:
class ApplicationController < ActionController:Base
protect_from_forgery
end
最终会发生的是(在攻击者的情况下)会话将被破坏。这意味着如果我们做一些检查用户是否经过身份验证的事情,因为没有会话,它将停止请求。
所以,我的问题是,我相信:
class ApplicationController < ActionController:Base
protect_from_forgery
before_action :authenticate_user
private
def authenticate_user
raise NotAuthenticated unless session.key?(:user)
end
end
是正确的做法,而不是
class ApplicationController < ActionController:Base
before_action :authenticate_user
protect_from_forgery
private
def authenticate_user
raise NotAuthenticated unless session.key?(:user)
end
end
或者换句话说,protect_from_forgery 应该是我们在控制器中做的第一件事。
我的假设是否正确,或者我在控制器中的操作顺序如何工作方面遗漏了一些东西?
【问题讨论】:
标签: ruby-on-rails csrf csrf-protection