From 5538b947d2873e8a06dd45182b8c43081fc56d32 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 11 Oct 2026 02:20:13 +0530 Subject: [PATCH 1/2] caddyhttp: skip matchers for satisfied route groups (#8189) * caddyhttp: Skip matchers of grouped routes once the group is satisfied * caddyhttp: Test that a skipped grouped route keeps regexp captures A later rewrite in the same group must not run its path_regexp matcher and overwrite {re.x.1} from the rewrite that was used. --- caddytest/integration/try_files_test.go | 69 +++++++++++++++++++++++++ modules/caddyhttp/routes.go | 27 +++++----- 2 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 caddytest/integration/try_files_test.go diff --git a/caddytest/integration/try_files_test.go b/caddytest/integration/try_files_test.go new file mode 100644 index 000000000..c318d0ad8 --- /dev/null +++ b/caddytest/integration/try_files_test.go @@ -0,0 +1,69 @@ +package integration + +import ( + "fmt" + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/caddyserver/caddy/v2/caddytest" +) + +func TestTryFilesErrorCodeAfterQueryItem(t *testing.T) { + root := t.TempDir() + for name, content := range map[string]string{ + "style.css": "css", + "about.html": "about", + } { + if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + + tester := caddytest.NewTester(t) + tester.InitServer(fmt.Sprintf(` + { + skip_install_trust + admin localhost:2999 + http_port 9080 + grace_period 1ns + } + http://localhost:9080 { + root * %q + try_files {path} {path}.html?{query} =404 + file_server + } + `, filepath.ToSlash(root)), "caddyfile") + + tester.AssertGetResponse("http://localhost:9080/style.css", 200, "css") + tester.AssertGetResponse("http://localhost:9080/about?x=1", 200, "about") + + req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/missing", nil) + if err != nil { + t.Fatal(err) + } + tester.AssertResponseCode(req, http.StatusNotFound) +} + +func TestSkippedGroupedRouteKeepsRegexpCapture(t *testing.T) { + tester := caddytest.NewTester(t) + tester.InitServer(` + { + skip_install_trust + admin localhost:2999 + http_port 9080 + grace_period 1ns + } + http://localhost:9080 { + @x path_regexp x ^/x/(.*)$ + @y path_regexp x ^/(.*)$ + rewrite @x /one/{re.x.1} + rewrite @y /two/{re.x.1} + respond "path={path} cap={re.x.1}" + } + `, "caddyfile") + + tester.AssertGetResponse("http://localhost:9080/x/q", 200, "path=/one/q cap=q") + tester.AssertGetResponse("http://localhost:9080/z", 200, "path=/two/z cap=z") +} diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go index 7cc6dd79d..ad674908c 100644 --- a/modules/caddyhttp/routes.go +++ b/modules/caddyhttp/routes.go @@ -268,6 +268,19 @@ func wrapRoute(route Route) Middleware { // but I just thought this made more sense nextCopy := next + // if route is part of a group, ensure only the + // first matching route in the group is applied + var groups map[string]struct{} + if route.Group != "" { + groups = req.Context().Value(routeGroupCtxKey).(map[string]struct{}) + + if _, ok := groups[route.Group]; ok { + // this group has already been satisfied by a + // matching route; skip without running matchers + return nextCopy.ServeHTTP(rw, req) + } + } + // route must match at least one of the matcher sets matches, err := route.MatcherSets.AnyMatchWithError(req) if err != nil { @@ -281,18 +294,8 @@ func wrapRoute(route Route) Middleware { return nextCopy.ServeHTTP(rw, req) } - // if route is part of a group, ensure only the - // first matching route in the group is applied - if route.Group != "" { - groups := req.Context().Value(routeGroupCtxKey).(map[string]struct{}) - - if _, ok := groups[route.Group]; ok { - // this group has already been - // satisfied by a matching route - return nextCopy.ServeHTTP(rw, req) - } - - // this matching route satisfies the group + // this matching route satisfies the group + if groups != nil { groups[route.Group] = struct{}{} } From d4165a4eb1fdb129e8ce8390099021a00239ecf7 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 11 Oct 2026 02:20:29 +0530 Subject: [PATCH 2/2] caddyhttp: Reject repeated expression matchers in a matcher set (#8183) * caddyhttp: Combine repeated expression matchers in a matcher set The Caddyfile parser groups repeated matchers of the same type and hands all their segments to the module, but the expression matcher only read the first segment, so every later `expression` line in a named matcher or `not` block was dropped without an error. Read every segment and AND the expressions, like the other matchers in a set. * caddyhttp: Put each joined expression on its own line A trailing // comment in one expression no longer swallows the closing parenthesis and the expressions joined after it. * caddyhttp: Reject repeated expression matchers instead of joining them A second expression line in a named matcher or a not block is now a Caddyfile error that points to combining them with && in one expression, instead of being dropped or joined with an implied operator. --- modules/caddyhttp/celmatcher.go | 34 ++++++++++++++++------------ modules/caddyhttp/celmatcher_test.go | 19 ++++++++++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/modules/caddyhttp/celmatcher.go b/modules/caddyhttp/celmatcher.go index 0118cc7c8..e01995039 100644 --- a/modules/caddyhttp/celmatcher.go +++ b/modules/caddyhttp/celmatcher.go @@ -243,25 +243,29 @@ func (m *MatchExpression) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { // CEL expression (e.g. strings) and we should retain that if d.CountRemainingArgs() > 1 { m.Expr = strings.Join(d.RemainingArgsRaw(), " ") - return nil + } else { + // there should at least be one arg + if !d.NextArg() { + return d.ArgErr() + } + + // if there's only one token, then we can safely grab the + // cleaned token (no quotes) and use that as the expression + // because there's no valid CEL expression that is only a + // quoted string; commonly quotes are used in Caddyfile to + // define the expression + m.Expr = d.Val() + + // use the named matcher's name, to fill regexp + // matchers names by default + m.Name = d.GetContextString(caddyfile.MatcherNameCtxKey) } - // there should at least be one arg - if !d.NextArg() { - return d.ArgErr() + // a matcher set holds only one expression matcher + if d.Next() { + return d.Err("repeated expression matchers are not supported, combine them into one expression with &&") } - // if there's only one token, then we can safely grab the - // cleaned token (no quotes) and use that as the expression - // because there's no valid CEL expression that is only a - // quoted string; commonly quotes are used in Caddyfile to - // define the expression - m.Expr = d.Val() - - // use the named matcher's name, to fill regexp - // matchers names by default - m.Name = d.GetContextString(caddyfile.MatcherNameCtxKey) - return nil } diff --git a/modules/caddyhttp/celmatcher_test.go b/modules/caddyhttp/celmatcher_test.go index 3e44fbbc9..7c8e706d3 100644 --- a/modules/caddyhttp/celmatcher_test.go +++ b/modules/caddyhttp/celmatcher_test.go @@ -21,9 +21,11 @@ import ( "encoding/pem" "net/http" "net/http/httptest" + "strings" "testing" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) var ( @@ -605,3 +607,20 @@ func TestMatchExpressionProvision(t *testing.T) { }) } } + +func TestMatchExpressionUnmarshalCaddyfileRepeated(t *testing.T) { + for _, input := range []string{` + expression {http.request.method} == 'GET' + expression {http.request.uri.path}.startsWith('/public/') + `, ` + expression "{http.request.method} == 'GET'" + expression {http.request.uri.path}.startsWith('/public/') + `} { + d := caddyfile.NewTestDispenser(input) + m := new(MatchExpression) + err := m.UnmarshalCaddyfile(d) + if err == nil || !strings.Contains(err.Error(), "repeated expression matchers are not supported") { + t.Errorf("UnmarshalCaddyfile() error = %v, want repeated expression error (expression: %s)", err, m.Expr) + } + } +}