Skip to content

Commit

Permalink
Require full version and method regex matches (#7701)
Browse files Browse the repository at this point in the history
## What do these changes do?

These changes ensure that HTTP versions and methods fully match the
regular expressions for those constructs. AIOHTTP currently only applies
prefix-matching, which I assume was unintentional.

## Are there changes in behavior for the user?

There should be no observable changes to the user, unless they use HTTP
servers/clients that generate very malformed request lines. Such
clients/servers are unlikely to exist because most other web servers
reject these malformed messages.

## Related issue number

Fixes #7700

(cherry picked from commit 312f747)
  • Loading branch information
kenballus authored and patchback[bot] committed Oct 15, 2023
1 parent 6791b9e commit 9c65fcf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/7700.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue with insufficient HTTP method and version validation.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Arthur Darcet
Austin Scola
Ben Bader
Ben Greiner
Ben Kallus
Ben Timby
Benedikt Reinartz
Bob Haddleton
Expand Down
6 changes: 3 additions & 3 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,11 @@ def parse_message(self, lines: List[bytes]) -> RawRequestMessage:
)

# method
if not METHRE.match(method):
if not METHRE.fullmatch(method):
raise BadStatusLine(method)

# version
match = VERSRE.match(version)
match = VERSRE.fullmatch(version)
if match is None:
raise BadStatusLine(line)
version_o = HttpVersion(int(match.group(1)), int(match.group(2)))
Expand Down Expand Up @@ -659,7 +659,7 @@ def parse_message(self, lines: List[bytes]) -> RawResponseMessage:
)

# version
match = VERSRE.match(version)
match = VERSRE.fullmatch(version)
if match is None:
raise BadStatusLine(line)
version_o = HttpVersion(int(match.group(1)), int(match.group(2)))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def test_http_request_parser_two_slashes(parser) -> None:

def test_http_request_parser_bad_method(parser) -> None:
with pytest.raises(http_exceptions.BadStatusLine):
parser.feed_data(b'=":<G>(e),[T];?" /get HTTP/1.1\r\n\r\n')
parser.feed_data(b'G=":<>(e),[T];?" /get HTTP/1.1\r\n\r\n')


def test_http_request_parser_bad_version(parser) -> None:
Expand All @@ -738,7 +738,7 @@ def test_http_request_parser_bad_version(parser) -> None:

def test_http_request_parser_bad_version_number(parser: Any) -> None:
with pytest.raises(http_exceptions.BadHttpMessage):
parser.feed_data(b"GET /test HTTP/12.3\r\n\r\n")
parser.feed_data(b"GET /test HTTP/1.32\r\n\r\n")


@pytest.mark.parametrize("size", [40965, 8191])
Expand Down

0 comments on commit 9c65fcf

Please sign in to comment.