#25637: Add label and hostname length validation in URLValidator
--------------------------------------+------------------------------------
Reporter: DheerendraRathor | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by raphaelmerx):
The label limit of 64 characters can be done like so:
{{{#!diff
diff --git a/django/core/validators.py b/django/core/validators.py
index 15b16bc..26ec822 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -83,7 +83,7 @@ class URLValidator(RegexValidator):
ipv6_re = r'\[[0-9a-f:\.]+\]' # (simple regex, validated later)
# Host patterns
- hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]*[a-z' + ul
+ r'0-9])?'
+ hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]{0,61}[a-z'
+ ul + r'0-9])?'
domain_re = r'(?:\.(?!-)[a-z' + ul + r'0-9-]+(?<!-))*'
tld_re = r'\.(?:[a-z' + ul + r']{2,}|xn--[a-z0-9]+)\.?'
host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
diff --git a/tests/validators/tests.py b/tests/validators/tests.py
index ad82eb6..52002fe 100644
--- a/tests/validators/tests.py
+++ b/tests/validators/tests.py
@@ -214,6 +214,8 @@ TEST_DATA = [
# Trailing junk does not take forever to reject
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br ',
ValidationError),
(URLValidator(), 'http://www.asdasdasdasdsadfm.com.br z',
ValidationError),
+ # hostname label with length >= 64
+ (URLValidator(),
'http://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.a.com',
ValidationError),
(BaseValidator(True), True, None),
(BaseValidator(True), False, ValidationError),
}}}
The total length limit seems harder to implement.
--
Ticket URL: <https://code.djangoproject.com/ticket/25637#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/074.ad25e6118b86f4087c992ddf5ac5a8a9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.