diff options
author | matthewbelisle-wf <matthew.belisle@workiva.com> | 2018-10-19 05:52:59 -0500 |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-10-19 03:52:59 -0700 |
commit | 209144831b0a19715bda3bd72b14a3e6192d9cc1 (patch) | |
tree | 872e8d5460807aaf6f02b72a0c3b8f4202739ba2 /Lib/test/test_cgi.py | |
parent | f081fd83032be48aefdb1bbcc38ab5deb03785d5 (diff) | |
download | cpython-git-209144831b0a19715bda3bd72b14a3e6192d9cc1.tar.gz |
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`.
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 b284a0d098..8ea9d6aee6 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -381,6 +381,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" |