Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 6012f09

Browse files
singlethinkericchiang
authored andcommitted
Make Parser used by Walker configurable
This allows Parser non-default Parser configuration to be specified when scanning with Walker.
1 parent 48dbe0d commit 6012f09

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

jar/walker.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,20 @@ type Walker struct {
6262
HandleReport func(path string, r *Report)
6363
// HandleRewrite is called when a JAR is rewritten successfully.
6464
HandleRewrite func(path string, r *Report)
65+
// Parser will be used when checking JARs, if provided. If
66+
// unset, a Parser with sensible defaults will be created.
67+
Parser *Parser
6568
}
6669

6770
// Walk attempts to scan a directory for vulnerable JARs.
6871
func (w *Walker) Walk(dir string) error {
72+
p := w.Parser
73+
if p == nil {
74+
p = &Parser{}
75+
}
76+
6977
fsys := os.DirFS(dir)
70-
wk := walker{w, fsys, dir}
78+
wk := walker{w, fsys, dir, p}
7179

7280
return fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
7381
if err != nil {
@@ -88,6 +96,8 @@ type walker struct {
8896
*Walker
8997
fs fs.FS
9098
dir string
99+
// p is the Parser to use for this walk. p is guaranteed to be non-nil.
100+
p *Parser
91101
}
92102

93103
func (w *walker) filepath(path string) string {
@@ -154,7 +164,7 @@ func (w *walker) visit(p string, d fs.DirEntry) error {
154164
if !IsJAR(zr) {
155165
return nil
156166
}
157-
r, err := Parse(zr)
167+
r, err := w.p.Parse(zr)
158168
if err != nil {
159169
return fmt.Errorf("scanning jar: %v", err)
160170
}

jar/walker_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package jar
1616

1717
import (
1818
"path/filepath"
19+
"strings"
1920
"testing"
2021

2122
"github.com/google/go-cmp/cmp"
@@ -177,3 +178,59 @@ func TestWalkerRewrite(t *testing.T) {
177178
t.Errorf("walking filesystem after rewrite returned diff (-want, +got): %s", diff)
178179
}
179180
}
181+
182+
// TestNonDefaultParser verifies that Walker can be configured with a
183+
// non-default Parser by scanning a large JAR file with two
184+
// configurations: one where Parser.MaxBytes is larger than the file
185+
// size and one where Parser.MaxBytes is smaller than the file
186+
// size. It ensures that the first case succeeds and the second fails.
187+
func TestNonDefaultParser(t *testing.T) {
188+
jar := "400mb_jar_in_jar.jar"
189+
190+
tempDir := t.TempDir()
191+
src := testdataPath(jar)
192+
dest := filepath.Join(tempDir, jar)
193+
cpFile(t, dest, src)
194+
195+
tests := []struct {
196+
desc string
197+
maxBytes int64
198+
wantErr bool
199+
}{
200+
{
201+
desc: "MaxBytes > JAR size",
202+
maxBytes: 4 << 30, // 4GiB
203+
wantErr: false,
204+
},
205+
{
206+
desc: "MaxBytes < JAR size",
207+
maxBytes: 4 << 20, // 4MiB
208+
wantErr: true,
209+
},
210+
}
211+
212+
for _, tc := range tests {
213+
t.Run(tc.desc, func(t *testing.T) {
214+
var gotErr error
215+
216+
p := &Parser{MaxBytes: tc.maxBytes}
217+
w := &Walker{
218+
Parser: p,
219+
HandleError: func(path string, err error) {
220+
if err != nil && strings.HasSuffix(path, filepath.FromSlash("/"+jar)) {
221+
gotErr = err
222+
}
223+
},
224+
}
225+
if err := w.Walk(tempDir); err != nil {
226+
t.Errorf("Walk returned unexpected error: %v", err)
227+
}
228+
229+
if tc.wantErr && gotErr == nil {
230+
t.Error("Parser failed to generate expected error when scanning JARs > MaxBytes, got nil, want error")
231+
} else if !tc.wantErr && gotErr != nil {
232+
t.Errorf("Parser generated unexpected error when scanning JARs <= MaxBytes, got %v, want nil error", gotErr)
233+
}
234+
})
235+
}
236+
}

0 commit comments

Comments
 (0)