Skip to content

Commit 1e8a307

Browse files
committed
Clear Authorization header when redirecting to cross-site
1 parent 4d60d8b commit 1e8a307

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.
@@ -1112,18 +1115,31 @@ def follow_redirect(method, uri, query, body, header, &block)
11121115
raise BadResponseError.new("Missing Location header for redirect", res)
11131116
end
11141117
method = :get if res.see_other? # See RFC2616 10.3.4
1118+
orig_uri = uri
11151119
uri = urify(@redirect_uri_callback.call(uri, res))
11161120
# To avoid duped query parameter. 'location' must include query part.
11171121
request_query = nil
11181122
previous = res
11191123
retry_number += 1
1124+
header = clear_auth_header(header, orig_uri, uri)
11201125
else
11211126
return res
11221127
end
11231128
end
11241129
raise BadResponseError.new("retry count exceeded", res)
11251130
end
11261131

1132+
def clear_auth_header(header, from_uri, to_uri)
1133+
return header if same_host?(from_uri, to_uri)
1134+
header.delete_if {|h| h[0] == AUTH_HEADER}
1135+
end
1136+
1137+
def same_host?(from_uri, to_uri)
1138+
return true if to_uri.path.start_with?("/")
1139+
1140+
[from_uri.scheme, from_uri.host, from_uri.port] == [to_uri.scheme, to_uri.host, to_uri.port]
1141+
end
1142+
11271143
def success_content(res)
11281144
if res.ok?
11291145
return res.content

0 commit comments

Comments
 (0)