1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

restructured the tld variable in the DomainValidator and modified the regex performance test to match the validation

This commit is contained in:
Chaitanya Rahalkar 2024-12-20 20:08:07 -06:00
parent 1ef8e66ed1
commit 1503a932a8
2 changed files with 6 additions and 9 deletions

View File

@ -75,14 +75,14 @@ class DomainNameValidator(RegexValidator):
# Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1. # Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1.
domain_re = r"(?:\.(?!-)[a-z" + ul + r"0-9-]{1,63}(?<!-))*" domain_re = r"(?:\.(?!-)[a-z" + ul + r"0-9-]{1,63}(?<!-))*"
# Top-level domain. # Top-level domain.
tld_re = ( tld_no_fqdn_re = (
r"\." # dot r"\." # dot
r"(?!-)" # can't start with a dash r"(?!-)" # can't start with a dash
r"(?:[a-z" + ul + "-]{2,63}" # domain label r"(?:[a-z" + ul + "-]{2,63}" # domain label
r"|xn--[a-z0-9]{1,59})" # or punycode label r"|xn--[a-z0-9]{1,59})" # or punycode label
r"(?<!-)" # can't end with a dash r"(?<!-)" # can't end with a dash
r"\.?" # may have a trailing dot
) )
tld_re = tld_no_fqdn_re + r"\.?"
ascii_only_hostname_re = r"[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?" ascii_only_hostname_re = r"[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?"
ascii_only_domain_re = r"(?:\.(?!-)[a-zA-Z0-9-]{1,63}(?<!-))*" ascii_only_domain_re = r"(?:\.(?!-)[a-zA-Z0-9-]{1,63}(?<!-))*"
ascii_only_tld_re = ( ascii_only_tld_re = (
@ -209,9 +209,9 @@ def validate_integer(value):
class EmailValidator: class EmailValidator:
message = _("Enter a valid email address.") message = _("Enter a valid email address.")
code = "invalid" code = "invalid"
host_re = DomainNameValidator.hostname_re hostname_re = DomainNameValidator.hostname_re
domain_re = DomainNameValidator.domain_re domain_re = DomainNameValidator.domain_re
ul = DomainNameValidator.ul tld_no_fqdn_re = DomainNameValidator.tld_no_fqdn_re
user_regex = _lazy_re_compile( user_regex = _lazy_re_compile(
# dot-atom # dot-atom
@ -222,10 +222,7 @@ class EmailValidator:
re.IGNORECASE, re.IGNORECASE,
) )
domain_regex = _lazy_re_compile( domain_regex = _lazy_re_compile(
# Negative look-ahead disallowing \r or \n r"^" + hostname_re + domain_re + tld_no_fqdn_re + r"\Z",
r"(?i)^(?!.*[\r\n])" r"(?:" + host_re + domain_re +
# Final TLD label:
r"\.(?!-)(?:[a-z" + ul + r"0-9-]{2,63}|xn--[a-z0-9]{1,59})(?<!-)" r")$",
re.IGNORECASE, re.IGNORECASE,
) )
literal_regex = _lazy_re_compile( literal_regex = _lazy_re_compile(

View File

@ -30,7 +30,7 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
f = EmailField() f = EmailField()
# Check for runaway regex security problem. This will take a long time # Check for runaway regex security problem. This will take a long time
# if the security fix isn't in place. # if the security fix isn't in place.
addr = "viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058" addr = "viewx3dtextx26qx3d@yahoo.comx26latlngx3d15854521645943074058.aa"
self.assertEqual(addr, f.clean(addr)) self.assertEqual(addr, f.clean(addr))
def test_emailfield_not_required(self): def test_emailfield_not_required(self):