diff options
author | Ashwin Ramaswami <aramaswamis@gmail.com> | 2020-03-14 18:56:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-14 18:56:06 (GMT) |
commit | 9165addc22d05e776a54319a8531ebd0b2fe01ef (patch) | |
tree | 288869da768bf6c9c5c930bf50e58a36114b04e6 /Lib/http | |
parent | 6672c16b1d7f83789bf3a2016bd19edfd3568e71 (diff) | |
download | cpython-9165addc22d05e776a54319a8531ebd0b2fe01ef.zip cpython-9165addc22d05e776a54319a8531ebd0b2fe01ef.tar.gz cpython-9165addc22d05e776a54319a8531ebd0b2fe01ef.tar.bz2 |
bpo-38576: Disallow control characters in hostnames in http.client (GH-18995)
Add host validation for control characters for more CVE-2019-18348 protection.
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/client.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py index 33a4347..019380a 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -828,6 +828,8 @@ class HTTPConnection: (self.host, self.port) = self._get_hostport(host, port) + self._validate_host(self.host) + # This is stored as an instance variable to allow unit # tests to replace it with a suitable mockup self._create_connection = socket.create_connection @@ -1183,6 +1185,14 @@ class HTTPConnection: raise InvalidURL(f"URL can't contain control characters. {url!r} " f"(found at least {match.group()!r})") + def _validate_host(self, host): + """Validate a host so it doesn't contain control characters.""" + # Prevent CVE-2019-18348. + match = _contains_disallowed_url_pchar_re.search(host) + if match: + raise InvalidURL(f"URL can't contain control characters. {host!r} " + f"(found at least {match.group()!r})") + def putheader(self, header, *values): """Send a request header line to the server. |