diff --git a/utils/tar.go b/utils/tar.go index 477aa9cc..26a47a50 100644 --- a/utils/tar.go +++ b/utils/tar.go @@ -58,7 +58,10 @@ func SelectivelyExtractArchive(r io.Reader, prefix string, toExtract []string, m // Get element filename filename := hdr.Name - filename = strings.TrimPrefix(filename, prefix) + filename = strings.TrimPrefix(filename, "./") + if prefix != "" { + filename = strings.TrimPrefix(filename, prefix) + } // Determine if we should extract the element toBeExtracted := false diff --git a/utils/utils_test.go b/utils/utils_test.go index e543c1c1..6b46b055 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -65,13 +65,13 @@ func TestTar(t *testing.T) { testArchivePath := path.Join(path.Dir(filepath)) + filename // Extract non compressed data - data, err = SelectivelyExtractArchive(bytes.NewReader([]byte("that string does not represent a tar or tar-gzip file")), "./", []string{}, 0) + data, err = SelectivelyExtractArchive(bytes.NewReader([]byte("that string does not represent a tar or tar-gzip file")), "", []string{}, 0) assert.Error(t, err, "Extracting non compressed data should return an error") // Extract an archive f, _ := os.Open(testArchivePath) defer f.Close() - data, err = SelectivelyExtractArchive(f, "./", []string{"test/"}, 0) + data, err = SelectivelyExtractArchive(f, "", []string{"test/"}, 0) assert.Nil(t, err) if c, n := data["test/test.txt"]; !n { @@ -86,7 +86,7 @@ func TestTar(t *testing.T) { // File size limit f, _ = os.Open(testArchivePath) defer f.Close() - data, err = SelectivelyExtractArchive(f, "./", []string{"test"}, 50) + data, err = SelectivelyExtractArchive(f, "", []string{"test"}, 50) assert.Equal(t, ErrExtractedFileTooBig, err) } } diff --git a/worker/detectors/data/tar.go b/worker/detectors/data/tar.go index ae1cc5a2..93a457d1 100644 --- a/worker/detectors/data/tar.go +++ b/worker/detectors/data/tar.go @@ -42,5 +42,5 @@ func (detector *TarDataDetector) Supported(path string, format string) bool { } func (detector *TarDataDetector) Detect(layerReader io.ReadCloser, toExtract []string, maxFileSize int64) (map[string][]byte, error) { - return utils.SelectivelyExtractArchive(layerReader, "./", toExtract, maxFileSize) + return utils.SelectivelyExtractArchive(layerReader, "", toExtract, maxFileSize) }