Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cd4ff6

Browse files
committed
Issue #26560: Avoid potential ValueError in BaseHandler.start_response
Initial patch by Peter Inglesby.
1 parent adcb654 commit 1cd4ff6

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lib/test/test_wsgiref.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ def bad_app(environ,start_response):
166166
" be of type list: <class 'tuple'>"
167167
)
168168

169+
def test_status_validation_errors(self):
170+
def create_bad_app(status):
171+
def bad_app(environ, start_response):
172+
start_response(status, [("Content-Type", "text/plain; charset=utf-8")])
173+
return [b"Hello, world!"]
174+
return bad_app
175+
176+
tests = [
177+
('200', 'AssertionError: Status must be at least 4 characters'),
178+
('20X OK', 'AssertionError: Status message must begin w/3-digit code'),
179+
('200OK', 'AssertionError: Status message must have a space after code'),
180+
]
181+
182+
for status, exc_message in tests:
183+
with self.subTest(status=status):
184+
out, err = run_amock(create_bad_app(status))
185+
self.assertTrue(out.endswith(
186+
b"A server error occurred. Please contact the administrator."
187+
))
188+
self.assertEqual(err.splitlines()[-2], exc_message)
189+
169190
def test_wsgi_input(self):
170191
def bad_app(e,s):
171192
e["wsgi.input"].read()

Lib/wsgiref/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def start_response(self, status, headers,exc_info=None):
226226
self.headers = self.headers_class(headers)
227227
status = self._convert_string_type(status, "Status")
228228
assert len(status)>=4,"Status must be at least 4 characters"
229-
assert int(status[:3]),"Status message must begin w/3-digit code"
229+
assert status[:3].isdigit(), "Status message must begin w/3-digit code"
230230
assert status[3]==" ", "Status message must have a space after code"
231231

232232
if __debug__:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ Core and Builtins
9191
Library
9292
-------
9393

94+
- Issue #26560: Avoid potential ValueError in BaseHandler.start_response.
95+
Initial patch by Peter Inglesby.
96+
9497
- Issue #26313: ssl.py _load_windows_store_certs fails if windows cert store
9598
is empty. Patch by Baji.
9699

0 commit comments

Comments
 (0)