fix: don't cache DNS responses with CD flag

This commit is contained in:
Dimitri Herzog 2023-09-20 22:36:45 +02:00
parent 6f60bea5c2
commit 431b9be3e5
2 changed files with 40 additions and 1 deletions

View File

@ -229,6 +229,11 @@ func removeEdns0Extra(msg *dns.Msg) {
}
}
func shouldBeCached(msg *dns.Msg) bool {
// we don't cache truncated responses and responses with CD flag
return !msg.Truncated && !msg.CheckingDisabled
}
func (r *CachingResolver) putInCache(cacheKey string, response *model.Response, ttl time.Duration,
prefetch, publish bool,
) {
@ -237,7 +242,7 @@ func (r *CachingResolver) putInCache(cacheKey string, response *model.Response,
// don't cache any EDNS OPT records
removeEdns0Extra(respCopy)
if response.Res.Rcode == dns.RcodeSuccess && !response.Res.Truncated {
if response.Res.Rcode == dns.RcodeSuccess && shouldBeCached(response.Res) {
// put value into cache
r.resultCache.Put(cacheKey, &cacheValue{respCopy, prefetch}, ttl)
} else if response.Res.Rcode == dns.RcodeNameError {

View File

@ -565,6 +565,40 @@ var _ = Describe("CachingResolver", func() {
})
})
Describe("Responses with CD flag should not be cached", func() {
When("Some query returns response with CD flag", func() {
BeforeEach(func() {
mockAnswer, _ = util.NewMsgWithAnswer("google.de.", 180, A, "1.1.1.1")
mockAnswer.CheckingDisabled = true
})
It("Should not be cached", func() {
By("first request", func() {
Expect(sut.Resolve(newRequest("google.de.", A))).
Should(SatisfyAll(
HaveResponseType(ResponseTypeRESOLVED),
HaveReturnCode(dns.RcodeSuccess),
BeDNSRecord("google.de.", A, "1.1.1.1"),
HaveTTL(BeNumerically("==", 180)),
))
Expect(m.Calls).Should(HaveLen(1))
})
By("second request", func() {
Expect(sut.Resolve(newRequest("google.de.", A))).
Should(SatisfyAll(
HaveResponseType(ResponseTypeRESOLVED),
HaveReturnCode(dns.RcodeSuccess),
BeDNSRecord("google.de.", A, "1.1.1.1"),
HaveTTL(BeNumerically("==", 180)),
))
Expect(m.Calls).Should(HaveLen(2))
})
})
})
})
Describe("EDNS pseudo records should not be cached", func() {
When("Some query returns EDNS OPT RRs", func() {
BeforeEach(func() {