Skip to content

Commit 44e3d3f

Browse files
committed
refactor: simplify binary detection with filetype
- replace gofile mime detection with filetype matches check - remove custom isBinaryMIME helper and associated tests - update module dependencies to use github.com/h2non/filetype
1 parent 00f6a9c commit 44e3d3f

4 files changed

Lines changed: 5 additions & 79 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require github.com/fatih/color v1.19.0
77
require github.com/rs/zerolog v1.35.0
88

99
require (
10-
github.com/shirou/gofile v0.0.0-20260314143841-7dc06be96404
10+
github.com/h2non/filetype v1.1.3
1111
github.com/spf13/pflag v1.0.10
1212
)
1313

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w=
22
github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE=
3+
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
4+
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
35
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
46
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
57
github.com/mattn/go-isatty v0.0.21 h1:xYae+lCNBP7QuW4PUnNG61ffM4hVIfm+zUzDuSzYLGs=
68
github.com/mattn/go-isatty v0.0.21/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
79
github.com/rs/zerolog v1.35.0 h1:VD0ykx7HMiMJytqINBsKcbLS+BJ4WYjz+05us+LRTdI=
810
github.com/rs/zerolog v1.35.0/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
9-
github.com/shirou/gofile v0.0.0-20260314143841-7dc06be96404 h1:FSesQzaVDGqNiF1pyQ8QTNpRZrBjXq+AQijCoEqY1Gw=
10-
github.com/shirou/gofile v0.0.0-20260314143841-7dc06be96404/go.mod h1:5R2Lq6rCwzvUkQLMyzYn2lkwhkXgrlgz803ZRWViHdw=
1111
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
1212
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1313
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=

internal/app/coverage_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,6 @@ import (
2727
"testing"
2828
)
2929

30-
// --- isBinaryMIME ---
31-
32-
func TestIsBinaryMIME(t *testing.T) {
33-
t.Parallel()
34-
35-
tests := []struct {
36-
mime string
37-
want bool
38-
}{
39-
{"", false},
40-
{"text/plain", false},
41-
{"text/html", false},
42-
{"application/json", false},
43-
{"application/javascript", false},
44-
{"application/pdf", true},
45-
{"application/octet-stream", true},
46-
{"image/png", true},
47-
{"image/jpeg", true},
48-
{"image/gif", true},
49-
{"image/svg+xml", true},
50-
{"audio/mpeg", true},
51-
{"video/mp4", true},
52-
{"font/ttf", true},
53-
{"application/zip", true},
54-
{"application/gzip", true},
55-
{"application/x-gzip", true},
56-
{"application/x-bzip2", true},
57-
{"application/x-tar", true},
58-
{"application/x-7z-compressed", true},
59-
{"application/vnd.rar", true},
60-
{"application/java-archive", true},
61-
{"application/x-java-class", true},
62-
{"application/wasm", true},
63-
{"application/x-sqlite3", true},
64-
}
65-
66-
for _, tt := range tests {
67-
t.Run(tt.mime, func(t *testing.T) {
68-
t.Parallel()
69-
if got := isBinaryMIME(tt.mime); got != tt.want {
70-
t.Fatalf("isBinaryMIME(%q) = %v, want %v", tt.mime, got, tt.want)
71-
}
72-
})
73-
}
74-
}
75-
7630
// --- Run with invalid format ---
7731

7832
func TestRunInvalidFormat(t *testing.T) {

internal/app/run.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ import (
2727
"io"
2828
"runtime"
2929
"sort"
30-
"strings"
3130
"time"
3231

32+
"github.com/h2non/filetype"
3333
"github.com/jcouture/ghostscan/internal/filesystem"
3434
"github.com/jcouture/ghostscan/internal/finding"
3535
"github.com/jcouture/ghostscan/internal/report"
3636
"github.com/jcouture/ghostscan/internal/scan"
37-
"github.com/shirou/gofile"
3837
)
3938

4039
type Options struct {
@@ -110,12 +109,8 @@ func Run(ctx context.Context, opts Options) (Result, error) {
110109
now = time.Now
111110
}
112111

113-
identifier, err := gofile.New(gofile.Options{MimeType: true})
114-
if err != nil {
115-
return Result{}, fmt.Errorf("initialize binary identifier: %w", err)
116-
}
117112
binaryCheck := func(buf []byte) bool {
118-
return isBinaryMIME(identifier.IdentifyBuffer(buf))
113+
return filetype.Matches(buf)
119114
}
120115

121116
runStart := now().UTC()
@@ -325,29 +320,6 @@ func reportErrors(scanErrors []scanError) []report.ErrorEntry {
325320
return items
326321
}
327322

328-
func isBinaryMIME(mimeType string) bool {
329-
if mimeType == "" {
330-
return false
331-
}
332-
return strings.HasPrefix(mimeType, "image/") ||
333-
strings.HasPrefix(mimeType, "audio/") ||
334-
strings.HasPrefix(mimeType, "video/") ||
335-
strings.HasPrefix(mimeType, "font/") ||
336-
mimeType == "application/pdf" ||
337-
mimeType == "application/octet-stream" ||
338-
mimeType == "application/zip" ||
339-
mimeType == "application/gzip" ||
340-
mimeType == "application/x-gzip" ||
341-
mimeType == "application/x-bzip2" ||
342-
mimeType == "application/x-tar" ||
343-
mimeType == "application/x-7z-compressed" ||
344-
mimeType == "application/vnd.rar" ||
345-
mimeType == "application/java-archive" ||
346-
mimeType == "application/x-java-class" ||
347-
mimeType == "application/wasm" ||
348-
mimeType == "application/x-sqlite3"
349-
}
350-
351323
func mapSkipReason(reason filesystem.EligibilityReason) string {
352324
switch reason {
353325
case filesystem.EligibilityReasonBinaryNUL, filesystem.EligibilityReasonBinaryMagic:

0 commit comments

Comments
 (0)