Skip to content

Commit 7e841ab

Browse files
claudebdarnell
authored andcommitted
http1connection: Only allow HTTP whitespace in Content-Length lists
A Content-Length header with a list of identical values ("5, 5") is accepted, but the split used \s, which also matches non-HTTP whitespace such as \xa0 and \x85 (both valid obs-text in a header value). Only allow spaces and tabs, as RFC 9110's OWS does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
1 parent 21e1ad0 commit 7e841ab

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎tornado/http1connection.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def _read_body(
653653
# Proxies sometimes cause Content-Length headers to get
654654
# duplicated. If all the values are identical then we can
655655
# use them but if they differ it's an error.
656-
pieces = re.split(r",\s*", headers["Content-Length"])
656+
pieces = re.split(r",[ \t]*", headers["Content-Length"])
657657
if any(i != pieces[0] for i in pieces):
658658
raise httputil.HTTPInputError(
659659
"Multiple unequal Content-Lengths: %r"

‎tornado/test/httpserver_test.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ def test_chunked_request_body_transfer_encoding_and_content_length(self):
600600
)
601601
self.assertEqual(400, start_line.code)
602602

603+
@gen_test
604+
def test_content_length_list_whitespace(self):
605+
# A list of identical Content-Lengths is allowed, but only HTTP
606+
# whitespace (space and tab) may follow the commas, not other
607+
# characters that Python considers whitespace.
608+
for sep in [b"\xa0", b"\x85"]:
609+
with self.subTest(sep=sep), closing(IOStream(socket.socket())) as stream:
610+
with ExpectLog(
611+
gen_log, ".*Multiple unequal Content-Lengths", level=logging.INFO
612+
):
613+
yield stream.connect(("127.0.0.1", self.get_http_port()))
614+
stream.write(
615+
b"POST /echo HTTP/1.1\r\nHost: 127.0.0.1\r\n"
616+
b"Content-Length: 5," + sep + b"5\r\n"
617+
b"Connection: close\r\n\r\nhello"
618+
)
619+
start_line, headers, response = yield read_stream_body(stream)
620+
self.assertEqual(400, start_line.code)
621+
603622
@gen_test
604623
def test_invalid_content_length(self):
605624
# HTTP only allows decimal digits in content-length. Make sure we don't

0 commit comments

Comments
 (0)