From 82b0baddbc8c49ce75342c7da6d0c58e203ed44c Mon Sep 17 00:00:00 2001 From: foyerunix Date: Fri, 16 May 2025 11:27:47 +0000 Subject: [PATCH] fix: correctly check for chart directory conflict This commit move the check for directory conflict to the Expand function. This let us check if we will overwrite the actual chart directory. The previous implementation would check if a directory suffixed with the chart version exist. This would results in: - Creating a spuerfluous directory with the chart name suffixed by the chart version. - Not preventing the overwrite of an existing chart with the same name but with a different version. Signed-off-by: foyerunix --- pkg/action/pull.go | 17 ----------------- pkg/chart/v2/util/expand.go | 9 +++++++++ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/pkg/action/pull.go b/pkg/action/pull.go index a2f53af0d..c228dde21 100644 --- a/pkg/action/pull.go +++ b/pkg/action/pull.go @@ -150,23 +150,6 @@ func (p *Pull) Run(chartRef string) (string, error) { if !filepath.IsAbs(ud) { ud = filepath.Join(p.DestDir, ud) } - // Let udCheck to check conflict file/dir without replacing ud when untarDir is the current directory(.). - udCheck := ud - if udCheck == "." { - _, udCheck = filepath.Split(chartRef) - } else { - _, chartName := filepath.Split(chartRef) - udCheck = filepath.Join(udCheck, chartName) - } - - if _, err := os.Stat(udCheck); err != nil { - if err := os.MkdirAll(udCheck, 0755); err != nil { - return out.String(), fmt.Errorf("failed to untar (mkdir): %w", err) - } - } else { - return out.String(), fmt.Errorf("failed to untar: a file or directory with the name %s already exists", udCheck) - } - return out.String(), chartutil.ExpandFile(ud, saved) } return out.String(), nil diff --git a/pkg/chart/v2/util/expand.go b/pkg/chart/v2/util/expand.go index 9d08571ed..b78482693 100644 --- a/pkg/chart/v2/util/expand.go +++ b/pkg/chart/v2/util/expand.go @@ -61,6 +61,15 @@ func Expand(dir string, r io.Reader) error { return err } + // Check if chartdir conflict with an already existing directory + if _, err := os.Stat(chartdir); err != nil { + if err := os.MkdirAll(chartdir, 0755); err != nil { + return fmt.Errorf("failed to untar (mkdir): %w", err) + } + } else { + return fmt.Errorf("failed to untar: a file or directory with the name %s already exists", chartdir) + } + // Copy all files verbatim. We don't parse these files because parsing can remove // comments. for _, file := range files {