Skip to content

Commit 76db76c

Browse files
authored
Merge pull request #1377 from fluxcd/chart-name-validations
Improve chart name validation
2 parents eff5a07 + 84b30d1 commit 76db76c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

internal/helm/chart/builder.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ func (r RemoteReference) Validate() error {
8181
if r.Name == "" {
8282
return fmt.Errorf("no name set for remote chart reference")
8383
}
84-
name := regexp.MustCompile("^([-a-z0-9]+/?)+$")
84+
name := regexp.MustCompile(`^([-a-z0-9]+/?\.?)+$`)
8585
if !name.MatchString(r.Name) {
86-
return fmt.Errorf("invalid chart name '%s': a valid name must be lower case letters and numbers and MAY be separated with dashes (-) or slashes (/)", r.Name)
86+
return fmt.Errorf("invalid chart name '%s': a valid name must be lower case letters and numbers and MAY be separated with dashes (-), slashes (/) or periods (.)", r.Name)
8787
}
8888
return nil
8989
}
@@ -199,6 +199,11 @@ func (b *Build) String() string {
199199

200200
// packageToPath attempts to package the given chart to the out filepath.
201201
func packageToPath(chart *helmchart.Chart, out string) error {
202+
// Names cannot have directory name characters.
203+
if chart.Name() != filepath.Base(chart.Name()) {
204+
return fmt.Errorf("%q is not a valid chart name", chart.Name())
205+
}
206+
202207
o, err := os.MkdirTemp("", "chart-build-*")
203208
if err != nil {
204209
return fmt.Errorf("failed to create temporary directory for chart: %w", err)

internal/helm/chart/builder_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ func TestRemoteReference_Validate(t *testing.T) {
113113
ref: RemoteReference{Name: "not//a/valid/chart"},
114114
wantErr: "invalid chart name 'not//a/valid/chart'",
115115
},
116+
{
117+
name: "ref with period in name",
118+
ref: RemoteReference{Name: "valid.chart.name"},
119+
},
120+
{
121+
name: "ref with double period in name",
122+
ref: RemoteReference{Name: "../valid-chart-name"},
123+
wantErr: "invalid chart name '../valid-chart-name",
124+
},
116125
}
117126
for _, tt := range tests {
118127
t.Run(tt.name, func(t *testing.T) {
@@ -246,6 +255,14 @@ func Test_packageToPath(t *testing.T) {
246255
g.Expect(out).To(BeARegularFile())
247256
_, err = secureloader.LoadFile(out)
248257
g.Expect(err).ToNot(HaveOccurred())
258+
259+
chart, err = secureloader.LoadFile("../testdata/charts/helmchart-badname-0.1.0.tgz")
260+
g.Expect(err).ToNot(HaveOccurred())
261+
g.Expect(chart).ToNot(BeNil())
262+
263+
out2 := tmpFile("chart-badname-0.1.0", ".tgz")
264+
err = packageToPath(chart, out2)
265+
g.Expect(err).To(HaveOccurred())
249266
}
250267

251268
func tmpFile(prefix, suffix string) string {
Binary file not shown.

0 commit comments

Comments
 (0)