10000 bpo-34866: Adding max_num_fields to cgi.FieldStorage by matthewbelisle-wf · Pull Request #9660 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-34866: Adding max_num_fields to cgi.FieldStorage #9660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 19, 2018
Prev Previous commit
Next Next commit
Using count() instead of finditer() for max_num_fields check
  • Loading branch information
matthewbelisle-wf committed Oct 18, 2018
commit 1fa59e4efd44aa3bf0cdcf7b9bd00a7c1ec3fcef
11 changes: 3 additions & 8 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,6 @@ def parse_qs(qs, keep_blank_values=False, strict_parsing=False,
return parsed_result


# Used for checking parse_qsl() with max_num_fields. Both & and ; are valid query
# string delimiters.
_QS_DELIMITER_RE = re.compile(r'[&;]')


def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
encoding='utf-8', errors='replace', max_num_fields=None):
"""Parse a query given as a string argument.
Expand Down Expand Up @@ -703,9 +698,9 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
# is less than max_num_fields. This prevents a memory exhaustion DOS
# attack via post bodies with many fields.
if max_num_fields is not None:
for num_fields, _ in enumerate(_QS_DELIMITER_RE.finditer(qs), 2):
if max_num_fields < num_fields:
raise ValueError('Max number of fields exceeded')
num_fields = 1 + qs.count('&') + qs.count(';')
if max_num_fields < num_fields:
raise ValueError('Max number of fields exceeded')

pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
r = []
Expand Down
0