Skip to content

Commit d06f4ac

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 284156d commit d06f4ac

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
15
## Unreleased
26

7+
* Default to the request's `cookies_same_site_protection` setting, brining
8+
`ActiveRecordStore` in line with the default behavior of `CookieStore`.
9+
[@sharman [#222](https://github.com/rails/activerecord-session_store/pull/222)]
310
* Drop Rails 7.0 support.
11+
[@sharman [#221](https://github.com/rails/activerecord-session_store/pull/221)]
412

513
## 2.2.0
614

lib/action_dispatch/session/active_record_store.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "active_support/core_ext/module/attribute_accessors"
2-
require 'action_dispatch/middleware/session/abstract_store'
2+
require "action_dispatch/middleware/session/abstract_store"
33

44
module ActionDispatch
55
module Session
@@ -57,12 +57,14 @@ class ActiveRecordStore < ActionDispatch::Session::AbstractSecureStore
5757
# ActiveRecord::SessionStore::Session
5858
class_attribute :session_class
5959

60-
SESSION_RECORD_KEY = 'rack.session.record'
60+
DEFAULT_SAME_SITE = proc { |request| request.cookies_same_site_protection } # :nodoc:
6161
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS
62+
SESSION_RECORD_KEY = "rack.session.record"
6263

6364
def initialize(app, options = {})
6465
@secure_session_only = options.delete(:secure_session_only) { false }
65-
super(app, options)
66+
options[:same_site] = DEFAULT_SAME_SITE unless options.key?(:same_site)
67+
super
6668
end
6769

6870
private
@@ -167,7 +169,6 @@ def self.private_session_id?(session_id)
167169
# user tried to retrieve a session by a private key?
168170
session_id =~ /\A\d+::/
169171
end
170-
171172
end
172173
end
173174
end

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)