Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
_is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
_is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search

# A CR that is not part of a CRLF line terminator. RFC 9112 section 2.2 tells
# a recipient to either reject it or replace it with SP before processing.
_bare_cr_re = re.compile(rb'\r(?!\n)')

# 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.
Expand Down Expand Up @@ -252,6 +256,10 @@ def _read_headers(fp, max_headers):
raise LineTooLong("header line")
if line in (b'\r\n', b'\n', b''):
break
if b'\r' in line:
# The email parser treats a bare CR as a line terminator, which
# would split one field line into several (gh-114782).
line = _bare_cr_re.sub(b' ', line)
headers.append(line)
if len(headers) > max_headers:
raise HTTPException(f"got more than {max_headers} headers")
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,36 @@ def test_response_headers(self):
cookies = r.getheader("Set-Cookie")
self.assertEqual(cookies, hdr)

def test_bare_cr_in_header_value(self):
# gh-114782: a bare CR is not a line terminator, so it must not
# split one field line into two.
for bare_cr in ('\r', '\r\r', '\r \r'):
with self.subTest(bare_cr=bare_cr):
sock = FakeSocket(
'HTTP/1.1 200 OK\r\n'
f'Visible: a{bare_cr}Smuggled: b\r\n'
'Content-Length: 0\r\n'
'\r\n'
)
resp = client.HTTPResponse(sock)
resp.begin()
self.assertIsNone(resp.getheader('Smuggled'))
self.assertEqual(
resp.getheader('Visible'),
'a' + bare_cr.replace('\r', ' ') + 'Smuggled: b',
)
self.assertEqual(resp.getheader('Content-Length'), '0')

def test_bare_cr_in_parse_headers(self):
headers = client.parse_headers(io.BytesIO(
b'Visible: a\rSmuggled: b\r\n'
b'Folded: c\r\n'
b' d\r\n'
b'\r\n'
))
self.assertEqual(headers.keys(), ['Visible', 'Folded'])
self.assertEqual(headers['Visible'], 'a Smuggled: b')

def test_read_head(self):
# Test that the library doesn't attempt to read any data
# from a HEAD request. (Tickles SF bug #622042.)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,15 @@ def test_too_many_headers(self):
self.assertFalse(self.handler.get_called)
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')

def test_bare_cr_in_header_value(self):
# gh-114782: a bare CR must not start a new header field.
result = self.send_typical_request(
b'GET / HTTP/1.1\r\nVisible: a\rSmuggled: b\r\n\r\n')
self.assertEqual(result[0], b'HTTP/1.1 200 OK\r\n')
self.verify_get_called()
self.assertIsNone(self.handler.headers.get('Smuggled'))
self.assertEqual(self.handler.headers['Visible'], 'a Smuggled: b')

def test_html_escape_on_error(self):
result = self.send_typical_request(
b'<script>alert("hello")</script> / HTTP/1.1')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`http.client` and :mod:`http.server` no longer treat a bare CR in a header
field line as a line terminator, which allowed one field line to be parsed as
several header fields. A bare CR is now replaced with a space, as permitted by
:rfc:`9112#section-2.2`.
Loading