Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SameSite attribute missing in cookies #214

Open
luma7 opened this issue Jul 22, 2024 · 3 comments
Open

SameSite attribute missing in cookies #214

luma7 opened this issue Jul 22, 2024 · 3 comments

Comments

@luma7
Copy link

luma7 commented Jul 22, 2024

Hi,
after migrating some application with activerecord-session_store from Rails 6.1.7 to 7.1.3.4, the SameSite attribute is missing in cookies:

curl -v http://localhost:3000
...
< set-cookie: _cta_session=9f0ec74db42eb11e0434f81fc257c348; path=/; httponly
...

I was expecting "SameSite=Lax" which is the default for Rails applications according to https://guides.rubyonrails.org/configuring.html#config-action-dispatch-cookies-same-site-protection:

< set-cookie: _cta_session=9f0ec74db42eb11e0434f81fc257c348; path=/; httponly; SameSite=Lax

It's the same issue with versions 2.0.0 and 2.1.0 of activerecord-session_store, and it's working with https://api.rubyonrails.org/classes/ActionDispatch/Session/CookieStore.html.

Steps to reproduce:

rails new cookies_test_app
cd cookies_test_app
rails generate controller Some

// app/controllers/some_controller.rb:
class SomeController < ApplicationController
  def index
    session[:something] = true
    render plain: "hi there"
  end
end

// add to config/routes.rb:
root "some#index"

// add to Gemfile:
gem 'activerecord-session_store'

// execute migrations
rails db:migrate

// config/session_store.rb:
CookiesTestApp::Application.config.session_store :active_record_store, key: '_cta_session', secure: Rails.env.production?

And then see "set-cookie" in the result of "curl -v http://localhost:3000".

@lcmen
Copy link

lcmen commented Mar 4, 2025

From my understanding an initializer needs to be added to ActionDispatch::Session::ActiveRecordStore class.

Similar to https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb#L64

def initialize(app, options = {})
  options[:same_site] = DEFAULT_SAME_SITE unless options.key?(:same_site)
  super
end

For the time being, I guess the workaround is to monkey patch the middleware?

# config/initializer/activerecord_session_store.rb
DEFAULT_SAME_SITE = proc { |request| request.cookies_same_site_protection }

ActionDispatch::Session::ActiveRecordStore.class_eval do
  def initialize(app, options = {})
    options[:same_site] = DEFAULT_SAME_SITE unless options.key?(:same_site)
    super
  end
end

@luma7
Copy link
Author

luma7 commented Mar 4, 2025

That's working, thanks!

@stevenharman
Copy link
Contributor

We're hitting this too and I agree with @lcmen - ActionDispatch::Session::ActiveRecordStore passes along whatever options it was configure with, and by default that DOES NOT include a :same_site value. So when Rack::Session::SessionId is created, it's defaulting :same_site to nil because the option is missing. That means, by the time ActionDispatch's cookie middleware runs, there is a :same_site key, so it won't set the default.

So yes, the monkey patch does work, or you could set the same_site: :lax (or whatever value you want) in your activerecord-session_store initializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants