From 89124aa570004c9b565d6a7841aa847b64dee692 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 18 Mar 2020 12:18:10 -0600 Subject: [PATCH] httpcaddyfile: Prevent rewrite routes from consolidating (fix #3108) It's hard to say whether this was actually a bug, but the linked issue shows why the old behavior was confusing. Basically, we infer that a rewrite handler is supposed to act as an internal redirect, which likely means it will no longer match the matcher(s) it did before the rewrite. So if the rewrite directive shares a matcher with any adjacent route or directive, it can be confusing/misleading if we consolidate the rewrite into the same route as the next handler, which shouldn't (probably) match after the rewrite is complete. This is kiiiind of a hacky workaround to a quirky problem. For edge cases like these, it is probably "cleaner" to just use handle blocks instead, to group handlers under the same matcher, nginx-style. --- caddyconfig/httpcaddyfile/httptype.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index 18dd0a0f..473aee3a 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -618,7 +618,16 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro } } // if there is more than one, put them in a group - if info.count > 1 { + // (special case: "rewrite" directive must always be in + // its own group--even if there is only one--because we + // do not want a rewrite to be consolidated into other + // adjacent routes that happen to have the same matcher, + // see caddyserver/caddy#3108 - because the implied + // intent of rewrite is to do an internal redirect, + // we can't assume that the request will continue to + // match the same matcher; anyway, giving a route a + // unique group name should keep it from consolidating) + if info.count > 1 || meDir == "rewrite" { info.groupName = groupCounter.nextGroup() } } @@ -662,7 +671,6 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro // consolidateRoutes combines routes with the same properties // (same matchers, same Terminal and Group settings) for a // cleaner overall output. -// FIXME: See caddyserver/caddy#3108 func consolidateRoutes(routes caddyhttp.RouteList) caddyhttp.RouteList { for i := 0; i < len(routes)-1; i++ { if reflect.DeepEqual(routes[i].MatcherSetsRaw, routes[i+1].MatcherSetsRaw) &&