Skip to content

Commit 1704be7

Browse files
Strict match when choosing cookie domain for host
Prior to this commit, when multiple cookie domains were specified, the first domain that was a substring of the request host was chosen. This allowed, for example, the "example.com" domain to be chosen when the request host was "example.com.au" or even "myexample.com". This commit ensures a domain is chosen only if it is equal to or is a superdomain of the request host. Fixes #37760.
1 parent 95af87f commit 1704be7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

actionpack/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* When multiple domains are specified for a cookie, a domain will now be
2+
chosen only if it is equal to or is a superdomain of the request host.
3+
4+
*Jonathan Hefner*
5+
16
* `ActionDispatch::Static` handles precompiled Brotli (.br) files.
27

38
Adds to existing support for precompiled gzip (.gz) files.

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,11 @@ def handle_options(options)
457457
".#{$&}"
458458
end
459459
elsif options[:domain].is_a? Array
460-
# If host matches one of the supplied domains without a dot in front of it.
461-
options[:domain] = options[:domain].find { |domain| request.host.include? domain.sub(/^\./, "") }
460+
# If host matches one of the supplied domains.
461+
options[:domain] = options[:domain].find do |domain|
462+
domain = domain.delete_prefix(".")
463+
request.host == domain || request.host.end_with?(".#{domain}")
464+
end
462465
end
463466
end
464467
end

actionpack/test/dispatch/cookies_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,27 @@ def test_cookie_with_several_preset_domains_using_one_of_these_domains
11061106
assert_cookie_header "user_name=rizwanreza; domain=example1.com; path=/; SameSite=Lax"
11071107
end
11081108

1109+
def test_cookie_with_several_preset_domains_using_subdomain
1110+
@request.host = "subdomain.example1.com"
1111+
get :set_cookie_with_domains
1112+
assert_response :success
1113+
assert_cookie_header "user_name=rizwanreza; domain=example1.com; path=/; SameSite=Lax"
1114+
end
1115+
1116+
def test_cookie_with_several_preset_domains_using_similar_tld
1117+
@request.host = "example1.com.au"
1118+
get :set_cookie_with_domains
1119+
assert_response :success
1120+
assert_cookie_header "user_name=rizwanreza; path=/; SameSite=Lax"
1121+
end
1122+
1123+
def test_cookie_with_several_preset_domains_using_similar_domain
1124+
@request.host = "myexample1.com"
1125+
get :set_cookie_with_domains
1126+
assert_response :success
1127+
assert_cookie_header "user_name=rizwanreza; path=/; SameSite=Lax"
1128+
end
1129+
11091130
def test_cookie_with_several_preset_domains_using_other_domain
11101131
@request.host = "other-domain.com"
11111132
get :set_cookie_with_domains

0 commit comments

Comments
 (0)