Debian Codegraph @

debian/patches/security/CVE-2026-1502

raw symbols SCIP
1 From: Seth Larson <[email protected]>
2 Date: Fri, 10 Apr 2026 10:21:42 -0500
3 Subject: gh-146211: Reject CR/LF in HTTP tunnel request headers (GH-146212)
4 (cherry picked from commit 05ed7ce7ae9e17c23a04085b2539fe6d6d3cef69)
5
6 Co-authored-by: Seth Larson <[email protected]>
7 Co-authored-by: Illia Volochii <[email protected]>
8
9 Origin: https://github.com/python/cpython/pull/148351
10 ---
11 lib-python/3/http/client.py | 11 +++++++++-
12 lib-python/3/test/test_httplib.py | 45 +++++++++++++++++++++++++++++++++++++++
13 2 files changed, 55 insertions(+), 1 deletion(-)
14
15 diff --git a/lib-python/3/http/client.py b/lib-python/3/http/client.py
16 index c977612..8cf783a 100644
17 --- a/lib-python/3/http/client.py
18 +++ b/lib-python/3/http/client.py
19 @@ -941,12 +941,21 @@ class HTTPConnection:
20 return ip
21
22 def _tunnel(self):
23 + if _contains_disallowed_url_pchar_re.search(self._tunnel_host):
24 + raise ValueError('Tunnel host can\'t contain control characters %r'
25 + % (self._tunnel_host,))
26 connect = b"CONNECT %s:%d HTTP/1.0\r\n" % (
27 self._wrap_ipv6(self._tunnel_host.encode("ascii")),
28 self._tunnel_port)
29 headers = [connect]
30 for header, value in self._tunnel_headers.items():
31 - headers.append(f"{header}: {value}\r\n".encode("latin-1"))
32 + header_bytes = header.encode("latin-1")
33 + value_bytes = value.encode("latin-1")
34 + if not _is_legal_header_name(header_bytes):
35 + raise ValueError('Invalid header name %r' % (header_bytes,))
36 + if _is_illegal_header_value(value_bytes):
37 + raise ValueError('Invalid header value %r' % (value_bytes,))
38 + headers.append(b"%s: %s\r\n" % (header_bytes, value_bytes))
39 headers.append(b"\r\n")
40 # Making a single send() call instead of one per line encourages
41 # the host OS to use a more optimal packet size instead of
42 diff --git a/lib-python/3/test/test_httplib.py b/lib-python/3/test/test_httplib.py
43 index 5536341..a4ab909 100644
44 --- a/lib-python/3/test/test_httplib.py
45 +++ b/lib-python/3/test/test_httplib.py
46 @@ -371,6 +371,51 @@ class HeaderTests(TestCase):
47 with self.assertRaisesRegex(ValueError, 'Invalid header'):
48 conn.putheader(name, value)
49
50 + def test_invalid_tunnel_headers(self):
51 + cases = (
52 + ('Invalid\r\nName', 'ValidValue'),
53 + ('Invalid\rName', 'ValidValue'),
54 + ('Invalid\nName', 'ValidValue'),
55 + ('\r\nInvalidName', 'ValidValue'),
56 + ('\rInvalidName', 'ValidValue'),
57 + ('\nInvalidName', 'ValidValue'),
58 + (' InvalidName', 'ValidValue'),
59 + ('\tInvalidName', 'ValidValue'),
60 + ('Invalid:Name', 'ValidValue'),
61 + (':InvalidName', 'ValidValue'),
62 + ('ValidName', 'Invalid\r\nValue'),
63 + ('ValidName', 'Invalid\rValue'),
64 + ('ValidName', 'Invalid\nValue'),
65 + ('ValidName', 'InvalidValue\r\n'),
66 + ('ValidName', 'InvalidValue\r'),
67 + ('ValidName', 'InvalidValue\n'),
68 + )
69 + for name, value in cases:
70 + with self.subTest((name, value)):
71 + conn = client.HTTPConnection('example.com')
72 + conn.set_tunnel('tunnel', headers={
73 + name: value
74 + })
75 + conn.sock = FakeSocket('')
76 + with self.assertRaisesRegex(ValueError, 'Invalid header'):
77 + conn._tunnel() # Called in .connect()
78 +
79 + def test_invalid_tunnel_host(self):
80 + cases = (
81 + 'invalid\r.host',
82 + '\ninvalid.host',
83 + 'invalid.host\r\n',
84 + 'invalid.host\x00',
85 + 'invalid host',
86 + )
87 + for tunnel_host in cases:
88 + with self.subTest(tunnel_host):
89 + conn = client.HTTPConnection('example.com')
90 + conn.set_tunnel(tunnel_host)
91 + conn.sock = FakeSocket('')
92 + with self.assertRaisesRegex(ValueError, 'Tunnel host can\'t contain control characters'):
93 + conn._tunnel() # Called in .connect()
94 +
95 def test_headers_debuglevel(self):
96 body = (
97 b'HTTP/1.1 200 OK\r\n'