Skip to content

Commit

Permalink
Use different approach to fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
  • Loading branch information
egibs committed Dec 18, 2024
1 parent 53dc757 commit d3c9c8c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func processArchive(ctx context.Context, c malcontent.Config, rfs []fs.FS, archi
if err != nil {
return nil, fmt.Errorf("failed to determine file type: %w", err)
}
if ft != nil && ft.MIME == "application/octet-stream" {
if ft != nil && ft.MIME == "application/x-python-joblib" {
logger.Debugf("skipping unsupported archive: %s", archivePath)
return nil, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func extractNestedArchive(
if err != nil {
return fmt.Errorf("failed to determine file type: %w", err)
}
if ft != nil && ft.MIME == "application/octet-stream" {
if ft != nil && ft.MIME == "application/x-python-joblib" {
return nil
}
if ft != nil && ft.MIME == "application/zlib" {
Expand All @@ -55,7 +55,7 @@ func extractNestedArchive(
if err != nil {
return fmt.Errorf("failed to determine file type: %w", err)
}
if ft != nil && ft.MIME == "application/octet-stream" {
if ft != nil && ft.MIME == "application/x-python-joblib" {
return nil
}
if ft != nil && ft.MIME == "application/zlib" {
Expand Down Expand Up @@ -109,7 +109,7 @@ func ExtractArchiveToTempDir(ctx context.Context, path string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to determine file type: %w", err)
}
if ft != nil && ft.MIME == "application/octet-stream" {
if ft != nil && ft.MIME == "application/x-python-joblib" {
return "", nil
}
if ft != nil && ft.MIME == "application/zlib" {
Expand Down
35 changes: 19 additions & 16 deletions pkg/programkind/programkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var supportedKind = map[string]string{
"hh": "text/x-h",
"html": "",
"java": "text/x-java",
"joblib": "application/x-python-joblib",
"js": "application/javascript",
"lnk": "application/x-ms-shortcut",
"lua": "text/x-lua",
Expand Down Expand Up @@ -140,20 +141,6 @@ func makeFileType(path string, ext string, mime string) *FileType {
return &FileType{MIME: "application/json", Ext: ext}
}

// Capture joblib files that cannot be decompressed externally
// https://joblib.readthedocs.io/en/stable/generated/joblib.dump.html
joblibExts := []string{
"z",
"gz",
"bz2",
"xz",
"lzma",
}

if slices.Contains(joblibExts, ext) && mime == "application/octet-stream" {
return &FileType{MIME: "application/octet-stream", Ext: ext}
}

if supportedKind[ext] == "" {
return nil
}
Expand Down Expand Up @@ -193,8 +180,9 @@ func File(path string) (*FileType, error) {
// first strategy: mimetype
mtype, err := mimetype.DetectFile(path)
if err == nil {
ft := makeFileType(path, GetExt(path), mtype.String())
return ft, nil
if ft := makeFileType(path, mtype.Extension(), mtype.String()); ft != nil {
return ft, nil
}
}

// second strategy: path (extension, mostly)
Expand Down Expand Up @@ -249,6 +237,21 @@ func File(path string) (*FileType, error) {
return Path(".gzip"), nil
case hdr[0] == '\x78' && hdr[1] == '\x5E':
return Path(".Z"), nil
// Capture joblib files that cannot be decompressed externally
// https://joblib.readthedocs.io/en/stable/generated/joblib.dump.html
// Check the header, file extension, and MIME type to be as specific as possible
case hdr[0] == '\x5A' && hdr[1] == '\x46' && hdr[2] == '\x30' && hdr[3] == '\x78':
joblibExts := []string{
".z",
".gz",
".bz2",
".xz",
".lzma",
}

if slices.Contains(joblibExts, GetExt(path)) && mtype.String() == "application/octet-stream" {
return Path(".joblib"), nil
}
}
return nil, nil
}
Expand Down

0 comments on commit d3c9c8c

Please sign in to comment.