Skip to content

Commit ba77dbe

Browse files
authored
fix(misconf): identify the chart file exactly by name (#8590)
Signed-off-by: nikpivkin <[email protected]>
1 parent 7bafdca commit ba77dbe

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pkg/iac/scanners/helm/scanner.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"fmt"
66
"io"
77
"io/fs"
8+
"path"
89
"path/filepath"
910
"strings"
1011
"sync"
1112

1213
"github.com/liamg/memoryfs"
14+
"helm.sh/helm/v3/pkg/chartutil"
1315

1416
"github.com/aquasecurity/trivy/pkg/iac/detection"
1517
"github.com/aquasecurity/trivy/pkg/iac/ignore"
@@ -55,14 +57,14 @@ func (s *Scanner) Name() string {
5557
return "Helm"
5658
}
5759

58-
func (s *Scanner) ScanFS(ctx context.Context, target fs.FS, path string) (scan.Results, error) {
60+
func (s *Scanner) ScanFS(ctx context.Context, fsys fs.FS, dir string) (scan.Results, error) {
5961

60-
if err := s.initRegoScanner(target); err != nil {
62+
if err := s.initRegoScanner(fsys); err != nil {
6163
return nil, fmt.Errorf("failed to init rego scanner: %w", err)
6264
}
6365

6466
var results []scan.Result
65-
if err := fs.WalkDir(target, path, func(path string, d fs.DirEntry, err error) error {
67+
if err := fs.WalkDir(fsys, dir, func(filePath string, d fs.DirEntry, err error) error {
6668
select {
6769
case <-ctx.Done():
6870
return ctx.Err()
@@ -77,16 +79,14 @@ func (s *Scanner) ScanFS(ctx context.Context, target fs.FS, path string) (scan.R
7779
return nil
7880
}
7981

80-
if detection.IsArchive(path) {
81-
if scanResults, err := s.getScanResults(path, ctx, target); err != nil {
82+
if detection.IsArchive(filePath) {
83+
scanResults, err := s.getScanResults(filePath, ctx, fsys)
84+
if err != nil {
8285
return err
83-
} else {
84-
results = append(results, scanResults...)
8586
}
86-
}
87-
88-
if strings.HasSuffix(path, "Chart.yaml") {
89-
if scanResults, err := s.getScanResults(filepath.Dir(path), ctx, target); err != nil {
87+
results = append(results, scanResults...)
88+
} else if path.Base(filePath) == chartutil.ChartfileName {
89+
if scanResults, err := s.getScanResults(filepath.Dir(filePath), ctx, fsys); err != nil {
9090
return err
9191
} else {
9292
results = append(results, scanResults...)

pkg/iac/scanners/helm/test/scanner_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strings"
99
"testing"
10+
"testing/fstest"
1011

1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
@@ -217,3 +218,11 @@ func fsysForAcrhive(t *testing.T, src string) fs.FS {
217218
require.NoError(t, err)
218219
return os.DirFS(tmpDir)
219220
}
221+
222+
func TestScaningNonHelmChartDoesNotCauseError(t *testing.T) {
223+
fsys := fstest.MapFS{
224+
"testChart.yaml": &fstest.MapFile{Data: []byte(`foo: bar`)},
225+
}
226+
_, err := helm.New().ScanFS(t.Context(), fsys, ".")
227+
require.NoError(t, err)
228+
}

0 commit comments

Comments
 (0)