diff --git a/Lib/http/client.py b/Lib/http/client.py index c05d7d93302843..68d77db58bdbf5 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -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. @@ -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") diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index eb604bd2c4421b..898c887660c039 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -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.) diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index c82084ce299d77..2db69fc50a25b0 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -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' / HTTP/1.1') diff --git a/Misc/NEWS.d/next/Security/2026-11-15-12-10-00.gh-issue-114782.bR7cQx.rst b/Misc/NEWS.d/next/Security/2026-11-15-12-10-00.gh-issue-114782.bR7cQx.rst new file mode 100644 index 00000000000000..3f22c15cbdcdcb --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-15-12-10-00.gh-issue-114782.bR7cQx.rst @@ -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`.