Skip to content

Commit f42744b

Browse files
committed
changes made as per reviews
1 parent 5cea514 commit f42744b

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Lib/test/test_urlparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,10 @@ def test_parse_qs_encoding(self):
12291229

12301230
def test_qsl_strict_parsing_raises(self):
12311231
with self.assertRaises(ValueError):
1232-
urllib.parse.parse_qsl("foo", strict_parsing=True)
1232+
urllib.parse.parse_qsl("foo=^", strict_parsing=True)
12331233

12341234
with self.assertRaises(ValueError):
1235-
urllib.parse.parse_qsl(b"foo", strict_parsing=True)
1235+
urllib.parse.parse_qsl(b"foo=`", strict_parsing=True)
12361236

12371237
def test_parse_qsl_encoding(self):
12381238
result = urllib.parse.parse_qsl("key=\u0141%E9", encoding="latin-1")

Lib/urllib/parse.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
# Unsafe bytes to be removed per WHATWG spec
9292
_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
9393

94-
# Allowed valid characters in parse_qsl
95-
_VALID_QUERY_CHARS = "-._~!$&'()*+,;=:@/?%"
94+
# Allowed valid characters in parse_qsl as per RFC 3986.
95+
_VALID_RFC3986_QUERY_CHARS = "-._~!$&'()*+,;=:@/?%"
9696

9797
def clear_cache():
9898
"""Clear internal performance caches. Undocumented; some tests want it."""
@@ -781,12 +781,12 @@ def parse_qs(qs, keep_blank_values=False, strict_parsing=False,
781781
parsed_result[name] = [value]
782782
return parsed_result
783783

784-
def _is_valid_query(to_check: str) -> bool:
784+
def _is_valid_rfc3986_query(chars):
785785
"""Return True if all characters are valid per RFC 3986."""
786-
for ch in to_check:
786+
for ch in chars:
787787
if not ch.isascii():
788788
return False
789-
if ch.isalnum() or ch in _VALID_QUERY_CHARS:
789+
if ch.isalnum() or ch in _VALID_RFC3986_QUERY_CHARS:
790790
continue
791791
return False
792792
return True
@@ -868,8 +868,10 @@ def _unquote(s):
868868
raise ValueError("bad query field: %r" % (name_value,))
869869
if strict_parsing:
870870
# Validate RFC3986 characters
871-
to_check = (name_value.decode() if isinstance(name_value, bytes) else name_value)
872-
if not _is_valid_query(to_check):
871+
to_check = _unquote(name_value)
872+
if isinstance(to_check, (bytes, bytearray)):
873+
to_check = to_check.decode(encoding, errors)
874+
if not _is_valid_rfc3986_query(to_check):
873875
raise ValueError(f"Invalid characters in query string per RFC 3986: {name_value!r}")
874876
if value or keep_blank_values:
875877
name = _unquote(name)

0 commit comments

Comments
 (0)