diff --git a/Lib/http/server.py b/Lib/http/server.py index a90c8d34c394db..fc5e2429fa1b23 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -287,6 +287,15 @@ def parse_request(self): requestline = str(self.raw_requestline, 'iso-8859-1') requestline = requestline.rstrip('\r\n') self.requestline = requestline + + # Detect TLS handshake attempt (common when browser forces HTTPS) + if self.raw_requestline[0] == 0x16: # First TLS handshake bytes + self.requestline = "[TLS handshake bytes]" + self.send_error( + HTTPStatus.BAD_REQUEST, + "Unsupported protocol: HTTPS is not available") + return False + words = requestline.split() if len(words) == 0: return False diff --git a/Misc/NEWS.d/next/Library/2025-02-12-18-31-12.gh-issue-109765.QqoBW2.rst b/Misc/NEWS.d/next/Library/2025-02-12-18-31-12.gh-issue-109765.QqoBW2.rst new file mode 100644 index 00000000000000..4565bd605c077a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-12-18-31-12.gh-issue-109765.QqoBW2.rst @@ -0,0 +1,2 @@ +Detect TLS handshake attempt in :class:`http.server.BaseHTTPRequestHandler` to +more clear the output. Patch by Semyon Moroz.