Skip to content

Commit 8746e96

Browse files
authored
Allow SameSite cookie attribute to be set (#37)
* Allow SameSite cookie attribute to be set Why: Changes to how browsers will handle the default SameSite attribute means it's generally considered better to explicitely set it. Lax is what browsers will default to. Further reading: https://www.chromium.org/updates/same-site https://tools.ietf.org/html/draft-west-cookie-incrementalism-00 https://web.dev/samesite-cookie-recipes/ * Automatically set cookie to Secure if SameSite is set to None.
1 parent 3c8d869 commit 8746e96

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ end
6666

6767
`angular_rails_csrf_secure` defaults to `false`.
6868

69+
### SameSite
70+
71+
The SameSite attribute now defaults to `:lax`. You can override this in the config:
72+
73+
```ruby
74+
# application.rb
75+
class Application < Rails::Application
76+
#...
77+
config.angular_rails_csrf_same_site = :strict
78+
end
79+
```
80+
81+
**NOTE**: When using `SameSite=None`, this gem automatically sets the cookie to `Secure`.
82+
6983
### Exclusions
7084

7185
Sometimes you will want to skip setting the XSRF token for certain controllers (for example, when using SSE or ActionCable, as discussed [here](https://github.com/jsanders/angular_rails_csrf/issues/7)):

lib/angular_rails_csrf/concern.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ def set_xsrf_token_cookie
1313

1414
config = Rails.application.config
1515

16+
same_site = config.respond_to?(:angular_rails_csrf_same_site) ? config.angular_rails_csrf_same_site : :lax
17+
secure = config.angular_rails_csrf_secure if config.respond_to?(:angular_rails_csrf_secure)
18+
1619
cookie_options = {
1720
value: form_authenticity_token,
18-
domain: config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil
21+
domain: config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil,
22+
same_site: same_site,
23+
secure: same_site == :none || secure
1924
}
20-
cookie_options[:secure] = config.angular_rails_csrf_secure if config.respond_to?(:angular_rails_csrf_secure)
2125

2226
cookie_name = config.respond_to?(:angular_rails_csrf_cookie_name) ? config.angular_rails_csrf_cookie_name : 'XSRF-TOKEN'
2327
cookies[cookie_name] = cookie_options

test/angular_rails_csrf_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,44 @@ def config.angular_rails_csrf_domain
7373
end
7474
end
7575

76+
test 'same_site is set to Lax by default' do
77+
get :index
78+
assert @response.headers['Set-Cookie'].include?('SameSite=Lax')
79+
assert_valid_cookie
80+
assert_response :success
81+
end
82+
83+
test 'same_site can be configured' do
84+
begin
85+
config = Rails.application.config
86+
config.define_singleton_method(:angular_rails_csrf_same_site) { :strict }
87+
88+
get :index
89+
assert @response.headers['Set-Cookie'].include?('SameSite=Strict')
90+
assert_valid_cookie
91+
assert_response :success
92+
ensure
93+
config.instance_eval('undef :angular_rails_csrf_same_site', __FILE__, __LINE__)
94+
end
95+
end
96+
97+
test 'secure is set automatically when same_site is set to none' do
98+
begin
99+
@request.headers['HTTPS'] = 'on'
100+
101+
config = Rails.application.config
102+
config.define_singleton_method(:angular_rails_csrf_same_site) { :none }
103+
104+
get :index
105+
assert @response.headers['Set-Cookie'].include?('SameSite=None')
106+
assert @response.headers['Set-Cookie'].include?('secure')
107+
assert_valid_cookie
108+
assert_response :success
109+
ensure
110+
config.instance_eval('undef :angular_rails_csrf_same_site', __FILE__, __LINE__)
111+
end
112+
end
113+
76114
private
77115

78116
# Helpers

0 commit comments

Comments
 (0)