Skip to content

Commit

Permalink
fix: don't cache DNS responses with CD flag
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Sep 20, 2023
1 parent 6f60bea commit 431b9be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion resolver/caching_resolver.go
Expand Up @@ -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,
) {
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions resolver/caching_resolver_test.go
Expand Up @@ -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() {
Expand Down

0 comments on commit 431b9be

Please sign in to comment.