-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency_check_test.go
More file actions
423 lines (359 loc) · 9.74 KB
/
dependency_check_test.go
File metadata and controls
423 lines (359 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package inspect
import (
"os"
"path/filepath"
"testing"
)
func TestScanGoMod_VulnerablePackage(t *testing.T) {
dir := t.TempDir()
gomod := filepath.Join(dir, "go.mod")
content := `module example.com/myapp
go 1.21
require (
golang.org/x/net v0.7.0
github.com/gin-gonic/gin v1.9.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
)
`
if err := os.WriteFile(gomod, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanGoMod(gomod)
if len(findings) == 0 {
t.Fatal("expected vulnerability findings")
}
// Check for specific vulnerabilities
foundNet := false
foundGin := false
foundJWT := false
for _, f := range findings {
if f.Element == "golang.org/x/net@0.7.0" {
foundNet = true
}
if f.Element == "github.com/gin-gonic/gin@1.9.0" {
foundGin = true
}
if f.Element == "github.com/dgrijalva/jwt-go@3.2.0+incompatible" {
foundJWT = true
}
}
if !foundNet {
t.Error("expected finding for golang.org/x/net v0.7.0")
}
if !foundGin {
t.Error("expected finding for gin v1.9.0")
}
if !foundJWT {
t.Error("expected finding for jwt-go v3.2.0")
}
}
func TestScanGoMod_SafeVersions(t *testing.T) {
dir := t.TempDir()
gomod := filepath.Join(dir, "go.mod")
content := `module example.com/myapp
go 1.21
require (
golang.org/x/net v0.40.0
github.com/gin-gonic/gin v1.10.0
)
`
if err := os.WriteFile(gomod, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanGoMod(gomod)
if len(findings) != 0 {
t.Errorf("expected no findings for safe versions, got %d: %+v", len(findings), findings)
}
}
func TestScanGoMod_SingleLineRequire(t *testing.T) {
dir := t.TempDir()
gomod := filepath.Join(dir, "go.mod")
content := `module example.com/myapp
go 1.21
require golang.org/x/net v0.7.0
`
if err := os.WriteFile(gomod, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanGoMod(gomod)
if len(findings) == 0 {
t.Fatal("expected finding for single-line require")
}
}
func TestScanGoMod_FileNotFound(t *testing.T) {
checker := NewDependencyChecker("/tmp")
findings := checker.ScanGoMod("/nonexistent/go.mod")
if len(findings) != 1 {
t.Fatalf("expected 1 info finding, got %d", len(findings))
}
if findings[0].Severity != SeverityInfo {
t.Errorf("expected info severity, got %s", findings[0].Severity)
}
}
func TestScanPackageJSON_VulnerablePackages(t *testing.T) {
dir := t.TempDir()
pkgJSON := filepath.Join(dir, "package.json")
content := `{
"name": "test-app",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.15",
"express": "4.17.1",
"axios": "^1.4.0"
},
"devDependencies": {
"minimist": "1.2.5"
}
}
`
if err := os.WriteFile(pkgJSON, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanPackageJSON(pkgJSON)
if len(findings) == 0 {
t.Fatal("expected vulnerability findings")
}
foundLodash := false
foundMinimist := false
for _, f := range findings {
if f.Element == "lodash@4.17.15" {
foundLodash = true
}
if f.Element == "minimist@1.2.5" {
foundMinimist = true
}
}
if !foundLodash {
t.Error("expected finding for lodash 4.17.15")
}
if !foundMinimist {
t.Error("expected finding for minimist 1.2.5")
}
}
func TestScanPackageJSON_SafeVersions(t *testing.T) {
dir := t.TempDir()
pkgJSON := filepath.Join(dir, "package.json")
content := `{
"name": "test-app",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.21",
"express": "4.19.2"
}
}
`
if err := os.WriteFile(pkgJSON, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanPackageJSON(pkgJSON)
if len(findings) != 0 {
t.Errorf("expected no findings for safe versions, got %d: %+v", len(findings), findings)
}
}
func TestScanPackageJSON_InvalidJSON(t *testing.T) {
dir := t.TempDir()
pkgJSON := filepath.Join(dir, "package.json")
if err := os.WriteFile(pkgJSON, []byte("not json"), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanPackageJSON(pkgJSON)
if len(findings) != 1 {
t.Fatalf("expected 1 info finding, got %d", len(findings))
}
if findings[0].Severity != SeverityInfo {
t.Errorf("expected info severity, got %s", findings[0].Severity)
}
}
func TestScanRequirements_VulnerablePackages(t *testing.T) {
dir := t.TempDir()
reqFile := filepath.Join(dir, "requirements.txt")
content := `# Python dependencies
django==3.2.15
flask>=2.2.3
requests==2.28.0
pillow==9.5.0
urllib3==1.26.10
pyyaml==5.3
`
if err := os.WriteFile(reqFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanRequirements(reqFile)
if len(findings) == 0 {
t.Fatal("expected vulnerability findings")
}
foundDjango := false
foundFlask := false
foundPyyaml := false
for _, f := range findings {
if f.Element == "django@3.2.15" {
foundDjango = true
}
if f.Element == "flask@2.2.3" {
foundFlask = true
}
if f.Element == "pyyaml@5.3" {
foundPyyaml = true
}
}
if !foundDjango {
t.Error("expected finding for django 3.2.15")
}
if !foundFlask {
t.Error("expected finding for flask 2.2.3")
}
if !foundPyyaml {
t.Error("expected finding for pyyaml 5.3")
}
}
func TestScanRequirements_SafeVersions(t *testing.T) {
dir := t.TempDir()
reqFile := filepath.Join(dir, "requirements.txt")
content := `django==4.2.0
flask==3.0.0
requests==2.31.0
`
if err := os.WriteFile(reqFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanRequirements(reqFile)
if len(findings) != 0 {
t.Errorf("expected no findings for safe versions, got %d: %+v", len(findings), findings)
}
}
func TestScanRequirements_WithExtrasAndMarkers(t *testing.T) {
dir := t.TempDir()
reqFile := filepath.Join(dir, "requirements.txt")
content := `requests[security]==2.28.0; python_version >= "3.6"
urllib3==1.26.10
`
if err := os.WriteFile(reqFile, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
checker := NewDependencyChecker(dir)
findings := checker.ScanRequirements(reqFile)
if len(findings) == 0 {
t.Fatal("expected findings for vulnerable packages with extras")
}
}
func TestScanRequirements_FileNotFound(t *testing.T) {
checker := NewDependencyChecker("/tmp")
findings := checker.ScanRequirements("/nonexistent/requirements.txt")
if len(findings) != 1 {
t.Fatalf("expected 1 info finding, got %d", len(findings))
}
if findings[0].Severity != SeverityInfo {
t.Errorf("expected info severity, got %s", findings[0].Severity)
}
}
func TestParseGoModDep(t *testing.T) {
tests := []struct {
line string
pkg string
version string
}{
{"golang.org/x/net v0.7.0", "golang.org/x/net", "0.7.0"},
{"github.com/foo/bar v1.2.3 // indirect", "github.com/foo/bar", "1.2.3"},
{"github.com/dgrijalva/jwt-go v3.2.0+incompatible", "github.com/dgrijalva/jwt-go", "3.2.0+incompatible"},
}
for _, tt := range tests {
pkg, version := parseGoModDep(tt.line)
if pkg != tt.pkg {
t.Errorf("parseGoModDep(%q) pkg = %q, want %q", tt.line, pkg, tt.pkg)
}
if version != tt.version {
t.Errorf("parseGoModDep(%q) version = %q, want %q", tt.line, version, tt.version)
}
}
}
func TestParseRequirementLine(t *testing.T) {
tests := []struct {
line string
name string
version string
}{
{"django==3.2.15", "django", "3.2.15"},
{"flask>=2.2.3", "flask", "2.2.3"},
{"requests~=2.28.0", "requests", "2.28.0"},
{"numpy", "numpy", ""},
{"PyYAML==5.3", "pyyaml", "5.3"},
{"requests[security]==2.28.0; python_version >= \"3.6\"", "requests", "2.28.0"},
}
for _, tt := range tests {
name, version := parseRequirementLine(tt.line)
if name != tt.name {
t.Errorf("parseRequirementLine(%q) name = %q, want %q", tt.line, name, tt.name)
}
if version != tt.version {
t.Errorf("parseRequirementLine(%q) version = %q, want %q", tt.line, version, tt.version)
}
}
}
func TestCleanNPMVersion(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"^4.17.15", "4.17.15"},
{"~1.2.3", "1.2.3"},
{">=2.0.0", "2.0.0"},
{"1.0.0", "1.0.0"},
{">=1.0.0 <2.0.0", "1.0.0"},
}
for _, tt := range tests {
result := cleanNPMVersion(tt.input)
if result != tt.expected {
t.Errorf("cleanNPMVersion(%q) = %q, want %q", tt.input, result, tt.expected)
}
}
}
func TestIsVersionAffected(t *testing.T) {
tests := []struct {
version string
affected []string
expected bool
}{
{"4.17.15", []string{"4.17.0", "4.17.1", "4.17.15"}, true},
{"4.17.21", []string{"4.17.0", "4.17.1", "4.17.15"}, false},
{"0.7.0", []string{"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7"}, true},
{"0.40.0", []string{"0.0", "0.1", "0.2", "0.3"}, false},
}
for _, tt := range tests {
result := isVersionAffected(tt.version, tt.affected)
if result != tt.expected {
t.Errorf("isVersionAffected(%q, ...) = %v, want %v", tt.version, result, tt.expected)
}
}
}
func TestKnownVulnerabilities_Coverage(t *testing.T) {
// Ensure we have a reasonable number of entries
if len(KnownVulnerabilities) < 20 {
t.Errorf("expected at least 20 packages in KnownVulnerabilities, got %d", len(KnownVulnerabilities))
}
// Verify Log4Shell is in the database
log4j, ok := KnownVulnerabilities["org.apache.logging.log4j:log4j-core"]
if !ok {
t.Fatal("expected log4j-core in KnownVulnerabilities")
}
foundLog4Shell := false
for _, v := range log4j {
if v.CVE == "CVE-2021-44228" {
foundLog4Shell = true
if v.Severity != SeverityCritical {
t.Error("Log4Shell should be critical severity")
}
break
}
}
if !foundLog4Shell {
t.Error("expected CVE-2021-44228 (Log4Shell) in log4j vulnerabilities")
}
}