@marcel91200 / caddy Mirror · read-only / commits / a8d45277
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 14, 05:15 UTC

caddyfile: Fix `import` replacing unrelated placeholders (#4129)

* caddyfile: Fix `import` replacing unrelated placeholders See https://caddy.community/t/snippet-issue-works-outside-snippet/12231 So it turns out that `NewReplacer()` gives a replacer with some global defaults (like `{env.*}` and some system and time placeholders), which is not ideal when running `import` because we just want to replace `{args.*}` only, and nothing else. * caddyfile: Add test

Francis Lavoie committed Apr 22, 2021 at 20:29 UTC a8d45277ca387ca4769fb4e881d242a5896cca5a
3 files changed +44 -1
caddyconfig/caddyfile/parse.go
+1 -1
@@ -321,7 +321,7 @@ func (p *parser) doImport() error {
321 args := p.RemainingArgs()
322
323 // add args to the replacer
324 - repl := caddy.NewReplacer()
324 + repl := caddy.NewEmptyReplacer()
325 for index, arg := range args {
326 repl.Set("args."+strconv.Itoa(index), arg)
327 }
caddytest/integration/caddyfile_adapt/import_args_snippet_env_placeholder.txt new
+31
@@ -0,0 +1,31 @@
1 +(foo) {
2 + respond {env.FOO}
3 +}
4 +
5 +:80 {
6 + import foo
7 +}
8 +----------
9 +{
10 + "apps": {
11 + "http": {
12 + "servers": {
13 + "srv0": {
14 + "listen": [
15 + ":80"
16 + ],
17 + "routes": [
18 + {
19 + "handle": [
20 + {
21 + "body": "{env.FOO}",
22 + "handler": "static_response"
23 + }
24 + ]
25 + }
26 + ]
27 + }
28 + }
29 + }
30 + }
31 +}
\ No newline at end of file
replacer.go
+12
@@ -36,6 +36,18 @@ func NewReplacer() *Replacer {
36 return rep
37 }
38
39 +// NewEmptyReplacer returns a new Replacer,
40 +// without the global default replacements.
41 +func NewEmptyReplacer() *Replacer {
42 + rep := &Replacer{
43 + static: make(map[string]interface{}),
44 + }
45 + rep.providers = []ReplacerFunc{
46 + rep.fromStatic,
47 + }
48 + return rep
49 +}
50 +
51 // Replacer can replace values in strings.
52 // A default/empty Replacer is not valid;
53 // use NewReplacer to make one.