From 7c4d680911abc2a3be3195663a9fcfdabd4d8486 Mon Sep 17 00:00:00 2001 From: PerryLink Date: Sun, 1 Nov 2026 12:21:50 +0800 Subject: [PATCH] map: escape only real capture references in output templates The previous pass escaped every "${" before placeholder expansion, which also suppressed a Caddy placeholder after a literal dollar: "${http.request.method}" stayed literal instead of becoming "$GET". Escape the brace only for braced references that name an actual numeric or named capture, and double every other literal "$" so ExpandString restores it. Adds a parity case for a placeholder after a literal dollar. --- modules/caddyhttp/map/map.go | 73 ++++++++++++++++++++++++++++++- modules/caddyhttp/map/map_test.go | 18 ++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/modules/caddyhttp/map/map.go b/modules/caddyhttp/map/map.go index 30da116c4..dc87f4e2a 100644 --- a/modules/caddyhttp/map/map.go +++ b/modules/caddyhttp/map/map.go @@ -19,6 +19,7 @@ import ( "net/http" "regexp" "slices" + "strconv" "strings" "github.com/caddyserver/caddy/v2" @@ -124,6 +125,73 @@ func (h *Handler) Validate() error { return nil } +// escapeTemplate prepares an operator-written output template for the two +// expansion passes in ServeHTTP. Placeholders ({...}) are expanded by the +// replacer first, capture references ($1, ${1}, $name) by ExpandString last. +// +// A braced capture (${1}, ${name}) must not be read as a Caddy placeholder, so +// its opening brace is escaped for the replacer. Every other "$" is data to +// ExpandString and is doubled so a literal dollar survives; this keeps a Caddy +// placeholder that follows a literal dollar working: "${http.request.method}" +// becomes "$${http.request.method}", the replacer yields "$$GET", and +// ExpandString restores "$GET". +func escapeTemplate(template string, re *regexp.Regexp) string { + isCapture := func(name string) bool { + if name == "" { + return false + } + if n, err := strconv.Atoi(name); err == nil { + return n >= 0 && n <= re.NumSubexp() + } + for _, group := range re.SubexpNames() { + if group == name { + return true + } + } + return false + } + + var b strings.Builder + for i := 0; i < len(template); { + if template[i] != '$' { + b.WriteByte(template[i]) + i++ + continue + } + if i+1 < len(template) && template[i+1] == '$' { + b.WriteString("$$") + i += 2 + continue + } + if i+1 < len(template) && template[i+1] == '{' { + if end := strings.IndexByte(template[i+2:], '}'); end >= 0 { + name := template[i+2 : i+2+end] + if isCapture(name) { + b.WriteString(`$\{`) + } else { + b.WriteString("$${") + } + b.WriteString(name) + b.WriteByte('}') + i += 2 + end + 1 + continue + } + } + j := i + 1 + for j < len(template) && (template[j] == '_' || template[j] >= '0' && template[j] <= '9' || template[j] >= 'A' && template[j] <= 'Z' || template[j] >= 'a' && template[j] <= 'z') { + j++ + } + if j > i+1 && isCapture(template[i+1:j]) { + b.WriteString(template[i:j]) + i = j + continue + } + b.WriteString("$$") + i++ + } + return b.String() +} + func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) @@ -164,7 +232,10 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt // A braced capture reference such as ${1} would otherwise look // like a placeholder to the replacer, so escape its opening // brace first; the replacer drops the escape character again. - template := strings.ReplaceAll(outputStr, "${", `$\{`) + // Only real captures are escaped: a Caddy placeholder that + // follows a literal dollar, such as ${http.request.method}, + // must still be expanded by the replacer. + template := escapeTemplate(outputStr, m.re) expanded, _ := repl.ReplaceFunc(template, func(_ string, val any) (any, error) { return strings.ReplaceAll(caddy.ToString(val), "$", "$$"), nil }) diff --git a/modules/caddyhttp/map/map_test.go b/modules/caddyhttp/map/map_test.go index d94217611..580689873 100644 --- a/modules/caddyhttp/map/map_test.go +++ b/modules/caddyhttp/map/map_test.go @@ -198,6 +198,24 @@ func TestHandler(t *testing.T) { "output": "GET:abc", }, }, + { + // A literal dollar before a Caddy placeholder must not suppress + // the placeholder: ${http.request.method} is "$" + "{...}". + reqURI: "/?key=abc", + handler: Handler{ + Source: "{http.request.uri.query.key}", + Destinations: []string{"{output}"}, + Mappings: []Mapping{ + { + InputRegexp: "^abc$", + Outputs: []any{"${http.request.method}"}, + }, + }, + }, + expect: map[string]any{ + "output": "$GET", + }, + }, { // A capture group is data too: placeholder syntax inside it // must not be evaluated, or request data could inject one.