Skip to content

Commit 83fc701

Browse files
bpo-38576: Disallow control characters in hostnames in http.client (pythonGH-18995) (pythonGH-19002)
Add host validation for control characters for more CVE-2019-18348 protection. (cherry picked from commit 9165add) Co-authored-by: Ashwin Ramaswami <aramaswamis@gmail.com>
1 parent 6b6756f commit 83fc701

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

Lib/http/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,8 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
858858

859859
(self.host, self.port) = self._get_hostport(host, port)
860860

861+
self._validate_host(self.host)
862+
861863
# This is stored as an instance variable to allow unit
862864
# tests to replace it with a suitable mockup
863865
self._create_connection = socket.create_connection
@@ -1215,6 +1217,14 @@ def _validate_path(self, url):
12151217
raise InvalidURL(f"URL can't contain control characters. {url!r} "
12161218
f"(found at least {match.group()!r})")
12171219

1220+
def _validate_host(self, host):
1221+
"""Validate a host so it doesn't contain control characters."""
1222+
# Prevent CVE-2019-18348.
1223+
match = _contains_disallowed_url_pchar_re.search(host)
1224+
if match:
1225+
raise InvalidURL(f"URL can't contain control characters. {host!r} "
1226+
f"(found at least {match.group()!r})")
1227+
12181228
def putheader(self, header, *values):
12191229
"""Send a request header line to the server.
12201230

Lib/test/test_httplib.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def run_server():
11321132
thread.join()
11331133
self.assertEqual(result, b"proxied data\n")
11341134

1135-
def test_putrequest_override_validation(self):
1135+
def test_putrequest_override_domain_validation(self):
11361136
"""
11371137
It should be possible to override the default validation
11381138
behavior in putrequest (bpo-38216).
@@ -1145,6 +1145,17 @@ def _validate_path(self, url):
11451145
conn.sock = FakeSocket('')
11461146
conn.putrequest('GET', '/\x00')
11471147

1148+
def test_putrequest_override_host_validation(self):
1149+
class UnsafeHTTPConnection(client.HTTPConnection):
1150+
def _validate_host(self, url):
1151+
pass
1152+
1153+
conn = UnsafeHTTPConnection('example.com\r\n')
1154+
conn.sock = FakeSocket('')
1155+
# set skip_host so a ValueError is not raised upon adding the
1156+
# invalid URL as the value of the "Host:" header
1157+
conn.putrequest('GET', '/', skip_host=1)
1158+
11481159
def test_putrequest_override_encoding(self):
11491160
"""
11501161
It should be possible to override the default encoding

Lib/test/test_urllib.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_willclose(self):
331331
self.unfakehttp()
332332

333333
@unittest.skipUnless(ssl, "ssl module required")
334-
def test_url_with_control_char_rejected(self):
334+
def test_url_path_with_control_char_rejected(self):
335335
for char_no in list(range(0, 0x21)) + [0x7f]:
336336
char = chr(char_no)
337337
schemeless_url = f"//localhost:7777/test{char}/"
@@ -358,7 +358,7 @@ def test_url_with_control_char_rejected(self):
358358
self.unfakehttp()
359359

360360
@unittest.skipUnless(ssl, "ssl module required")
361-
def test_url_with_newline_header_injection_rejected(self):
361+
def test_url_path_with_newline_header_injection_rejected(self):
362362
self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
363363
host = "localhost:7777?a=1 HTTP/1.1\r\nX-injected: header\r\nTEST: 123"
364364
schemeless_url = "//" + host + ":8080/test/?test=a"
@@ -383,6 +383,38 @@ def test_url_with_newline_header_injection_rejected(self):
383383
finally:
384384
self.unfakehttp()
385385

386+
@unittest.skipUnless(ssl, "ssl module required")
387+
def test_url_host_with_control_char_rejected(self):
388+
for char_no in list(range(0, 0x21)) + [0x7f]:
389+
char = chr(char_no)
390+
schemeless_url = f"//localhost{char}/test/"
391+
self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
392+
try:
393+
escaped_char_repr = repr(char).replace('\\', r'\\')
394+
InvalidURL = http.client.InvalidURL
395+
with self.assertRaisesRegex(
396+
InvalidURL, f"contain control.*{escaped_char_repr}"):
397+
urlopen(f"http:{schemeless_url}")
398+
with self.assertRaisesRegex(InvalidURL, f"contain control.*{escaped_char_repr}"):
399+
urlopen(f"https:{schemeless_url}")
400+
finally:
401+
self.unfakehttp()
402+
403+
@unittest.skipUnless(ssl, "ssl module required")
404+
def test_url_host_with_newline_header_injection_rejected(self):
405+
self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello.")
406+
host = "localhost\r\nX-injected: header\r\n"
407+
schemeless_url = "//" + host + ":8080/test/?test=a"
408+
try:
409+
InvalidURL = http.client.InvalidURL
410+
with self.assertRaisesRegex(
411+
InvalidURL, r"contain control.*\\r"):
412+
urlopen(f"http:{schemeless_url}")
413+
with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
414+
urlopen(f"https:{schemeless_url}")
415+
finally:
416+
self.unfakehttp()
417+
386418
def test_read_0_9(self):
387419
# "0.9" response accepted (but not "simple responses" without
388420
# a status line)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disallow control characters in hostnames in http.client, addressing CVE-2019-18348. Such potentially malicious header injection URLs now cause a InvalidURL to be raised.

0 commit comments

Comments
 (0)