plugin/pkg/response: classify nxdomain without soa as denial (#8199)

Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>
This commit is contained in:
houyuwushang
2026-07-06 13:48:51 +08:00
committed by GitHub
parent 31559f43e8
commit 6662dd58ea
5 changed files with 93 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ import (
"strings"
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/replacer"
@@ -186,6 +187,59 @@ func TestLoggedSynthesizesDeferredRefused(t *testing.T) {
}
}
func TestLoggedClassNameErrorWithoutSOA(t *testing.T) {
tests := []struct {
name string
class response.Class
shouldLog bool
}{
{name: "error", class: response.Error, shouldLog: false},
{name: "denial", class: response.Denial, shouldLog: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rule := Rule{
NameScope: ".",
Format: DefaultLogFormat,
Class: map[response.Class]struct{}{tc.class: {}},
}
var f bytes.Buffer
log.SetOutput(&f)
logger := Logger{
Rules: []Rule{rule},
Next: plugin.HandlerFunc(func(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeNameError)
return dns.RcodeNameError, w.WriteMsg(m)
}),
repl: replacer.New(),
}
ctx := context.TODO()
r := new(dns.Msg)
r.SetQuestion("1.1.168.192.in-addr.arpa.", dns.TypePTR)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := logger.ServeDNS(ctx, rec, r)
if err != nil {
t.Fatal(err)
}
logged := f.String()
if !tc.shouldLog && len(logged) != 0 {
t.Errorf("Expected it not to be logged, but got string: %s", logged)
}
if tc.shouldLog && !strings.Contains(logged, "NXDOMAIN") {
t.Errorf("Expected it to be logged as NXDOMAIN. Logged string: %s", logged)
}
})
}
}
func TestLogged(t *testing.T) {
tests := []struct {
Rules []Rule