Files
coredns/plugin/sign/setup_test.go
Ville Vesilehto 6676e6185d fix(sign): reject invalid UTF‑8 dbfile token (#7589)
The coredns/caddy lexer replaces invalid UTF‑8 bytes in tokens with
U+FFFD. When that lossy-decoded value is used as `dbfile` in the sign
plugin, the source zone file path never exists. On startup/refresh,
the `resign()` function sees the signed file missing and triggers
signing. Consequently `Sign()` then fails opening the bogus path,
the signed file is never created, and the cycle repeats across all
expanded origins (e.g., reverse CIDRs), causing unbounded churn/OOM.

Validate `dbfile` in setup and error if it contains U+FFFD. Add a
regression test.

Note: Unicode paths are supported; only U+FFFD (replacement-rune) is rejected.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
2025-10-06 00:06:28 -07:00

87 lines
2.1 KiB
Go

package sign
import (
"testing"
"github.com/coredns/caddy"
)
func TestParse(t *testing.T) {
tests := []struct {
input string
shouldErr bool
exp *Signer
}{
{`sign testdata/db.miek.nl miek.nl {
key file testdata/Kmiek.nl.+013+59725
}`,
false,
&Signer{
keys: []Pair{},
origin: "miek.nl.",
dbfile: "testdata/db.miek.nl",
directory: "/var/lib/coredns",
signedfile: "db.miek.nl.signed",
},
},
{`sign testdata/db.miek.nl example.org {
key file testdata/Kmiek.nl.+013+59725
directory testdata
}`,
false,
&Signer{
keys: []Pair{},
origin: "example.org.",
dbfile: "testdata/db.miek.nl",
directory: "testdata",
signedfile: "db.example.org.signed",
},
},
// errors
{`sign db.example.org {
key file /etc/coredns/keys/Kexample.org
}`,
true,
nil,
},
}
for i, tc := range tests {
c := caddy.NewTestController("dns", tc.input)
sign, err := parse(c)
if err == nil && tc.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
}
if err != nil && !tc.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
}
if tc.shouldErr {
continue
}
signer := sign.signers[0]
if x := signer.origin; x != tc.exp.origin {
t.Errorf("Test %d expected %s as origin, got %s", i, tc.exp.origin, x)
}
if x := signer.dbfile; x != tc.exp.dbfile {
t.Errorf("Test %d expected %s as dbfile, got %s", i, tc.exp.dbfile, x)
}
if x := signer.directory; x != tc.exp.directory {
t.Errorf("Test %d expected %s as directory, got %s", i, tc.exp.directory, x)
}
if x := signer.signedfile; x != tc.exp.signedfile {
t.Errorf("Test %d expected %s as signedfile, got %s", i, tc.exp.signedfile, x)
}
}
}
// With setup validation in place, an invalid utf-8 dbfile token must cause parse() to error.
func TestParseRejectsInvalidDbfileToken(t *testing.T) {
input := "sign \"\xff\" 8.44.in-addr.arpa. 9.44.in-addr.arpa. {}"
c := caddy.NewTestController("dns", input)
_, err := parse(c)
if err == nil {
t.Fatalf("expected parse to fail for invalid dbfile token")
}
}