File urlvalidator.patch of Package python-Django.19101
From e8b4feddc34ffe5759ec21da8fa027e86e653f1c Mon Sep 17 00:00:00 2001
From: mendespedro <windowsxpedro@gmail.com>
Date: Wed, 15 Dec 2021 11:55:19 -0300
Subject: [PATCH] Fixed #33367 -- Fixed URLValidator crash in some edge cases.
---
django/core/validators.py | 13 +++++++------
tests/forms_tests/field_tests/test_urlfield.py | 4 ++++
2 files changed, 11 insertions(+), 6 deletions(-)
Index: Django-2.2.28/django/core/validators.py
===================================================================
--- Django-2.2.28.orig/django/core/validators.py
+++ Django-2.2.28/django/core/validators.py
@@ -121,14 +121,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)
+ scheme, netloc, path, query, fragment = splitted_url
try:
netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
except UnicodeError: # invalid domain part
@@ -139,7 +140,7 @@ class URLValidator(RegexValidator):
raise
else:
# Now verify IPv6 in the netloc part
- host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc)
+ host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', splitted_url.netloc)
if host_match:
potential_ip = host_match.groups()[0]
try:
@@ -151,7 +152,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).netloc) > 253:
+ if splitted_url.hostname is None or len(urlsplit(value).netloc) > 253:
raise ValidationError(self.message, code=self.code)
Index: Django-2.2.28/tests/forms_tests/field_tests/test_urlfield.py
===================================================================
--- Django-2.2.28.orig/tests/forms_tests/field_tests/test_urlfield.py
+++ Django-2.2.28/tests/forms_tests/field_tests/test_urlfield.py
@@ -69,6 +69,13 @@ class URLFieldTest(FormFieldAssertionsMi
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://%s' % ("X" * 60,))
+ # urlsplit() raises ValueError.
+ with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
+ f.clean('////]@N.AN')
+ # Empty hostname.
+ with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
+ f.clean('#@A.bO')
+
def test_urlfield_2(self):
f = URLField(required=False)
self.assertEqual('', f.clean(''))