Skip to content

Commit d7d4a49

Browse files
committed
Default to request's cookies_same_site_protection option
This brings the ActiveRecordStore in line with the CookieStore that ships with Rails. (see: rails/rails#45501) `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.
1 parent d517758 commit d7d4a49

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/action_dispatch/session/active_record_store.rb

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class ActiveRecordStore < ActionDispatch::Session::AbstractSecureStore
5959

6060
SESSION_RECORD_KEY = 'rack.session.record'
6161
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS
62+
DEFAULT_SAME_SITE = proc { |request| request.cookies_same_site_protection } # :nodoc:
63+
64+
def initialize(app, options = {})
65+
options[:same_site] = DEFAULT_SAME_SITE unless options.key?(:same_site)
66+
super
67+
end
6268

6369
private
6470
def get_session(request, sid)

test/action_controller_test.rb

+25
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ def test_getting_nil_session_value
9090
end
9191
end
9292

93+
def test_default_same_site_derives_SameSite_from_env
94+
with_test_route_set do
95+
get "/set_session_value"
96+
assert_match %r{SameSite=Lax}i, headers["Set-Cookie"]
97+
end
98+
end
99+
100+
def test_explicit_same_site_sets_SameSite
101+
session_options(same_site: :strict)
102+
103+
with_test_route_set do
104+
get "/set_session_value"
105+
assert_match %r{SameSite=Strict}i, headers["Set-Cookie"]
106+
end
107+
end
108+
109+
def test_explicit_nil_same_site_omits_SameSite
110+
session_options(same_site: nil)
111+
112+
with_test_route_set do
113+
get "/set_session_value"
114+
assert_no_match %r{SameSite=}i, headers["Set-Cookie"]
115+
end
116+
end
117+
93118
def test_calling_reset_session_twice_does_not_raise_errors
94119
with_test_route_set do
95120
get '/call_reset_session', :params => { :twice => "true" }

test/helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ def self.build_app(routes = nil)
6161

6262
private
6363

64+
# Overwrite `get` to set env hash
65+
def get(path, **options)
66+
options[:headers] ||= {}
67+
options[:headers].tap do |config|
68+
config["action_dispatch.cookies_same_site_protection"] ||= ->(_) { :lax }
69+
end
70+
71+
super
72+
end
73+
6474
def session_options(options = {})
6575
(@session_options ||= {key: "_session_id"}).merge!(options)
6676
end

0 commit comments

Comments
 (0)