Skip to content

Commit c8f6d57

Browse files
gpsheadsethmlarson
authored andcommitted
pythongh-143916: Reject control characters in wsgiref.headers.Headers (pythonGH-143917) * Add 'test.support' fixture for C0 control characters * pythongh-143916: Reject control characters in wsgiref.headers.Headers (cherry picked from commit f7fceed) (cherry picked from commit 22e4d55) Co-authored-by: Seth Michael Larson <seth@python.org>
1 parent eae17ba commit c8f6d57

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Lib/test/support/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,3 +2902,10 @@ def adjust_int_max_str_digits(max_digits):
29022902
yield
29032903
finally:
29042904
sys.set_int_max_str_digits(current)
2905+
2906+
2907+
def control_characters_c0():
2908+
"""Returns a list of C0 control characters as strings.
2909+
C0 control characters defined as the byte range 0x00-0x1F, and 0x7F.
2910+
"""
2911+
return [chr(c) for c in range(0x00, 0x20)] + ["\x7F"]

Lib/test/test_wsgiref.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest import mock
22
from test import support
3+
from test.support import control_characters_c0
34
from test.test_httpservers import NoLogRequestHandler
45
from unittest import TestCase
56
from wsgiref.util import setup_testing_defaults
@@ -517,6 +518,16 @@ def testExtras(self):
517518
'\r\n'
518519
)
519520

521+
def testRaisesControlCharacters(self):
522+
headers = Headers()
523+
for c0 in control_characters_c0():
524+
self.assertRaises(ValueError, headers.__setitem__, f"key{c0}", "val")
525+
self.assertRaises(ValueError, headers.__setitem__, "key", f"val{c0}")
526+
self.assertRaises(ValueError, headers.add_header, f"key{c0}", "val", param="param")
527+
self.assertRaises(ValueError, headers.add_header, "key", f"val{c0}", param="param")
528+
self.assertRaises(ValueError, headers.add_header, "key", "val", param=f"param{c0}")
529+
530+
520531
class ErrorHandler(BaseCGIHandler):
521532
"""Simple handler subclass for testing BaseHandler"""
522533

Lib/wsgiref/headers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# existence of which force quoting of the parameter value.
1010
import re
1111
tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
12+
_control_chars_re = re.compile(r'[\x00-\x1F\x7F]')
1213

1314
def _formatparam(param, value=None, quote=1):
1415
"""Convenience function to format and return a key=value pair.
@@ -41,6 +42,8 @@ def __init__(self, headers=None):
4142
def _convert_string_type(self, value):
4243
"""Convert/check value type."""
4344
if type(value) is str:
45+
if _control_chars_re.search(value):
46+
raise ValueError("Control characters not allowed in headers")
4447
return value
4548
raise AssertionError("Header names/values must be"
4649
" of type str (got {0})".format(repr(value)))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reject C0 control characters within wsgiref.headers.Headers fields, values,
2+
and parameters.

0 commit comments

Comments
 (0)