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

我还没有找到一个简单的指南来保护Ruby on Rails应用程序对Firesheep 。

如果你不知道,如果你的应用程序没有强制SSL并在cookie中设置安全标志,Firesheep会议会话cookie。 我不得不做一些搜索才能找到这两件事,所以我想我会发布我在这里找到的东西,看看是否还有其他我想念的东西。

步骤1强制SSL

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

最好的方法似乎是使用Rack Middleware,通过这篇文章: 在Rails 2应用程序中使用ssl_requirement强制SSL 。 奇迹般有效。

第2步使cookie安全

为此我遵循了这些指示 ,告诉您将以下内容放在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有什么不同吗?

看起来对我很好。 它在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。 让我觉得这是一个更合适的地方,但我想不是每个人都有这个选择。

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

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

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

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

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