Skip to content

[3.12] gh-74668: Fix support of bytes in urllib.parse.parse_qsl() (GH-115771) #116366

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 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
("=a", [('', 'a')]),
("a", [('a', '')]),
("a=", [('a', '')]),
("a=b=c", [('a', 'b=c')]),
("a%3Db=c", [('a=b', 'c')]),
("a=b&c=d", [('a', 'b'), ('c', 'd')]),
("a=b%26c=d", [('a', 'b&c=d')]),
("&a=b", [('a', 'b')]),
("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
("a=1&a=2", [('a', '1'), ('a', '2')]),
Expand All @@ -29,13 +33,25 @@
(b"=a", [(b'', b'a')]),
(b"a", [(b'a', b'')]),
(b"a=", [(b'a', b'')]),
(b"a=b=c", [(b'a', b'b=c')]),
(b"a%3Db=c", [(b'a=b', b'c')]),
(b"a=b&c=d", [(b'a', b'b'), (b'c', b'd')]),
(b"a=b%26c=d", [(b'a', b'b&c=d')]),
(b"&a=b", [(b'a', b'b')]),
(b"a=a+b&b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
(b"a=1&a=2", [(b'a', b'1'), (b'a', b'2')]),
(";a=b", [(';a', 'b')]),
("a=a+b;b=b+c", [('a', 'a b;b=b c')]),
(b";a=b", [(b';a', b'b')]),
(b"a=a+b;b=b+c", [(b'a', b'a b;b=b c')]),

("\u0141=\xE9", [('\u0141', '\xE9')]),
("%C5%81=%C3%A9", [('\u0141', '\xE9')]),
("%81=%A9", [('\ufffd', '\ufffd')]),
(b"\xc5\x81=\xc3\xa9", [(b'\xc5\x81', b'\xc3\xa9')]),
(b"%C5%81=%C3%A9", [(b'\xc5\x81', b'\xc3\xa9')]),
(b"\x81=\xA9", [(b'\x81', b'\xa9')]),
(b"%81=%A9", [(b'\x81', b'\xa9')]),
]

# Each parse_qs testcase is a two-tuple that contains
Expand All @@ -49,6 +65,10 @@
("=a", {'': ['a']}),
("a", {'a': ['']}),
("a=", {'a': ['']}),
("a=b=c", {'a': ['b=c']}),
("a%3Db=c", {'a=b': ['c']}),
("a=b&c=d", {'a': ['b'], 'c': ['d']}),
("a=b%26c=d", {'a': ['b&c=d']}),
("&a=b", {'a': ['b']}),
("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
("a=1&a=2", {'a': ['1', '2']}),
Expand All @@ -59,13 +79,26 @@
(b"=a", {b'': [b'a']}),
(b"a", {b'a': [b'']}),
(b"a=", {b'a': [b'']}),
(b"a=b=c", {b'a': [b'b=c']}),
(b"a%3Db=c", {b'a=b': [b'c']}),
(b"a=b&c=d", {b'a': [b'b'], b'c': [b'd']}),
(b"a=b%26c=d", {b'a': [b'b&c=d']}),
(b"&a=b", {b'a': [b'b']}),
(b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
(b"a=1&a=2", {b'a': [b'1', b'2']}),
(";a=b", {';a': ['b']}),
("a=a+b;b=b+c", {'a': ['a b;b=b c']}),
(b";a=b", {b';a': [b'b']}),
(b"a=a+b;b=b+c", {b'a':[ b'a b;b=b c']}),
(b"a=a%E2%80%99b", {b'a': [b'a\xe2\x80\x99b']}),

("\u0141=\xE9", {'\u0141': ['\xE9']}),
("%C5%81=%C3%A9", {'\u0141': ['\xE9']}),
("%81=%A9", {'\ufffd': ['\ufffd']}),
(b"\xc5\x81=\xc3\xa9", {b'\xc5\x81': [b'\xc3\xa9']}),
(b"%C5%81=%C3%A9", {b'\xc5\x81': [b'\xc3\xa9']}),
(b"\x81=\xA9", {b'\x81': [b'\xa9']}),
(b"%81=%A9", {b'\x81': [b'\xa9']}),
]

class UrlParseTestCase(unittest.TestCase):
Expand Down Expand Up @@ -995,8 +1028,8 @@ def test_parse_qsl_encoding(self):

def test_parse_qsl_max_num_fields(self):
with self.assertRaises(ValueError):
urllib.parse.parse_qs('&'.join(['a=a']*11), max_num_fields=10)
urllib.parse.parse_qs('&'.join(['a=a']*10), max_num_fields=10)
urllib.parse.parse_qsl('&'.join(['a=a']*11), max_num_fields=10)
urllib.parse.parse_qsl('&'.join(['a=a']*10), max_num_fields=10)

def test_parse_qs_separator(self):
parse_qs_semicolon_cases = [
Expand Down
50 changes: 26 additions & 24 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,42 +763,44 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,

Returns a list, as G-d intended.
"""
qs, _coerce_result = _coerce_args(qs)
separator, _ = _coerce_args(separator)

if not separator or (not isinstance(separator, (str, bytes))):
if not separator or not isinstance(separator, (str, bytes)):
raise ValueError("Separator must be of type string or bytes.")
if isinstance(qs, str):
if not isinstance(separator, str):
separator = str(separator, 'ascii')
eq = '='
def _unquote(s):
return unquote_plus(s, encoding=encoding, errors=errors)
else:
qs = bytes(qs)
if isinstance(separator, str):
separator = bytes(separator, 'ascii')
eq = b'='
def _unquote(s):
return unquote_to_bytes(s.replace(b'+', b' '))

if not qs:
return []

# If max_num_fields is defined then check that the number of fields
# 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:
num_fields = 1 + qs.count(separator) if qs else 0
num_fields = 1 + qs.count(separator)
if max_num_fields < num_fields:
raise ValueError('Max number of fields exceeded')

r = []
query_args = qs.split(separator) if qs else []
for name_value in query_args:
if not name_value and not strict_parsing:
continue
nv = name_value.split('=', 1)
if len(nv) != 2:
if strict_parsing:
for name_value in qs.split(separator):
if name_value or strict_parsing:
name, has_eq, value = name_value.partition(eq)
if not has_eq and strict_parsing:
raise ValueError("bad query field: %r" % (name_value,))
# Handle case of a control-name with no equal sign
if keep_blank_values:
nv.append('')
else:
continue
if len(nv[1]) or keep_blank_values:
name = nv[0].replace('+', ' ')
name = unquote(name, encoding=encoding, errors=errors)
name = _coerce_result(name)
value = nv[1].replace('+', ' ')
value = unquote(value, encoding=encoding, errors=errors)
value = _coerce_result(value)
r.append((name, value))
if value or keep_blank_values:
name = _unquote(name)
value = _unquote(value)
r.append((name, value))
return r

def unquote_plus(string, encoding='utf-8', errors='replace'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`urllib.parse` functions :func:`~urllib.parse.parse_qs` and
:func:`~urllib.parse.parse_qsl` now support bytes arguments containing raw
and percent-encoded non-ASCII data.