Whole file

python-validators/validators

The author described this change as fix: Valid URLs failing validation - query and fragment parts (#297). It counts as a record because the check below fails on the code as it stood at 71b40bd54 and passes on 960b48b8a, with nothing else changed between the two runs.

Fix saved2023-09-17
Sharing licenceMIT · LICENSE.txt
Change size+9 11

What the code was meant to do, written into the code itself as a save note

fix: Valid URLs failing validation - query and fragment parts (#297)

The change

33 # standard
44 from functools import lru_cache
55 import re
6-from urllib.parse import unquote, urlsplit
6+from urllib.parse import parse_qs, unquote, urlsplit
77
88 # local
99 from .hostname import hostname
3434 )
3535
3636
37-@lru_cache
38-def _query_regex():
39- return re.compile(r"&?(\w+=?[^\s&]*)", re.IGNORECASE)
40-
41-
4237 def _validate_scheme(value: str):
4338 """Validate scheme."""
4439 # More schemes will be considered later.
108103 ) and _validate_auth_segment(basic_auth)
109104
110105
111-def _validate_optionals(path: str, query: str, fragment: str):
106+def _validate_optionals(path: str, query: str, fragment: str, strict_query: bool):
112107 """Validate path query and fragments."""
113108 optional_segments = True
114109 if path:
115110 optional_segments &= bool(_path_regex().match(path))
116- if query:
117- optional_segments &= bool(_query_regex().match(query))
111+ if query and parse_qs(query, strict_parsing=strict_query):
112+ optional_segments &= True
118113 if fragment:
119114 fragment = fragment.lstrip("/") if fragment.startswith("/") else fragment
120- optional_segments &= all(char_to_avoid not in fragment for char_to_avoid in ("/", "?"))
115+ optional_segments &= all(char_to_avoid not in fragment for char_to_avoid in ("?",))
121116 return optional_segments
122117
123118
130125 skip_ipv4_addr: bool = False,
131126 may_have_port: bool = True,
132127 simple_host: bool = False,
128+ strict_query: bool = True,
133129 rfc_1034: bool = False,
134130 rfc_2782: bool = False,
135131 ):
167163 URL string may contain port number.
168164 simple_host:
169165 URL string maybe only hyphens and alpha-numerals.
166+ strict_query:
167+ Fail validation on query string parsing error.
170168 rfc_1034:
171169 Allow trailing dot in domain/host name.
172170 Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034).
214212 rfc_1034,
215213 rfc_2782,
216214 )
217- and _validate_optionals(path, query, fragment)
215+ and _validate_optionals(path, query, fragment, strict_query)
218216 )
219217

The check that tells the two apart

failpass·tests/test_url.py::test_returns_true_on_valid_url[https://www.foo.com/bar#/baz/test]

Check file tests/test_url.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it71b40bd54ecf88de792b15acc831801903cc6bc0
Broken version dated2023-09-02
Modulevalidators.url
Units changed_validate_optionals, url
Fingerprint44f504539f1a9e4a
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in python-validators/validators