From ad52cb23e847c454c42e3db45d08830ddc1084d1 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 16:08:15 -0300 Subject: [PATCH 1/3] fix: validate env templates Signed-off-by: Carlos A Becker --- internal/tmpl/tmpl.go | 5 ++++- internal/tmpl/tmpl_test.go | 13 +++++++++++-- www/docs/customization/build.md | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index e6371a95971..4e6d7d214e9 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -119,7 +119,10 @@ func New(ctx *context.Context) *Template { func (t *Template) WithEnvS(envs []string) *Template { result := map[string]string{} for _, env := range envs { - k, v, _ := strings.Cut(env, "=") + k, v, ok := strings.Cut(env, "=") + if !ok || k == "" || v == "" { + continue + } result[k] = v } return t.WithEnv(result) diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 14f8bb169a6..9a504d3e96f 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -159,12 +159,21 @@ func TestWithEnv(t *testing.T) { "FOO": "BAR", } ctx.Git.CurrentTag = "v1.2.3" - out, err := New(ctx).WithEnvS([]string{ + tpl := New(ctx).WithEnvS([]string{ "FOO=foo", "BAR=bar", - }).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}") + "NOVAL=", + "=NOKEY", + "=", + "NOTHING", + }) + out, err := tpl.Apply("{{ .Env.FOO }}-{{ .Env.BAR }}") require.NoError(t, err) require.Equal(t, "foo-bar", out) + + out, err = tpl.Apply(`{{ range $idx, $key := .Env }}{{ $idx }},{{ end }}`) + require.NoError(t, err) + require.Equal(t, "BAR,FOO,", out) } func TestFuncMap(t *testing.T) { diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index 472cf50c197..5eea2d8dce2 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -71,6 +71,8 @@ builds: # Custom environment variables to be set during the builds. # + # Invalid and empty environment variables are ignored. + # # Default: `os.Environ()` merged with what you set the root `env` section. env: - CGO_ENABLED=0 From b30aa1d832f9fd9b2636f99042d951262e179fcd Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 17:10:07 -0300 Subject: [PATCH 2/3] fix: keys with no values are actually valid --- internal/tmpl/tmpl.go | 2 +- internal/tmpl/tmpl_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index 4e6d7d214e9..1fda16039a6 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -120,7 +120,7 @@ func (t *Template) WithEnvS(envs []string) *Template { result := map[string]string{} for _, env := range envs { k, v, ok := strings.Cut(env, "=") - if !ok || k == "" || v == "" { + if !ok || k == "" { continue } result[k] = v diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 9a504d3e96f..bccb35ecc26 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -173,7 +173,7 @@ func TestWithEnv(t *testing.T) { out, err = tpl.Apply(`{{ range $idx, $key := .Env }}{{ $idx }},{{ end }}`) require.NoError(t, err) - require.Equal(t, "BAR,FOO,", out) + require.Equal(t, "BAR,FOO,NOVAL,", out) } func TestFuncMap(t *testing.T) { From 4ba8cee494e1ff1100f4dd74cdbde4fab97e4851 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 17:10:52 -0300 Subject: [PATCH 3/3] docs: wording improvements --- www/docs/customization/build.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index 5eea2d8dce2..c3dbbcad0cd 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -71,7 +71,7 @@ builds: # Custom environment variables to be set during the builds. # - # Invalid and empty environment variables are ignored. + # Invalid environment variables will be ignored. # # Default: `os.Environ()` merged with what you set the root `env` section. env: