@@ -19,6 +19,7 @@ import (
19
19
"fmt"
20
20
"io"
21
21
"path/filepath"
22
+ "strings"
22
23
"testing"
23
24
24
25
"github.com/google/go-cmp/cmp"
@@ -33,54 +34,181 @@ func TestParse(t *testing.T) {
33
34
testCases := []struct {
34
35
filename string
35
36
wantBad bool
37
+ wantCVEs []cveID
36
38
}{
37
- {"400mb.jar" , false },
38
- {"400mb_jar_in_jar.jar" , false },
39
- {"arara.jar" , true },
40
- {"arara.jar.patched" , false },
41
- {"arara.signed.jar" , true },
42
- {"arara.signed.jar.patched" , false },
43
- {"log4j-core-2.0-beta9.jar" , true },
44
- {"log4j-core-2.12.1.jar" , true },
45
- {"log4j-core-2.12.1.jar.patched" , false },
39
+ {
40
+ filename : "400mb.jar" ,
41
+ wantBad : false ,
42
+ },
43
+ {
44
+ filename : "400mb_jar_in_jar.jar" ,
45
+ wantBad : false ,
46
+ },
47
+ {
48
+ filename : "arara.jar" ,
49
+ wantBad : true ,
50
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
51
+ },
52
+ {
53
+ filename : "arara.jar.patched" ,
54
+ wantBad : false ,
55
+ },
56
+ {
57
+ filename : "arara.signed.jar" ,
58
+ wantBad : true ,
59
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
60
+ },
61
+ {
62
+ filename : "arara.signed.jar.patched" ,
63
+ wantBad : false ,
64
+ },
65
+ {
66
+ filename : "log4j-core-2.0-beta9.jar" ,
67
+ wantBad : true ,
68
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
69
+ },
70
+ {
71
+ filename : "log4j-core-2.12.1.jar" ,
72
+ wantBad : true ,
73
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
74
+ },
75
+ {
76
+ filename : "log4j-core-2.12.1.jar.patched" ,
77
+ wantBad : false ,
78
+ },
46
79
// log4j 2.12.2 is not affected by log4shell.
47
80
// See: https://logging.apache.org/log4j/2.x/security.html
48
- {"log4j-core-2.12.2.jar" , false },
49
- {"log4j-core-2.14.0.jar" , true },
50
- {"log4j-core-2.14.0.jar.patched" , false },
51
- {"log4j-core-2.15.0.jar" , true },
52
- {"log4j-core-2.15.0.jar.patched" , false },
53
- {"log4j-core-2.16.0.jar" , false },
54
- {"log4j-core-2.1.jar" , true },
55
- {"log4j-core-2.1.jar.patched" , false },
56
- {"safe1.jar" , false },
57
- {"safe1.signed.jar" , false },
81
+ {
82
+ filename : "log4j-core-2.12.2.jar" ,
83
+ wantBad : false ,
84
+ },
85
+ {
86
+ filename : "log4j-core-2.14.0.jar" ,
87
+ wantBad : true ,
88
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
89
+ },
90
+ {
91
+ filename : "log4j-core-2.14.0.jar.patched" ,
92
+ wantBad : false ,
93
+ },
94
+ {
95
+ filename : "log4j-core-2.15.0.jar" ,
96
+ wantBad : true ,
97
+ wantCVEs : []cveID {cve_2021_45046 },
98
+ },
99
+ {
100
+ filename : "log4j-core-2.15.0.jar.patched" ,
101
+ wantBad : false ,
102
+ },
103
+ {
104
+ filename : "log4j-core-2.16.0.jar" ,
105
+ wantBad : false ,
106
+ },
107
+ {
108
+ filename : "log4j-core-2.1.jar" ,
109
+ wantBad : true ,
110
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
111
+ },
112
+ {
113
+ filename : "log4j-core-2.1.jar.patched" ,
114
+ wantBad : false ,
115
+ },
116
+ {
117
+ filename : "safe1.jar" ,
118
+ wantBad : false ,
119
+ },
120
+ {
121
+ filename : "safe1.signed.jar" ,
122
+ wantBad : false ,
123
+ },
58
124
// Archive contains a malformed directory that causes archive/zip to
59
125
// return an error.
60
126
// See https://go.dev/issues/50390
61
- {"selenium-api-3.141.59.jar" , false },
127
+ {
128
+ filename : "selenium-api-3.141.59.jar" ,
129
+ wantBad : false ,
130
+ },
62
131
// Test case where it contains a JndiLookupOther.class file that shouldn't be detected as vulnerable
63
- {"similarbutnotvuln.jar" , false },
64
- {"vuln-class.jar" , true },
65
- {"vuln-class-executable" , true },
66
- {"vuln-class.jar.patched" , false },
67
- {"good_jar_in_jar.jar" , false },
68
- {"good_jar_in_jar_in_jar.jar" , false },
69
- {"bad_jar_in_jar.jar" , true },
70
- {"bad_jar_in_jar.jar.patched" , false },
71
- {"bad_jar_in_jar_in_jar.jar" , true },
72
- {"bad_jar_in_jar_in_jar.jar.patched" , false },
73
- {"bad_jar_with_invalid_jar.jar" , true },
74
- {"bad_jar_with_invalid_jar.jar.patched" , false },
75
- {"good_jar_with_invalid_jar.jar" , false },
76
- {"helloworld-executable" , false },
77
- {"helloworld.jar" , false },
78
- {"helloworld.signed.jar" , false },
132
+ {
133
+ filename : "similarbutnotvuln.jar" ,
134
+ wantBad : false ,
135
+ },
136
+ {
137
+ filename : "vuln-class.jar" ,
138
+ wantBad : true ,
139
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
140
+ },
141
+ {
142
+ filename : "vuln-class-executable" ,
143
+ wantBad : true ,
144
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
145
+ },
146
+ {
147
+ filename : "vuln-class.jar.patched" ,
148
+ wantBad : false ,
149
+ },
150
+ {
151
+ filename : "good_jar_in_jar.jar" ,
152
+ wantBad : false ,
153
+ },
154
+ {
155
+ filename : "good_jar_in_jar_in_jar.jar" ,
156
+ wantBad : false ,
157
+ },
158
+ {
159
+ filename : "bad_jar_in_jar.jar" ,
160
+ wantBad : true ,
161
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
162
+ },
163
+ {
164
+ filename : "bad_jar_in_jar.jar.patched" ,
165
+ wantBad : false ,
166
+ },
167
+ {
168
+ filename : "bad_jar_in_jar_in_jar.jar" ,
169
+ wantBad : true ,
170
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
171
+ },
172
+ {
173
+ filename : "bad_jar_in_jar_in_jar.jar.patched" ,
174
+ wantBad : false ,
175
+ },
176
+ {
177
+ filename : "bad_jar_with_invalid_jar.jar" ,
178
+ wantBad : true ,
179
+ wantCVEs : []cveID {cve_2021_44228 , cve_2021_45046 },
180
+ },
181
+ {
182
+ filename : "bad_jar_with_invalid_jar.jar.patched" ,
183
+ wantBad : false ,
184
+ },
185
+ {
186
+ filename : "good_jar_with_invalid_jar.jar" ,
187
+ wantBad : false ,
188
+ },
189
+ {
190
+ filename : "helloworld-executable" ,
191
+ wantBad : false ,
192
+ },
193
+ {
194
+ filename : "helloworld.jar" ,
195
+ wantBad : false ,
196
+ },
197
+ {
198
+ filename : "helloworld.signed.jar" ,
199
+ wantBad : false ,
200
+ },
79
201
80
202
// Ensure robustness to zip bombs from
81
203
// https://www.bamsoftware.com/hacks/zipbomb/.
82
- {"zipbombs/zbsm_in_jar.jar" , false },
83
- {"zipbombs/zbsm.jar" , false },
204
+ {
205
+ filename : "zipbombs/zbsm_in_jar.jar" ,
206
+ wantBad : false ,
207
+ },
208
+ {
209
+ filename : "zipbombs/zbsm.jar" ,
210
+ wantBad : false ,
211
+ },
84
212
}
85
213
for _ , tc := range testCases {
86
214
t .Run (tc .filename , func (t * testing.T ) {
@@ -92,11 +220,15 @@ func TestParse(t *testing.T) {
92
220
defer zr .Close ()
93
221
report , err := Parse (& zr .Reader )
94
222
if err != nil {
95
- t .Fatalf ("Scan () returned an unexpected error, got %v, want nil" , err )
223
+ t .Fatalf ("Parse () returned an unexpected error, got %v, want nil" , err )
96
224
}
97
225
got := report .Vulnerable
98
226
if tc .wantBad != got {
99
- t .Errorf ("checkJAR() returned unexpected value, got bad=%t, want bad=%t" , got , tc .wantBad )
227
+ t .Errorf ("Parse() returned unexpected value, got bad=%t, want bad=%t" , got , tc .wantBad )
228
+ }
229
+
230
+ if diff := cmp .Diff (tc .wantCVEs , vulnIDs (report .Vulns ), cmpopts .EquateEmpty (), cmpopts .SortSlices (cveIDLess )); diff != "" {
231
+ t .Errorf ("Parse() returned unexpected Vulns, diff (-want +got):\n %s" , diff )
100
232
}
101
233
})
102
234
}
@@ -417,3 +549,18 @@ func (f *faultReader) Read(b []byte) (int, error) {
417
549
}
418
550
return n , err
419
551
}
552
+
553
+ // vulnIDs extracts the cveIDs from an array of Vulns.
554
+ func vulnIDs (vs []* Vuln ) []cveID {
555
+ var ids []cveID
556
+ for _ , v := range vs {
557
+ ids = append (ids , cveID (v .CVE ))
558
+ }
559
+ return ids
560
+ }
561
+
562
+ // cveIDLess returns true if a comes lexically before b. It can be
563
+ // used with cmpopts.SortSlices.
564
+ func cveIDLess (a , b cveID ) bool {
565
+ return strings .Compare (a .String (), b .String ()) < 0
566
+ }
0 commit comments