caddytls: keep repeated matchers in nested matcher sets (#8137)

(cherry picked from commit 1706dc8f72)
This commit is contained in:
Simon Sudarushkin 2026-10-04 21:13:35 +05:00 • committed by Francis Lavoie
parent ea0a202294
commit d598c71d65
No known key found for this signature in database
2 changed files with 70 additions and 4 deletions

View file

@ -1097,6 +1097,10 @@ var (
func ParseCaddyfileNestedMatcherSet(d *caddyfile.Dispenser) (caddy.ModuleMap, error) {
matcherMap := make(map[string]ConnectionMatcher)
// in case there are multiple instances of the same matcher, concatenate
// their tokens (we expect that UnmarshalCaddyfile should be able to
// handle more than one segment); otherwise, we'd overwrite other
// instances of the matcher in this set
tokensByMatcherName := make(map[string][]caddyfile.Token)
for nesting := d.Nesting(); d.NextArg() || d.NextBlock(nesting); {
matcherName := d.Val()
@ -1104,10 +1108,15 @@ func ParseCaddyfileNestedMatcherSet(d *caddyfile.Dispenser) (caddy.ModuleMap, er
}
for matcherName, tokens := range tokensByMatcherName {
dd := caddyfile.NewDispenser(tokens)
dd.Next() // consume wrapper name
unm, err := caddyfile.UnmarshalModule(dd, "tls.handshake_match."+matcherName)
mod, err := caddy.GetModule("tls.handshake_match." + matcherName)
if err != nil {
return nil, d.Errf("getting matcher module '%s': %v", matcherName, err)
}
unm, ok := mod.New().(caddyfile.Unmarshaler)
if !ok {
return nil, d.Errf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
}
err = unm.UnmarshalCaddyfile(caddyfile.NewDispenser(tokens))
if err != nil {
return nil, err
}

View file

@ -361,3 +361,60 @@ func TestClientAuthenticationProvision(t *testing.T) {
})
}
}
func TestParseCaddyfileNestedMatcherSet(t *testing.T) {
for i, tc := range []struct {
input string
expect string
wantErr bool
}{
{
input: `match sni example.com`,
expect: `{"sni":["example.com"]}`,
},
{
input: `match {
sni example.com
sni example.net
}`,
expect: `{"sni":["example.com","example.net"]}`,
},
{
input: `match {
remote_ip 10.0.0.0/8
sni example.com
remote_ip !10.1.2.3
}`,
expect: `{"remote_ip":{"ranges":["10.0.0.0/8"],"not_ranges":["10.1.2.3"]},"sni":["example.com"]}`,
},
{
input: `match {
sni_regexp ^a
sni_regexp ^b
}`,
wantErr: true,
},
} {
d := caddyfile.NewTestDispenser(tc.input)
d.Next() // consume "match"
matcherSet, err := ParseCaddyfileNestedMatcherSet(d)
if tc.wantErr {
if err == nil {
t.Errorf("Test %d: expected error but got none", i)
}
continue
}
if err != nil {
t.Errorf("Test %d: unexpected error: %v", i, err)
continue
}
actual, err := json.Marshal(matcherSet)
if err != nil {
t.Fatalf("Test %d: marshaling matcher set: %v", i, err)
}
if string(actual) != tc.expect {
t.Errorf("Test %d: expected %s, got %s", i, tc.expect, actual)
}
}
}