mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-28 08:03:36 +00:00
Updated domain name validation and unit tests
This commit is contained in:
parent
0236bfa9a6
commit
e7fb549349
@ -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():
|
||||
|
||||
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user