Skip to content

Commit c0f71da

Browse files
Merge pull request rails#45501 from ghiculescu/same-site-false
Allow opting out of the `SameSite` cookie attribute when setting a cookie
2 parents b70947b + d29e755 commit c0f71da

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

actionpack/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Allow opting out of the `SameSite` cookie attribute when setting a cookie.
2+
3+
You can opt out of `SameSite` by passing `same_site: nil`.
4+
5+
`cookies[:foo] = { value: "bar", same_site: nil }`
6+
7+
Previously, this incorrectly set the `SameSite` attribute to the value of the `cookies_same_site_protection` setting.
8+
9+
*Alex Ghiculescu*
10+
111
* Allow using `helper_method`s in `content_security_policy` and `permissions_policy`
212

313
Previously you could access basic helpers (defined in helper modules), but not

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ def handle_options(options)
453453

454454
options[:path] ||= "/"
455455

456-
cookies_same_site_protection = request.cookies_same_site_protection
457-
options[:same_site] ||= cookies_same_site_protection.call(request)
456+
unless options.key?(:same_site)
457+
options[:same_site] = request.cookies_same_site_protection.call(request)
458+
end
458459

459460
if options[:domain] == :all || options[:domain] == "all"
460461
# If there is a provided tld length then we use it otherwise default domain regexp.

actionpack/test/dispatch/cookies_test.rb

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ def rails_5_2_stable_signed_cookie_with_authenticated_encryption_flag_off
334334

335335
head :ok
336336
end
337+
338+
def set_same_site_strict
339+
cookies["user_name"] = { value: "david", same_site: :strict }
340+
head :ok
341+
end
342+
343+
def set_same_site_nil
344+
cookies["user_name"] = { value: "david", same_site: nil }
345+
head :ok
346+
end
337347
end
338348

339349
tests TestController
@@ -362,15 +372,15 @@ def setup
362372
@request.host = "www.nextangle.com"
363373
end
364374

365-
def test_setting_cookie_with_no_protection
375+
def test_setting_cookie_with_no_same_site_protection
366376
@request.env["action_dispatch.cookies_same_site_protection"] = proc { :none }
367377

368378
get :authenticate
369379
assert_cookie_header "user_name=david; path=/; SameSite=None"
370380
assert_equal({ "user_name" => "david" }, @response.cookies)
371381
end
372382

373-
def test_setting_cookie_with_protection_proc_normal_user_agent
383+
def test_setting_cookie_with_same_site_protection_proc_normal_user_agent
374384
@request.env["action_dispatch.cookies_same_site_protection"] = Proc.new do |request|
375385
:strict unless request.user_agent == "spooky browser"
376386
end
@@ -380,7 +390,7 @@ def test_setting_cookie_with_protection_proc_normal_user_agent
380390
assert_equal({ "user_name" => "david" }, @response.cookies)
381391
end
382392

383-
def test_setting_cookie_with_protection_proc_special_user_agent
393+
def test_setting_cookie_with_same_site_protection_proc_special_user_agent
384394
@request.env["action_dispatch.cookies_same_site_protection"] = Proc.new do |request|
385395
:strict unless request.user_agent == "spooky browser"
386396
end
@@ -391,7 +401,7 @@ def test_setting_cookie_with_protection_proc_special_user_agent
391401
assert_equal({ "user_name" => "david" }, @response.cookies)
392402
end
393403

394-
def test_setting_cookie_with_misspelled_protection_raises
404+
def test_setting_cookie_with_misspelled_same_site_protection_raises
395405
@request.env["action_dispatch.cookies_same_site_protection"] = proc { :funky }
396406

397407
error = assert_raise ArgumentError do
@@ -400,14 +410,38 @@ def test_setting_cookie_with_misspelled_protection_raises
400410
assert_match "Invalid SameSite value: :funky", error.message
401411
end
402412

403-
def test_setting_cookie_with_strict
413+
def test_setting_cookie_with_same_site_strict
404414
@request.env["action_dispatch.cookies_same_site_protection"] = proc { :strict }
405415

406416
get :authenticate
407417
assert_cookie_header "user_name=david; path=/; SameSite=Strict"
408418
assert_equal({ "user_name" => "david" }, @response.cookies)
409419
end
410420

421+
def test_setting_cookie_with_same_site_nil
422+
@request.env["action_dispatch.cookies_same_site_protection"] = proc { nil }
423+
424+
get :authenticate
425+
assert_cookie_header "user_name=david; path=/"
426+
assert_equal({ "user_name" => "david" }, @response.cookies)
427+
end
428+
429+
def test_setting_cookie_with_specific_same_site_strict
430+
@request.env["action_dispatch.cookies_same_site_protection"] = proc { :lax }
431+
432+
get :set_same_site_strict
433+
assert_cookie_header "user_name=david; path=/; SameSite=Strict"
434+
assert_equal({ "user_name" => "david" }, @response.cookies)
435+
end
436+
437+
def test_setting_cookie_with_specific_same_site_nil
438+
@request.env["action_dispatch.cookies_same_site_protection"] = proc { :lax }
439+
440+
get :set_same_site_nil
441+
assert_cookie_header "user_name=david; path=/"
442+
assert_equal({ "user_name" => "david" }, @response.cookies)
443+
end
444+
411445
def test_setting_cookie
412446
get :authenticate
413447
assert_cookie_header "user_name=david; path=/; SameSite=Lax"

0 commit comments

Comments
 (0)