From e7fb549349b6ca4eaedff92efac3f0155cd11e0a Mon Sep 17 00:00:00 2001 From: Joel Valleroy Date: Sun, 15 Nov 2015 18:57:15 -0500 Subject: [PATCH] Updated domain name validation and unit tests --- plinth/modules/config/config.py | 16 +++++++++++++--- plinth/modules/config/tests/test_config.py | 7 +++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/plinth/modules/config/config.py b/plinth/modules/config/config.py index fefc02d3e..51ce593be 100644 --- a/plinth/modules/config/config.py +++ b/plinth/modules/config/config.py @@ -22,9 +22,11 @@ Plinth module for configuring hostname and domainname. from django import forms from django.contrib import messages from django.core import validators +from django.core.exceptions import ValidationError from django.template.response import TemplateResponse from django.utils.translation import ugettext as _, ugettext_lazy import logging +import re import socket from plinth import actions @@ -33,6 +35,8 @@ from plinth.signals import pre_hostname_change, post_hostname_change from plinth.signals import domainname_change +HOSTNAME_REGEX = r'^[a-zA-Z0-9]([-a-zA-Z0-9]{,61}[a-zA-Z0-9])?$' + LOGGER = logging.getLogger(__name__) @@ -56,6 +60,11 @@ class TrimmedCharField(forms.CharField): return super(TrimmedCharField, self).clean(value) +def domain_labelvalidator(domainname): + """Validating Domain Name Labels""" + for label in domainname.split("."): + if not re.match(HOSTNAME_REGEX, label): + raise ValidationError(ugettext_lazy('Invalid Domain Name')) class ConfigurationForm(forms.Form): """Main system configuration form""" @@ -74,7 +83,7 @@ class ConfigurationForm(forms.Form): 'hyphens. Total length must be 63 characters or less.'), validators=[ validators.RegexValidator( - r'^[a-zA-Z0-9]([-a-zA-Z0-9]{,61}[a-zA-Z0-9])?$', + HOSTNAME_REGEX, ugettext_lazy('Invalid hostname'))]) domainname = TrimmedCharField( @@ -90,8 +99,9 @@ class ConfigurationForm(forms.Form): required=False, validators=[ validators.RegexValidator( - r'^[a-zA-Z0-9]([-a-zA-Z0-9.]*[a-zA-Z0-9])?$', - ugettext_lazy('Invalid domain name'))]) + r'^[a-zA-Z0-9]([-a-zA-Z0-9.]{,251}[a-zA-Z0-9])?$', + ugettext_lazy('Invalid domain name')), + domain_labelvalidator]) def init(): diff --git a/plinth/modules/config/tests/test_config.py b/plinth/modules/config/tests/test_config.py index 0e1dfaa55..d22b83fff 100644 --- a/plinth/modules/config/tests/test_config.py +++ b/plinth/modules/config/tests/test_config.py @@ -47,12 +47,15 @@ class TestConfig(unittest.TestCase): def test_domainname_field(self): """Test that domainname field accepts only valid domainnames.""" + valid_domainnames = [ '', 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', 'example', 'example.org', 'a.b.c.d', 'a-0.b-0.c-0', - '012345678901234567890123456789012345678901234567890123456789012'] + '012345678901234567890123456789012345678901234567890123456789012', + ((('x' * 63) + '.') * 3) + 'x' * 61] invalid_domainnames = [ - '-', '-a', 'a-', '.a', 'a.', '?', 'a?a'] + '-', '-a', 'a-', '.a', 'a.', '?', 'a?a', 'a..a', 'a.-a', '.', + ((('x' * 63) + '.') * 3) + 'x' * 62, 'x' * 64] for domainname in valid_domainnames: form = ConfigurationForm({'hostname': 'example',