FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reject invalid percent-encoding in URL validator by uwezkhan · Pull Request #456 · python-validators/validators · GitHub

Repository navigation

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
32 changes: 17 additions & 15 deletions src/validators/url.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from .utils import validator


def _validate_percent_encoding(value: str):
"""Validate percent-encoding in a URL component."""
return not bool(re.search(r"%(?![0-9A-Fa-f]{2})", value))


@lru_cache
def _username_regex():
return re.compile(
Expand Down Expand Up @@ -138,25 +143,22 @@ def _validate_optionals(path: str, query: str, fragment: str, strict_query: bool
"""Validate path query and fragments."""
optional_segments = True
if path:
optional_segments &= bool(_path_regex().match(path))
try:
if (
query
# ref: https://github.com/python/cpython/issues/117109
and parse_qs(query, strict_parsing=strict_query, separator="&")
and parse_qs(query, strict_parsing=strict_query, separator=";")
):
optional_segments &= True
except TypeError:
# for Python < v3.9.2 (official v3.10)
if query and parse_qs(query, strict_parsing=strict_query):
optional_segments &= True
optional_segments &= bool(_path_regex().match(path)) and _validate_percent_encoding(path)
if query:
optional_segments &= _validate_percent_encoding(query)
if optional_segments:
try:
optional_segments &= bool(parse_qs(query, strict_parsing=strict_query, separator="&")) and bool(
parse_qs(query, strict_parsing=strict_query, separator=";")
)
except TypeError:
optional_segments &= bool(parse_qs(query, strict_parsing=strict_query))
if fragment:
# See RFC3986 Section 3.5 Fragment for allowed characters
# Adding "#", see https://github.com/python-validators/validators/issues/403
optional_segments &= bool(
re.fullmatch(r"[0-9a-z?/:@\-._~%!$&'()*+,;=#]*", fragment, re.IGNORECASE)
)
) and _validate_percent_encoding(fragment)
return optional_segments


Expand Down Expand Up @@ -254,4 +256,4 @@ def url(
rfc_2782,
)
and _validate_optionals(path, query, fragment, strict_query)
)
)

Back | FazBrowse Home | New Git URL