Skip to content

Commit f016f06

Browse files
committed
Raise '400: Content-Length can't be present with Transfer-Encoding' if both Content-Length and Transfer-Encoding are sent by peer (#6182)
1 parent a8f01d7 commit f016f06

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGES/6182.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise ``400: Content-Length can't be present with Transfer-Encoding`` if both ``Content-Length`` and ``Transfer-Encoding`` are sent by peer by both C and Python implementations

aiohttp/http_parser.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from .base_protocol import BaseProtocol
2929
from .helpers import NO_EXTENSIONS, BaseTimerContext
3030
from .http_exceptions import (
31+
BadHttpMessage,
3132
BadStatusLine,
3233
ContentEncodingError,
3334
ContentLengthError,
@@ -489,8 +490,15 @@ def parse_headers(
489490

490491
# chunking
491492
te = headers.get(hdrs.TRANSFER_ENCODING)
492-
if te and "chunked" in te.lower():
493-
chunked = True
493+
if te is not None:
494+
te_lower = te.lower()
495+
if "chunked" in te_lower:
496+
chunked = True
497+
498+
if hdrs.CONTENT_LENGTH in headers:
499+
raise BadHttpMessage(
500+
"Content-Length can't be present with Transfer-Encoding",
501+
)
494502

495503
return (headers, raw_headers, close_conn, encoding, upgrade, chunked)
496504

tests/test_http_parser.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,20 @@ def test_request_chunked(parser) -> None:
291291
assert isinstance(payload, streams.StreamReader)
292292

293293

294-
def test_conn_upgrade(parser) -> None:
294+
def test_request_te_chunked_with_content_length(parser: Any) -> None:
295+
text = (
296+
b"GET /test HTTP/1.1\r\n"
297+
b"content-length: 1234\r\n"
298+
b"transfer-encoding: chunked\r\n\r\n"
299+
)
300+
with pytest.raises(
301+
http_exceptions.BadHttpMessage,
302+
match="Content-Length can't be present with Transfer-Encoding",
303+
):
304+
parser.feed_data(text)
305+
306+
307+
def test_conn_upgrade(parser: Any) -> None:
295308
text = (
296309
b"GET /test HTTP/1.1\r\n"
297310
b"connection: upgrade\r\n"

0 commit comments

Comments
 (0)