Skip to content

Commit 6ba77fe

Browse files
authored
Avoid propagating errors for invalid extractions when ExitExtraction is false (#1086)
* Avoid propagating errors for invalid extractions when not using ExitExtraction Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> * Include error in debug log Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> --------- Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 78972f2 commit 6ba77fe

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

pkg/action/archive_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func TestExtractionMultiple(t *testing.T) {
7676
t.Run(tt.path, func(t *testing.T) {
7777
t.Parallel()
7878
ctx := context.Background()
79-
dir, err := archive.ExtractArchiveToTempDir(ctx, tt.path)
79+
80+
dir, err := archive.ExtractArchiveToTempDir(ctx, malcontent.Config{}, tt.path)
8081
if err != nil {
8182
t.Fatal(err)
8283
}
@@ -103,7 +104,7 @@ func TestExtractionMultiple(t *testing.T) {
103104
func TestExtractTar(t *testing.T) {
104105
t.Parallel()
105106
ctx := context.Background()
106-
dir, err := archive.ExtractArchiveToTempDir(ctx, filepath.Join("testdata", "apko.tar.gz"))
107+
dir, err := archive.ExtractArchiveToTempDir(ctx, malcontent.Config{}, filepath.Join("testdata", "apko.tar.gz"))
107108
if err != nil {
108109
t.Fatal(err)
109110
}
@@ -131,7 +132,7 @@ func TestExtractTar(t *testing.T) {
131132
func TestExtractGzip(t *testing.T) {
132133
t.Parallel()
133134
ctx := context.Background()
134-
dir, err := archive.ExtractArchiveToTempDir(ctx, filepath.Join("testdata", "apko.gz"))
135+
dir, err := archive.ExtractArchiveToTempDir(ctx, malcontent.Config{}, filepath.Join("testdata", "apko.gz"))
135136
if err != nil {
136137
t.Fatal(err)
137138
}
@@ -159,7 +160,7 @@ func TestExtractGzip(t *testing.T) {
159160
func TestExtractZip(t *testing.T) {
160161
t.Parallel()
161162
ctx := context.Background()
162-
dir, err := archive.ExtractArchiveToTempDir(ctx, filepath.Join("testdata", "apko.zip"))
163+
dir, err := archive.ExtractArchiveToTempDir(ctx, malcontent.Config{}, filepath.Join("testdata", "apko.zip"))
163164
if err != nil {
164165
t.Fatal(err)
165166
}
@@ -187,7 +188,7 @@ func TestExtractZip(t *testing.T) {
187188
func TestExtractNestedArchive(t *testing.T) {
188189
t.Parallel()
189190
ctx := context.Background()
190-
dir, err := archive.ExtractArchiveToTempDir(ctx, filepath.Join("testdata", "apko_nested.tar.gz"))
191+
dir, err := archive.ExtractArchiveToTempDir(ctx, malcontent.Config{}, filepath.Join("testdata", "apko_nested.tar.gz"))
191192
if err != nil {
192193
t.Fatal(err)
193194
}

pkg/action/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func processArchive(ctx context.Context, c malcontent.Config, rfs []fs.FS, archi
666666

667667
var frs sync.Map
668668

669-
tmpRoot, err := archive.ExtractArchiveToTempDir(ctx, archivePath)
669+
tmpRoot, err := archive.ExtractArchiveToTempDir(ctx, c, archivePath)
670670
if err != nil {
671671
return nil, fmt.Errorf("extract to temp: %w", err)
672672
}

pkg/archive/archive.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/chainguard-dev/clog"
16+
"github.com/chainguard-dev/malcontent/pkg/malcontent"
1617
"github.com/chainguard-dev/malcontent/pkg/pool"
1718
"github.com/chainguard-dev/malcontent/pkg/programkind"
1819
)
@@ -33,7 +34,7 @@ func IsValidPath(target, dir string) bool {
3334
return strings.HasPrefix(filepath.Clean(target), filepath.Clean(dir))
3435
}
3536

36-
func extractNestedArchive(ctx context.Context, d string, f string, extracted *sync.Map, logger *clog.Logger) error {
37+
func extractNestedArchive(ctx context.Context, c malcontent.Config, d string, f string, extracted *sync.Map, logger *clog.Logger) error {
3738
if ctx.Err() != nil {
3839
return ctx.Err()
3940
}
@@ -104,7 +105,10 @@ func extractNestedArchive(ctx context.Context, d string, f string, extracted *sy
104105

105106
err = extract(ctx, archivePath, fullPath)
106107
if err != nil {
107-
return fmt.Errorf("failed to extract archive: %w", err)
108+
if c.ExitExtraction {
109+
return fmt.Errorf("failed to extract archive: %w", err)
110+
}
111+
logger.Debugf("ignoring extraction error for %s: %s", f, err.Error())
108112
}
109113

110114
extracted.Store(f, true)
@@ -124,7 +128,7 @@ func extractNestedArchive(ctx context.Context, d string, f string, extracted *sy
124128
}
125129
rel := file.Name()
126130
if _, alreadyProcessed := extracted.Load(rel); !alreadyProcessed {
127-
if err := extractNestedArchive(ctx, d, rel, extracted, logger); err != nil {
131+
if err := extractNestedArchive(ctx, c, d, rel, extracted, logger); err != nil {
128132
return fmt.Errorf("process nested file %s: %w", rel, err)
129133
}
130134
}
@@ -133,7 +137,7 @@ func extractNestedArchive(ctx context.Context, d string, f string, extracted *sy
133137
}
134138

135139
// extractArchiveToTempDir creates a temporary directory and extracts the archive file for scanning.
136-
func ExtractArchiveToTempDir(ctx context.Context, path string) (string, error) {
140+
func ExtractArchiveToTempDir(ctx context.Context, c malcontent.Config, path string) (string, error) {
137141
if ctx.Err() != nil {
138142
return "", ctx.Err()
139143
}
@@ -196,7 +200,7 @@ func ExtractArchiveToTempDir(ctx context.Context, path string) (string, error) {
196200

197201
ext := programkind.GetExt(path)
198202
if _, ok := programkind.ArchiveMap[ext]; ok {
199-
if err := extractNestedArchive(ctx, tmpDir, rel, &extractedFiles, logger); err != nil {
203+
if err := extractNestedArchive(ctx, c, tmpDir, rel, &extractedFiles, logger); err != nil {
200204
return err
201205
}
202206
}

0 commit comments

Comments
 (0)