Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject data after close message #9018

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGES/9018.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated Python parser to reject messages after a close message, matching C parser behaviour -- by :user:`Dreamsorcerer`.
5 changes: 5 additions & 0 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def feed_data(
start_pos = 0
loop = self.loop

should_close = False
while start_pos < data_len:
# read HTTP message (request/response line + headers), \r\n\r\n
# and split by lines
Expand All @@ -317,6 +318,9 @@ def feed_data(
continue

if pos >= start_pos:
if should_close:
raise BadHttpMessage("Data after `Connection: close`")

# line found
line = data[start_pos:pos]
if SEP == b"\n": # For lax response parsing
Expand Down Expand Up @@ -426,6 +430,7 @@ def get_content_length() -> Optional[int]:
payload = EMPTY_PAYLOAD

messages.append((msg, payload))
should_close = msg.should_close
else:
self._tail = data[start_pos:]
data = EMPTY
Expand Down
8 changes: 8 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,14 @@ def test_http_request_bad_status_line_whitespace(parser: HttpRequestParser) -> N
parser.feed_data(text)


def test_http_request_message_after_close(parser: HttpRequestParser) -> None:
text = b"GET / HTTP/1.1\r\nConnection: close\r\n\r\nInvalid\r\n\r\n"
with pytest.raises(
http_exceptions.BadHttpMessage, match="Data after `Connection: close`"
):
parser.feed_data(text)


def test_http_request_upgrade(parser: HttpRequestParser) -> None:
text = (
b"GET /test HTTP/1.1\r\n"
Expand Down
Loading