From d598c71d655a03e5a2e3915a82da1a90301ddcd0 Mon Sep 17 00:00:00 2001 From: Simon Sudarushkin <68971770+simonether@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:13:35 +0500 Subject: [PATCH] caddytls: keep repeated matchers in nested matcher sets (#8137) (cherry picked from commit 1706dc8f722c0542433a7b280f756ff8dd62315e) --- modules/caddytls/connpolicy.go | 17 +++++++-- modules/caddytls/connpolicy_test.go | 57 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go index 852c78d54..a28c9c93b 100644 --- a/modules/caddytls/connpolicy.go +++ b/modules/caddytls/connpolicy.go @@ -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 } diff --git a/modules/caddytls/connpolicy_test.go b/modules/caddytls/connpolicy_test.go index b3c091d47..a90d639ed 100644 --- a/modules/caddytls/connpolicy_test.go +++ b/modules/caddytls/connpolicy_test.go @@ -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) + } + } +}