Coalesce identical misses, refuse non-QUERY opcodes, refresh hot entries early, check the answered question

Concurrent cache misses for one name each went upstream. After a restart, a
cache flush or a cluster-object change that is a burst of identical queries
exactly when the cache is empty. Identical exchanges (same name, type, class,
DO bit, pinned upstream set and forwarded client subnet) now share one: the
first query runs it on its own goroutine and context, the rest wait and get
private copies, and the leader alone writes the cache. If the leader's client
leaves, waiters take over instead of failing with its cancellation. Shared
answers are counted in fancydns_upstream_coalesced_total.

validate() accepted OpcodeUpdate, so a dynamic update was forwarded upstream as
an ordinary lookup, and every other opcode got FORMERR. A recursive resolver
implements QUERY only: anything else is now NOTIMP (RFC 1035 4.1.1), checked
before the question count since STATUS carries no question.

Prefetch only fired when a stale entry was served, so the first client after a
hot name expired got a TTL-1 stale answer. A fresh entry is now refreshed in
the background when it has been served at least twice and under a tenth of its
lifetime remains; the hit counter restarts with each stored entry, so a name
nobody comes back for stops being refreshed. Counted in
fancydns_cache_prefetch_total.

miekg/dns matches only the message ID of an upstream reply, so a forged reply
with the right ID for a different question was accepted and cached under the
name that was asked. exchangeOne now requires the reply to carry the question
that was sent (an empty section only for FORMERR, SERVFAIL, NOTIMP and
REFUSED). A mismatch fails the attempt without counting against the upstream,
so a spoofer cannot trip the circuit breaker.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
matsandClaude Sonnet 5.5 committed 2026-10-16 08:58:56 +02:00
1 parent 1a536e7d0e
commit dd1117fd91
8 files changed
+498 -9

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
package resolver
import (
"errors"
"testing"
"time"
"github.com/miekg/dns"
)
// TestAnswerForAnotherQuestionIsRejected pins the check the DNS library does
// not make: it matches the message ID and nothing else, so a reply with the
// right ID for a different question would otherwise be cached under the name
// that was asked.
func TestAnswerForAnotherQuestionIsRejected(t *testing.T) {
spoof := newFakeUpstream(t, fakeOpt{wrongQuestion: true})
p := NewPool(poolCfg(udpServer(spoof.addr)))
for i := 0; i < 5; i++ { // more than the breaker threshold
resp, _, err := p.Exchange(testCtx(t), query("www.example.test"), nil, time.Second)
if !errors.Is(err, errQuestionMismatch) || resp != nil {
t.Fatalf("attempt %d: resp=%v err=%v, want the mismatch error", i, resp, err)
}
}
// A forged reply must not be able to take the upstream out of rotation.
if up := p.All()[0]; up.BreakerOpen() {
t.Fatal("spoofed replies opened the circuit breaker")
}
}
func TestAnswersQuestion(t *testing.T) {
q := query("WWW.Example.Test")
reply := func(name string, qtype uint16, rcode int, drop bool) *dns.Msg {
m := new(dns.Msg)
m.SetReply(q)
m.Question[0].Name, m.Question[0].Qtype = name, qtype
m.Rcode = rcode
if drop {
m.Question = nil
}
return m
}
for name, tc := range map[string]struct {
resp *dns.Msg
want bool
}{
"same": {reply("www.example.test.", dns.TypeA, 0, false), true},
"case differs": {reply("WWW.EXAMPLE.TEST.", dns.TypeA, 0, false), true},
"other name": {reply("evil.example.", dns.TypeA, 0, false), false},
"other type": {reply("www.example.test.", dns.TypeAAAA, 0, false), false},
"formerr, no question": {reply("", 0, dns.RcodeFormatError, true), true},
"refused, no question": {reply("", 0, dns.RcodeRefused, true), true},
"success, no question": {reply("", 0, dns.RcodeSuccess, true), false},
"nxdomain, no question": {reply("", 0, dns.RcodeNameError, true), false},
"nil": {nil, false},
} {
if got := answersQuestion(q, tc.resp); got != tc.want {
t.Errorf("%s: got %v, want %v", name, got, tc.want)
}
}
}