Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3b0169b
Disallow usage of control characters in status, headers and values fo…
benediktjohannes Jan 31, 2026
9414df2
Add missing import of "re"
benediktjohannes Jan 31, 2026
49ddbca
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 31, 2026
5dd863b
Update Lib/wsgiref/handlers.py
benediktjohannes Feb 4, 2026
3daaa72
Update Lib/wsgiref/handlers.py
benediktjohannes Feb 4, 2026
8b149df
Update handlers.py
benediktjohannes Feb 4, 2026
010fd50
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
8c9a691
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
f301791
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
e3b78a0
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
24cfb00
Test whether if statement is reachable
benediktjohannes Feb 4, 2026
75a89b8
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
e322666
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
d731520
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
c039ef2
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
edb54a2
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
b491245
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
379937e
Update test_wsgiref.py
benediktjohannes Feb 4, 2026
db12d86
Use more strict name=True for status because it shouldn't IMO include…
benediktjohannes Feb 4, 2026
f206bf3
Change it back at first to see if test passes (and then change it bac…
benediktjohannes Feb 4, 2026
b899f69
Test if assertRaise raises an error that there is a mistake if no err…
benediktjohannes Feb 6, 2026
2d1b890
this is just a temporary check as described above
benediktjohannes Feb 6, 2026
df5cfdf
Change this back
benediktjohannes Feb 6, 2026
fb527db
Update handlers.py
benediktjohannes Feb 6, 2026
e84de9a
Remove this because I use the more strict one for status without the …
benediktjohannes Feb 6, 2026
95701e4
Remove f"keys"
benediktjohannes Feb 6, 2026
82d7f7a
Add string again without keys
benediktjohannes Feb 6, 2026
76d011e
Update handlers.py
benediktjohannes Feb 6, 2026
5a4448b
Update handlers.py
benediktjohannes Feb 6, 2026
ecff2b9
Update 2026-01-31-21-56-54.gh-issue-144370.fp9m8t.rst
benediktjohannes Feb 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_wsgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,13 @@ def write(self, b):
self.assertIsNotNone(h.status)
self.assertIsNotNone(h.environ)

def testRaisesControlCharacters(self):
for c0 in control_characters_c0():
with self.subTest(c0):
base = BaseHandler()
headers = [('x','y')]
self.assertRaises(ValueError, base.start_response, f"{c0}", headers)


class TestModule(unittest.TestCase):
def test_deprecated__version__(self):
Expand Down
16 changes: 11 additions & 5 deletions Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .util import FileWrapper, guess_scheme, is_hop_by_hop
from .headers import Headers

import sys, os, time
import sys, os, time, re

__all__ = [
'BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler',
Expand All @@ -16,6 +16,9 @@
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

_name_disallowed_re = re.compile(r'[\x00-\x1F\x7F]')
_value_disallowed_re = re.compile(r'[\x00-\x08\x0A-\x1F\x7F]')

def format_date_time(timestamp):
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
Expand Down Expand Up @@ -237,13 +240,13 @@ def start_response(self, status, headers,exc_info=None):

self.status = status
self.headers = self.headers_class(headers)
status = self._convert_string_type(status, "Status")
status = self._convert_string_type(status, "Status", name=True)
self._validate_status(status)

if __debug__:
for name, val in headers:
name = self._convert_string_type(name, "Header name")
val = self._convert_string_type(val, "Header value")
name = self._convert_string_type(name, "Header name", name=True)
val = self._convert_string_type(val, "Header value", name=False)
assert not is_hop_by_hop(name),\
f"Hop-by-hop header, '{name}: {val}', not allowed"

Expand All @@ -257,9 +260,12 @@ def _validate_status(self, status):
if status[3] != " ":
raise AssertionError("Status message must have a space after code")

def _convert_string_type(self, value, title):
def _convert_string_type(self, value, title, *, name):
"""Convert/check value type."""
if type(value) is str:
regex = (_name_disallowed_re if name else _value_disallowed_re)
if regex.search(value):
raise ValueError("Control characters not allowed in header names, values and statuses")
return value
raise AssertionError(
"{0} must be of type str (got {1})".format(title, repr(value))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disallow usage of control characters in header names, values and statuses in ``Lib/wsgiref/handlers.py`` for security. Patch by Benedikt Johannes.
Loading