caddyhttp: Fix `vars_regexp` matcher with placeholders (#5408)

Changed to match the `vars` matcher's logic for handling placeholders
This commit is contained in:
Francis Lavoie 2023-03-02 11:01:54 -05:00 committed by GitHub
parent f6bab8ba85
commit 85375861f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 8 deletions

View File

@ -255,9 +255,18 @@ func (m MatchVarsRE) Provision(ctx caddy.Context) error {
func (m MatchVarsRE) Match(r *http.Request) bool { func (m MatchVarsRE) Match(r *http.Request) bool {
vars := r.Context().Value(VarsCtxKey).(map[string]any) vars := r.Context().Value(VarsCtxKey).(map[string]any)
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
for k, rm := range m { for key, val := range m {
var varValue any
if strings.HasPrefix(key, "{") &&
strings.HasSuffix(key, "}") &&
strings.Count(key, "{") == 1 {
varValue, _ = repl.Get(strings.Trim(key, "{}"))
} else {
varValue = vars[key]
}
var varStr string var varStr string
switch vv := vars[k].(type) { switch vv := varValue.(type) {
case string: case string:
varStr = vv varStr = vv
case fmt.Stringer: case fmt.Stringer:
@ -267,13 +276,9 @@ func (m MatchVarsRE) Match(r *http.Request) bool {
default: default:
varStr = fmt.Sprintf("%v", vv) varStr = fmt.Sprintf("%v", vv)
} }
valExpanded := repl.ReplaceAll(varStr, "")
if match := rm.Match(valExpanded, repl); match {
return match
}
replacedVal := repl.ReplaceAll(k, "") valExpanded := repl.ReplaceAll(varStr, "")
if match := rm.Match(replacedVal, repl); match { if match := val.Match(valExpanded, repl); match {
return match return match
} }
} }