plugin/loop: avoid panic on invalid server block (#7568)

Ignore invalid ServerBlockKeys in loop plugin that fail
normalization. Retain the default “.” zone instead of
indexing into an empty slice. This prevents an
index-out-of-range panic triggered by malformed
inputs such as "unix://".

Added tests to validate and increase test coverage.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-09-22 08:44:36 +03:00
committed by GitHub
parent 0d05791404
commit 31e285994b
3 changed files with 105 additions and 2 deletions

View File

@@ -17,3 +17,32 @@ func TestSetup(t *testing.T) {
t.Fatal("Expected errors, but got none")
}
}
func TestParseServerBlockKeys(t *testing.T) {
tests := []struct {
name string
key string
want string
wantOk bool
}{
{name: "valid domain", key: "example.org", want: "example.org.", wantOk: true},
{name: "invalid scheme", key: "unix://", want: ".", wantOk: true},
{name: "empty", key: "", want: ".", wantOk: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := caddy.NewTestController("dns", `loop`)
if tt.key != "" {
c.ServerBlockKeys = []string{tt.key}
}
l, err := parse(c)
if (err == nil) != tt.wantOk {
t.Fatalf("parse err=%v, wantOk=%v", err, tt.wantOk)
}
if l.zone != tt.want {
t.Fatalf("zone=%q, want %q", l.zone, tt.want)
}
})
}
}