Skip to content
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/10142.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved the parser error message shown when TLS handshake bytes are received on an HTTP port -- by :user:`puneetdixit200`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Phebe Polk
Philipp A.
Pierre-Louis Peeters
Pieter van Beek
Puneet Dixit
Qiao Han
Rafael Viotti
Rahul Nahata
Expand Down
3 changes: 3 additions & 0 deletions aiohttp/_http_parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ from aiohttp.helpers import DEBUG, set_exception

from .helpers import HeadersDictProxy as _HeadersDictProxy
from .http_exceptions import (
HTTPS_ON_HTTP_PORT_ERROR,
BadHttpMessage,
BadHttpMethod,
BadStatusLine,
Expand Down Expand Up @@ -932,6 +933,8 @@ cdef parser_error_from_errno(cparser.llhttp_t* parser, data, pointer):
cparser.HPE_INVALID_TRANSFER_ENCODING}:
return BadHttpMessage(err_msg)
elif errno == cparser.HPE_INVALID_METHOD:
if data.startswith(b"\x16\x03"):
return BadHttpMethod(error=HTTPS_ON_HTTP_PORT_ERROR)
return BadHttpMethod(error=err_msg)
elif errno in {cparser.HPE_INVALID_STATUS,
cparser.HPE_INVALID_VERSION,
Expand Down
5 changes: 5 additions & 0 deletions aiohttp/http_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ def __init__(self, line: str = "", error: str | None = None) -> None:
self.line = line


HTTPS_ON_HTTP_PORT_ERROR = "Received HTTPS traffic on an HTTP port"


class BadHttpMethod(BadStatusLine):
"""Invalid HTTP method in status line."""

def __init__(self, line: str = "", error: str | None = None) -> None:
if error is None and line.startswith("\x16\x03"):
error = HTTPS_ON_HTTP_PORT_ERROR
super().__init__(line, error or f"Bad HTTP method in status line {line!r}")


Expand Down
9 changes: 9 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,15 @@ def test_http_request_parser_bad_method(
)


def test_http_request_parser_tls_handshake_on_http_port(
parser: HttpRequestParser,
) -> None:
with pytest.raises(http_exceptions.BadHttpMethod) as ctx:
parser.feed_data(b"\x16\x03\x03\x01F\x01\r\n\r\n")

assert "Received HTTPS traffic on an HTTP port" in str(ctx.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\nHost: a\r\n\r\n")
Expand Down
Loading