Skip to content

Commit 9d16438

Browse files
authored
chore: address a few small bugs (#1255)
Signed-off-by: egibs <[email protected]>
1 parent ac80d7d commit 9d16438

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

Makefile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ test:
140140

141141
.PHONY: fuzz
142142
fuzz:
143-
go test -fuzz=FuzzExtractTar -fuzztime=10s ./pkg/archive/
144-
go test -fuzz=FuzzExtractZip -fuzztime=10s ./pkg/archive/
145-
go test -fuzz=FuzzExtractArchive -fuzztime=10s ./pkg/archive/
146-
go test -fuzz=FuzzIsValidPath -fuzztime=10s ./pkg/archive/
147-
go test -fuzz=FuzzFile -fuzztime=30s ./pkg/programkind/
148-
go test -fuzz=FuzzPath -fuzztime=10s ./pkg/programkind/
149-
go test -fuzz=FuzzGetExt -fuzztime=10s ./pkg/programkind/
150-
go test -fuzz=FuzzLongestUnique -fuzztime=10s ./pkg/report/
151-
go test -fuzz=FuzzTrimPrefixes -fuzztime=10s ./pkg/report/
152-
go test -fuzz=FuzzMatchToString -fuzztime=10s ./pkg/report/
143+
go test -timeout 0 -fuzz=FuzzExtractTar -fuzztime=10s ./pkg/archive/
144+
go test -timeout 0 -fuzz=FuzzExtractZip -fuzztime=10s ./pkg/archive/
145+
go test -timeout 0 -fuzz=FuzzExtractArchive -fuzztime=10s ./pkg/archive/
146+
go test -timeout 0 -fuzz=FuzzIsValidPath -fuzztime=10s ./pkg/archive/
147+
go test -timeout 0 -fuzz=FuzzFile -fuzztime=30s ./pkg/programkind/
148+
go test -timeout 0 -fuzz=FuzzPath -fuzztime=10s ./pkg/programkind/
149+
go test -timeout 0 -fuzz=FuzzGetExt -fuzztime=10s ./pkg/programkind/
150+
go test -timeout 0 -fuzz=FuzzLongestUnique -fuzztime=10s ./pkg/report/
151+
go test -timeout 0 -fuzz=FuzzTrimPrefixes -fuzztime=10s ./pkg/report/
152+
go test -timeout 0 -fuzz=FuzzMatchToString -fuzztime=10s ./pkg/report/
153153

154154
# fuzz tests - runs continuously (use Ctrl+C to stop)
155155
# Usage: make fuzz-continuous FUZZ_TARGET=FuzzExtractArchive FUZZ_PKG=./pkg/archive/

pkg/archive/archive.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,29 @@ var (
2626

2727
// isValidPath checks if the target file is within the given directory.
2828
func IsValidPath(target, dir string) bool {
29+
if strings.Contains(target, "\x00") || strings.Contains(dir, "\x00") {
30+
return false
31+
}
32+
2933
cleanTarget := filepath.Clean(target)
3034
cleanDir := filepath.Clean(dir)
3135

36+
// avoid evaluating symlinks if the target is not a symlink
37+
if fi, err := os.Lstat(cleanTarget); err == nil && fi.Mode()&os.ModeSymlink == os.ModeSymlink {
38+
var evalTarget, evalDir, rel string
39+
40+
if evalTarget, err = filepath.EvalSymlinks(cleanTarget); err != nil {
41+
return false
42+
}
43+
if evalDir, err = filepath.EvalSymlinks(cleanDir); err != nil {
44+
return false
45+
}
46+
if rel, err = filepath.Rel(evalDir, evalTarget); err == nil &&
47+
rel == ".." || strings.HasPrefix(rel, "..") {
48+
return false
49+
}
50+
}
51+
3252
switch {
3353
case cleanDir == "", cleanTarget == "":
3454
return false

pkg/archive/fuzz_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88
"testing"
9+
"time"
910

1011
"github.com/chainguard-dev/malcontent/pkg/malcontent"
1112
)
@@ -46,7 +47,8 @@ func FuzzExtractTar(f *testing.F) {
4647
}
4748
defer os.RemoveAll(tmpDir)
4849

49-
ctx := context.Background()
50+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
51+
defer cancel()
5052
_ = ExtractTar(ctx, tmpDir, tmpFile.Name())
5153

5254
err = filepath.WalkDir(tmpDir, func(path string, _ os.DirEntry, err error) error {
@@ -100,7 +102,8 @@ func FuzzExtractZip(f *testing.F) {
100102
}
101103
defer os.RemoveAll(tmpDir)
102104

103-
ctx := context.Background()
105+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
106+
defer cancel()
104107
_ = ExtractZip(ctx, tmpDir, tmpFile.Name())
105108

106109
err = filepath.WalkDir(tmpDir, func(path string, _ os.DirEntry, err error) error {
@@ -163,7 +166,8 @@ func FuzzExtractArchive(f *testing.F) {
163166
}
164167
tmpFile.Close()
165168

166-
ctx := context.Background()
169+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
170+
defer cancel()
167171
cfg := malcontent.Config{}
168172
extractedDir, err := ExtractArchiveToTempDir(ctx, cfg, tmpFile.Name())
169173
if err == nil && extractedDir != "" {

pkg/archive/zlib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
3232
}
3333

3434
buf := archivePool.Get(file.ExtractBuffer) //nolint:nilaway // the buffer pool is created in archive.go
35-
defer archivePool.Put(buf)
3635

3736
zf, err := os.Open(f)
3837
if err != nil {
@@ -54,6 +53,7 @@ func ExtractZlib(ctx context.Context, d string, f string) error {
5453

5554
defer func() {
5655
archivePool.Put(buf)
56+
zf.Close()
5757
zr.Close()
5858
out.Close()
5959
}()

pkg/compile/compile.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"regexp"
1414
"strings"
15+
"unicode/utf8"
1516

1617
"github.com/minio/sha256-simd"
1718

@@ -153,9 +154,18 @@ func getRulesToRemove() []string {
153154

154155
// removeRules removes rule matches from the file data.
155156
func removeRules(data []byte, rulesToRemove []string) []byte {
157+
if len(rulesToRemove) == 0 {
158+
return data
159+
}
160+
156161
modified := data
157162
ruleNames := make([]string, len(rulesToRemove))
158163
for i, name := range rulesToRemove {
164+
// we only ever include rules listed above in badRules and rulesWithWarnings
165+
// but ignore any rule names that aren't valid UTF-8
166+
if !utf8.ValidString(name) {
167+
continue
168+
}
159169
ruleNames[i] = regexp.QuoteMeta(name)
160170
}
161171
pattern := regexp.MustCompile(fmt.Sprintf(

pkg/programkind/fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func FuzzFile(f *testing.F) {
6161
}
6262
tmpFile.Close()
6363

64-
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
64+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
6565
defer cancel()
6666

6767
ft, err := File(ctx, tmpFile.Name())

0 commit comments

Comments
 (0)