Skip to content

Commit df65455

Browse files
authored
caddyhttp: Sync placeholder expansion in vars and vars_regexp (#7573)
* vars: Expand placeholders in custom variables like in `vars_regexp` * vars: Reuse variables inside match loops
1 parent 8499e34 commit df65455

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

modules/caddyhttp/vars.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,33 +181,46 @@ func (m VarsMatcher) MatchWithError(r *http.Request) (bool, error) {
181181
vars := r.Context().Value(VarsCtxKey).(map[string]any)
182182
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
183183

184+
var fromPlaceholder bool
185+
var matcherValExpanded, valExpanded, varStr, v string
186+
var varValue any
184187
for key, vals := range m {
185-
var varValue any
186188
if strings.HasPrefix(key, "{") &&
187189
strings.HasSuffix(key, "}") &&
188190
strings.Count(key, "{") == 1 {
189191
varValue, _ = repl.Get(strings.Trim(key, "{}"))
192+
fromPlaceholder = true
190193
} else {
191194
varValue = vars[key]
195+
fromPlaceholder = false
196+
}
197+
198+
switch vv := varValue.(type) {
199+
case string:
200+
varStr = vv
201+
case fmt.Stringer:
202+
varStr = vv.String()
203+
case error:
204+
varStr = vv.Error()
205+
case nil:
206+
varStr = ""
207+
default:
208+
varStr = fmt.Sprintf("%v", vv)
209+
}
210+
211+
// Only expand placeholders in values from literal variable names
212+
// (e.g. map outputs). Values resolved from placeholder keys are
213+
// already final and must not be re-expanded, as that would allow
214+
// user input like {env.SECRET} to be evaluated.
215+
valExpanded = varStr
216+
if !fromPlaceholder {
217+
valExpanded = repl.ReplaceAll(varStr, "")
192218
}
193219

194220
// see if any of the values given in the matcher match the actual value
195-
for _, v := range vals {
196-
matcherValExpanded := repl.ReplaceAll(v, "")
197-
var varStr string
198-
switch vv := varValue.(type) {
199-
case string:
200-
varStr = vv
201-
case fmt.Stringer:
202-
varStr = vv.String()
203-
case error:
204-
varStr = vv.Error()
205-
case nil:
206-
varStr = ""
207-
default:
208-
varStr = fmt.Sprintf("%v", vv)
209-
}
210-
if varStr == matcherValExpanded {
221+
for _, v = range vals {
222+
matcherValExpanded = repl.ReplaceAll(v, "")
223+
if valExpanded == matcherValExpanded {
211224
return true, nil
212225
}
213226
}
@@ -310,19 +323,21 @@ func (m MatchVarsRE) Match(r *http.Request) bool {
310323
func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) {
311324
vars := r.Context().Value(VarsCtxKey).(map[string]any)
312325
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
326+
327+
var fromPlaceholder, match bool
328+
var valExpanded, varStr string
329+
var varValue any
313330
for key, val := range m {
314-
var varValue any
315-
var fromPlaceholder bool
316331
if strings.HasPrefix(key, "{") &&
317332
strings.HasSuffix(key, "}") &&
318333
strings.Count(key, "{") == 1 {
319334
varValue, _ = repl.Get(strings.Trim(key, "{}"))
320335
fromPlaceholder = true
321336
} else {
322337
varValue = vars[key]
338+
fromPlaceholder = false
323339
}
324340

325-
var varStr string
326341
switch vv := varValue.(type) {
327342
case string:
328343
varStr = vv
@@ -340,11 +355,11 @@ func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) {
340355
// (e.g. map outputs). Values resolved from placeholder keys are
341356
// already final and must not be re-expanded, as that would allow
342357
// user input like {env.SECRET} to be evaluated.
343-
valExpanded := varStr
358+
valExpanded = varStr
344359
if !fromPlaceholder {
345360
valExpanded = repl.ReplaceAll(varStr, "")
346361
}
347-
if match := val.Match(valExpanded, repl); match {
362+
if match = val.Match(valExpanded, repl); match {
348363
return match, nil
349364
}
350365
}

0 commit comments

Comments
 (0)