summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorRaphael Michel <mail@raphaelmichel.de>2015-11-07 11:08:16 +0100
committerTim Graham <timograham@gmail.com>2015-12-08 15:46:45 -0500
commit82976e5c3f7abf20dfd4c3cc5aa586e57edef104 (patch)
tree89ab4bbf7b042bbbcb3dcfe24b4d370168edab42 /django
parentd7a58f285b4c0c1b4ec032f9a7bf8f31913d0f5f (diff)
Fixed #25637 -- Added URLValidator hostname length validation.
URLValidator now validates the maximum length of a hostname and the maximum length of all labels inside the hostname.
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.'),