From 1440ee9cebe2599239ed910b3f19cba6f79ff92c Mon Sep 17 00:00:00 2001 From: Tom Usher Date: Wed, 7 Dec 2022 16:25:10 +0000 Subject: Support deeper subdomains in domain validation (#110) Use a new validator class with regex based on the URLValidator from Django--- tests/users/views/test_domains.py | 40 +++++++++++++++++++++++++++++++++++++++ users/views/admin/domains.py | 34 +++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 tests/users/views/test_domains.py diff --git a/tests/users/views/test_domains.py b/tests/users/views/test_domains.py new file mode 100644 index 0000000..13d4b11 --- /dev/null +++ b/tests/users/views/test_domains.py @@ -0,0 +1,40 @@ +import pytest +from django.core.exceptions import ValidationError + +from users.views.admin.domains import DomainValidator + +VALID_DOMAINS = [ + "takahe.social", + "subdomain.takahe.social", + "another.subdomain.takahe.social", + "jointakahe.org", + "xn--c6h.com", + "takahe.xn--social", + "example.com", + "www.example.com", + "example.co.uk", +] + +INVALID_DOMAINS = [ + "example.c", + "example,com", + "example,com.com", + "example", + ".com", + "example.com/example", + "-example.com", + "example-.com", + "example.com-", + "https://example.com", +] + + +@pytest.mark.parametrize("domain", VALID_DOMAINS) +def test_domain_validation_accepts_valid_domains(domain): + DomainValidator()(domain) + + +@pytest.mark.parametrize("domain", INVALID_DOMAINS) +def test_domain_validation_raises_exception_for_invalid_domains(domain): + with pytest.raises(ValidationError): + DomainValidator()(domain) diff --git a/users/views/admin/domains.py b/users/views/admin/domains.py index c42137c..ed4d184 100644 --- a/users/views/admin/domains.py +++ b/users/views/admin/domains.py @@ -1,6 +1,5 @@ -import re - from django import forms +from django.core.validators import RegexValidator from django.db import models from django.shortcuts import get_object_or_404, redirect from django.utils.decorators import method_decorator @@ -10,6 +9,27 @@ from users.decorators import admin_required from users.models import Domain +class DomainValidator(RegexValidator): + ul = "\u00a1-\uffff" # Unicode letters range (must not be a raw string). + + # Host patterns + 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}(?