Skip to content

Commit c5b63a1

Browse files
committed
gh-114782: Fix http.client splitting header lines on a bare CR
The email parser used by http.client.parse_headers() treats a bare CR as a line terminator, so one field line could be parsed as several header fields. Replace a bare CR with SP before parsing, as RFC 9112 section 2.2 permits. This also covers http.server and wsgiref.simple_server.
1 parent 1822ff5 commit c5b63a1

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎Lib/http/client.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@
162162
_is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
163163
_is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
164164

165+
# A CR that is not part of a CRLF line terminator. RFC 9112 section 2.2 tells
166+
# a recipient to either reject it or replace it with SP before processing.
167+
_bare_cr_re = re.compile(rb'\r(?!\n)')
168+
165169
# These characters are not allowed within HTTP URL paths.
166170
# See https://tools.ietf.org/html/rfc3986#section-3.3 and the
167171
# https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.
@@ -252,6 +256,10 @@ def _read_headers(fp, max_headers):
252256
raise LineTooLong("header line")
253257
if line in (b'\r\n', b'\n', b''):
254258
break
259+
if b'\r' in line:
260+
# The email parser treats a bare CR as a line terminator, which
261+
# would split one field line into several (gh-114782).
262+
line = _bare_cr_re.sub(b' ', line)
255263
headers.append(line)
256264
if len(headers) > max_headers:
257265
raise HTTPException(f"got more than {max_headers} headers")

‎Lib/test/test_httplib.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,36 @@ def test_response_headers(self):
11261126
cookies = r.getheader("Set-Cookie")
11271127
self.assertEqual(cookies, hdr)
11281128

1129+
def test_bare_cr_in_header_value(self):
1130+
# gh-114782: a bare CR is not a line terminator, so it must not
1131+
# split one field line into two.
1132+
for bare_cr in ('\r', '\r\r', '\r \r'):
1133+
with self.subTest(bare_cr=bare_cr):
1134+
sock = FakeSocket(
1135+
'HTTP/1.1 200 OK\r\n'
1136+
f'Visible: a{bare_cr}Smuggled: b\r\n'
1137+
'Content-Length: 0\r\n'
1138+
'\r\n'
1139+
)
1140+
resp = client.HTTPResponse(sock)
1141+
resp.begin()
1142+
self.assertIsNone(resp.getheader('Smuggled'))
1143+
self.assertEqual(
1144+
resp.getheader('Visible'),
1145+
'a' + bare_cr.replace('\r', ' ') + 'Smuggled: b',
1146+
)
1147+
self.assertEqual(resp.getheader('Content-Length'), '0')
1148+
1149+
def test_bare_cr_in_parse_headers(self):
1150+
headers = client.parse_headers(io.BytesIO(
1151+
b'Visible: a\rSmuggled: b\r\n'
1152+
b'Folded: c\r\n'
1153+
b' d\r\n'
1154+
b'\r\n'
1155+
))
1156+
self.assertEqual(headers.keys(), ['Visible', 'Folded'])
1157+
self.assertEqual(headers['Visible'], 'a Smuggled: b')
1158+
11291159
def test_read_head(self):
11301160
# Test that the library doesn't attempt to read any data
11311161
# from a HEAD request. (Tickles SF bug #622042.)

‎Lib/test/test_httpservers.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,15 @@ def test_too_many_headers(self):
12561256
self.assertFalse(self.handler.get_called)
12571257
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
12581258

1259+
def test_bare_cr_in_header_value(self):
1260+
# gh-114782: a bare CR must not start a new header field.
1261+
result = self.send_typical_request(
1262+
b'GET / HTTP/1.1\r\nVisible: a\rSmuggled: b\r\n\r\n')
1263+
self.assertEqual(result[0], b'HTTP/1.1 200 OK\r\n')
1264+
self.verify_get_called()
1265+
self.assertIsNone(self.handler.headers.get('Smuggled'))
1266+
self.assertEqual(self.handler.headers['Visible'], 'a Smuggled: b')
1267+
12591268
def test_html_escape_on_error(self):
12601269
result = self.send_typical_request(
12611270
b'<script>alert("hello")</script> / HTTP/1.1')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`http.client` and :mod:`http.server` no longer treat a bare CR in a header
2+
field line as a line terminator, which allowed one field line to be parsed as
3+
several header fields. A bare CR is now replaced with a space, as permitted by
4+
:rfc:`9112#section-2.2`.

0 commit comments

Comments
 (0)