From: Bob Halley Date: Tue, 11 Aug 2020 01:12:29 +0000 (-0700) Subject: detect various bad ttls X-Git-Tag: v2.1.0rc1~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8a032fa375555b83d31df77191dd66e669421ab7;p=thirdparty%2Fdnspython.git detect various bad ttls --- 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('')