psf / psf/requests

urllib3 LocationParseError (label empty or too long) uncaught by requests

Open
#5,744 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

When accessing an URL with a label with more than 63 characters, an urllib3.exceptions.LocationParseError (label empty or too long) is returned without being caught by requests.
Maybe this is related to #4746?

Expected Result

No exception, or an exception raised by requests (maybe InvalidURL?).

Actual Result

Exception is not caught by requests.

Reproduction Steps

import requests
requests.get('http://1234567890123456789012345678901234567890123456789012345678901234.com')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/lib64/python3.8/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib64/python3.8/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.8/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.8/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.8/http/client.py", line 950, in send
    self.connect()
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/connection.py", line 200, in connect
    conn = self._new_conn()
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/home/christian/Projects/EUvsDisinfo/venv/lib64/python3.8/site-packages/urllib3/util/connection.py", line 69, in create_connection
    return six.raise_from(
  File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: '1234567890123456789012345678901234567890123456789012345678901234.com', label empty or too long

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "4.0.0"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.7"
  },
  "platform": {
    "release": "5.10.11-100.fc32.x86_64",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.25.1"
  },
  "system_ssl": {
    "version": "1010109f"
  },
  "urllib3": {
    "version": "1.26.2"
  },
  "using_pyopenssl": false
}    

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The traceback leads to requests/adapters.py, after Session.send and during the urllib3 connection attempt. Reproduce the request with the overlong hostname, then inspect how urllib3.exceptions.LocationParseError is handled there. Done means the malformed URL produces the agreed requests-level behavior rather than leaking the urllib3 exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.