summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/core/validators.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index 15b16bcd2f..8c43644ed6 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -83,9 +83,10 @@ 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])?'
- domain_re = r'(?:\.(?!-)[a-z' + ul + r'0-9-]+(?<!-))*'
- tld_re = r'\.(?:[a-z' + ul + r']{2,}|xn--[a-z0-9]+)\.?'
+ hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]{0,61}[a-z' + ul + r'0-9])?'
+ # 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}(?<!-))*'
+ tld_re = r'\.(?:[a-z' + ul + r']{2,63}|xn--[a-z0-9]{1,59})\.?'
host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
regex = _lazy_re_compile(
@@ -136,6 +137,13 @@ class URLValidator(RegexValidator):
raise ValidationError(self.message, code=self.code)
url = value
+ # The maximum length of a full host name is 253 characters per RFC 1034
+ # 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:
+ raise ValidationError(self.message, code=self.code)
+
integer_validator = RegexValidator(
_lazy_re_compile('^-?\d+\Z'),
message=_('Enter a valid integer.'),