From 8a032fa375555b83d31df77191dd66e669421ab7 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Mon, 10 Aug 2020 18:12:29 -0700 Subject: [PATCH] detect various bad ttls --- dns/ttl.py | 9 +++++++-- tests/test_ttl.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/dns/ttl.py b/dns/ttl.py index 6c67d385..1a2aaeb7 100644 --- a/dns/ttl.py +++ b/dns/ttl.py @@ -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: diff --git a/tests/test_ttl.py b/tests/test_ttl.py index 07c512b1..2bf298ef 100644 --- a/tests/test_ttl.py +++ b/tests/test_ttl.py @@ -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('') -- 2.47.3