Skip to content

Commit 1d6de16

Browse files
committed
Merge pull request #47087 from jhawthorn/cookie_domain
Fix cookie domain for `domain: all` on two letter single level TLD
1 parent 23e0345 commit 1d6de16

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

actionpack/lib/action_dispatch/middleware/cookies.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ def handle_options(options)
461461
# Case where tld_length is not provided
462462
else
463463
# Regular TLDs
464-
if !(/([^.]{2,3}\.[^.]{2})$/.match?(request.host))
465-
cookie_domain = dot_splitted_host.last(2).join('.')
464+
if !(/\.[^.]{2,3}\.[^.]{2}\z/.match?(request.host))
465+
cookie_domain = dot_splitted_host.last(2).join(".")
466466
# **.**, ***.** style TLDs like co.uk and com.au
467467
else
468468
cookie_domain = dot_splitted_host.last(3).join('.')

actionpack/test/dispatch/cookies_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,20 @@ def test_cookie_with_all_domain_option_using_uk_style_tld
10701070
assert_cookie_header "user_name=rizwanreza; domain=.nextangle.co.uk; path=/; SameSite=Lax"
10711071
end
10721072

1073+
def test_cookie_with_all_domain_option_using_two_letter_one_level_tld
1074+
@request.host = "hawth.ca"
1075+
get :set_cookie_with_domain
1076+
assert_response :success
1077+
assert_cookie_header "user_name=rizwanreza; domain=.hawth.ca; path=/; SameSite=Lax"
1078+
end
1079+
1080+
def test_cookie_with_all_domain_option_using_two_letter_one_level_tld_and_subdomain
1081+
@request.host = "x.hawth.ca"
1082+
get :set_cookie_with_domain
1083+
assert_response :success
1084+
assert_cookie_header "user_name=rizwanreza; domain=.hawth.ca; path=/; SameSite=Lax"
1085+
end
1086+
10731087
def test_cookie_with_all_domain_option_using_uk_style_tld_and_two_subdomains
10741088
@request.host = "x.nextangle.co.uk"
10751089
get :set_cookie_with_domain

0 commit comments

Comments
 (0)