diff options
author | Miro Hrončok <miro@hroncok.cz> | 2019-05-08 18:33:24 +0200 |
---|---|---|
committer | Ned Deily <nad@python.org> | 2019-05-08 12:33:24 -0400 |
commit | c50d437e942d4c4c45c8cd76329b05340c02eb31 (patch) | |
tree | e7da4e6be490c8da2beb5e6fc26f1f1e9bc3eb4f /Lib/http | |
parent | bpo-36742: Fixes handling of pre-normalization characters in urlsplit() (GH-1... (diff) | |
download | cpython-c50d437e942d4c4c45c8cd76329b05340c02eb31.tar.gz cpython-c50d437e942d4c4c45c8cd76329b05340c02eb31.tar.bz2 cpython-c50d437e942d4c4c45c8cd76329b05340c02eb31.zip |
bpo-30458: Disallow control chars in http URLs. (GH-12755) (GH-13155)
Disallow control chars in http URLs in urllib.urlopen. This addresses a potential security problem for applications that do not sanity check their URLs where http request headers could be injected.
Disable https related urllib tests on a build without ssl (GH-13032)
These tests require an SSL enabled build. Skip these tests when python is built without SSL to fix test failures.
Use http.client.InvalidURL instead of ValueError as the new error case's exception. (GH-13044)
Co-Authored-By: Miro Hrončok <miro@hroncok.cz>
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/client.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py index baabfeb2ea8..1a6bd8ac42e 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -141,6 +141,16 @@ _MAXHEADERS = 100 _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search +# These characters are not allowed within HTTP URL paths. +# See https://tools.ietf.org/html/rfc3986#section-3.3 and the +# https://tools.ietf.org/html/rfc3986#appendix-A pchar definition. +# Prevents CVE-2019-9740. Includes control characters such as \r\n. +# We don't restrict chars above \x7f as putrequest() limits us to ASCII. +_contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]') +# Arguably only these _should_ allowed: +# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$") +# We are more lenient for assumed real world compatibility purposes. + # We always set the Content-Length header for these methods because some # servers will otherwise respond with a 411 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'} @@ -1111,6 +1121,11 @@ class HTTPConnection: self._method = method if not url: url = '/' + # Prevent CVE-2019-9740. + match = _contains_disallowed_url_pchar_re.search(url) + if match: + raise InvalidURL(f"URL can't contain control characters. {url!r} " + f"(found at least {match.group()!r})") request = '%s %s %s' % (method, url, self._http_vsn_str) # Non-ASCII characters should have been eliminated earlier |