@marcel91200 / caddy Mirror · read-only / commits / 85375861
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 05:30 UTC

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

Changed to match the `vars` matcher's logic for handling placeholders

Francis Lavoie committed Mar 2, 2023 at 11:01 UTC 85375861f6a903da9af8aa665552a6546d075c9f
1 file changed +13 -8
modules/caddyhttp/vars.go
+13 -8
@@ -255,9 +255,18 @@ func (m MatchVarsRE) Provision(ctx caddy.Context) error {
255 func (m MatchVarsRE) Match(r *http.Request) bool {
256 vars := r.Context().Value(VarsCtxKey).(map[string]any)
257 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
258 - for k, rm := range m {
258 + for key, val := range m {
259 + var varValue any
260 + if strings.HasPrefix(key, "{") &&
261 + strings.HasSuffix(key, "}") &&
262 + strings.Count(key, "{") == 1 {
263 + varValue, _ = repl.Get(strings.Trim(key, "{}"))
264 + } else {
265 + varValue = vars[key]
266 + }
267 +
268 var varStr string
260 - switch vv := vars[k].(type) {
269 + switch vv := varValue.(type) {
270 case string:
271 varStr = vv
272 case fmt.Stringer:
@@ -267,13 +276,9 @@ func (m MatchVarsRE) Match(r *http.Request) bool {
276 default:
277 varStr = fmt.Sprintf("%v", vv)
278 }
270 - valExpanded := repl.ReplaceAll(varStr, "")
271 - if match := rm.Match(valExpanded, repl); match {
272 - return match
273 - }
279
275 - replacedVal := repl.ReplaceAll(k, "")
276 - if match := rm.Match(replacedVal, repl); match {
280 + valExpanded := repl.ReplaceAll(varStr, "")
281 + if match := val.Match(valExpanded, repl); match {
282 return match
283 }
284 }