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

Skip to content

Commit b4cbb92

Browse files
committed
Issue #19092 - Raise a correct exception when cgi.FieldStorage is given an
invalid file-obj. Also use __bool__ to determine the bool of the FieldStorage object.
1 parent 5636eb7 commit b4cbb92

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lib/cgi.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
# =======
3333

3434
from io import StringIO, BytesIO, TextIOWrapper
35+
from collections import Mapping
3536
import sys
3637
import os
3738
import urllib.parse
3839
from email.parser import FeedParser
40+
from email.message import Message
3941
from warnings import warn
4042
import html
4143
import locale
@@ -472,18 +474,24 @@ def __init__(self, fp=None, headers=None, outerboundary=b'',
472474
self.qs_on_post = environ['QUERY_STRING']
473475
if 'CONTENT_LENGTH' in environ:
474476
headers['content-length'] = environ['CONTENT_LENGTH']
477+
else:
478+
if not (isinstance(headers, (Mapping, Message))):
479+
raise TypeError("headers must be mapping or an instance of "
480+
"email.message.Message")
481+
self.headers = headers
475482
if fp is None:
476483
self.fp = sys.stdin.buffer
477484
# self.fp.read() must return bytes
478485
elif isinstance(fp, TextIOWrapper):
479486
self.fp = fp.buffer
480487
else:
488+
if not (hasattr(fp, 'read') and hasattr(fp, 'readline')):
489+
raise TypeError("fp must be file pointer")
481490
self.fp = fp
482491

483492
self.encoding = encoding
484493
self.errors = errors
485494

486-
self.headers = headers
487495
if not isinstance(outerboundary, bytes):
488496
raise TypeError('outerboundary must be bytes, not %s'
489497
% type(outerboundary).__name__)
@@ -636,7 +644,9 @@ def __len__(self):
636644
"""Dictionary style len(x) support."""
637645
return len(self.keys())
638646

639-
def __nonzero__(self):
647+
def __bool__(self):
648+
if self.list is None:
649+
raise TypeError("Cannot be converted to bool.")
640650
return bool(self.list)
641651

642652
def read_urlencoded(self):

Lib/test/test_cgi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ def test_fieldstorage_properties(self):
137137
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))
138138
self.assertTrue(fs)
139139

140+
def test_fieldstorage_invalid(self):
141+
self.assertRaises(TypeError, cgi.FieldStorage, "not-a-file-obj",
142+
environ={"REQUEST_METHOD":"PUT"})
143+
self.assertRaises(TypeError, cgi.FieldStorage, "foo", "bar")
144+
fs = cgi.FieldStorage(headers={'content-type':'text/plain'})
145+
self.assertRaises(TypeError, bool, fs)
146+
140147
def test_escape(self):
141148
# cgi.escape() is deprecated.
142149
with warnings.catch_warnings():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #19097: Raise the correct Exception when cgi.FieldStorage is given an
47+
Invalid fileobj.
48+
4649
- Issue #20217: Fix build in SCHED_SPORADIC is defined.
4750

4851
- Issue #13107: argparse and optparse no longer raises an exception when output

0 commit comments

Comments
 (0)