Skip to content
Myr

Attempt to quote anyway if unquoting fails

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

Fixes #2356

authored by Ian Cordasco · committed by Ian Cordasco · parent bd3cf95 · 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
416 unmodified lines
417418 This function passes the given URI through an unquote/quote cycle to419 ensure that it is fully and consistently quoted.420 """421 safe_with_percent = "!#$%&'()*+,/:;=?@[]~"422 safe_without_percent = "!#$&'()*+,/:;=?@[]~"423 try:421 # Unquote only the unreserved characters422 # Then quote only illegal characters (do not quote reserved, unreserved,423 # or '%')424 return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")424 # Unquote only the unreserved characters425 # Then quote only illegal characters (do not quote reserved,426 # unreserved, or '%')427 return quote(unquote_unreserved(uri), safe=safe_with_percent)428 except InvalidURL:429 # We couldn't unquote the given URI, so let's try quoting it, but430 # there may be unquoted '%'s in the URI. We need to make sure they're431 # properly quoted so they do not cause issues elsewhere.432 return quote(uri, safe=safe_without_percent)433434435def address_in_network(ip, net):436 """
269 unmodified lines
modified:test_requests.py+16 added
1299 unmodified lines
1300 (username, password) = get_auth_from_url(url)1301 assert username == percent_encoding_test_chars1302 assert password == percent_encoding_test_chars13031304 def test_requote_uri_with_unquoted_percents(self):1305 """Ensure we handle unquoted percent signs in redirects.13061307 See: https://github.com/kennethreitz/requests/issues/23561308 """1309 from requests.utils import requote_uri1310 bad_uri = 'http://example.com/fiz?buz=%ppicture'1311 quoted = 'http://example.com/fiz?buz=%25ppicture'1312 assert quoted == requote_uri(bad_uri)13131314 def test_requote_uri_properly_requotes(self):1315 """Ensure requoting doesn't break expectations."""1316 from requests.utils import requote_uri1317 quoted = 'http://example.com/fiz?buz=%25ppicture'1318 assert quoted == requote_uri(quoted)131913201321class TestMorselToCookieExpires(unittest.TestCase):13221323 """Tests for morsel_to_cookie when morsel contains expires."""1324
250 unmodified lines