Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix .lib false positives in binary artifacts #1879

Merged
merged 5 commits into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions checks/raw/binary_artifact.go
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"path/filepath"
"strings"
"unicode"

"github.com/h2non/filetype"
"github.com/h2non/filetype/types"
Expand Down Expand Up @@ -91,6 +92,11 @@ var checkBinaryFileContent fileparser.DoWhileTrueOnFileContent = func(path strin
return false, sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("filetype.Get:%v", err))
}

// Sanity check the file contains non-readable characters.
if isText(content) {
return true, nil
}

exists1 := binaryFileTypes[t.Extension]
exists2 := binaryFileTypes[strings.ReplaceAll(filepath.Ext(path), ".", "")]
if exists1 || exists2 {
Expand All @@ -103,3 +109,16 @@ var checkBinaryFileContent fileparser.DoWhileTrueOnFileContent = func(path strin

return true, nil
}

// TODO: refine this function.
laurentsimon marked this conversation as resolved.
Show resolved Hide resolved
func isText(content []byte) bool {
for _, c := range string(content) {
if c == '\t' || c == '\n' || c == '\r' {
continue
}
if !unicode.IsPrint(c) {
return false
}
}
return true
}
33 changes: 17 additions & 16 deletions checks/raw/binary_artifact_test.go
Expand Up @@ -16,7 +16,6 @@ package raw

import (
"fmt"
"log"
"os"
"testing"

Expand All @@ -29,37 +28,40 @@ import (
func TestBinaryArtifacts(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputFile string
err error
files []string
expect int
name string
err error
files []string
expect int
}{
{
name: "Jar file",
inputFile: "../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
err: nil,
name: "Jar file",
err: nil,
files: []string{
"../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
},
expect: 1,
},
{
name: "non binary file",
inputFile: "../testdata/licensedir/withlicense/LICENSE",
err: nil,
name: "non binary file",
err: nil,
files: []string{
"../testdata/licensedir/withlicense/LICENSE",
},
},
{
name: "non binary file",
inputFile: "../doesnotexist",
err: nil,
name: "non binary file",
err: nil,
files: []string{
"../doesnotexist",
},
},
{
name: "printable character .lib",
err: nil,
files: []string{
"../testdata/binaryartifacts/printable.lib",
},
},
}
for _, tt := range tests {
tt := tt // Re-initializing variable so it is not changed while executing the closure below
Expand All @@ -72,7 +74,6 @@ func TestBinaryArtifacts(t *testing.T) {
mockRepoClient.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(file string) ([]byte, error) {
// This will read the file and return the content
content, err := os.ReadFile(file)
log.Println(os.Getwd())
if err != nil {
return content, fmt.Errorf("%w", err)
}
Expand Down