Skip to content

Commit

Permalink
Fix cookie domain calculation for two letter tld
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Jan 24, 2023
1 parent 5a16ead commit 941e0cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/middleware/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def handle_options(options)
# Case where tld_length is not provided
else
# Regular TLDs
if !(/([^.]{2,3}\.[^.]{2})$/.match?(request.host))
if !(/\.[^.]{2,3}\.[^.]{2}\z/.match?(request.host))
cookie_domain = dot_splitted_host.last(2).join(".")
# **.**, ***.** style TLDs like co.uk and com.au
else
Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/dispatch/cookies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,20 @@ def test_cookie_with_all_domain_option_using_uk_style_tld
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.co.uk; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_two_letter_one_level_tld
@request.host = "hawth.ca"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.hawth.ca; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_two_letter_one_level_tld_and_subdomain
@request.host = "x.hawth.ca"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.hawth.ca; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_uk_style_tld_and_two_subdomains
@request.host = "x.nextangle.co.uk"
get :set_cookie_with_domain
Expand Down

0 comments on commit 941e0cd

Please sign in to comment.