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

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.6.0/aiohttp/_http_parser.pyx
===================================================================
--- aiohttp-3.6.0.orig/aiohttp/_http_parser.pyx
+++ aiohttp-3.6.0/aiohttp/_http_parser.pyx
@@ -20,6 +20,7 @@ from .http_writer import (HttpVersion as
                           HttpVersion10 as _HttpVersion10,
                           HttpVersion11 as _HttpVersion11)
 from .http_parser import DeflateBuffer as _DeflateBuffer
+from .http_parser import string_isascii
 from .streams import (EMPTY_PAYLOAD as _EMPTY_PAYLOAD,
                       StreamReader as _StreamReader)
 
@@ -412,8 +413,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 string_isascii(enc) and enc.lower() in ("gzip", "deflate", "br"):
                 encoding = enc
 
         if self._cparser.type == cparser.HTTP_REQUEST:
Index: aiohttp-3.6.0/aiohttp/http_parser.py
===================================================================
--- aiohttp-3.6.0.orig/aiohttp/http_parser.py
+++ aiohttp-3.6.0/aiohttp/http_parser.py
@@ -409,17 +409,19 @@ class HttpParser(abc.ABC):
                 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 string_isascii(enc) and enc.lower() in ("gzip", "deflate", "br"):
+            encoding = enc
 
         # chunking
         te = headers.get(hdrs.TRANSFER_ENCODING)
         if te is not None:
-            if 'chunked' in te.lower():
+            te = te.rsplit(",", maxsplit=1)[-1].strip(" \t")
+            # .lower() transforms some non-ascii chars, so must check first.
+            if string_isascii(te) and te.lower() == "chunked":
                 chunked = True
+            else:
+                raise BadHttpMessage("Request has invalid `Transfer-Encoding`")
             if hdrs.CONTENT_LENGTH in headers:
                 raise BadHttpMessage('unexpected content-length header')
 
@@ -824,3 +826,10 @@ try:
         RawResponseMessageC = RawResponseMessage
 except ImportError:  # pragma: no cover
     pass
+
+
+def string_isascii(check):
+    for c in check:
+        if ord(c) > 127:
+            return False
+    return True
Index: aiohttp-3.6.0/tests/test_http_parser.py
===================================================================
--- aiohttp-3.6.0.orig/tests/test_http_parser.py
+++ aiohttp-3.6.0/tests/test_http_parser.py
@@ -272,6 +272,13 @@ def test_request_chunked(parser) -> None
     assert isinstance(payload, streams.StreamReader)
 
 
+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_request_te_chunked_with_content_length(parser) -> None:
     text = (b'GET /test HTTP/1.1\r\n'
             b'content-length: 1234\r\n'
@@ -281,6 +288,15 @@ def test_request_te_chunked_with_content
         parser.feed_data(text)
 
 
+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_conn_upgrade(parser) -> None:
     text = (b'GET /test HTTP/1.1\r\n'
             b'connection: upgrade\r\n'
openSUSE Build Service is sponsored by