From 00cd96d12089db61e1183eb6c25ea1ae34ec0cfa Mon Sep 17 00:00:00 2001 From: KangManJoo Date: Thu, 3 Oct 2024 04:04:22 +0900 Subject: [PATCH] fix(helm): escape consecutive commas in cleanSetParameters (#19269) (#20113) Signed-off-by: KangManJoo Signed-off-by: daengdaengLee Co-authored-by: daengdaengLee Signed-off-by: Adrian Aneci --- util/helm/cmd.go | 29 +++++++++++++++++++++++------ util/helm/helm_test.go | 2 ++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/util/helm/cmd.go b/util/helm/cmd.go index 6b0e30ed2fe75..55d9e8670b461 100644 --- a/util/helm/cmd.go +++ b/util/helm/cmd.go @@ -342,19 +342,36 @@ type TemplateOpts struct { SkipCrds bool } -var ( - re = regexp.MustCompile(`([^\\]),`) - apiVersionsRemover = regexp.MustCompile(`(--api-versions [^ ]+ )+`) -) - func cleanSetParameters(val string) string { // `{}` equal helm list parameters format, so don't escape `,`. if strings.HasPrefix(val, `{`) && strings.HasSuffix(val, `}`) { return val } - return re.ReplaceAllString(val, `$1\,`) + + val = replaceAllWithLookbehind(val, ',', `\,`, '\\') + return val } +func replaceAllWithLookbehind(val string, old rune, new string, lookbehind rune) string { + var result strings.Builder + var prevR rune + for _, r := range val { + if r == old { + if prevR != lookbehind { + result.WriteString(new) + } else { + result.WriteRune(old) + } + } else { + result.WriteRune(r) + } + prevR = r + } + return result.String() +} + +var apiVersionsRemover = regexp.MustCompile(`(--api-versions [^ ]+ )+`) + func (c *Cmd) template(chartPath string, opts *TemplateOpts) (string, string, error) { if callback, err := cleanupChartLockFile(filepath.Clean(path.Join(c.WorkDir, chartPath))); err == nil { defer callback() diff --git a/util/helm/helm_test.go b/util/helm/helm_test.go index 8468b9f36624b..58dd273481f27 100644 --- a/util/helm/helm_test.go +++ b/util/helm/helm_test.go @@ -166,6 +166,8 @@ func TestHelmArgCleaner(t *testing.T) { `not, clean`: `not\, clean`, `a\,b,c`: `a\,b\,c`, `{a,b,c}`: `{a,b,c}`, + `,,,,,\,`: `\,\,\,\,\,\,`, + `\,,\\,,`: `\,\,\\,\,`, } { cleaned := cleanSetParameters(input) assert.Equal(t, expected, cleaned)