12

在使用 Devise 进行身份验证后,我发现其中存在一个安全漏洞,在用户注销后,会话变量被保留。这允许任何人按下后退按钮并访问已登录用户的上一个屏幕。

我看了这些帖子 Num 1 Num 2 Num 3

我将这些行添加到我的 application_controller

before_filter :set_no_cache
def set_no_cache
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end

在 _form.html.erb 我在顶部添加了这个

<%if user_signed_in? %>
<%=link_to "Sign Out",  destroy_user_session_path, :method => :delete %><br/>
<%= form_for(@listing) do |f| %>
<% if @listing.errors.any? %>
...........

然后我在 Firefox、Chrome 和 Safari 上测试了该应用程序。

Firefox 和 Chrome 很好,因为我注销并点击后退按钮并且看不到用户的前一个屏幕,但是在 Safari 和 Opera 中,不安全的行为仍然存在。此代码没有效果。

对于如何解决这个问题,有任何的建议吗?

谢谢

4

3 回答 3

13

我遇到了同样的问题,找到了一个很好的解决方案,我把它写到

http://www.fordevs.com/2011/10/how-to-prevent-browser-from-caching-a-page-in-rails.html

要添加“无缓存”,请在 application_controller.rb 文件中添加以下行

before_filter :set_no_cache

和功能

def set_no_cache
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
于 2013-03-22T08:06:02.333 回答
1

首先,对于缓存的任何问题,请使用 Mark Nottingham 的HTTP 缓存指南

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

尝试这个。

于 2012-06-08T19:04:08.320 回答
0

我发现在我的应用程序控制器中执行此操作非常适合开发。

after_filter  :expire_for_development

protected

def expire_for_development
  expires_now if Rails.env.development?
end
于 2014-02-15T21:27:52.717 回答