diff --git a/modules/caddyhttp/vars.go b/modules/caddyhttp/vars.go index 68aaca331..6c17fe9bb 100644 --- a/modules/caddyhttp/vars.go +++ b/modules/caddyhttp/vars.go @@ -181,18 +181,15 @@ func (m VarsMatcher) MatchWithError(r *http.Request) (bool, error) { vars := r.Context().Value(VarsCtxKey).(map[string]any) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) - var fromPlaceholder bool - var matcherValExpanded, valExpanded, varStr, v string + var matcherValExpanded, varStr, v string var varValue any for key, vals := range m { if strings.HasPrefix(key, "{") && strings.HasSuffix(key, "}") && strings.Count(key, "{") == 1 { varValue, _ = repl.Get(strings.Trim(key, "{}")) - fromPlaceholder = true } else { varValue = vars[key] - fromPlaceholder = false } switch vv := varValue.(type) { @@ -208,19 +205,15 @@ func (m VarsMatcher) MatchWithError(r *http.Request) (bool, error) { varStr = fmt.Sprintf("%v", vv) } - // Only expand placeholders in values from literal variable names - // (e.g. map outputs). Values resolved from placeholder keys are + // Don't expand placeholders in values from literal variable names + // (e.g. map outputs) or other placeholders. These values are // already final and must not be re-expanded, as that would allow // user input like {env.SECRET} to be evaluated. - valExpanded = varStr - if !fromPlaceholder { - valExpanded = repl.ReplaceAll(varStr, "") - } // see if any of the values given in the matcher match the actual value for _, v = range vals { matcherValExpanded = repl.ReplaceAll(v, "") - if valExpanded == matcherValExpanded { + if varStr == matcherValExpanded { return true, nil } } @@ -324,18 +317,16 @@ func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) { vars := r.Context().Value(VarsCtxKey).(map[string]any) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) - var fromPlaceholder, match bool - var valExpanded, varStr string + var match bool + var varStr string var varValue any for key, val := range m { if strings.HasPrefix(key, "{") && strings.HasSuffix(key, "}") && strings.Count(key, "{") == 1 { varValue, _ = repl.Get(strings.Trim(key, "{}")) - fromPlaceholder = true } else { varValue = vars[key] - fromPlaceholder = false } switch vv := varValue.(type) { @@ -351,15 +342,12 @@ func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) { varStr = fmt.Sprintf("%v", vv) } - // Only expand placeholders in values from literal variable names - // (e.g. map outputs). Values resolved from placeholder keys are + // Don't expand placeholders in values from literal variable names + // (e.g. map outputs) or other placeholders. These values are // already final and must not be re-expanded, as that would allow // user input like {env.SECRET} to be evaluated. - valExpanded = varStr - if !fromPlaceholder { - valExpanded = repl.ReplaceAll(varStr, "") - } - if match = val.Match(valExpanded, repl); match { + + if match = val.Match(varStr, repl); match { return match, nil } }