plugin/rewrite: apply rcode rewrites to responses with no records (#8421)

* plugin/rewrite: apply rcode rewrites to record-less responses

An rcode rewrite rewrites the message-level RCODE, but the reverter only ran
response rules from inside the per-record loops in WriteMsg. When a response
carries no answer, authority or additional records - for example a bare
SERVFAIL that a downstream plugin returns to a non-EDNS client - none of the
loops iterate, so the rcode rewrite was silently skipped and the client
received the original RCODE.

Apply message-level response rules once when the response has no records, using
a small marker interface that mirrors the existing requestExtraRevertRule
pattern. This fixes the plugin's documented SERVFAIL-to-NOERROR use case for
responses without records.

Signed-off-by: Sueun Cho <sueun.dev@gmail.com>

* plugin/rewrite: apply fallback rcode rewrites for continue

Signed-off-by: Sueun Cho <sueun.dev@gmail.com>

---------

Signed-off-by: Sueun Cho <sueun.dev@gmail.com>
This commit is contained in:
Sueun Cho
2026-08-18 12:12:32 +09:00
committed by GitHub
parent 897b4ce643
commit 9a623cdeed
5 changed files with 99 additions and 13 deletions

View File

@@ -1,9 +1,11 @@
package rewrite
import (
"context"
"strings"
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
@@ -72,6 +74,45 @@ func TestRCodeRewrite(t *testing.T) {
}
}
// TestRCodeRewriteEmptyResponse drives an rcode rewrite end-to-end through
// ServeDNS for a response that carries no resource records, such as a bare
// SERVFAIL from a downstream plugin. Rewriting SERVFAIL to NOERROR is the
// plugin's documented use case (see README.md), and a non-EDNS client's failure
// reply has no answer, authority or additional records, so the rewrite must be
// applied at the message level rather than only per record.
func TestRCodeRewriteEmptyResponse(t *testing.T) {
for _, mode := range []string{"stop", "continue"} {
for _, oldRcode := range []int{
dns.RcodeFormatError,
dns.RcodeServerFailure,
dns.RcodeRefused,
dns.RcodeNotImplemented,
} {
t.Run(mode+"/"+dns.RcodeToString[oldRcode], func(t *testing.T) {
rule, err := newRCodeRule(mode, "exact", "srv1.coredns.rocks", dns.RcodeToString[oldRcode], "NOERROR")
if err != nil {
t.Fatal(err)
}
// Downstream plugin fails without writing a response of its own.
next := plugin.HandlerFunc(func(_ context.Context, _ dns.ResponseWriter, _ *dns.Msg) (int, error) {
return oldRcode, nil
})
req := new(dns.Msg)
req.SetQuestion("srv1.coredns.rocks.", dns.TypeA) // no EDNS OPT record
resp := serveEdns0Rewrite(t, rule, next, req)
if resp == nil {
t.Fatal("no response was written to the client")
}
if resp.Rcode != dns.RcodeSuccess {
t.Fatalf("expected RCODE rewritten to NOERROR (%d), got %s (%d)",
dns.RcodeSuccess, dns.RcodeToString[resp.Rcode], resp.Rcode)
}
})
}
}
}
func TestNewRCodeRuleLargeRegex(t *testing.T) {
largeRegex := strings.Repeat("a", maxRegexpLen+1)
_, err := newRCodeRule("stop", "regex", largeRegex, "SERVFAIL", "NXDOMAIN")