使用 Rails 存储加密的 cookie

发布于 2025-01-07 04:42:37 字数 840 浏览 0 评论 0原文

我需要在 Rails 的 cookie 中存储一小段数据(少于 10 个字符),并且我需要它是安全的。我不希望任何人能够读取该数据或注入自己的数据(因为这将使应用程序面临多种攻击)。我认为加密 cookie 的内容是可行的方法(我也应该签名吗?)。最好的方法是什么?

现在我正在这样做,这看起来很安全,但是对于那些比我更了解安全的人来说,很多事情看起来很安全,然后发现它并不真正安全。

我以这种方式保存秘密:

encryptor = ActiveSupport::MessageEncryptor.new(Example::Application.config.secret_token)
cookies[:secret] = {
  :value => encryptor.encrypt(secret),
  :domain => "example.com",
  :secure => !(Rails.env.test? || Rails.env.development?)
}

然后我像这样阅读它:

encryptor = ActiveSupport::MessageEncryptor.new(Example::Application.config.secret_token)
secret = encryptor.decrypt(cookies[:secret])

这安全吗?还有更好的方法吗?

更新:我了解 Rails 的会话及其安全性,通过签署 cookie 和选择性地存储会话服务器端的内容,并且我确实将会话用于其用途。但我的问题是关于存储 cookie,这是我在会话中不想要的一条信息,但我仍然需要它的安全。

I need to store a small piece of data (less than 10 characters) in a cookie in Rails and I need it to be secure. I don't want anybody being able to read that piece of data or injecting their own piece of data (as that would open up the app to many kinds of attacks). I think encrypting the contents of the cookie is the way to go (should I also sign it?). What is the best way to do it?

Right now I'm doing this, which looks secure, but many things looked secure to people that knew much more than I about security and then it was discovered it wasn't really secure.

I'm saving the secret in this way:

encryptor = ActiveSupport::MessageEncryptor.new(Example::Application.config.secret_token)
cookies[:secret] = {
  :value => encryptor.encrypt(secret),
  :domain => "example.com",
  :secure => !(Rails.env.test? || Rails.env.development?)
}

and then I'm reading it like this:

encryptor = ActiveSupport::MessageEncryptor.new(Example::Application.config.secret_token)
secret = encryptor.decrypt(cookies[:secret])

Is that secure? Any better ways of doing it?

Update: I know about Rails' session and how it is secure, both by signing the cookie and by optionally storing the contents of the session server side and I do use the session for what it is for. But my question here is about storing a cookie, a piece of information I do not want in the session but I still need it to be secure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽袖吟 2025-01-14 04:42:37
  • 设置安全 cookie

    cookies.signed[:secret] = {
     :值=> “富酒吧”,
     :域=> “example.com”,
     :安全=> !(Rails.env.test? || Rails.env.development?)
    }
    
  • 访问 cookie

    cookies.signed[:secret] # 返回“foo bar”
    

使用 ActionController::Base 进行签名.cookie_verifier_secret。您可以在初始化程序文件中设置cookie_verifier_secret

  • Setting a secure cookie

    cookies.signed[:secret] = {
     :value => "foo bar",
     :domain => "example.com",
     :secure => !(Rails.env.test? || Rails.env.development?)
    }
    
  • Accessing the cookie

    cookies.signed[:secret] # returns "foo bar"
    

The cookie is signed using ActionController::Base.cookie_verifier_secret. You can set the cookie_verifier_secret in the initializer file.

用心笑 2025-01-14 04:42:37

正如 KandadaBoggu 所说,看起来你想要的是一个会话变量,而会话变量默认是加密的并存储在 cookie 中。但是,如果您查看 config/initializers/session_store.rb 的内容,您会发现类似以下内容:

 # Be sure to restart your server when you modify this file.
 MyRailsApp::Application.config.session_store :cookie_store, :key => '_my_rails_app_session'

 # Use the database for sessions instead of the cookie-based default,
 # which shouldn't be used to store highly confidential information
 # (create the session table with "rails generate session_migration")
 # MyRailsApp::Application.config.session_store :active_record_store

这对我来说应该使用数据库而不是 cookie-基于默认值,不应该用于存储高度机密的信息。预先准备的迁移使一切都非常容易设置,因此这样做的开销非常小,一旦完成,如果您以后需要添加新的秘密信息,开销基本上为零!

As KandadaBoggu says, it looks like what you want is a session variable, and session variables are by default encrypted and stored in cookies. However, if you have a look at the contents of config/initializers/session_store.rb you will find something like the following:

 # Be sure to restart your server when you modify this file.
 MyRailsApp::Application.config.session_store :cookie_store, :key => '_my_rails_app_session'

 # Use the database for sessions instead of the cookie-based default,
 # which shouldn't be used to store highly confidential information
 # (create the session table with "rails generate session_migration")
 # MyRailsApp::Application.config.session_store :active_record_store

Which suggests to me that you should use the database for sessions instead of the cookie-based default, which shouldn't be used to store highly confidential information. The pre-cooked migration makes everything really easy to set up so there's very little overhead in doing so, and once it's done there's basically zero overhead if you need to add a new piece of secret information at a later date!

鱼窥荷 2025-01-14 04:42:37

我重新发布 JacobM 的答案,他删除了它,因为这是正确的答案,并在正确的方向。如果他取消删除,我会删除这个并选择他作为最佳答案。

首先,如果您使用encrypt_and_verify而不是encrypt,它将
为您签署 cookie。

但是,当涉及到安全性时,我总是更喜欢依赖
经过公开审查的解决方案,而不是我自己推出的解决方案。
一个例子是加密cookies gem

I'm re-posting JacobM's answer, that he deleted, because it was the correct answer and pointed me in the right direction. If he undeletes it, I'll delete this one and pick his as the best answer.

First of all, if you use encrypt_and_verify instead of encrypt it will
sign the cookie for you.

However, when it comes to security, I always prefer to rely on
solutions that have been vetted in public, rather than rolling my own.
An example would be the encrypted-cookies gem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文