mirror of
https://github.com/python/cpython.git
synced 2026-10-28 10:25:05 +08:00
gh-144370: Disallow usage of control characters in status in wsgiref.handlers for security (#144371)
Disallow usage of control characters in status in wsgiref.handlers to prevent HTTP header injections. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
4 files changed
+25
-1
No files matched your search
@@ -855,6 +855,25 @@ class HandlerTests(TestCase):
|
||||
self.assertIsNotNone(h.status)
|
||||
self.assertIsNotNone(h.environ)
|
||||
|
||||
def testRaisesControlCharacters(self):
|
||||
for c0 in control_characters_c0():
|
||||
with self.subTest(c0):
|
||||
base = BaseHandler()
|
||||
with self.assertRaises(ValueError):
|
||||
base.start_response(c0, [('x', 'y')])
|
||||
|
||||
base = BaseHandler()
|
||||
with self.assertRaises(ValueError):
|
||||
base.start_response('200 OK', [(c0, 'y')])
|
||||
|
||||
# HTAB (\x09) is allowed in header values, but not in names.
|
||||
base = BaseHandler()
|
||||
if c0 != "\t":
|
||||
with self.assertRaises(ValueError):
|
||||
base.start_response('200 OK', [('x', c0)])
|
||||
else:
|
||||
base.start_response('200 OK', [('x', c0)])
|
||||
|
||||
|
||||
class TestModule(unittest.TestCase):
|
||||
def test_deprecated__version__(self):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Base classes for server/gateway implementations"""
|
||||
|
||||
from .util import FileWrapper, guess_scheme, is_hop_by_hop
|
||||
from .headers import Headers
|
||||
from .headers import Headers, _name_disallowed_re
|
||||
|
||||
import sys, os, time
|
||||
|
||||
@@ -250,6 +250,8 @@ class BaseHandler:
|
||||
return self.write
|
||||
|
||||
def _validate_status(self, status):
|
||||
if _name_disallowed_re.search(status):
|
||||
raise ValueError("Control characters are not allowed in status")
|
||||
if len(status) < 4:
|
||||
raise AssertionError("Status must be at least 4 characters")
|
||||
if not status[:3].isdigit():
|
||||
|
||||
@@ -1086,6 +1086,7 @@ Wolfgang Langner
|
||||
Detlef Lannert
|
||||
Rémi Lapeyre
|
||||
Soren Larsen
|
||||
Seth Michael Larson
|
||||
Amos Latteier
|
||||
Keenan Lau
|
||||
Piers Lauder
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
Disallow usage of control characters in status in :mod:`wsgiref.handlers` to prevent HTTP header injections.
|
||||
Patch by Benedikt Johannes.
|
||||
Reference in new issue
Block a user