Skip to content

Commit

Permalink
Fixed #35845 -- Updated DomainNameValidator to require entire string …
Browse files Browse the repository at this point in the history
…to be a valid domain name.

Bug in 4971a9a.

Thank you to kazet for the report and Claude Paroz for the review.
  • Loading branch information
justin-thurman authored and sarahboyce committed Oct 17, 2024
1 parent 65f3cfc commit 99dcc59
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 6 additions & 3 deletions django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ def __init__(self, **kwargs):

if self.accept_idna:
self.regex = _lazy_re_compile(
self.hostname_re + self.domain_re + self.tld_re, re.IGNORECASE
r"^" + self.hostname_re + self.domain_re + self.tld_re + r"$",
re.IGNORECASE,
)
else:
self.regex = _lazy_re_compile(
self.ascii_only_hostname_re
r"^"
+ self.ascii_only_hostname_re
+ self.ascii_only_domain_re
+ self.ascii_only_tld_re,
+ self.ascii_only_tld_re
+ r"$",
re.IGNORECASE,
)
super().__init__(**kwargs)
Expand Down
5 changes: 4 additions & 1 deletion docs/releases/5.1.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ Django 5.1.3 fixes several bugs in 5.1.2 and adds compatibility with Python
Bugfixes
========

* ...
* Fixed a bug in Django 5.1 where
:class:`~django.core.validators.DomainNameValidator` accepted any input value
that contained a valid domain name, rather than only input values that were a
valid domain name (:ticket:`35845`).
14 changes: 12 additions & 2 deletions tests/validators/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@
(validate_domain_name, "python-python.com", None),
(validate_domain_name, "python.name.uk", None),
(validate_domain_name, "python.tips", None),
(validate_domain_name, "http://例子.测试", None),
(validate_domain_name, "http://dashinpunytld.xn---c", None),
(validate_domain_name, "例子.测试", None),
(validate_domain_name, "dashinpunytld.xn---c", None),
(validate_domain_name, "python..org", ValidationError),
(validate_domain_name, "python-.org", ValidationError),
(validate_domain_name, "too-long-name." * 20 + "com", ValidationError),
Expand All @@ -652,6 +652,16 @@
),
(DomainNameValidator(accept_idna=False), "ıçğü.com", ValidationError),
(DomainNameValidator(accept_idna=False), "not-domain-name", ValidationError),
(
DomainNameValidator(accept_idna=False),
"not-domain-name, but-has-domain-name-suffix.com",
ValidationError,
),
(
DomainNameValidator(accept_idna=False),
"not-domain-name.com, but has domain prefix",
ValidationError,
),
]

# Add valid and invalid URL tests.
Expand Down

0 comments on commit 99dcc59

Please sign in to comment.