Skip to content

Improve chart name validation #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/helm/chart/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func (r RemoteReference) Validate() error {
if r.Name == "" {
return fmt.Errorf("no name set for remote chart reference")
}
name := regexp.MustCompile("^([-a-z0-9]+/?)+$")
name := regexp.MustCompile(`^([-a-z0-9]+/?\.?)+$`)
Copy link
Contributor Author

@darkowlzz darkowlzz Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using "`" here to avoid double escaping "\". Suggested by linter.

if !name.MatchString(r.Name) {
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)
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)
}
return nil
}
Expand Down Expand Up @@ -199,6 +199,11 @@ func (b *Build) String() string {

// packageToPath attempts to package the given chart to the out filepath.
func packageToPath(chart *helmchart.Chart, out string) error {
// Names cannot have directory name characters.
if chart.Name() != filepath.Base(chart.Name()) {
return fmt.Errorf("%q is not a valid chart name", chart.Name())
}

o, err := os.MkdirTemp("", "chart-build-*")
if err != nil {
return fmt.Errorf("failed to create temporary directory for chart: %w", err)
Expand Down
17 changes: 17 additions & 0 deletions internal/helm/chart/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func TestRemoteReference_Validate(t *testing.T) {
ref: RemoteReference{Name: "not//a/valid/chart"},
wantErr: "invalid chart name 'not//a/valid/chart'",
},
{
name: "ref with period in name",
ref: RemoteReference{Name: "valid.chart.name"},
},
{
name: "ref with double period in name",
ref: RemoteReference{Name: "../valid-chart-name"},
wantErr: "invalid chart name '../valid-chart-name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -246,6 +255,14 @@ func Test_packageToPath(t *testing.T) {
g.Expect(out).To(BeARegularFile())
_, err = secureloader.LoadFile(out)
g.Expect(err).ToNot(HaveOccurred())

chart, err = secureloader.LoadFile("../testdata/charts/helmchart-badname-0.1.0.tgz")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(chart).ToNot(BeNil())

out2 := tmpFile("chart-badname-0.1.0", ".tgz")
err = packageToPath(chart, out2)
g.Expect(err).To(HaveOccurred())
}

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