Skip to content

Commit

Permalink
Merge pull request #48036 from gareth/rfc6265-compliant-domain-all-co…
Browse files Browse the repository at this point in the history
…okies

Make Rails cookies RFC6265-compliant with domain: :all
  • Loading branch information
guilleiguaran committed Apr 24, 2023
2 parents 21d0ef0 + 8f3c6a1 commit 3c08d35
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Remove leading dot from domains on cookies set with `domain: :all`, to meet RFC6265 requirements

*Gareth Adams*

* Include source location in routes extended view.

```bash
Expand Down
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 @@ -470,7 +470,7 @@ def handle_options(options)
end

options[:domain] = if cookie_domain.present?
".#{cookie_domain}"
cookie_domain
end
elsif options[:domain].is_a? Array
# If host matches one of the supplied domains.
Expand Down
32 changes: 16 additions & 16 deletions actionpack/test/dispatch/cookies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,63 +1122,63 @@ def test_cookie_with_hash_value_not_modified_by_rotation
def test_cookie_with_all_domain_option
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.com; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.com; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_a_non_standard_tld
@request.host = "two.subdomains.nextangle.local"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.local; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.local; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_australian_style_tld
@request.host = "nextangle.com.au"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.com.au; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.com.au; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_australian_style_tld_and_two_subdomains
@request.host = "x.nextangle.com.au"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.com.au; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.com.au; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_uk_style_tld
@request.host = "nextangle.co.uk"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.co.uk; path=/; SameSite=Lax"
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"
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"
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
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.co.uk; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.co.uk; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_host_with_port
@request.host = "nextangle.local:3000"
get :set_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.local; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.local; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_localhost
Expand Down Expand Up @@ -1206,48 +1206,48 @@ def test_deleting_cookie_with_all_domain_option
request.cookies[:user_name] = "Joe"
get :delete_cookie_with_domain
assert_response :success
assert_set_cookie_header "user_name=; domain=.nextangle.com; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax"
assert_set_cookie_header "user_name=; domain=nextangle.com; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax"
end

def test_cookie_with_all_domain_option_and_tld_length
get :set_cookie_with_domain_and_tld
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.com; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.com; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_a_non_standard_tld_and_tld_length
@request.host = "two.subdomains.nextangle.local"
get :set_cookie_with_domain_and_tld
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.local; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.local; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_a_non_standard_2_letter_tld
@request.host = "admin.lvh.me"
get :set_cookie_with_domain_and_tld
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.lvh.me; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=lvh.me; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_host_with_port_and_tld_length
@request.host = "nextangle.local:3000"
get :set_cookie_with_domain_and_tld
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.nextangle.local; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=nextangle.local; path=/; SameSite=Lax"
end

def test_cookie_with_all_domain_option_using_longer_tld_length
@request.host = "x.y.z.t.com"
get :set_cookie_with_domain_and_longer_tld
assert_response :success
assert_set_cookie_header "user_name=rizwanreza; domain=.y.z.t.com; path=/; SameSite=Lax"
assert_set_cookie_header "user_name=rizwanreza; domain=y.z.t.com; path=/; SameSite=Lax"
end

def test_deleting_cookie_with_all_domain_option_and_tld_length
request.cookies[:user_name] = "Joe"
get :delete_cookie_with_domain_and_tld
assert_response :success
assert_set_cookie_header "user_name=; domain=.nextangle.com; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax"
assert_set_cookie_header "user_name=; domain=nextangle.com; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax"
end

def test_cookie_with_several_preset_domains_using_one_of_these_domains
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/dispatch/session/cookie_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def test_session_store_with_nil_domain
def test_session_store_with_all_domains
with_test_route_set(domain: :all) do
get "/set_session_value"
assert_match(/domain=\.example\.com/, headers["Set-Cookie"])
assert_match(/domain=example\.com/, headers["Set-Cookie"])
end
end

Expand Down

0 comments on commit 3c08d35

Please sign in to comment.