From e252572bda922e200cd8e3b508088bf87f930139 Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Tue, 25 Nov 2025 13:05:57 +0000 Subject: [PATCH] fix: panic in lint when CRD file is empty Fixes #31571 The YAML decoder can return nil when a CRD file contains only comments or whitespace. Added nil check to skip validation in these cases. This fixes a panic in the CRD linting rule. Signed-off-by: Evans Mungai --- pkg/chart/v2/lint/rules/crds.go | 5 +++++ pkg/chart/v2/lint/rules/crds_test.go | 13 +++++++++++++ .../v2/lint/rules/testdata/emptycrd/Chart.yaml | 4 ++++ .../v2/lint/rules/testdata/emptycrd/crds/empty.yaml | 3 +++ .../lint/rules/testdata/emptycrd/templates/.gitkeep | 0 .../v2/lint/rules/testdata/emptycrd/values.yaml | 1 + 6 files changed, 26 insertions(+) create mode 100644 pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml create mode 100644 pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml create mode 100644 pkg/chart/v2/lint/rules/testdata/emptycrd/templates/.gitkeep create mode 100644 pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml diff --git a/pkg/chart/v2/lint/rules/crds.go b/pkg/chart/v2/lint/rules/crds.go index 49e30192a..09ea17bd8 100644 --- a/pkg/chart/v2/lint/rules/crds.go +++ b/pkg/chart/v2/lint/rules/crds.go @@ -80,6 +80,11 @@ func Crds(linter *support.Linter) { return } + // Skip validation if yamlStruct is nil (empty document or only comments/whitespace) + if yamlStruct == nil { + continue + } + linter.RunLinterRule(support.ErrorSev, fpath, validateCrdAPIVersion(yamlStruct)) linter.RunLinterRule(support.ErrorSev, fpath, validateCrdKind(yamlStruct)) } diff --git a/pkg/chart/v2/lint/rules/crds_test.go b/pkg/chart/v2/lint/rules/crds_test.go index e644f182f..d0553dc0d 100644 --- a/pkg/chart/v2/lint/rules/crds_test.go +++ b/pkg/chart/v2/lint/rules/crds_test.go @@ -25,6 +25,7 @@ import ( ) const invalidCrdsDir = "./testdata/invalidcrdsdir" +const emptyCrdDir = "./testdata/emptycrd" func TestInvalidCrdsDir(t *testing.T) { linter := support.Linter{ChartDir: invalidCrdsDir} @@ -34,3 +35,15 @@ func TestInvalidCrdsDir(t *testing.T) { assert.Len(t, res, 1) assert.ErrorContains(t, res[0].Err, "not a directory") } + +// TestEmptyCrd tests that empty CRD files (containing only comments/whitespace) +// don't cause a panic when the YAML decoder returns nil. +// This is a regression test for issue #31571. +func TestEmptyCrd(t *testing.T) { + linter := support.Linter{ChartDir: emptyCrdDir} + Crds(&linter) + res := linter.Messages + + // Should not panic and should have no errors + assert.Len(t, res, 0) +} diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml new file mode 100644 index 000000000..80f4faaca --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: emptycrd +description: A Helm chart with empty CRD file +version: 0.1.0 diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml new file mode 100644 index 000000000..743b6be06 --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/crds/empty.yaml @@ -0,0 +1,3 @@ +# This file contains only comments and whitespace +# It will decode to nil in the YAML decoder + diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/templates/.gitkeep b/pkg/chart/v2/lint/rules/testdata/emptycrd/templates/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml b/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml new file mode 100644 index 000000000..0eba2d28f --- /dev/null +++ b/pkg/chart/v2/lint/rules/testdata/emptycrd/values.yaml @@ -0,0 +1 @@ +# Empty values file