Skip to content

Commit

Permalink
Allow failure to unzip invalid zip-files (#164)
Browse files Browse the repository at this point in the history
* handle files that are not valid zip files

* improve error handling
  • Loading branch information
emilwareus authored Dec 11, 2023
1 parent c3bda1e commit bde0708
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
22 changes: 13 additions & 9 deletions internal/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"bufio"
"crypto/md5" // #nosec
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -162,29 +163,32 @@ func (f *Fingerprinter) FingerprintFiles(rootPath string, exclusions []string) (
}

func computeMD5ForFileAndZip(fileInfo os.FileInfo, path string, exclusions []string) ([]FileFingerprint, error) {
fingerprints := []FileFingerprint{}

if !shouldProcessFile(fileInfo, exclusions, path) {
return fingerprints, nil
return nil, nil
}

// Scan the contents of compressed files
// such as .jar and .nupkg
var fingerprints []FileFingerprint

// If the file should be unzipped, try to unzip and fingerprint it
if shouldUnzip(path) {
fingerprintsZip, err := inMemFingerprintingCompressedContent(path, exclusions)
if err != nil {
return nil, err
if errors.Is(err, zip.ErrFormat) {
fmt.Printf("WARNING: Could not unpack and fingerprint contents of compressed file [%s]. Error: %v\n", path, err)
} else {
return nil, err
}
}
fingerprints = append(fingerprints, fingerprintsZip...)
}

// Compute the MD5 for the file
fingerprint, err := computeMD5ForFile(path)
if err != nil {
return nil, err
}

fingerprints = append(fingerprints, fingerprint)

return fingerprints, nil
return append(fingerprints, fingerprint), nil
}

func isSymlink(filename string) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/fingerprint/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestFingerprintFiles(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, fingerprints)
assert.NotEmpty(t, fingerprints)
assert.Equal(t, 1, fingerprints.Len())
assert.Equal(t, 2, fingerprints.Len())
assert.Equal(t, "file=72214db4e1e543018d1bafe86ea3b444,21,testdata/fingerprinter/testfile.py", fingerprints.Entries[0].ToString())

// Test no file
Expand Down
1 change: 1 addition & 0 deletions internal/fingerprint/testdata/fingerprinter/wfailing.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xxx

0 comments on commit bde0708

Please sign in to comment.