lexiforest / curl_cffi

Python binding for curl-impersonate fork via cffi. A http client that can impersonate browser tls/ja3/http2 fingerprints.
https://curl-cffi.readthedocs.io/
MIT License
2.49k stars 265 forks source link

[BUG] Curl.setopt() 中糟糕的类型传递 #341

Closed qishipai closed 4 months ago

qishipai commented 4 months ago

我尝试在aarch64 Android平台上运行这个项目,用最简单的方式测试:

from curl_cffi import requests
res = requests.get("https://www.baidu.com")
print(res.text)

我得到了如下报错:

Traceback (most recent call last):
  File "/data/user/0/coding.yu.pythoncompiler.new/files/default.py", line 3, in <module>
    res = requests.get("https://www.baidu.com")
  File "/data/user/0/coding.yu.pythoncompiler.new/files/PYROOT3/lib/python3.8/site-packages/curl_cffi/requests/__init__.py", line 125, in request
    return s.request(
  File "/data/user/0/coding.yu.pythoncompiler.new/files/PYROOT3/lib/python3.8/site-packages/curl_cffi/requests/session.py", line 899, in request
    req, buffer, header_buffer, q, header_recved, quit_now = self._set_curl_options(
  File "/data/user/0/coding.yu.pythoncompiler.new/files/PYROOT3/lib/python3.8/site-packages/curl_cffi/requests/session.py", line 652, in _set_curl_options
    c.setopt(CurlOpt.MAX_RECV_SPEED_LARGE, max_recv_speed)
  File "/data/user/0/coding.yu.pythoncompiler.new/files/PYROOT3/lib/python3.8/site-packages/curl_cffi/curl.py", line 217, in setopt
    self._check_error(ret, "setopt", option, value)
  File "/data/user/0/coding.yu.pythoncompiler.new/files/PYROOT3/lib/python3.8/site-packages/curl_cffi/curl.py", line 137, in _check_error
    raise error
curl_cffi.curl.CurlError: Failed to setopt CurlOpt.MAX_RECV_SPEED_LARGE 0, curl: (43) . See https://curl.se/libcurl/c/libcurl-errors.html first for more details.

Versions

Additional context 错误码: CURLE_BAD_FUNCTION_ARGUMENT (43) Curl.setopt()中对于curl_off_t的类型的传参方式与int相同,但在_curl_easy_setopt()中并不会在curl_off_t的情况下对指针解引用,导致实际传给curl_easy_setopt()的值是指针本身的值。由于目前版本内部仅有一处

# request/session.py
c.setopt(CurlOpt.MAX_RECV_SPEED_LARGE, max_recv_speed)

涉及到curl_off_t类型的使用,而指针通常具有的数值范围刚好可以使得这个隐患隐藏至今。Android的MTE机制将指针高位打上标记使其变为负数,从而使curl_easy_setopt()返回CURLE_BAD_FUNCTION_ARGUMENT。

预期行为 在Curl.setopt()中对于curl_off_t类型应构造int64_t类型并传递指针(curl/system.h中注明curl_off_t在任何平台下均为64位有符号整型),_curl_easy_setopt()中应对传curl_off_t的情况解引用。