From 96d9ab9663b69cbd85444ca5232d8283017eeeea Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Tue, 1 Sep 2020 10:44:52 -0600 Subject: [PATCH] fix name length check on lint (#8543) Signed-off-by: Matt Butcher --- go.mod | 1 + pkg/action/upgrade.go | 3 ++- pkg/lint/rules/template.go | 3 +++ pkg/lint/rules/template_test.go | 10 ++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 66447ee3d..4b7c90f9d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.13 require ( github.com/BurntSushi/toml v0.3.1 github.com/DATA-DOG/go-sqlmock v1.4.1 + github.com/Masterminds/goutils v1.1.0 github.com/Masterminds/semver/v3 v3.1.0 github.com/Masterminds/sprig/v3 v3.1.0 github.com/Masterminds/squirrel v1.4.0 diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index e7c2aec25..b707e7e69 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -147,7 +147,8 @@ func validateReleaseName(releaseName string) error { return errMissingRelease } - if !ValidName.MatchString(releaseName) || (len(releaseName) > releaseNameMaxLen) { + // Check length first, since that is a less expensive operation. + if len(releaseName) > releaseNameMaxLen || !ValidName.MatchString(releaseName) { return errInvalidName } diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 9a3a2a1ba..cd54c8915 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -165,6 +165,9 @@ func validateYamlContent(err error) error { } func validateMetadataName(obj *K8sYamlStruct) error { + if len(obj.Metadata.Name) == 0 || len(obj.Metadata.Name) > 253 { + return fmt.Errorf("object name must be between 0 and 253 characters: %q", obj.Metadata.Name) + } // This will return an error if the characters do not abide by the standard OR if the // name is left empty. if validName.MatchString(obj.Metadata.Name) { diff --git a/pkg/lint/rules/template_test.go b/pkg/lint/rules/template_test.go index ae82c8922..3b0307c1d 100644 --- a/pkg/lint/rules/template_test.go +++ b/pkg/lint/rules/template_test.go @@ -22,6 +22,8 @@ import ( "strings" "testing" + "github.com/Masterminds/goutils" + "helm.sh/helm/v3/internal/test/ensure" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chartutil" @@ -119,6 +121,14 @@ func TestValidateMetadataName(t *testing.T) { "a..b": false, "%^&#$%*@^*@&#^": false, } + + // The length checker should catch this first. So this is not true fuzzing. + tooLong, err := goutils.RandomAlphaNumeric(300) + if err != nil { + t.Fatalf("Randomizer failed to initialize: %s", err) + } + names[tooLong] = false + for input, expectPass := range names { obj := K8sYamlStruct{Metadata: k8sYamlMetadata{Name: input}} if err := validateMetadataName(&obj); (err == nil) != expectPass {