Skip to content

Commit 74ea0f4

Browse files
authored
🐛 Fix .lib false positives in binary artifacts (#1879)
* ignore printable files * updates * e2e tests * e2e fix * comments
1 parent 2cb6541 commit 74ea0f4

File tree

4 files changed

+470
-35
lines changed

4 files changed

+470
-35
lines changed

checks/raw/binary_artifact.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"path/filepath"
2020
"strings"
21+
"unicode"
2122

2223
"github.com/h2non/filetype"
2324
"github.com/h2non/filetype/types"
@@ -92,8 +93,17 @@ var checkBinaryFileContent fileparser.DoWhileTrueOnFileContent = func(path strin
9293
}
9394

9495
exists1 := binaryFileTypes[t.Extension]
96+
if exists1 {
97+
*pfiles = append(*pfiles, checker.File{
98+
Path: path,
99+
Type: checker.FileTypeBinary,
100+
Offset: checker.OffsetDefault,
101+
})
102+
return true, nil
103+
}
104+
95105
exists2 := binaryFileTypes[strings.ReplaceAll(filepath.Ext(path), ".", "")]
96-
if exists1 || exists2 {
106+
if !isText(content) && exists2 {
97107
*pfiles = append(*pfiles, checker.File{
98108
Path: path,
99109
Type: checker.FileTypeBinary,
@@ -103,3 +113,16 @@ var checkBinaryFileContent fileparser.DoWhileTrueOnFileContent = func(path strin
103113

104114
return true, nil
105115
}
116+
117+
// TODO: refine this function.
118+
func isText(content []byte) bool {
119+
for _, c := range string(content) {
120+
if c == '\t' || c == '\n' || c == '\r' {
121+
continue
122+
}
123+
if !unicode.IsPrint(c) {
124+
return false
125+
}
126+
}
127+
return true
128+
}

checks/raw/binary_artifact_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package raw
1616

1717
import (
1818
"fmt"
19-
"log"
2019
"os"
2120
"testing"
2221

@@ -29,37 +28,40 @@ import (
2928
func TestBinaryArtifacts(t *testing.T) {
3029
t.Parallel()
3130
tests := []struct {
32-
name string
33-
inputFile string
34-
err error
35-
files []string
36-
expect int
31+
name string
32+
err error
33+
files []string
34+
expect int
3735
}{
3836
{
39-
name: "Jar file",
40-
inputFile: "../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
41-
err: nil,
37+
name: "Jar file",
38+
err: nil,
4239
files: []string{
4340
"../testdata/binaryartifacts/jars/aws-java-sdk-core-1.11.571.jar",
4441
},
4542
expect: 1,
4643
},
4744
{
48-
name: "non binary file",
49-
inputFile: "../testdata/licensedir/withlicense/LICENSE",
50-
err: nil,
45+
name: "non binary file",
46+
err: nil,
5147
files: []string{
5248
"../testdata/licensedir/withlicense/LICENSE",
5349
},
5450
},
5551
{
56-
name: "non binary file",
57-
inputFile: "../doesnotexist",
58-
err: nil,
52+
name: "non binary file",
53+
err: nil,
5954
files: []string{
6055
"../doesnotexist",
6156
},
6257
},
58+
{
59+
name: "printable character .lib",
60+
err: nil,
61+
files: []string{
62+
"../testdata/binaryartifacts/printable.lib",
63+
},
64+
},
6365
}
6466
for _, tt := range tests {
6567
tt := tt // Re-initializing variable so it is not changed while executing the closure below
@@ -72,7 +74,6 @@ func TestBinaryArtifacts(t *testing.T) {
7274
mockRepoClient.EXPECT().GetFileContent(gomock.Any()).DoAndReturn(func(file string) ([]byte, error) {
7375
// This will read the file and return the content
7476
content, err := os.ReadFile(file)
75-
log.Println(os.Getwd())
7677
if err != nil {
7778
return content, fmt.Errorf("%w", err)
7879
}

0 commit comments

Comments
 (0)