Skip to content
Myr

Merge pull request #2393 from sigmavirus24/bug/2356

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

Attempt to quote anyway if unquoting fails

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 383b20b, 751b35c · Browse files · 2 files+28 added−4 removed
This commit

State

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

Diff stats

2 files+28 added−4 removed

  • other:+28 added−4 removed

2 files+28 added−4 removed

modified:requests/utils.py+12 added-4 removed
418 unmodified lines
419420 This function passes the given URI through an unquote/quote cycle to421 ensure that it is fully and consistently quoted.422 """423 safe_with_percent = "!#$%&'()*+,/:;=?@[]~"424 safe_without_percent = "!#$&'()*+,/:;=?@[]~"425 try:423 # Unquote only the unreserved characters424 # Then quote only illegal characters (do not quote reserved, unreserved,425 # or '%')426 return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")426 # Unquote only the unreserved characters427 # Then quote only illegal characters (do not quote reserved,428 # unreserved, or '%')429 return quote(unquote_unreserved(uri), safe=safe_with_percent)430 except InvalidURL:431 # We couldn't unquote the given URI, so let's try quoting it, but432 # there may be unquoted '%'s in the URI. We need to make sure they're433 # properly quoted so they do not cause issues elsewhere.434 return quote(uri, safe=safe_without_percent)435436437def address_in_network(ip, net):438 """
269 unmodified lines
modified:test_requests.py+16 added
1325 unmodified lines
1326 (username, password) = get_auth_from_url(url)1327 assert username == percent_encoding_test_chars1328 assert password == percent_encoding_test_chars13291330 def test_requote_uri_with_unquoted_percents(self):1331 """Ensure we handle unquoted percent signs in redirects.13321333 See: https://github.com/kennethreitz/requests/issues/23561334 """1335 from requests.utils import requote_uri1336 bad_uri = 'http://example.com/fiz?buz=%ppicture'1337 quoted = 'http://example.com/fiz?buz=%25ppicture'1338 assert quoted == requote_uri(bad_uri)13391340 def test_requote_uri_properly_requotes(self):1341 """Ensure requoting doesn't break expectations."""1342 from requests.utils import requote_uri1343 quoted = 'http://example.com/fiz?buz=%25ppicture'1344 assert quoted == requote_uri(quoted)134513461347class TestMorselToCookieExpires(unittest.TestCase):13481349 """Tests for morsel_to_cookie when morsel contains expires."""1350
250 unmodified lines