Skip to content

Commit

Permalink
Fixed #33367 -- Fixed URLValidator crash in some edge cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-schlickmann authored and felixxm committed Dec 20, 2021
1 parent 4fd3044 commit e8b4fed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 7 additions & 6 deletions django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ def __call__(self, value):
raise ValidationError(self.message, code=self.code, params={'value': value})

# 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
Expand All @@ -127,7 +128,7 @@ def __call__(self, value):
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:
Expand All @@ -139,7 +140,7 @@ def __call__(self, value):
# 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})


Expand Down
4 changes: 4 additions & 0 deletions tests/forms_tests/field_tests/test_urlfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def test_urlfield_clean_invalid(self):
# 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:
Expand Down

0 comments on commit e8b4fed

Please sign in to comment.