Skip to content
Myr

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

3 files+24 added−12 removed

modified:requests/adapters.py-4 removed
284 unmodified lines
285 headers = {}286 username, password = get_auth_from_url(proxy)287288 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)291292 return headers
104 unmodified lines
modified:requests/utils.py+8 added-6 removed
632 unmodified lines
633634def 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 ('', '')638639 try:640 auth = (unquote(parsed.username), unquote(parsed.password))641 except (AttributeError, TypeError):642 auth = ('', '')643644 return auth645646647def 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 lines
modified:test_requests.py+16 added-2 removed
192 unmodified lines
193 # 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 197198 def test_param_cookiejar_works(self):199 cj = cookielib.CookieJar()200 cookiejar_from_dict({'foo' : 'bar'}, cj)201 s = requests.session()
499 unmodified lines
701 url = 'http://user:pass pass@complex.url.com/path?query=yes'702 assert ('user', 'pass pass') == requests.utils.get_auth_from_url(url)703704 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)707708 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)711712 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 lines
1095 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')10981099 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_chars110911101111class TestMorselToCookieExpires(unittest.TestCase):1112
58 unmodified lines