Skip to content
Closed
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/10142.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved error message for HTTPS requests sent to an HTTP port by detecting the TLS handshake bytes. -- by :user:`NIK-TIGER-BILL`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Moss Collum
Mun Gwan-gyeong
Navid Sheikhol
Nicolas Braem
NIK-TIGER-BILL
Nikolay Kim
Nikolay Novik
Nikolay Tiunov
Expand Down
12 changes: 12 additions & 0 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,18 @@ class HttpRequestParser(HttpParser[RawRequestMessage]):
def parse_message(self, lines: list[bytes]) -> RawRequestMessage:
# request line
line = lines[0].decode("utf-8", "surrogateescape")
if lines[0].startswith(b"\x16\x03"):
raise BadHttpMethod(
line,
error="Client appears to be trying to connect via HTTPS to an HTTP port",
)

if lines[0].startswith(b"\x16\x03"):
raise BadHttpMethod(
line,
error="Client appears to be trying to connect via HTTPS to an HTTP port",
)

try:
method, path, version = line.split(" ", maxsplit=2)
except ValueError:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,22 @@ def test_http_request_parser_bad_method(
parser.feed_data(rfc9110_5_6_2_token_delim + b'ET" /get HTTP/1.1\r\n\r\n')


def test_http_request_parser_bad_method_https_on_http_port(
parser: HttpRequestParser,
) -> None:
with pytest.raises(http_exceptions.BadHttpMethod) as exc_info:
parser.feed_data(b"\x16\x03\x01\x00\xa5\x01\x00\x00\xa1\x03\x03")
assert "HTTPS" in str(exc_info.value)


def test_http_request_parser_bad_method_https_on_http_port(
parser: HttpRequestParser,
) -> None:
with pytest.raises(http_exceptions.BadHttpMethod) as exc_info:
parser.feed_data(b"\x16\x03\x01\x00\xa5\x01\x00\x00\xa1\x03\x03")
assert "HTTPS" in str(exc_info.value)


def test_http_request_parser_bad_version(parser: HttpRequestParser) -> None:
with pytest.raises(http_exceptions.BadHttpMessage):
parser.feed_data(b"GET //get HT/11\r\n\r\n")
Expand Down
Loading