File CVE-2026-0865-wsgiref-ctrl-chars.patch of Package python3.42754

From 123bfbbe9074ef7fa28e1e7b25575665296560fa Mon Sep 17 00:00:00 2001
From: "Gregory P. Smith" <68491+gpshead@users.noreply.github.com>
Date: Sat, 17 Jan 2026 10:23:57 -0800
Subject: [PATCH] [3.10] gh-143916: Reject control characters in
 wsgiref.headers.Headers (GH-143917) (GH-143973)

gh-143916: Reject control characters in wsgiref.headers.Headers  (GH-143917)

* Add 'test.support' fixture for C0 control characters
* gh-143916: Reject control characters in wsgiref.headers.Headers

(cherry picked from commit f7fceed79ca1bceae8dbe5ba5bc8928564da7211)
(cherry picked from commit 22e4d55285cee52bc4dbe061324e5f30bd4dee58)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Seth Michael Larson <seth@python.org>
---
 Lib/test/support/__init__.py                                             |    8 +++++
 Lib/test/test_wsgiref.py                                                 |   14 ++++++++--
 Lib/wsgiref/headers.py                                                   |    3 ++
 Misc/NEWS.d/next/Security/2026-01-16-11-07-36.gh-issue-143916.dpWeOD.rst |    2 +
 4 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Security/2026-01-16-11-07-36.gh-issue-143916.dpWeOD.rst

Index: Python-3.4.10/Lib/test/support/__init__.py
===================================================================
--- Python-3.4.10.orig/Lib/test/support/__init__.py	2026-02-12 19:35:48.491806336 +0100
+++ Python-3.4.10/Lib/test/support/__init__.py	2026-02-12 19:36:03.255310120 +0100
@@ -107,6 +107,7 @@
     "check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item",
     "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
     "run_with_tz", "fails_with_expat_2_6_0", "is_expat_2_6_0",
+    "control_characters_c0",
     # FakePath
     "FakePath",
     ]
@@ -2494,3 +2495,10 @@
             raise self.path
         else:
             return self.path
+
+
+def control_characters_c0():
+    """Returns a list of C0 control characters as strings.
+    C0 control characters defined as the byte range 0x00-0x1F, and 0x7F.
+    """
+    return [chr(c) for c in range(0x00, 0x20)] + ["\x7F"]
Index: Python-3.4.10/Lib/test/test_wsgiref.py
===================================================================
--- Python-3.4.10.orig/Lib/test/test_wsgiref.py	2026-02-12 19:35:48.491806336 +0100
+++ Python-3.4.10/Lib/test/test_wsgiref.py	2026-02-12 20:00:34.687622549 +0100
@@ -15,7 +15,7 @@
 import sys
 import unittest
 
-from test import support
+from test.support import run_unittest, control_characters_c0
 
 
 class MockServer(WSGIServer):
@@ -420,6 +420,16 @@
             '\r\n'
         )
 
+    def testRaisesControlCharacters(self):
+        headers = Headers([])
+        for c0 in control_characters_c0():
+            self.assertRaises(ValueError, headers.__setitem__, "key{}".format(c0), "val")
+            self.assertRaises(ValueError, headers.__setitem__, "key", "val{}".format(c0))
+            self.assertRaises(ValueError, headers.add_header, "key{}".format(c0), "val", param="param")
+            self.assertRaises(ValueError, headers.add_header, "key", "val{}".format(c0), param="param")
+            self.assertRaises(ValueError, headers.add_header, "key", "val", param="param{}".format(c0))
+
+
 class ErrorHandler(BaseCGIHandler):
     """Simple handler subclass for testing BaseHandler"""
 
@@ -660,7 +670,7 @@
 
 
 def test_main():
-    support.run_unittest(__name__)
+    run_unittest(__name__)
 
 if __name__ == "__main__":
     test_main()
Index: Python-3.4.10/Lib/wsgiref/headers.py
===================================================================
--- Python-3.4.10.orig/Lib/wsgiref/headers.py	2026-02-12 19:35:48.491806336 +0100
+++ Python-3.4.10/Lib/wsgiref/headers.py	2026-02-12 19:36:03.256141637 +0100
@@ -9,6 +9,7 @@
 # existence of which force quoting of the parameter value.
 import re
 tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
+_control_chars_re = re.compile(r'[\x00-\x1F\x7F]')
 
 def _formatparam(param, value=None, quote=1):
     """Convenience function to format and return a key=value pair.
@@ -41,6 +42,8 @@
     def _convert_string_type(self, value):
         """Convert/check value type."""
         if type(value) is str:
+            if _control_chars_re.search(value):
+                raise ValueError("Control characters not allowed in headers")
             return value
         raise AssertionError("Header names/values must be"
             " of type str (got {0})".format(repr(value)))
Index: Python-3.4.10/Misc/NEWS.d/next/Security/2026-01-16-11-07-36.gh-issue-143916.dpWeOD.rst
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ Python-3.4.10/Misc/NEWS.d/next/Security/2026-01-16-11-07-36.gh-issue-143916.dpWeOD.rst	2026-02-12 19:36:03.256393472 +0100
@@ -0,0 +1,2 @@
+Reject C0 control characters within wsgiref.headers.Headers fields, values,
+and parameters.
openSUSE Build Service is sponsored by