Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions 19 Lib/test/test_wsgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,25 @@ 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()
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)])


if __name__ == "__main__":
unittest.main()
4 changes: 3 additions & 1 deletion 4 Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -249,6 +249,8 @@ def start_response(self, status, headers,exc_info=None):
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():
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ Wolfgang Langner
Detlef Lannert
Rémi Lapeyre
Soren Larsen
Seth Michael Larson
Amos Latteier
Keenan Lau
Piers Lauder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disallow usage of control characters in status in :mod:`wsgiref.handlers` to prevent HTTP header injections.
Patch by Benedikt Johannes.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.