Merge pull request #1862 from sybeck2k/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixed parsing of username and password encoded in the URI
authored by Kenneth Reitz · committed by Kenneth Reitz · parents ac4e058, eb12435 · Browse files · 3 files·+24 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−12 removed
- other:+24 added−12 removed
Provenance
- Commit
0465d34- Tree
ef4e32f- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
ac4e058- Parent 2
eb12435
3 files changed+24 added−12 removed
modified:requests/adapters.py-4 removed
284 unmodified lines285 headers = {}286 username, password = get_auth_from_url(proxy)287 288 if username and password:289- # Proxy auth usernames and passwords will be urlencoded, we need290- # to decode them.291- username = unquote(username)292- password = unquote(password)289 headers['Proxy-Authorization'] = _basic_auth_str(username,290 password)291 292 return headers104 unmodified linesmodified:requests/utils.py+8 added-6 removed
632 unmodified lines633 634 def get_auth_from_url(url):635 """Given a url with authentication components, extract them into a tuple of636 username,password."""637- if url:638- url = unquote(url)639- parsed = urlparse(url)637+ parsed = urlparse(url)640- return (parsed.username, parsed.password)641- else:642- return ('', '')638 639+ try:640+ auth = (unquote(parsed.username), unquote(parsed.password))641+ except (AttributeError, TypeError):642+ auth = ('', '')643 644+ return auth645+646+647 def to_native_string(string, encoding='ascii'):648 """649 Given a string object, regardless of type, returns a representation of that650 string in the native string type, encoding and decoding where necessary.13 unmodified linesmodified:test_requests.py+16 added-2 removed
192 unmodified lines193 # Make sure the cookie was sent194 assert r.json()['cookies']['foo'] == 'bar'195 # Make sure the session cj is still the custom one196 assert s.cookies is cj197- 197+198 def test_param_cookiejar_works(self):199 cj = cookielib.CookieJar()200 cookiejar_from_dict({'foo' : 'bar'}, cj)201 s = requests.session()499 unmodified lines701 url = 'http://user:pass pass@complex.url.com/path?query=yes'702 assert ('user', 'pass pass') == requests.utils.get_auth_from_url(url)703 704 def test_get_auth_from_url_percent_chars(self):705- url = 'http://user%user:pass@complex.url.com/path?query=yes'705+ url = 'http://user%25user:pass@complex.url.com/path?query=yes'706 assert ('user%user', 'pass') == requests.utils.get_auth_from_url(url)707 708+ def test_get_auth_from_url_encoded_hashes(self):709+ url = 'http://user:pass%23pass@complex.url.com/path?query=yes'710+ assert ('user', 'pass#pass') == requests.utils.get_auth_from_url(url)711+712 def test_cannot_send_unprepared_requests(self):713 r = requests.Request(url=HTTPBIN)714 with pytest.raises(ValueError):715 requests.Session().send(r)379 unmodified lines1095 from requests.utils import address_in_network1096 assert address_in_network('192.168.1.1', '192.168.1.0/24')1097 assert not address_in_network('172.16.0.1', '192.168.1.0/24')1098 1099+ def test_get_auth_from_url(self):1100+ """ Ensures that username and password in well-encoded URI as per RFC 3986 are correclty extracted """1101+ from requests.utils import get_auth_from_url1102+ from requests.compat import quote1103+ percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "1104+ url_address = "request.com/url.html#test"1105+ url = "http://" + quote(percent_encoding_test_chars, '') + ':' + quote(percent_encoding_test_chars, '') + '@' + url_address1106+ (username, password) = get_auth_from_url(url)1107+ assert username == percent_encoding_test_chars1108+ assert password == percent_encoding_test_chars1109 1110 1111 class TestMorselToCookieExpires(unittest.TestCase):1112 58 unmodified lines