Skip to content

Commit 8d6363e

Browse files
committed
httputil: Enforce a new limit on the number of arguments in a request
Large POST bodies can be very expensive to parse in the worst case, so use the (new in Python 3.8) max_num_fields argument to limit the cost. A new field in ParseBodyConfig allows users to configure this limit. The default is 1000, which is the same as that used in php and node.js.
1 parent de85b3f commit 8d6363e

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎tornado/escape.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,33 @@ def url_unescape(
171171

172172

173173
def parse_qs_bytes(
174-
qs: Union[str, bytes], keep_blank_values: bool = False, strict_parsing: bool = False
174+
qs: Union[str, bytes],
175+
keep_blank_values: bool = False,
176+
strict_parsing: bool = False,
177+
*,
178+
max_num_fields: Optional[int] = None,
175179
) -> Dict[str, List[bytes]]:
176180
"""Parses a query string like urlparse.parse_qs,
177181
but takes bytes and returns the values as byte strings.
178182
179183
Keys still become type str (interpreted as latin1 in python3!)
180184
because it's too painful to keep them as byte strings in
181185
python3 and in practice they're nearly always ascii anyway.
186+
187+
.. versionadded:: 6.5.8
188+
The ``max_num_fields`` argument. ValueError is raised if this limit is exceeded.
182189
"""
183190
# This is gross, but python3 doesn't give us another way.
184191
# Latin1 is the universal donor of character encodings.
185192
if isinstance(qs, bytes):
186193
qs = qs.decode("latin1")
187194
result = urllib.parse.parse_qs(
188-
qs, keep_blank_values, strict_parsing, encoding="latin1", errors="strict"
195+
qs,
196+
keep_blank_values,
197+
strict_parsing,
198+
encoding="latin1",
199+
errors="strict",
200+
max_num_fields=max_num_fields,
189201
)
190202
encoded = {}
191203
for k, v in result.items():

‎tornado/httputil.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,23 @@ class ParseMultipartConfig:
947947
"""
948948

949949

950+
@dataclasses.dataclass
951+
class ParseUrlEncodedConfig:
952+
"""This class configures the parsing of ``application/x-www-form-urlencoded`` request bodies.
953+
954+
Its primary purpose is to place limits on the size and complexity of request messages
955+
to avoid potential denial-of-service attacks.
956+
957+
.. versionadded:: 6.5.8
958+
"""
959+
960+
max_arguments: int = 1000
961+
"""The maximum number of arguments accepted in a urlencoded request.
962+
963+
Each ``<input>`` element in an HTML form corresponds to at least one argument.
964+
"""
965+
966+
950967
@dataclasses.dataclass
951968
class ParseBodyConfig:
952969
"""This class configures the parsing of request bodies.
@@ -957,6 +974,9 @@ class ParseBodyConfig:
957974
multipart: ParseMultipartConfig = dataclasses.field(
958975
default_factory=ParseMultipartConfig
959976
)
977+
urlencoded: ParseUrlEncodedConfig = dataclasses.field(
978+
default_factory=ParseUrlEncodedConfig
979+
)
960980
"""Configuration for ``multipart/form-data`` request bodies."""
961981

962982

@@ -1015,7 +1035,11 @@ def parse_body_arguments(
10151035
)
10161036
try:
10171037
# real charset decoding will happen in RequestHandler.decode_argument()
1018-
uri_arguments = parse_qs_bytes(body, keep_blank_values=True)
1038+
uri_arguments = parse_qs_bytes(
1039+
body,
1040+
keep_blank_values=True,
1041+
max_num_fields=config.urlencoded.max_arguments,
1042+
)
10191043
except Exception as e:
10201044
raise HTTPInputError("Invalid x-www-form-urlencoded body: %s" % e) from e
10211045
for name, values in uri_arguments.items():

‎tornado/test/httputil_test.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from tornado.httputil import (
2+
parse_body_arguments,
23
url_concat,
34
parse_multipart_form_data,
45
HTTPHeaders,
@@ -95,6 +96,23 @@ def test_parsing(self):
9596
self.assertIn(("b", "2"), qsl)
9697

9798

99+
class UrlEncodedDataTest(unittest.TestCase):
100+
def test_urlencoded_data(self):
101+
data = b"a=1&b=2&a=3"
102+
args, files = form_data_args()
103+
parse_body_arguments("application/x-www-form-urlencoded", data, args, files)
104+
self.assertEqual(args["a"], [b"1", b"3"])
105+
self.assertEqual(args["b"], [b"2"])
106+
self.assertEqual(files, {})
107+
108+
def test_max_arguments(self):
109+
data = b"".join(b"a=1&" for _ in range(1001))
110+
args, files = form_data_args()
111+
with self.assertRaises(HTTPInputError) as cm:
112+
parse_body_arguments("application/x-www-form-urlencoded", data, args, files)
113+
self.assertIn("Max number of fields exceeded", str(cm.exception))
114+
115+
98116
class MultipartFormDataTest(unittest.TestCase):
99117
def test_file_upload(self):
100118
data = b"""\

0 commit comments

Comments
 (0)