summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormendespedro <windowsxpedro@gmail.com>2021-12-15 11:55:19 -0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-12-20 07:30:22 +0100
commite8b4feddc34ffe5759ec21da8fa027e86e653f1c (patch)
treee390fb07dad9bbe7a7d915c81b6c04ce7aeaf142
parent4fd3044ca0135da903a70dfb66992293f529ecf1 (diff)
Fixed #33367 -- Fixed URLValidator crash in some edge cases.
-rw-r--r--django/core/validators.py13
-rw-r--r--tests/forms_tests/field_tests/test_urlfield.py4
2 files changed, 11 insertions, 6 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index 72fbe15551..eec7803714 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -109,14 +109,15 @@ class URLValidator(RegexValidator):
# Then check full URL
try:
+ splitted_url = urlsplit(value)
+ except ValueError:
+ raise ValidationError(self.message, code=self.code, params={'value': value})
+ try:
super().__call__(value)
except ValidationError as e:
# Trivial case failed. Try for possible IDN domain
if value:
- try:
- scheme, netloc, path, query, fragment = urlsplit(value)
- except ValueError: # for example, "Invalid IPv6 URL"
- raise ValidationError(self.message, code=self.code, params={'value': value})
+ scheme, netloc, path, query, fragment = splitted_url
try:
netloc = punycode(netloc) # IDN -> ACE
except UnicodeError: # invalid domain part
@@ -127,7 +128,7 @@ class URLValidator(RegexValidator):
raise
else:
# Now verify IPv6 in the netloc part
- host_match = re.search(r'^\[(.+)\](?::\d{1,5})?$', urlsplit(value).netloc)
+ host_match = re.search(r'^\[(.+)\](?::\d{1,5})?$', splitted_url.netloc)
if host_match:
potential_ip = host_match[1]
try:
@@ -139,7 +140,7 @@ class URLValidator(RegexValidator):
# section 3.1. It's defined to be 255 bytes or less, but this includes
# one byte for the length of the name and one byte for the trailing dot
# that's used to indicate absolute names in DNS.
- if len(urlsplit(value).hostname) > 253:
+ if splitted_url.hostname is None or len(splitted_url.hostname) > 253:
raise ValidationError(self.message, code=self.code, params={'value': value})
diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py
index 152456f216..187d44a050 100644
--- a/tests/forms_tests/field_tests/test_urlfield.py
+++ b/tests/forms_tests/field_tests/test_urlfield.py
@@ -100,6 +100,10 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
# even on domains that don't fail the domain label length check in
# the regex.
'http://%s' % ("X" * 200,),
+ # urlsplit() raises ValueError.
+ '////]@N.AN',
+ # Empty hostname.
+ '#@A.bO',
]
msg = "'Enter a valid URL.'"
for value in tests: