requests icon indicating copy to clipboard operation
requests copied to clipboard

Proxy password contains square bracket "[" will failed with error "Invalid IPv6 URL" from urllib3

Open Hugh---- opened this issue 8 years ago • 1 comments

Summary.

Expected Result

When I have passed a proxy with square bracket "[" in the password, the request session should work.

Actual Result

There will be error "Invalid IPv6 URL" generated by urllib3 which requests is based on.

Reproduction Steps

from requests import Request, Session

proxies = {
    'http': 'http://username:passw[[email protected]',
    'https': 'http://username:passw[ord@proxy-server:8081',
    }

s = Session()

req = Request('POST', auth_url, data=login_data)
prepped = s.prepare_request(req)
resp = s.send(prepped, proxies=proxies)
print(resp.cookies)


======The error message======
    resp = s.send(prepped, proxies=proxies)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 410, in send
    conn = self.get_connection(request.url, proxies)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 303, in get_connection
    proxy = prepend_scheme_if_needed(proxy, 'http')
  File "/usr/local/lib/python3.7/site-packages/requests/utils.py", line 894, in prepend_scheme_if_needed
    scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 368, in urlparse
    splitresult = urlsplit(url, scheme, allow_fragments)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 417, in urlsplit
    raise ValueError("Invalid IPv6 URL")
ValueError: Invalid IPv6 URL
======End of the error message======

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": "2.2.2"
  },
  "idna": {
    "version": "2.7"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.0"
  },
  "platform": {
    "release": "17.5.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "1010008f",
    "version": "18.0.0"
  },
  "requests": {
    "version": "2.19.1"
  },
  "system_ssl": {
    "version": "100020ff"
  },
  "urllib3": {
    "version": "1.23"
  },
  "using_pyopenssl": true
}

This command is only available on Requests v2.16.4 and greater. Otherwise, please provide some basic information about your system (Python version, operating system, &c).

Hugh---- avatar Jul 18 '18 03:07 Hugh----

Hi @Hugh----, the stacktrace you provided shows this error is from the standard library urllib module rather than urllib3. RFC 3986 states the allowable ABNF for a URI's authority component which doesn't include gen-delims (i.e. square brackets).

You'll need to percent-encode the password before using it in the URI in order to get things working as expected.

nateprewitt avatar Jul 18 '18 05:07 nateprewitt