Repository navigation
Description
Subject
A SOCKS proxy URI userinfo subcomponent containing percent encoded values (e.g. socks5h://user:%70ass@host:1080) will not have the username and password be percent decoded or unquoted before they're passed to the proxy server for authentication.
Environment
OS: Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.36
Python: 3.13.0
urllib3: 2.6.3
Steps to Reproduce
I ran into this problem with a password containing a delimiter character that needed to be percent encoded to have the proxy URI correctly parsed. Despite fixing the encoding, authentication failed. As a synthetic test, one can percent encode any username or password character from a case that works and it will fail.
import urllib3
p = 'socks5h://user:pass@host:1080'
spm = urllib3.contrib.socks.SOCKSProxyManager(proxy_url=p)
spm.request('GET', 'https://httpbin.org/ip').data
# suppose this works, now replace a character in the username or password with the percent encoded equivalent:
p = 'socks5h://user:%70ass@host:1080'
spm = urllib3.contrib.socks.SOCKSProxyManager(proxy_url=p)
spm.request('GET', 'https://httpbin.org/ip').data
# this should fail with: socks.SOCKS5AuthError: SOCKS5 authentication failedExpected Behavior
Pass percent decoded or unquoted username and password values to the proxy for authentication.
Actual Behavior
Percent encoded usernames and passwords are presented to the proxy causing authentication failures.
Additional Notes
While exploring this issue I convinced myself that it's reasonable for the parse_uri function and the Uri object to not hold percent decoded values, because they only break the URI down as far as subcomponents. I would think differently if, like urllib's urlsplit function, there were accessors for username and password directly.
I believe the problem lies in the SOCKSProxyManager:
parsed = parse_url(proxy_url)
if username is None and password is None and parsed.auth is not None:
split = parsed.auth.split(":")
if len(split) == 2:
username, password = splitAfter the username and password no longer have the semantics of the URI userinfo subcomponent, they should be percent decoded or unquoted for use in the rest of the application. The ready example is "how does cURL handle this?" which is here:
/* URL decode the components */
if(!result && u) {
Curl_safefree(s->str[STRING_PROXYUSERNAME]);
result = Curl_urldecode(u, 0, &s->str[STRING_PROXYUSERNAME], NULL,
REJECT_ZERO);
}
if(!result && p) {
Curl_safefree(s->str[STRING_PROXYPASSWORD]);
result = Curl_urldecode(p, 0, &s->str[STRING_PROXYPASSWORD], NULL,
REJECT_ZERO);
}I also tested using requests v2.32.5 which works, and uses urllib3, but manually configures the SOCKSProxyManager username and password instead of relying on parsing the proxy_url:
elif proxy.lower().startswith("socks"):
username, password = get_auth_from_url(proxy)
manager = self.proxy_manager[proxy] = SOCKSProxyManager(
proxy,
username=username,
password=password,
num_pools=self._pool_connections,
maxsize=self._pool_maxsize,
block=self._pool_block,
**proxy_kwargs,
)The get_auth_from_url utility performs the percent decode here.
Activity
Fix SOCKS proxy auth failure with percent-encoded userinfo
Fix SOCKS proxy auth failure with percent-encoded userinfo