diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-10-19 11:11:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-19 11:11:16 (GMT) |
commit | a66f279a1381dd5c1c27232ccf9f210d575e1dcc (patch) | |
tree | f496d5feb844bbac663a0f8d166e2a94bb14abf6 /Lib/test/test_cgi.py | |
parent | d6d35d0a005bb3d1e04095074c255bf8d0b651d7 (diff) | |
download | cpython-a66f279a1381dd5c1c27232ccf9f210d575e1dcc.zip cpython-a66f279a1381dd5c1c27232ccf9f210d575e1dcc.tar.gz cpython-a66f279a1381dd5c1c27232ccf9f210d575e1dcc.tar.bz2 |
bpo-34866: Adding max_num_fields to cgi.FieldStorage (GH-9660)
Adding `max_num_fields` to `cgi.FieldStorage` to make DOS attacks harder by
limiting the number of `MiniFieldStorage` objects created by `FieldStorage`.
(cherry picked from commit 209144831b0a19715bda3bd72b14a3e6192d9cc1)
Co-authored-by: matthewbelisle-wf <matthew.belisle@workiva.com>
Diffstat (limited to 'Lib/test/test_cgi.py')
-rw-r--r-- | Lib/test/test_cgi.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 4f2bba1..ff9c005 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -391,6 +391,55 @@ Larry v = gen_result(data, environ) self.assertEqual(self._qs_result, v) + def test_max_num_fields(self): + # For application/x-www-form-urlencoded + data = '&'.join(['a=a']*11) + environ = { + 'CONTENT_LENGTH': str(len(data)), + 'CONTENT_TYPE': 'application/x-www-form-urlencoded', + 'REQUEST_METHOD': 'POST', + } + + with self.assertRaises(ValueError): + cgi.FieldStorage( + fp=BytesIO(data.encode()), + environ=environ, + max_num_fields=10, + ) + + # For multipart/form-data + data = """---123 +Content-Disposition: form-data; name="a" + +a +---123 +Content-Type: application/x-www-form-urlencoded + +a=a&a=a +---123-- +""" + environ = { + 'CONTENT_LENGTH': str(len(data)), + 'CONTENT_TYPE': 'multipart/form-data; boundary=-123', + 'QUERY_STRING': 'a=a&a=a', + 'REQUEST_METHOD': 'POST', + } + + # 2 GET entities + # 2 top level POST entities + # 2 entities within the second POST entity + with self.assertRaises(ValueError): + cgi.FieldStorage( + fp=BytesIO(data.encode()), + environ=environ, + max_num_fields=5, + ) + cgi.FieldStorage( + fp=BytesIO(data.encode()), + environ=environ, + max_num_fields=6, + ) + def testQSAndFormData(self): data = """---123 Content-Disposition: form-data; name="key2" |