@@ -13,6 +13,7 @@ import (
1313 "time"
1414
1515 "github.com/chainguard-dev/clog"
16+ "github.com/chainguard-dev/malcontent/pkg/file"
1617 "github.com/chainguard-dev/malcontent/pkg/pool"
1718 "github.com/chainguard-dev/malcontent/pkg/programkind"
1819 zip "github.com/klauspost/compress/zip"
@@ -72,11 +73,11 @@ func ExtractZip(ctx context.Context, d string, f string) error {
7273 return fmt .Errorf ("failed to create extraction directory: %w" , err )
7374 }
7475
75- for _ , file := range read .File {
76- if file .Mode ().IsDir () {
77- clean := filepath .Clean (filepath .ToSlash (file .Name ))
76+ for _ , zf := range read .File {
77+ if zf .Mode ().IsDir () {
78+ clean := filepath .Clean (filepath .ToSlash (zf .Name ))
7879 if strings .Contains (clean , ".." ) {
79- logger .Warnf ("skipping potentially unsafe directory path: %s" , file .Name )
80+ logger .Warnf ("skipping potentially unsafe directory path: %s" , zf .Name )
8081 continue
8182 }
8283
@@ -95,12 +96,12 @@ func ExtractZip(ctx context.Context, d string, f string) error {
9596 g , gCtx := errgroup .WithContext (ctx )
9697 g .SetLimit (runtime .GOMAXPROCS (0 ))
9798
98- for _ , file := range read .File {
99- if file .Mode ().IsDir () {
99+ for _ , zf := range read .File {
100+ if zf .Mode ().IsDir () {
100101 continue
101102 }
102103 g .Go (func () error {
103- return extractFile (gCtx , file , d , logger )
104+ return extractFile (gCtx , zf , d , logger )
104105 })
105106 }
106107
@@ -110,24 +111,24 @@ func ExtractZip(ctx context.Context, d string, f string) error {
110111 return nil
111112}
112113
113- func extractFile (ctx context.Context , file * zip.File , destDir string , logger * clog.Logger ) error {
114+ func extractFile (ctx context.Context , zf * zip.File , destDir string , logger * clog.Logger ) error {
114115 if ctx .Err () != nil {
115116 return ctx .Err ()
116117 }
117118
118119 // macOS will encounter issues with paths like META-INF/LICENSE and META-INF/license/foo
119120 // this case insensitivity will break scans, so rename files that collide with existing directories
120121 if runtime .GOOS == "darwin" {
121- if _ , err := os .Stat (filepath .Join (destDir , file .Name )); err == nil {
122- file .Name = fmt .Sprintf ("%s%d" , file .Name , time .Now ().UnixNano ())
122+ if _ , err := os .Stat (filepath .Join (destDir , zf .Name )); err == nil {
123+ zf .Name = fmt .Sprintf ("%s%d" , zf .Name , time .Now ().UnixNano ())
123124 }
124125 }
125126
126- buf := zipPool .Get (zipBuffer ) //nolint:nilaway // the buffer pool is created in archive.go
127+ buf := zipPool .Get (file . ZipBuffer ) //nolint:nilaway // the buffer pool is created in archive.go
127128
128- clean := filepath .Clean (filepath .ToSlash (file .Name ))
129+ clean := filepath .Clean (filepath .ToSlash (zf .Name ))
129130 if strings .Contains (clean , ".." ) {
130- logger .Warnf ("skipping potentially unsafe file path: %s" , file .Name )
131+ logger .Warnf ("skipping potentially unsafe file path: %s" , zf .Name )
131132 return nil
132133 }
133134
@@ -141,7 +142,7 @@ func extractFile(ctx context.Context, file *zip.File, destDir string, logger *cl
141142 return fmt .Errorf ("failed to create directory structure: %w" , err )
142143 }
143144
144- src , err := file .Open ()
145+ src , err := zf .Open ()
145146 if err != nil {
146147 return fmt .Errorf ("failed to open archived file: %w" , err )
147148 }
@@ -159,15 +160,15 @@ func extractFile(ctx context.Context, file *zip.File, destDir string, logger *cl
159160
160161 var written int64
161162 for {
162- if written > 0 && written % zipBuffer == 0 && ctx .Err () != nil {
163+ if written > 0 && written % file . ZipBuffer == 0 && ctx .Err () != nil {
163164 return ctx .Err ()
164165 }
165166
166167 n , err := src .Read (buf )
167168 if n > 0 {
168169 written += int64 (n )
169- if written > maxBytes {
170- return fmt .Errorf ("file exceeds maximum allowed size (%d bytes): %s" , maxBytes , target )
170+ if written > file . MaxBytes {
171+ return fmt .Errorf ("file exceeds maximum allowed size (%d bytes): %s" , file . MaxBytes , target )
171172 }
172173
173174 if _ , writeErr := dst .Write (buf [:n ]); writeErr != nil {
0 commit comments