File tree 4 files changed +48
-4
lines changed
lib/action_dispatch/session
4 files changed +48
-4
lines changed Original file line number Diff line number Diff line change
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
1
5
## Unreleased
2
6
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 )]
3
10
* Drop Rails 7.0 support.
11
+ [ @sharman [ #221 ] ( https://github.com/rails/activerecord-session_store/pull/221 )]
4
12
5
13
## 2.2.0
6
14
Original file line number Diff line number Diff line change 1
1
require "active_support/core_ext/module/attribute_accessors"
2
- require ' action_dispatch/middleware/session/abstract_store'
2
+ require " action_dispatch/middleware/session/abstract_store"
3
3
4
4
module ActionDispatch
5
5
module Session
@@ -57,12 +57,14 @@ class ActiveRecordStore < ActionDispatch::Session::AbstractSecureStore
57
57
# ActiveRecord::SessionStore::Session
58
58
class_attribute :session_class
59
59
60
- SESSION_RECORD_KEY = 'rack.session.record'
60
+ DEFAULT_SAME_SITE = proc { | request | request . cookies_same_site_protection } # :nodoc:
61
61
ENV_SESSION_OPTIONS_KEY = Rack ::RACK_SESSION_OPTIONS
62
+ SESSION_RECORD_KEY = "rack.session.record"
62
63
63
64
def initialize ( app , options = { } )
64
65
@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
66
68
end
67
69
68
70
private
@@ -167,7 +169,6 @@ def self.private_session_id?(session_id)
167
169
# user tried to retrieve a session by a private key?
168
170
session_id =~ /\A \d +::/
169
171
end
170
-
171
172
end
172
173
end
173
174
end
Original file line number Diff line number Diff line change @@ -90,6 +90,31 @@ def test_getting_nil_session_value
90
90
end
91
91
end
92
92
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
+
93
118
def test_calling_reset_session_twice_does_not_raise_errors
94
119
with_test_route_set do
95
120
get '/call_reset_session' , :params => { :twice => "true" }
Original file line number Diff line number Diff line change @@ -61,6 +61,16 @@ def self.build_app(routes = nil)
61
61
62
62
private
63
63
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
+
64
74
def session_options ( options = { } )
65
75
( @session_options ||= { key : "_session_id" } ) . merge! ( options )
66
76
end
You can’t perform that action at this time.
0 commit comments