From 1beaab12e8947a26d3d2ef642cb26e020f03ac2a Mon Sep 17 00:00:00 2001 From: execjosh Date: Wed, 19 Jul 2017 19:16:09 +0900 Subject: [PATCH] Escape newlines in env var substitution --- server/hook.go | 7 ++++++- server/hook_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 server/hook_test.go diff --git a/server/hook.go b/server/hook.go index cdacbfc8e..b6513efe5 100644 --- a/server/hook.go +++ b/server/hook.go @@ -8,6 +8,7 @@ import ( "math/rand" "regexp" "strconv" + "strings" "time" "github.com/gin-gonic/gin" @@ -461,7 +462,11 @@ func (b *builder) Build() ([]*buildItem, error) { y := b.Yaml s, err := envsubst.Eval(y, func(name string) string { - return environ[name] + env := environ[name] + if strings.Contains(env, "\n") { + env = fmt.Sprintf("%q", env) + } + return env }) if err != nil { return nil, err diff --git a/server/hook_test.go b/server/hook_test.go new file mode 100644 index 000000000..51af94e66 --- /dev/null +++ b/server/hook_test.go @@ -0,0 +1,31 @@ +package server + +import ( + "testing" + + "github.com/drone/drone/model" +) + +func TestMultilineEnvsubst(t *testing.T) { + b := builder{ + Repo: &model.Repo{}, + Curr: &model.Build{ + Message: `aaa +bbb`, + }, + Last: &model.Build{}, + Netrc: &model.Netrc{}, + Secs: []*model.Secret{}, + Regs: []*model.Registry{}, + Link: "", + Yaml: `pipeline: + xxx: + image: scratch + yyy: ${DRONE_COMMIT_MESSAGE} +`, + } + + if _, err := b.Build(); err != nil { + t.Fatal(err) + } +}