]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: datamodel: types: improved DomainName regex
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 18 Mar 2022 12:07:13 +0000 (13:07 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:30:44 +0000 (16:30 +0200)
manager/knot_resolver_manager/datamodel/types/types.py
manager/tests/unit/datamodel/types/test_custom_types.py

index 69cae176562687e0aa50f4bb72cef6296245e524..1d2806c4c41b6efb943cc7d4f1c1f277609c1a6b 100644 (file)
@@ -56,11 +56,14 @@ class TimeUnit(UnitBase):
 
 
 class DomainName(PatternBase):
+    _spec_chars = "ßàÁâãóôþüúðæåïçèõöÿýòäœêëìíøùîûñé"
     _re = re.compile(
-        r"^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|"
-        r"([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|"
-        r"([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\."
-        r"([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})($|.$)"
+        # max 253 chars
+        r"(?=^.{,253}$)"
+        # do not start/end with dash; 1-63 chars in name; allow special chars; max 126 levels+TLD
+        rf"^((?!-)([{_spec_chars}]|[a-zA-Z0-9-]){{1,62}}[a-zA-Z0-9]\.){{0,126}}"
+        # TLD
+        r"[a-zA-Z]{2,6}($|.$)"
     )
 
 
index 2aabd6187c82c701acf730f0bfd0792edb1d4ed6..2a0ec429c87254c46b17f397daf286dbb3f8f85f 100644 (file)
@@ -71,7 +71,7 @@ def test_parsing_units():
 
     o = TestSchema({"size": "3K", "time": "10m"})
     assert o.size == SizeUnit("3072B")
-    assert o.time == TimeUnit("10m")
+    assert o.time == TimeUnit("600s")
     assert o.size.bytes() == 3072
     assert o.time.seconds() == 10 * 60
 
@@ -83,20 +83,17 @@ def test_checked_path():
     assert str(TestSchema({"p": "/tmp"}).p) == "/tmp"
 
 
-def test_domain_name():
-    class TestSchema(SchemaNode):
-        name: DomainName
-
-    o = TestSchema({"name": "test.domain.com."})
-    assert str(o.name) == "test.domain.com."
-    assert o.name == DomainName("test.domain.com.")
+@pytest.mark.parametrize("val", ["example.com.", "test.example.com", "test-example.com"])
+def test_domain_name_valid(val: str):
+    o = DomainName(val)
+    assert str(o) == val
+    assert o == DomainName(val)
 
-    o = TestSchema({"name": "test.domain.com"})
-    assert str(o.name) == "test.domain.com"
-    assert o.name == DomainName("test.domain.com")
 
+@pytest.mark.parametrize("val", ["test.example.com..", "-example.com", "test-.example.net"])
+def test_domain_name_invalid(val: str):
     with raises(KresManagerException):
-        TestSchema({"name": "b@d.domain.com."})
+        DomainName(val)
 
 
 @pytest.mark.parametrize("val", ["lo", "eth0", "wlo1", "web_ifgrp", "e8-2"])
@@ -185,12 +182,7 @@ def test_ipv4_address_invalid(val: Any):
         IPv4Address(val)
 
 
-@pytest.mark.parametrize(
-    "val",
-    [
-        "2001:db8::1000",
-    ],
-)
+@pytest.mark.parametrize("val", ["2001:db8::1000", "2001:db8:85a3::8a2e:370:7334"])
 def test_ipv6_address_valid(val: str):
     o = IPv6Address(val)
     assert str(o) == val
@@ -214,7 +206,6 @@ def test_ip_network_valid(val: str):
 @pytest.mark.parametrize("val", ["10.11.12.13/8", "10.11.12.5/128"])
 def test_ip_network_invalid(val: str):
     with raises(KresManagerException):
-        # because only the prefix can have non-zero bits
         IPNetwork(val)