Skip to content
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
12 changes: 9 additions & 3 deletions pkg/attestation/crafter/materials/helmchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"archive/tar"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -121,12 +122,12 @@ func (c *HelmChartCrafter) craftLocalHelmChart(ctx context.Context, filepath str
// it was compressed from. So, we can check if the file name contains the required file names
// Ex: helm-chart/Chart.yaml, helm-chart/values.yaml
if strings.Contains(header.Name, chartFileName) {
if err := c.validateYamlFile(tarReader); err != nil {
if err := c.validateYamlFile(tarReader, false); err != nil {
return nil, fmt.Errorf("invalid Chart.yaml file: %w", err)
}
chartFileValid = true
} else if strings.Contains(header.Name, chartValuesYamlFileName) {
if err := c.validateYamlFile(tarReader); err != nil {
if err := c.validateYamlFile(tarReader, true); err != nil {
return nil, fmt.Errorf("invalid values.yaml file: %w", err)
}
chartValuesValid = true
Expand All @@ -148,9 +149,14 @@ func (c *HelmChartCrafter) craftLocalHelmChart(ctx context.Context, filepath str
}

// validateYamlFile validates the YAML file just by trying to unmarshal it
func (c *HelmChartCrafter) validateYamlFile(r io.Reader) error {
func (c *HelmChartCrafter) validateYamlFile(r io.Reader, allowEmpty bool) error {
v := make(map[string]interface{})
if err := yaml.NewDecoder(r).Decode(v); err != nil {
// io.EOF means the file is empty or contains only comments
// This is valid for values.yaml
if errors.Is(err, io.EOF) && allowEmpty {
return nil
}
return fmt.Errorf("failed to unmarshal YAML file: %w", err)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/attestation/crafter/materials/helmchart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ func TestHelmChartCraft(t *testing.T) {
wantDigest: "sha256:08a46a850789938ede61d6a53552f48cb8ba74c4e17dcf30c9c50e5783ca6a13",
wantFilename: "valid-chart.tgz",
},
{
name: "chart with empty values.yaml",
filePath: "./testdata/empty-values.tgz",
wantDigest: "sha256:6c5bc910da7ecb00aa1c7be70e51db237d129e3f41ff6ada1d11ea402ff7082e",
wantFilename: "empty-values.tgz",
},
}

assert := assert.New(t)
Expand Down
Binary file not shown.
Loading