[英]How to set cookie to secure in Rails
在我的 Rails 應用程序中,我想讓我的 cookies 得到保護,但它不起作用。
我目前在我的production.rb文件中強制使用SSL,但它並沒有保護cookies,即使它應該來自我讀到的內容。
我在 Rails 5+ 上,沒有我在不同線程中看到的 session_store.rb 文件。 我試過了,但我的 cookie 仍然不安全。
我試着添加這個:
Rails.application.config.session_store :cookie_store, key: '_application_session', secure: true, httponly: true
但是,當我在控制台中使用curl
時,我看不到“第二個”set-cookie。 因此,當我登錄我的網站時,它會將我重定向到索引而不登錄。
我的production.rb中有這個:
config.force_ssl = true
config.to_prepare { Devise::SessionsController.force_ssl }
config.to_prepare { Devise::RegistrationsController.force_ssl }
config.to_prepare { Devise::PasswordsController.force_ssl }
如何在 Rails 5+ 上解決此問題?
使用 Securityheaders gem 修復: https://github.com/github/secure_headers
制作了一個包含以下內容的初始化程序:
SecureHeaders::Configuration.default do |config|
config.cookies = {
secure: true, # mark all cookies as "Secure"
httponly: true, # mark all cookies as "HttpOnly"
samesite: {
lax: true
}
}
config.hsts = "max-age=#{1.week.to_i}"
config.x_frame_options = "DENY"
config.x_content_type_options = "nosniff"
config.x_xss_protection = "1; mode=block"
config.x_download_options = "noopen"
config.x_permitted_cross_domain_policies = "none"
config.referrer_policy = %w(origin-when-cross-origin strict-origin-when-cross-origin)
config.csp = SecureHeaders::OPT_OUT
end
rest 我只使用了 rails 的預構建 csp,因為它太復雜了,我無法用 gem 手動完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.