From f46da403d85e331fee2763d3704969f4ec92ebf8 Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Tue, 30 Oct 2018 10:58:37 -0700 Subject: [PATCH] caddyfile: fix env var expansion after Go template (#2304) --- caddyfile/parse.go | 7 ++++++- caddyfile/parse_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/caddyfile/parse.go b/caddyfile/parse.go index 54f7866ce..83dea0160 100644 --- a/caddyfile/parse.go +++ b/caddyfile/parse.go @@ -424,7 +424,12 @@ func replaceEnvVars(s string) string { func replaceEnvReferences(s, refStart, refEnd string) string { index := strings.Index(s, refStart) for index != -1 { - endIndex := strings.Index(s, refEnd) + endIndex := strings.Index(s[index:], refEnd) + if endIndex == -1 { + break + } + + endIndex += index if endIndex > index+len(refStart) { ref := s[index : endIndex+len(refEnd)] s = strings.Replace(s, ref, os.Getenv(ref[len(refStart):len(ref)-len(refEnd)]), -1) diff --git a/caddyfile/parse_test.go b/caddyfile/parse_test.go index a50507535..b42711a8a 100644 --- a/caddyfile/parse_test.go +++ b/caddyfile/parse_test.go @@ -526,6 +526,13 @@ func TestEnvironmentReplacement(t *testing.T) { if actual, expected := blocks[0].Tokens["dir1"][1].Text, "Test foobar test"; expected != actual { t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) } + + // after end token + p = testParser(":1234\nanswer \"{{ .Name }} {$FOOBAR}\"") + blocks, _ = p.parseAll() + if actual, expected := blocks[0].Tokens["answer"][1].Text, "{{ .Name }} foobar"; expected != actual { + t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) + } } func testParser(input string) parser {