]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
detect various bad ttls
authorBob Halley <halley@dnspython.org>
Tue, 11 Aug 2020 01:12:29 +0000 (18:12 -0700)
committerBob Halley <halley@dnspython.org>
Tue, 11 Aug 2020 01:13:31 +0000 (18:13 -0700)
dns/ttl.py
tests/test_ttl.py

index 6c67d3859c4c731cfea77cdfecf0ae54f81ec59b..1a2aaeb73fb19d578802aa8fd328772952fafcab 100644 (file)
@@ -39,16 +39,20 @@ def from_text(text):
 
     if text.isdigit():
         total = int(text)
+    elif len(text) == 0:
+        raise BadTTL
     else:
-        if not text[0].isdigit():
-            raise BadTTL
         total = 0
         current = 0
+        need_digit = True
         for c in text:
             if c.isdigit():
                 current *= 10
                 current += int(c)
+                need_digit = False
             else:
+                if need_digit:
+                    raise BadTTL
                 c = c.lower()
                 if c == 'w':
                     total += current * 604800
@@ -63,6 +67,7 @@ def from_text(text):
                 else:
                     raise BadTTL("unknown unit '%s'" % c)
                 current = 0
+                need_digit = True
         if not current == 0:
             raise BadTTL("trailing integer")
     if total < 0 or total > MAX_TTL:
index 07c512b1be91e7b40c1cba1e610b3315b5a4a47b..2bf298ef28197a96d32d63f51f7d3581ca4121f0 100644 (file)
@@ -22,3 +22,15 @@ class TTLTestCase(unittest.TestCase):
     def test_bind_style_no_unit(self):
         with self.assertRaises(dns.ttl.BadTTL):
             dns.ttl.from_text('1d5')
+
+    def test_bind_style_leading_unit(self):
+        with self.assertRaises(dns.ttl.BadTTL):
+            dns.ttl.from_text('s')
+
+    def test_bind_style_unit_without_digits(self):
+        with self.assertRaises(dns.ttl.BadTTL):
+            dns.ttl.from_text('1mw')
+
+    def test_empty(self):
+        with self.assertRaises(dns.ttl.BadTTL):
+            dns.ttl.from_text('')