Skip to content

Commit 803f338

Browse files
committed
Reject HTTP/1.1 requests without Host header (aio-libs#12264)
(cherry picked from commit af05010)
1 parent 04ed4bd commit 803f338

5 files changed

Lines changed: 143 additions & 97 deletions

File tree

CHANGES/10600.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed http parser not rejecting HTTP/1.1 requests that do not have valid Host header.
2+
-- by :user:`Cycloctane`.

aiohttp/_http_parser.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ cdef class HttpParser:
457457
cdef _on_headers_complete(self):
458458
self._process_header()
459459

460+
http_version = self.http_version()
460461
should_close = not cparser.llhttp_should_keep_alive(self._cparser)
461462
upgrade = self._cparser.upgrade
462463
chunked = self._cparser.flags & cparser.F_CHUNKED
@@ -465,6 +466,8 @@ cdef class HttpParser:
465466
headers = CIMultiDictProxy(CIMultiDict(self._headers))
466467

467468
if self._cparser.type == cparser.HTTP_REQUEST:
469+
if http_version == HttpVersion11 and hdrs.HOST not in headers:
470+
raise BadHttpMessage("Missing 'Host' header in request.")
468471
h_upg = headers.get("upgrade", "")
469472
allowed = upgrade and h_upg.isascii() and h_upg.lower() in ALLOWED_UPGRADES
470473
if allowed or self._cparser.method == cparser.HTTP_CONNECT:
@@ -488,11 +491,11 @@ cdef class HttpParser:
488491
method = http_method_str(self._cparser.method)
489492
msg = _new_request_message(
490493
method, self._path,
491-
self.http_version(), headers, raw_headers,
494+
http_version, headers, raw_headers,
492495
should_close, encoding, upgrade, chunked, self._url)
493496
else:
494497
msg = _new_response_message(
495-
self.http_version(), self._cparser.status_code, self._reason,
498+
http_version, self._cparser.status_code, self._reason,
496499
headers, raw_headers, should_close, encoding,
497500
upgrade, chunked)
498501

aiohttp/http_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
LineTooLong,
5050
TransferEncodingError,
5151
)
52-
from .http_writer import HttpVersion, HttpVersion10
52+
from .http_writer import HttpVersion, HttpVersion10, HttpVersion11
5353
from .streams import EMPTY_PAYLOAD, StreamReader
5454
from .typedefs import RawHeaders
5555

@@ -686,6 +686,9 @@ def parse_message(self, lines: list[bytes]) -> RawRequestMessage:
686686
chunked,
687687
) = self.parse_headers(lines[1:])
688688

689+
if version_o == HttpVersion11 and hdrs.HOST not in headers:
690+
raise BadHttpMessage("Missing 'Host' header in request.")
691+
689692
if close is None: # then the headers weren't set in the request
690693
if version_o <= HttpVersion10: # HTTP 1.0 must asks to not close
691694
close = True

0 commit comments

Comments
 (0)