mirror of
https://github.com/caddyserver/caddy.git
synced 2026-03-19 08:09:57 -04:00
caddyhttp: Sync placeholder expansion in vars and vars_regexp (#7573)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Failing after 1m26s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.26.0, 1.26, aix) (push) Successful in 1m29s
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Successful in 1m27s
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Successful in 1m24s
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Successful in 1m20s
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Successful in 1m40s
Cross-Build / build (~1.26.0, 1.26, linux) (push) Successful in 1m25s
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Successful in 1m26s
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Successful in 1m22s
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Successful in 1m20s
Cross-Build / build (~1.26.0, 1.26, windows) (push) Successful in 1m30s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m7s
Lint / govulncheck (push) Successful in 1m20s
Lint / dependency-review (push) Failing after 23s
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 31s
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Failing after 1m26s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.26.0, 1.26, aix) (push) Successful in 1m29s
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Successful in 1m27s
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Successful in 1m24s
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Successful in 1m20s
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Successful in 1m40s
Cross-Build / build (~1.26.0, 1.26, linux) (push) Successful in 1m25s
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Successful in 1m26s
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Successful in 1m22s
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Successful in 1m20s
Cross-Build / build (~1.26.0, 1.26, windows) (push) Successful in 1m30s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m7s
Lint / govulncheck (push) Successful in 1m20s
Lint / dependency-review (push) Failing after 23s
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 31s
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
* vars: Expand placeholders in custom variables like in `vars_regexp` * vars: Reuse variables inside match loops
This commit is contained in:
parent
8499e34e10
commit
df65455b1f
@ -181,33 +181,46 @@ 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 varValue any
|
||||
for key, vals := range m {
|
||||
var varValue any
|
||||
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) {
|
||||
case string:
|
||||
varStr = vv
|
||||
case fmt.Stringer:
|
||||
varStr = vv.String()
|
||||
case error:
|
||||
varStr = vv.Error()
|
||||
case nil:
|
||||
varStr = ""
|
||||
default:
|
||||
varStr = fmt.Sprintf("%v", vv)
|
||||
}
|
||||
|
||||
// Only expand placeholders in values from literal variable names
|
||||
// (e.g. map outputs). Values resolved from placeholder keys 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, "")
|
||||
var varStr string
|
||||
switch vv := varValue.(type) {
|
||||
case string:
|
||||
varStr = vv
|
||||
case fmt.Stringer:
|
||||
varStr = vv.String()
|
||||
case error:
|
||||
varStr = vv.Error()
|
||||
case nil:
|
||||
varStr = ""
|
||||
default:
|
||||
varStr = fmt.Sprintf("%v", vv)
|
||||
}
|
||||
if varStr == matcherValExpanded {
|
||||
for _, v = range vals {
|
||||
matcherValExpanded = repl.ReplaceAll(v, "")
|
||||
if valExpanded == matcherValExpanded {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
@ -310,9 +323,11 @@ func (m MatchVarsRE) Match(r *http.Request) bool {
|
||||
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 varValue any
|
||||
for key, val := range m {
|
||||
var varValue any
|
||||
var fromPlaceholder bool
|
||||
if strings.HasPrefix(key, "{") &&
|
||||
strings.HasSuffix(key, "}") &&
|
||||
strings.Count(key, "{") == 1 {
|
||||
@ -320,9 +335,9 @@ func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) {
|
||||
fromPlaceholder = true
|
||||
} else {
|
||||
varValue = vars[key]
|
||||
fromPlaceholder = false
|
||||
}
|
||||
|
||||
var varStr string
|
||||
switch vv := varValue.(type) {
|
||||
case string:
|
||||
varStr = vv
|
||||
@ -340,11 +355,11 @@ func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) {
|
||||
// (e.g. map outputs). Values resolved from placeholder keys are
|
||||
// already final and must not be re-expanded, as that would allow
|
||||
// user input like {env.SECRET} to be evaluated.
|
||||
valExpanded := varStr
|
||||
valExpanded = varStr
|
||||
if !fromPlaceholder {
|
||||
valExpanded = repl.ReplaceAll(varStr, "")
|
||||
}
|
||||
if match := val.Match(valExpanded, repl); match {
|
||||
if match = val.Match(valExpanded, repl); match {
|
||||
return match, nil
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user