Skip to content

Domains: more robust form #7523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def clean_project(self):
return self.project

def clean_domain(self):
domain = self.cleaned_data['domain']
domain = self.cleaned_data['domain'].lower()
parsed = urlparse(domain)

# Force the scheme to have a valid netloc.
Expand Down
36 changes: 4 additions & 32 deletions readthedocs/projects/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Validators for projects app."""

import re
Expand All @@ -12,39 +10,13 @@
from django.utils.translation import ugettext_lazy as _


domain_regex = (
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}(?<!-)\.?)|'
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
)


@deconstructible
class DomainNameValidator(RegexValidator):
message = _('Enter a valid plain or internationalized domain name value')
regex = re.compile(domain_regex, re.IGNORECASE)

def __init__(self, accept_idna=True, **kwargs):
message = kwargs.get('message')
self.accept_idna = accept_idna
super().__init__(**kwargs)
if not self.accept_idna and message is None:
self.message = _('Enter a valid domain name value')

def __call__(self, value):
try:
super().__call__(value)
except ValidationError as exc:
if not self.accept_idna:
raise
if not value:
raise
try:
idnavalue = value.encode('idna')
except UnicodeError:
raise exc
super().__call__(idnavalue)
# Based on the domain name pattern from https://api.cloudflare.com/#zone-list-zones.
regex = re.compile(
r'^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9-]{2,20}$'
)


validate_domain_name = DomainNameValidator()
Expand Down
29 changes: 25 additions & 4 deletions readthedocs/rtd_tests/tests/test_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,34 @@ def test_domain_with_path(self):
domain = form.save()
self.assertEqual(domain.domain, 'domain.com')

def test_valid_domains(self):
domains = [
'python.org',
'a.io',
'a.e.i.o.org',
'my.domain.com.edu',
'my-domain.fav',
]
for domain in domains:
form = DomainForm(
{'domain': domain},
project=self.project,
)
self.assertTrue(form.is_valid(), domain)

def test_invalid_domains(self):
invalid = [
domains = [
'python..org',
# FIXME: '****.foo.com', current validator says this is valid :shrug:
'invalid-.com'
'****.foo.com',
'domain',
'domain.com.',
'My domain.org',
'i.o',
'[special].com',
'some_thing.org',
'invalid-.com',
]
for domain in invalid:
for domain in domains:
form = DomainForm(
{'domain': domain},
project=self.project,
Expand Down