Set “secure” flag on session cookie in RoR even over HTTP

后端 未结 3 2181
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 14:39

In a Rails app, the session cookie can be easily set to include the secure cookie attribute, when sending over HTTPS to ensure that the cookie is not leaked over a

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 15:39

    The base issue is, that by definition of Set-Cookie, cookies with secure set may only be sent via secure connetions.
    So not sending cookies with secure set over HTTP is the expected behavior.

    You might want to set different cookie options in different environments. In config/environments/developent.rb you clould set

    Rails.application.configure do
      config.session_store :cache_store, key: COOKIE_NAME, same_site: :none
    end
    

    and in production (config/environments/production.rb), where you deploy your site with HTTPS:

    Rails.application.configure do
      config.session_store :cache_store, key: COOKIE_NAME, same_site: :lax, secure: true
    end
    

提交回复
热议问题