curl_cffi: [BUG] URLs being url decoded before request is sent

Describe the bug We have an endpoint which utilizes IDs that contain URL-encoded special characters. After the update to curl_cffi 0.7.2, these requests began failing. After some investigation we found the package had started url-decoding the url before sending the request. In our case, the special characters are %2f or /, which causes the URL to be malformed and the request to fail.

To Reproduce

from curl_cffi import requests

# used my own website because I know it doesn't redirect on 404
url = 'https://duncanlutz.dev/example/%2f%2f%2f'

session = requests.Session()
response = session.get(url)

# Asserts currently fail
assert response.url == url

I’ve also put a repo up with the example: https://github.com/duncanlutz/curl_cffi_issue

Expected behavior In previous versions, the url had not been decoded before making the request. Our expected behavior would be to either not decode the URL, or provide a way to opt out of decoding.

Versions

  • OS: macOS Sonoma 14.1.1
  • curl_cffi version 0.7.2
  • pip freeze dump:
certifi==2024.8.30
cffi==1.17.1
curl_cffi==0.7.2
pycparser==2.22
typing_extensions==4.12.2

Additional context

  • Which session are you using? async or sync? We are using sync.
  • If using async session, which loop implementation are you using?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@lexiforest.

Since curl_cffi aims to be API-compatible with the requests library, may I suggest using requests’ requote_uri()? It is the standard way the library deals with URL-encoded strings.

assert requote_uri("https://duncanlutz.dev/example/%2f%2f%2f") == "https://duncanlutz.dev/example/%2f%2f%2f"
assert requote_uri("https://duncanlutz.dev/e x a m p l e") == "https://duncanlutz.dev/e%20x%20a%20m%20p%20l%20e"

It covers #333 while also fixing this current issue.

+1, our test suite has also picked this up.

Might be due to https://github.com/lexiforest/curl_cffi/commit/9c13b830f378687900ddbb953ae8edb9998b3b1d


As a side note, I don’t think you should be url-decoding and then re-encoding the query component, as it may not produce the same result. https://datatracker.ietf.org/doc/html/rfc3986#section-2.4

Here’s what urllib3 does, for example.: https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/url.py#L227