如何保护Rails应用程序免受Firesheep的攻击?

时间:2010-11-10 17:47:09

标签: ruby-on-rails security

我找不到针对Firesheep保护Ruby on Rails应用的简单指南。

如果您不知道,如果您的应用不强制使用SSL并在Cookie中设置安全标记,则Firesheep会话会话Cookie。我不得不做一些搜索来找到这两件事,所以我想我会发布我在这里发现的东西,看看是否还有其他的东西我不知道。

第1步强制使用SSL

我发现有两种方法可以做到这一点。一个是使用ssl_requirement插件,但这很痛苦,因为你必须在每个控制器中专门指定ssl_required :action1, :action2

最好的方法似乎是使用Rack Middleware,通过这篇文章:Force SSL using ssl_requirement in Rails 2 app。像魅力一样。

第2步使Cookie安全

为此我跟着these directions,告诉您将以下内容放入config/environment/production.rb文件中:

config.action_controller.session = {
    :key     => 'name_of_session_goes_here',
    :secret          => 'you need to fill in a fairly long secret here and obviously do not copy paste this one',
    :expire_after    => 14 * 24 * 3600, #I keep folks logged in for two weeks
    :secure => true #The session will now not be sent or received on HTTP requests.
  }

在我的Rails 2.x应用程序上,这一切都很简单。我错过了什么吗? Rails 3有什么不同吗?

2 个答案:

答案 0 :(得分:17)

对我来说很好看。它在Rails 3中非常相似,但默认情况下会话配置存储在config / initializers / session_store.rb中。我经常调整我看起来像......

MyApp::Application.config.session_store :cookie_store, :key => '_my_app_session',
                                                       :secure => Rails.env == 'production', # Only send cookie over SSL when in production mode
                                                       :httponly => true, # Don't allow Javascript to access the cookie (mitigates cookie-based XSS exploits)
                                                       :expire_after => 60.minutes

秘密保存在config / initializers / secret_token.rb中:

MyApp::Application.config.secret_token = 'secret secrets are no fun...'

如果您可以访问Apache(或其他)配置,则还可以强制在该级别使用SSL。让我觉得这是一个更合适的地方,但我猜不是每个人都有这个选择。

答案 1 :(得分:1)

看到这篇SO帖子在Google中排名很高我认为我会分享我用来保护应用程序的方法。

如果您想确保SSL并确保安全的cookie,那么您可以使用Rack中间件:

https://github.com/tobmatth/rack-ssl-enforcer

为了做到这一点,我评估了许多不同的选项和配置设置,但是机架中间件感觉是配置最少的最佳选择 - 非常容易部署。它有一些很棒的配置选项来过滤特定的规则,主机,路径等。

我测试过它确实设置了正确的安全cookie,确实如此。我注意到的一件事是它只在注销并再次登录时才这样做 - 但那是使用Devise。

相关问题