Skip to content

Commit c7fe699

Browse files
authored
Merge pull request #600 from sparklemotion/flavorjones-redirect-headers
fix: clear credentials when redirecting to a different port
2 parents 70ebc34 + 907c778 commit c7fe699

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/mechanize/http/agent.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
class Mechanize::HTTP::Agent
1111

12-
CREDENTIAL_HEADERS = ['Authorization', 'Cookie']
12+
CREDENTIAL_HEADERS = ['Authorization']
13+
COOKIE_HEADERS = ['Cookie']
1314
POST_HEADERS = ['Content-Length', 'Content-MD5', 'Content-Type']
1415

1516
# :section: Headers
@@ -998,10 +999,14 @@ def response_redirect(response, method, page, redirects, headers,
998999
end
9991000

10001001
# Make sure we clear credential headers if being redirected to another site
1001-
if new_uri.host != page.uri.host
1002-
CREDENTIAL_HEADERS.each do |ch|
1003-
headers.delete_if { |h| h.casecmp?(ch) }
1002+
if new_uri.host == page.uri.host
1003+
if new_uri.port != page.uri.port
1004+
# https://datatracker.ietf.org/doc/html/rfc6265#section-8.5
1005+
# cookies are OK to be shared across ports on the same host
1006+
CREDENTIAL_HEADERS.each { |ch| headers.delete_if { |h| h.casecmp?(ch) } }
10041007
end
1008+
else
1009+
(COOKIE_HEADERS + CREDENTIAL_HEADERS).each { |ch| headers.delete_if { |h| h.casecmp?(ch) } }
10051010
end
10061011

10071012
fetch new_uri, redirect_method, headers, [], referer, redirects + 1

test/test_mechanize_http_agent.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ def test_response_redirect_to_cross_site_with_credential
15691569
refute_includes(headers.keys, "AUTHORIZATION")
15701570
refute_includes(headers.keys, "cookie")
15711571

1572-
assert_match 'range|bytes=0-9999', page.body
1572+
assert_match("range|bytes=0-9999", page.body)
15731573
refute_match("authorization|Basic xxx", page.body)
15741574
refute_match("cookie|name=value", page.body)
15751575
end
@@ -1590,11 +1590,32 @@ def test_response_redirect_to_same_site_with_credential
15901590
assert_includes(headers.keys, "AUTHORIZATION")
15911591
assert_includes(headers.keys, "cookie")
15921592

1593-
assert_match 'range|bytes=0-9999', page.body
1593+
assert_match("range|bytes=0-9999", page.body)
15941594
assert_match("authorization|Basic xxx", page.body)
15951595
assert_match("cookie|name=value", page.body)
15961596
end
15971597

1598+
def test_response_redirect_to_same_site_diff_port_with_credential
1599+
@agent.redirect_ok = true
1600+
1601+
headers = {
1602+
'Range' => 'bytes=0-9999',
1603+
'AUTHORIZATION' => 'Basic xxx',
1604+
'cookie' => 'name=value',
1605+
}
1606+
1607+
page = html_page ''
1608+
page = @agent.response_redirect({ 'Location' => 'http://example:81/http_headers' }, :get,
1609+
page, 0, headers)
1610+
1611+
refute_includes(headers.keys, "AUTHORIZATION")
1612+
assert_includes(headers.keys, "cookie")
1613+
1614+
assert_match("range|bytes=0-9999", page.body)
1615+
refute_match("authorization|Basic xxx", page.body)
1616+
assert_match("cookie|name=value", page.body)
1617+
end
1618+
15981619
def test_response_redirect_not_ok
15991620
@agent.redirect_ok = false
16001621

0 commit comments

Comments
 (0)