mirror of
https://github.com/coredns/coredns.git
synced 2026-10-15 23:10:43 -04:00
plugin/file: reject SOA owners that do not match the zone (#8555)
Signed-off-by: houyuwushang <liuluoqianqiu@outlook.com>
This commit is contained in:
@@ -1,14 +1,27 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
||||
"github.com/coredns/coredns/plugin/pkg/fall"
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
const dbRelative = `@ 500 IN SOA ns.example. hostmaster.example. 3 3600 600 86400 300
|
||||
@ 500 IN NS ns.example.
|
||||
foo 500 IN A 192.0.2.1
|
||||
`
|
||||
|
||||
func TestFileParse(t *testing.T) {
|
||||
zoneFileName1, rm, err := test.TempFile(".", dbMiekNL)
|
||||
if err != nil {
|
||||
@@ -22,6 +35,12 @@ func TestFileParse(t *testing.T) {
|
||||
}
|
||||
defer rm()
|
||||
|
||||
zoneFileName3, rm, err := test.TempFile(".", dbRelative)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rm()
|
||||
|
||||
tests := []struct {
|
||||
inputFileRules string
|
||||
shouldErr bool
|
||||
@@ -41,13 +60,13 @@ func TestFileParse(t *testing.T) {
|
||||
fall.Zero,
|
||||
},
|
||||
{
|
||||
`file ` + zoneFileName2 + ` 10.0.0.0/8`,
|
||||
`file ` + zoneFileName3 + ` 10.0.0.0/8`,
|
||||
false,
|
||||
Zones{Names: []string{"10.in-addr.arpa."}},
|
||||
fall.Zero,
|
||||
},
|
||||
{
|
||||
`file ` + zoneFileName2 + ` example.org. {
|
||||
`file ` + zoneFileName3 + ` example.org. {
|
||||
fallthrough
|
||||
}`,
|
||||
false,
|
||||
@@ -55,7 +74,7 @@ func TestFileParse(t *testing.T) {
|
||||
fall.Root,
|
||||
},
|
||||
{
|
||||
`file ` + zoneFileName2 + ` example.org. {
|
||||
`file ` + zoneFileName3 + ` example.org. {
|
||||
fallthrough www.example.org
|
||||
}`,
|
||||
false,
|
||||
@@ -78,7 +97,7 @@ func TestFileParse(t *testing.T) {
|
||||
fall.Zero,
|
||||
},
|
||||
{
|
||||
`file ` + zoneFileName1 + ` example.net. {
|
||||
`file ` + zoneFileName3 + ` example.net. {
|
||||
no_reload
|
||||
}`,
|
||||
true,
|
||||
@@ -86,7 +105,7 @@ func TestFileParse(t *testing.T) {
|
||||
fall.Zero,
|
||||
},
|
||||
{
|
||||
`file ` + zoneFileName1 + ` example.net. {
|
||||
`file ` + zoneFileName3 + ` example.net. {
|
||||
no_rebloat
|
||||
}`,
|
||||
true,
|
||||
@@ -120,7 +139,7 @@ func TestFileParse(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseReload(t *testing.T) {
|
||||
name, rm, err := test.TempFile(".", dbMiekNL)
|
||||
name, rm, err := test.TempFile(".", dbRelative)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -144,9 +163,87 @@ func TestParseReload(t *testing.T) {
|
||||
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("dns", test.input)
|
||||
z, _, _ := fileParse(c)
|
||||
z, _, err := fileParse(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if x := z.Z["example.org."].ReloadInterval; x != test.reload {
|
||||
t.Errorf("Test %d expected reload to be %s, but got %s", i, test.reload, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileParseSOAOrigin(t *testing.T) {
|
||||
for _, owner := range []string{"test", "@", "test."} {
|
||||
for _, explicit := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/explicit=%t", owner, explicit), func(t *testing.T) {
|
||||
contents := fmt.Sprintf(`%s 500 IN SOA ns1.outside.com. root.test 3 604800 86400 2419200 604800
|
||||
%s 500 IN NS ns1.outside.com.
|
||||
foo 500 IN A 1.1.1.1
|
||||
`, owner, owner)
|
||||
fileName := filepath.Join(t.TempDir(), "db.test")
|
||||
if err := os.WriteFile(fileName, []byte(contents), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
corefile := fmt.Sprintf("file %q", filepath.ToSlash(fileName))
|
||||
if explicit {
|
||||
corefile += " test"
|
||||
}
|
||||
c := caddy.NewTestController("dns", corefile)
|
||||
c.ServerBlockKeys = []string{"test:53"}
|
||||
zones, _, err := fileParse(c)
|
||||
if owner == "test" {
|
||||
if err == nil || !strings.Contains(err.Error(), "SOA owner test.test. that does not match origin test.") {
|
||||
t.Fatalf("expected mismatched SOA error, got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := File{Zones: zones}
|
||||
for _, tc := range []test.Case{
|
||||
{
|
||||
Qname: "foo.test.", Qtype: dns.TypeA, Authoritative: true,
|
||||
Answer: []dns.RR{test.A("foo.test. 500 IN A 1.1.1.1")},
|
||||
Ns: []dns.RR{test.NS("test. 500 IN NS ns1.outside.com.")},
|
||||
},
|
||||
{
|
||||
Qname: "bar.test.", Qtype: dns.TypeA, Rcode: dns.RcodeNameError, Authoritative: true,
|
||||
Ns: []dns.RR{test.SOA("test. 500 IN SOA ns1.outside.com. root.test.test. 3 604800 86400 2419200 604800")},
|
||||
},
|
||||
} {
|
||||
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||
if _, err := f.ServeDNS(context.Background(), rec, tc.Msg()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := test.SortAndCheck(rec.Msg, tc); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileParseRelativeZones(t *testing.T) {
|
||||
fileName, rm, err := test.TempFile(".", dbRelative)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rm()
|
||||
c := caddy.NewTestController("dns", "file "+fileName+" example.org example.net")
|
||||
zones, _, err := fileParse(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, origin := range []string{"example.org.", "example.net."} {
|
||||
z := zones.Z[origin]
|
||||
if z == nil || z.SOA == nil || z.SOA.Hdr.Name != origin {
|
||||
t.Fatalf("missing SOA at %s", origin)
|
||||
}
|
||||
if _, ok := z.Search("foo." + origin); !ok {
|
||||
t.Errorf("missing relative A record in %s", origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user