From 61c75f74dec6b0524feef9b24ef8d6005f096b09 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Tue, 13 Sep 2022 13:18:37 -0400 Subject: caddyhttp: Explicitly disallow multiple regexp matchers (#5030) * caddyhttp: Explicitly disallow multiple regexp matchers Fix #5028 Since the matchers would overwrite eachother, we should error out to tell the user their config doesn't make sense. * Update modules/caddyhttp/matchers.go Co-authored-by: Matt Holt --- modules/caddyhttp/matchers.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'modules/caddyhttp/matchers.go') diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index f86ce0a..3d5791f 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -1001,6 +1001,12 @@ func (m *MatchHeaderRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { val = second } + // If there's already a pattern for this field + // then we would end up overwriting the old one + if (*m)[field] != nil { + return d.Errf("header_regexp matcher can only be used once per named matcher, per header field: %s", field) + } + (*m)[field] = &MatchRegexp{Pattern: val, Name: name} if d.NextBlock(0) { @@ -1469,6 +1475,13 @@ func (mre *MatchRegexp) Match(input string, repl *caddy.Replacer) bool { // UnmarshalCaddyfile implements caddyfile.Unmarshaler. func (mre *MatchRegexp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { for d.Next() { + // If this is the second iteration of the loop + // then there's more than one path_regexp matcher + // and we would end up overwriting the old one + if mre.Pattern != "" { + return d.Err("regular expression can only be used once per named matcher") + } + args := d.RemainingArgs() switch len(args) { case 1: -- cgit v1.2.3