Skip to content

Commit 2f0605a

Browse files
committed
gh-142533: Validate CRLF in send_response_only and add test
1 parent 6d661e8 commit 2f0605a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Lib/http/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ def send_response_only(self, code, message=None):
552552
message = ''
553553
if not hasattr(self, '_headers_buffer'):
554554
self._headers_buffer = []
555+
_validate_header_string(message)
555556
self._headers_buffer.append(("%s %d %s\r\n" %
556557
(self.protocol_version, code, message)).encode(
557558
'latin-1', 'strict'))

Lib/test/test_httpservers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,19 @@ def test_header_buffering_of_send_response_only(self):
10521052
handler.end_headers()
10531053
self.assertEqual(output.numWrites, 1)
10541054

1055+
def test_send_response_only_rejects_crlf_message(self):
1056+
input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')
1057+
output = AuditableBytesIO()
1058+
handler = SocketlessRequestHandler()
1059+
handler.rfile = input
1060+
handler.wfile = output
1061+
handler.request_version = 'HTTP/1.1'
1062+
1063+
with self.assertRaises(ValueError) as ctx:
1064+
handler.send_response_only(418, 'value\r\nSet-Cookie: custom=true')
1065+
self.assertIn('Invalid header name/value: contains CR or LF',
1066+
str(ctx.exception))
1067+
10551068
def test_header_buffering_of_send_header(self):
10561069

10571070
input = BytesIO(b'GET / HTTP/1.1\r\n\r\n')

0 commit comments

Comments
 (0)