Skip to content

Commit e0fce7f

Browse files
nab-iha3rthalley
authored andcommitted
use a short negative TTL for a negative answer with no SOA in resolve_chaining (#1390)
* do not cache a negative answer that has no SOA in resolve_chaining * use a 300 second negative TTL for a negative answer with no SOA (cherry picked from commit 97abe89)
1 parent ad0803c commit e0fce7f

4 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎dns/message.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def __init__(self, exception: Exception, offset: int):
131131
DEFAULT_EDNS_PAYLOAD = dns.renderer.DEFAULT_EDNS_PAYLOAD
132132
MAX_CHAIN = 16
133133

134+
#: The negative caching TTL used by ``resolve_chaining()`` for a negative
135+
#: answer whose authority section has no SOA record.
136+
NEGATIVE_TTL_WITHOUT_SOA = 300
137+
134138
IndexKeyType = tuple[
135139
int,
136140
dns.name.Name,
@@ -984,7 +988,9 @@ class ChainingResult:
984988
The ``minimum_ttl`` attribute is the minimum TTL, i.e. the TTL to
985989
use if caching the data. It is the smallest of all the CNAME TTLs
986990
and either the answer TTL if it exists or the SOA TTL and SOA
987-
minimum values for negative answers.
991+
minimum values for negative answers. For a negative answer with no
992+
SOA record, ``NEGATIVE_TTL_WITHOUT_SOA`` is used in place of the SOA
993+
values.
988994
989995
The ``cnames`` attribute is a list of all the CNAME RRSets followed to
990996
get to the canonical name.
@@ -1072,6 +1078,10 @@ def resolve_chaining(self) -> ChainingResult:
10721078
try:
10731079
auname = auname.parent()
10741080
except dns.name.NoParent:
1081+
# There is no SOA, so there is no negative TTL.
1082+
# Like many other implementations, use a short
1083+
# default negative caching TTL.
1084+
min_ttl = min(min_ttl, NEGATIVE_TTL_WITHOUT_SOA)
10751085
break
10761086
return ChainingResult(qname, answer, min_ttl, cnames)
10771087

‎doc/whatsnew.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ What's New in dnspython
66
2.9.1 (in development)
77
----------------------
88

9+
* ``dns.message.QueryMessage.resolve_chaining()`` now caps the minimum TTL of
10+
a negative response with no SOA record at 300 seconds
11+
(``dns.message.NEGATIVE_TTL_WITHOUT_SOA``). Previously the TTL was left at
12+
its initial value of 2^32 - 1, so a single NXDOMAIN or NODATA response with
13+
no SOA, e.g. a spoofed one, stayed in the resolver cache for the life of
14+
the process.
15+
916
* DNSSEC validation now rejects an RRSIG whose type covered or class does not
1017
match the RRset, as RFC 4035 section 5.3.1 requires. This was not checked,
1118
so a revoked DNSKEY, which may only validate the DNSKEY RRset, still

‎tests/test_message.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,43 @@ def test_resolve_chaining_no_infinite_loop(self):
764764
self.assertEqual(result.minimum_ttl, 5)
765765
self.assertIsNone(result.answer)
766766

767+
def test_resolve_chaining_negative_without_soa(self):
768+
# With no SOA there is no negative TTL, so a short default is used
769+
# instead of the initial maximum.
770+
r = dns.message.from_text("""id 1
771+
flags QR
772+
rcode NXDOMAIN
773+
;QUESTION
774+
www.example. IN A
775+
""")
776+
result = r.resolve_chaining()
777+
self.assertIsNone(result.answer)
778+
self.assertEqual(result.minimum_ttl, dns.message.NEGATIVE_TTL_WITHOUT_SOA)
779+
self.assertEqual(result.minimum_ttl, 300)
780+
# NODATA after a CNAME, with no SOA for the canonical name.
781+
r = dns.message.from_text("""id 1
782+
flags QR
783+
;QUESTION
784+
www.example. IN A
785+
;ANSWER
786+
www.example. 600 IN CNAME www2.example.
787+
""")
788+
result = r.resolve_chaining()
789+
self.assertIsNone(result.answer)
790+
self.assertEqual(result.canonical_name, dns.name.from_text("www2.example."))
791+
self.assertEqual(result.minimum_ttl, 300)
792+
# A smaller CNAME TTL still wins.
793+
r = dns.message.from_text("""id 1
794+
flags QR
795+
;QUESTION
796+
www.example. IN A
797+
;ANSWER
798+
www.example. 60 IN CNAME www2.example.
799+
""")
800+
result = r.resolve_chaining()
801+
self.assertIsNone(result.answer)
802+
self.assertEqual(result.minimum_ttl, 60)
803+
767804
def test_bad_text_questions(self):
768805
with self.assertRaises(dns.exception.SyntaxError):
769806
dns.message.from_text("""id 1

‎tests/test_resolver.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,28 @@ def testCacheExpiration(self):
305305
cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None
306306
)
307307

308+
def testCacheNegativeWithoutSOA(self):
309+
# A negative answer with no SOA is cached for the default negative
310+
# TTL, not for the initial maximum TTL.
311+
with FakeTime() as fake_time:
312+
message = dns.message.from_text("""id 1
313+
flags QR RA
314+
rcode NXDOMAIN
315+
;QUESTION
316+
www.example. IN A
317+
""")
318+
name = dns.name.from_text("www.example.")
319+
key = (name, dns.rdatatype.ANY, dns.rdataclass.IN)
320+
answer = dns.resolver.Answer(
321+
name, dns.rdatatype.ANY, dns.rdataclass.IN, message
322+
)
323+
self.assertEqual(answer.expiration, fake_time.time() + 300)
324+
cache = dns.resolver.Cache()
325+
cache.put(key, answer)
326+
self.assertIs(cache.get(key), answer)
327+
fake_time.sleep(301)
328+
self.assertIsNone(cache.get(key))
329+
308330
def testCacheCleaning(self):
309331
with FakeTime() as fake_time:
310332
message = dns.message.from_text(message_text)
@@ -1103,6 +1125,19 @@ def testE164Query(self):
11031125
with self.assertRaises(dns.resolver.NXDOMAIN):
11041126
dns.e164.query("0123456789", ["e164.arpa"], res)
11051127

1128+
def testNXDOMAINWithoutSOACachedBriefly(self):
1129+
with AlwaysType3NXDOMAINNanoNameserver() as na:
1130+
res = dns.resolver.Resolver(configure=False)
1131+
res.port = na.udp_address[1]
1132+
res.nameservers = [na.udp_address[0]]
1133+
res.cache = dns.resolver.Cache()
1134+
qname = dns.name.from_text("www.example.")
1135+
with self.assertRaises(dns.resolver.NXDOMAIN):
1136+
res.resolve(qname, "A")
1137+
answer = res.cache.get((qname, dns.rdatatype.ANY, dns.rdataclass.IN))
1138+
self.assertIsNotNone(answer)
1139+
self.assertLessEqual(answer.expiration, time.time() + 300)
1140+
11061141

11071142
class AlwaysType3NXDOMAINNanoNameserver(Server):
11081143
def handle(self, request):

0 commit comments

Comments
 (0)