Skip to content

Commit 9cb0c88

Browse files
committed
Clear Authorization header when redirecting to cross-site
1 parent 27fbb07 commit 9cb0c88

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/httpclient.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ def attr_proxy(symbol, assignable = false)
378378
# Default User-Agent header
379379
DEFAULT_AGENT_NAME = 'HTTPClient/1.0'
380380

381+
# Authorization Header
382+
AUTH_HEADER = 'Authorization'
383+
381384
# Creates a HTTPClient instance which manages sessions, cookies, etc.
382385
#
383386
# HTTPClient.new takes optional arguments as a Hash.
@@ -1108,18 +1111,31 @@ def follow_redirect(method, uri, query, body, header, &block)
11081111
raise BadResponseError.new("Missing Location header for redirect", res)
11091112
end
11101113
method = :get if res.see_other? # See RFC2616 10.3.4
1114+
orig_uri = uri
11111115
uri = urify(@redirect_uri_callback.call(uri, res))
11121116
# To avoid duped query parameter. 'location' must include query part.
11131117
request_query = nil
11141118
previous = res
11151119
retry_number += 1
1120+
header = clear_auth_header(header, orig_uri, uri)
11161121
else
11171122
return res
11181123
end
11191124
end
11201125
raise BadResponseError.new("retry count exceeded", res)
11211126
end
11221127

1128+
def clear_auth_header(header, from_uri, to_uri)
1129+
return header if same_host?(from_uri, to_uri)
1130+
header.delete_if {|h| h[0] == AUTH_HEADER}
1131+
end
1132+
1133+
def same_host?(from_uri, to_uri)
1134+
return true if to_uri.path.start_with?("/")
1135+
1136+
[from_uri.scheme, from_uri.host, from_uri.port] == [to_uri.scheme, to_uri.host, to_uri.port]
1137+
end
1138+
11231139
def success_content(res)
11241140
if res.ok?
11251141
return res.content

0 commit comments

Comments
 (0)