File CVE-2025-69224-unicode-processing-header-values.patch of Package python-aiohttp.42491

From 32677f2adfd907420c078dda6b79225c6f4ebce0 Mon Sep 17 00:00:00 2001
From: Sam Bull <git@sambull.org>
Date: Sat, 3 Jan 2026 00:02:45 +0000
Subject: [PATCH] Reject non-ascii characters in some headers (#11886) (#11902)

(cherry picked from commit 5affd64f86d28a16a8f8e6fea2d217c99bf7831f)
---
 aiohttp/_http_parser.pyx  |  6 +++---
 aiohttp/http_parser.py    | 16 +++++++++-------
 tests/test_http_parser.py | 31 ++++++++++++++++++++++++++++++-
 3 files changed, 42 insertions(+), 11 deletions(-)

Index: aiohttp-3.9.3/aiohttp/_http_parser.pyx
===================================================================
--- aiohttp-3.9.3.orig/aiohttp/_http_parser.pyx
+++ aiohttp-3.9.3/aiohttp/_http_parser.pyx
@@ -436,8 +436,7 @@ cdef class HttpParser:
         enc = self._content_encoding
         if enc is not None:
             self._content_encoding = None
-            enc = enc.lower()
-            if enc in ('gzip', 'deflate', 'br'):
+            if enc.isascii() and enc.lower() in ("gzip", "deflate", "br"):
                 encoding = enc
 
         if self._cparser.type == cparser.HTTP_REQUEST:
Index: aiohttp-3.9.3/aiohttp/http_parser.py
===================================================================
--- aiohttp-3.9.3.orig/aiohttp/http_parser.py
+++ aiohttp-3.9.3/aiohttp/http_parser.py
@@ -527,20 +527,19 @@ class HttpParser(abc.ABC, Generic[_MsgT]
             elif v == "keep-alive":
                 close_conn = False
             # https://www.rfc-editor.org/rfc/rfc9110.html#name-101-switching-protocols
-            elif v == "upgrade" and headers.get(hdrs.UPGRADE):
+            elif v.isascii() and v == "upgrade" and headers.get(hdrs.UPGRADE):
                 upgrade = True
 
         # encoding
-        enc = headers.get(hdrs.CONTENT_ENCODING)
-        if enc:
-            enc = enc.lower()
-            if enc in ("gzip", "deflate", "br"):
-                encoding = enc
+        enc = headers.get(hdrs.CONTENT_ENCODING, "")
+        if enc.isascii() and enc.lower() in ("gzip", "deflate", "br"):
+            encoding = enc
 
         # chunking
         te = headers.get(hdrs.TRANSFER_ENCODING)
         if te is not None:
-            if "chunked" == te.lower():
+            # .lower() transforms some non-ascii chars, so must check first.
+            if te.isascii() and te.lower() == "chunked":
                 chunked = True
             else:
                 raise BadHttpMessage("Request has invalid `Transfer-Encoding`")
Index: aiohttp-3.9.3/tests/test_http_parser.py
===================================================================
--- aiohttp-3.9.3.orig/tests/test_http_parser.py
+++ aiohttp-3.9.3/tests/test_http_parser.py
@@ -446,7 +446,21 @@ def test_request_chunked(parser) -> None
     assert isinstance(payload, streams.StreamReader)
 
 
-def test_request_te_chunked_with_content_length(parser: Any) -> None:
+def test_te_header_non_ascii(parser: HttpRequestParser) -> None:
+    # K = Kelvin sign, not valid ascii.
+    text = "GET /test HTTP/1.1\r\nTransfer-Encoding: chunKed\r\n\r\n"
+    with pytest.raises(http_exceptions.BadHttpMessage):
+        parser.feed_data(text.encode())
+
+
+def test_upgrade_header_non_ascii(parser: HttpRequestParser) -> None:
+    # K = Kelvin sign, not valid ascii.
+    text = "GET /test HTTP/1.1\r\nUpgrade: websocKet\r\n\r\n"
+    messages, upgrade, tail = parser.feed_data(text.encode())
+    assert not upgrade
+
+
+def test_request_te_chunked_with_content_length(parser: HttpRequestParser) -> None:
     text = (
         b"GET /test HTTP/1.1\r\n"
         b"content-length: 1234\r\n"
@@ -515,6 +529,15 @@ def test_compression_brotli(parser) -> N
     assert msg.compression == "br"
 
 
+def test_compression_non_ascii(parser: HttpRequestParser) -> None:
+    enc = "deflate".encode()
+    text = b"GET /test HTTP/1.1\r\ncontent-encoding: " + enc + b"\r\n\r\n"
+    messages, upgrade, tail = parser.feed_data(text)
+    msg = messages[0][0]
+    # Non-ascii input should not evaluate to a valid encoding scheme.
+    assert msg.compression is None
+
+
 def test_compression_unknown(parser) -> None:
     text = b"GET /test HTTP/1.1\r\ncontent-encoding: compress\r\n\r\n"
     messages, upgrade, tail = parser.feed_data(text)
openSUSE Build Service is sponsored by