Commit b4cbb92f authored by Senthil Kumaran's avatar Senthil Kumaran

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.
parent 5636eb7b
...@@ -32,10 +32,12 @@ __version__ = "2.6" ...@@ -32,10 +32,12 @@ __version__ = "2.6"
# ======= # =======
from io import StringIO, BytesIO, TextIOWrapper from io import StringIO, BytesIO, TextIOWrapper
from collections import Mapping
import sys import sys
import os import os
import urllib.parse import urllib.parse
from email.parser import FeedParser from email.parser import FeedParser
from email.message import Message
from warnings import warn from warnings import warn
import html import html
import locale import locale
...@@ -472,18 +474,24 @@ class FieldStorage: ...@@ -472,18 +474,24 @@ class FieldStorage:
self.qs_on_post = environ['QUERY_STRING'] self.qs_on_post = environ['QUERY_STRING']
if 'CONTENT_LENGTH' in environ: if 'CONTENT_LENGTH' in environ:
headers['content-length'] = environ['CONTENT_LENGTH'] headers['content-length'] = environ['CONTENT_LENGTH']
else:
if not (isinstance(headers, (Mapping, Message))):
raise TypeError("headers must be mapping or an instance of "
"email.message.Message")
self.headers = headers
if fp is None: if fp is None:
self.fp = sys.stdin.buffer self.fp = sys.stdin.buffer
# self.fp.read() must return bytes # self.fp.read() must return bytes
elif isinstance(fp, TextIOWrapper): elif isinstance(fp, TextIOWrapper):
self.fp = fp.buffer self.fp = fp.buffer
else: else:
if not (hasattr(fp, 'read') and hasattr(fp, 'readline')):
raise TypeError("fp must be file pointer")
self.fp = fp self.fp = fp
self.encoding = encoding self.encoding = encoding
self.errors = errors self.errors = errors
self.headers = headers
if not isinstance(outerboundary, bytes): if not isinstance(outerboundary, bytes):
raise TypeError('outerboundary must be bytes, not %s' raise TypeError('outerboundary must be bytes, not %s'
% type(outerboundary).__name__) % type(outerboundary).__name__)
...@@ -636,7 +644,9 @@ class FieldStorage: ...@@ -636,7 +644,9 @@ class FieldStorage:
"""Dictionary style len(x) support.""" """Dictionary style len(x) support."""
return len(self.keys()) return len(self.keys())
def __nonzero__(self): def __bool__(self):
if self.list is None:
raise TypeError("Cannot be converted to bool.")
return bool(self.list) return bool(self.list)
def read_urlencoded(self): def read_urlencoded(self):
......
...@@ -137,6 +137,13 @@ class CgiTests(unittest.TestCase): ...@@ -137,6 +137,13 @@ class CgiTests(unittest.TestCase):
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue')) fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))
self.assertTrue(fs) self.assertTrue(fs)
def test_fieldstorage_invalid(self):
self.assertRaises(TypeError, cgi.FieldStorage, "not-a-file-obj",
environ={"REQUEST_METHOD":"PUT"})
self.assertRaises(TypeError, cgi.FieldStorage, "foo", "bar")
fs = cgi.FieldStorage(headers={'content-type':'text/plain'})
self.assertRaises(TypeError, bool, fs)
def test_escape(self): def test_escape(self):
# cgi.escape() is deprecated. # cgi.escape() is deprecated.
with warnings.catch_warnings(): with warnings.catch_warnings():
......
...@@ -43,6 +43,9 @@ Core and Builtins ...@@ -43,6 +43,9 @@ Core and Builtins
Library Library
------- -------
- Issue #19097: Raise the correct Exception when cgi.FieldStorage is given an
Invalid fileobj.
- Issue #20217: Fix build in SCHED_SPORADIC is defined. - Issue #20217: Fix build in SCHED_SPORADIC is defined.
- Issue #13107: argparse and optparse no longer raises an exception when output - Issue #13107: argparse and optparse no longer raises an exception when output
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment