Skip to content
Myr

Merge pull request #535 from Jonty/develop

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix decoding of invalid %encodings in URLs

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 51800bf, 06e4971 · Browse files · 3 files+77 added−72 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+77 added−72 removed

  • tests:+65 added−58 removed
  • other:+12 added−14 removed

3 files+77 added−72 removed

modified:requests/utils.py+10 added-13 removed
439 unmodified lines
440 return gen441442443# The unreserved URI characters (RFC 3986)444UNRESERVED_SET = frozenset(444_unreserved_set = frozenset(445 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"446 + "0123456789-._~")447448_unreserved_hextochr = dict((c.encode('hex'), c) for c in _unreserved_set)449450def unquote_unreserved(uri):451 """Un-escape any percent-escape sequences in a URI that are unreserved452 characters.453 This leaves all reserved, illegal and non-ASCII bytes encoded.454 """454 parts = uri.split('%')455 for i in range(1, len(parts)):456 h = parts[i][0:2]457 if len(h) == 2:458 c = chr(int(h, 16))459 if c in UNRESERVED_SET:460 parts[i] = c + parts[i][2:]461 else:462 parts[i] = '%' + parts[i]463 else:464 parts[i] = '%' + parts[i]465 return ''.join(parts)455 res = uri.split('%')456 s = res[0]457 for item in res[1:]:458 try:459 s += _unreserved_hextochr[item[:2].lower()] + item[2:]460 except (KeyError, UnicodeDecodeError):461 s += '%' + item462 return s463464465def requote_uri(uri):466 """Re-quote the given URI.
8 unmodified lines
modified:tests/test_requests.py+65 added-58 removed
115 unmodified lines
116 # heads = { u'User-Agent': u'\u30cd\u30c3\u30c8\u30ef\u30fc\u30af' }117 # self.assertRaises(UnicodeEncodeError, requests.get,118 # url=httpbin('get'), headers=heads)119120 # def test_session_with_escaped_url(self):121 # # Test a URL that contains percent-escaped characters122 # # This URL should not be modified (double-escaped)123 # # Tests:124 # # - Quoted illegal characters ("%20" (' '), "%3C" ('<'), "%3E" ('>'))125 # # - Quoted reserved characters ("%25" ('%'), "%23" ('#'), "%2F" ('/'))126 # # - Quoted non-ASCII characters ("%C3%98", "%C3%A5")127 # path_fully_escaped = '%3Ca%25b%23c%2Fd%3E/%C3%98%20%C3%A5'128 # url = httpbin('get/' + path_fully_escaped)129 # response = get(url)130 # self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))120 def test_session_with_escaped_url(self):121 # Test a URL that contains percent-escaped characters122 # This URL should not be modified (double-escaped)123 # Tests:124 # - Quoted illegal characters ("%20" (' '), "%3C" ('<'), "%3E" ('>'))125 # - Quoted reserved characters ("%25" ('%'), "%23" ('#'), "%2F" ('/'))126 # - Quoted non-ASCII characters ("%C3%98", "%C3%A5")127 path_fully_escaped = '%3Ca%25b%23c%2Fd%3E/%C3%98%20%C3%A5'128 url = httpbin('get/' + path_fully_escaped)129 response = get(url)130 self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))131132 # # Test that illegal characters in a path get properly percent-escaped133 # # Tests:134 # # - Bare illegal characters (space, '<')135 # # - Bare non-ASCII characters ('\u00d8')136 # path = u'<a%25b%23c%2Fd%3E/\u00d8 %C3%A5'137 # url = httpbin('get/' + path)138 # response = get(url)139 # self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))132 # Test that illegal characters in a path get properly percent-escaped133 # Tests:134 # - Bare illegal characters (space, '<')135 # - Bare non-ASCII characters ('\u00d8')136 path = u'<a%25b%23c%2Fd%3E/\u00d8 %C3%A5'137 url = httpbin('get/' + path)138 response = get(url)139 self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))140141 # # Test that reserved characters in a path do not get percent-escaped142 # # Tests:143 # # - All reserved characters (RFC 3986), except '?', '#', '[' and ']',144 # # which are not allowed in the path, and ';' which delimits145 # # parameters.146 # # All such characters must be allowed bare in path, and must not be147 # # encoded.148 # # - Special unreserved characters (RFC 3986), which should not be149 # # encoded (even though it wouldn't hurt).150 # path_reserved = '!$&\'()*+,/:=@-._~'151 # url = httpbin('get/' + path_reserved)152 # response = get(url)153 # self.assertEqual(response.url, httpbin('get/' + path_reserved))141 # Test that reserved characters in a path do not get percent-escaped142 # Tests:143 # - All reserved characters (RFC 3986), except '?', '#', '[' and ']',144 # which are not allowed in the path, and ';' which delimits145 # parameters.146 # All such characters must be allowed bare in path, and must not be147 # encoded.148 # - Special unreserved characters (RFC 3986), which should not be149 # encoded (even though it wouldn't hurt).150 path_reserved = '!$&\'()*+,/:=@-._~'151 url = httpbin('get/' + path_reserved)152 response = get(url)153 self.assertEqual(response.url, httpbin('get/' + path_reserved))154155 # # Test that percent-encoded unreserved characters in a path get156 # # normalised to their un-encoded forms.157 # path_unreserved = 'ABCDwxyz1234-._~'158 # path_unreserved_escaped = '%41%42%43%44%77%78%79%7A%31%32%33%34%2D%2E%5F%7E'159 # url = httpbin('get/' + path_unreserved_escaped)160 # response = get(url)161 # self.assertEqual(response.url, httpbin('get/' + path_unreserved))155 # Test that percent-encoded unreserved characters in a path get156 # normalised to their un-encoded forms.157 path_unreserved = 'ABCDwxyz1234-._~'158 path_unreserved_escaped = '%41%42%43%44%77%78%79%7A%31%32%33%34%2D%2E%5F%7E'159 url = httpbin('get/' + path_unreserved_escaped)160 response = get(url)161 self.assertEqual(response.url, httpbin('get/' + path_unreserved))162163 # # Re-run all of the same tests on the query part of the URI164 # query_fully_escaped = '%3Ca%25b%23c%2Fd%3E=%C3%98%20%C3%A5'165 # url = httpbin('get/?' + query_fully_escaped)166 # response = get(url)167 # self.assertEqual(response.url, httpbin('get/?' + query_fully_escaped))163 # Re-run all of the same tests on the query part of the URI164 query_fully_escaped = '%3Ca%25b%23c%2Fd%3E=%C3%98%20%C3%A5'165 url = httpbin('get/?' + query_fully_escaped)166 response = get(url)167 self.assertEqual(response.url, httpbin('get/?' + query_fully_escaped))168169 # query = u'<a%25b%23c%2Fd%3E=\u00d8 %C3%A5'170 # url = httpbin('get/?' + query)171 # response = get(url)172 # self.assertEqual(response.url, httpbin('get/?' + query_fully_escaped))169 query = u'<a%25b%23c%2Fd%3E=\u00d8 %C3%A5'170 url = httpbin('get/?' + query)171 response = get(url)172 self.assertEqual(response.url, httpbin('get/?' + query_fully_escaped))173174 # # The legal characters in query happens to be the same as in path175 # query_reserved = '!$&\'()*+,/:=@-._~'176 # url = httpbin('get/?' + query_reserved)177 # response = get(url)178 # self.assertEqual(response.url, httpbin('get/?' + query_reserved))174 # The legal characters in query happens to be the same as in path175 query_reserved = '!$&\'()*+,/:=@-._~'176 url = httpbin('get/?' + query_reserved)177 response = get(url)178 self.assertEqual(response.url, httpbin('get/?' + query_reserved))179180 # query_unreserved = 'ABCDwxyz=1234-._~'181 # query_unreserved_escaped = '%41%42%43%44%77%78%79%7A=%31%32%33%34%2D%2E%5F%7E'182 # url = httpbin('get/?' + query_unreserved_escaped)183 # response = get(url)184 # self.assertEqual(response.url, httpbin('get/?' + query_unreserved))180 query_unreserved = 'ABCDwxyz=1234-._~'181 query_unreserved_escaped = '%41%42%43%44%77%78%79%7A=%31%32%33%34%2D%2E%5F%7E'182 url = httpbin('get/?' + query_unreserved_escaped)183 response = get(url)184 self.assertEqual(response.url, httpbin('get/?' + query_unreserved))185186 # We should ignore invalid %encodings187 request = requests.Request("http://0.0.0.0/get/%TEST")188 self.assertEqual(request.path_url, "/get/%TEST")189190 request = requests.Request("http://0.0.0.0/get/%%TEST%%")191 self.assertEqual(request.path_url, "/get/%%TEST%%")192193 def test_user_agent_transfers(self):194 """Issue XX"""195196 heads = {
620 unmodified lines
modified:AUTHORS.rst+2 added-1 removed
91 unmodified lines
92- Jonathan Drosdeck93- Jiri Machalek94- Steve Pulec95- Michael Kelly96- Michael Newman <newmaniese@gmail.com>96- Michael Newman <newmaniese@gmail.com>97- Jonty Wareing <jonty@jonty.co.uk>