/
SuppressionRule.java
715 lines (663 loc) · 19.8 KB
/
SuppressionRule.java
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* This file is part of dependency-check-core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
*/
package org.owasp.dependencycheck.xml.suppression;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.owasp.dependencycheck.dependency.Dependency;
import org.owasp.dependencycheck.dependency.Vulnerability;
import org.owasp.dependencycheck.dependency.naming.CpeIdentifier;
import org.owasp.dependencycheck.dependency.naming.Identifier;
import org.owasp.dependencycheck.dependency.naming.PurlIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.springett.parsers.cpe.Cpe;
import us.springett.parsers.cpe.exceptions.CpeEncodingException;
/**
*
* @author Jeremy Long
*/
@NotThreadSafe
public class SuppressionRule {
/**
* The Logger for use throughout the class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SuppressionRule.class);
/**
* The file path for the suppression.
*/
private PropertyType filePath;
/**
* The SHA1 hash.
*/
private String sha1;
/**
* A list of CPEs to suppression
*/
private List<PropertyType> cpe = new ArrayList<>();
/**
* The list of cvssBelow scores.
*/
private List<Float> cvssBelow = new ArrayList<>();
/**
* The list of CWE entries to suppress.
*/
private List<String> cwe = new ArrayList<>();
/**
* The list of CVE entries to suppress.
*/
private List<String> cve = new ArrayList<>();
/**
* The list of vulnerability name entries to suppress.
*/
private List<PropertyType> vulnerabilityNames = new ArrayList<>();
/**
* A Maven GAV to suppression.
*/
private PropertyType gav = null;
/**
* The list of vulnerability name entries to suppress.
*/
private PropertyType packageUrl = null;
/**
* The notes added in suppression file
*/
private String notes;
/**
* A flag indicating whether or not the suppression rule is a core/base rule
* that should not be included in the resulting report in the "suppressed"
* section.
*/
private boolean base;
/**
* A date until which the suppression is to be retained. This can be used to
* make a temporary suppression that auto-expires to suppress a CVE while
* waiting for the vulnerability fix of the dependency to be released.
*/
private Calendar until;
/**
* A flag whether or not the rule matched a dependency & CPE.
*/
private boolean matched = false;
/**
* Get the value of matched.
*
* @return the value of matched
*/
public boolean isMatched() {
return matched;
}
/**
* Set the value of matched.
*
* @param matched new value of matched
*/
public void setMatched(boolean matched) {
this.matched = matched;
}
/**
* Get the (@code{nullable}) value of until.
*
* @return the value of until
*/
public Calendar getUntil() {
return until;
}
/**
* Set the value of until.
*
* @param until new value of until
*/
public void setUntil(Calendar until) {
this.until = until;
}
/**
* Get the value of filePath.
*
* @return the value of filePath
*/
public PropertyType getFilePath() {
return filePath;
}
/**
* Set the value of filePath.
*
* @param filePath new value of filePath
*/
public void setFilePath(PropertyType filePath) {
this.filePath = filePath;
}
/**
* Get the value of sha1.
*
* @return the value of sha1
*/
public String getSha1() {
return sha1;
}
/**
* Set the value of SHA1.
*
* @param sha1 new value of SHA1
*/
public void setSha1(String sha1) {
this.sha1 = sha1;
}
/**
* Get the value of CPE.
*
* @return the value of CPE
*/
public List<PropertyType> getCpe() {
return cpe;
}
/**
* Set the value of CPE.
*
* @param cpe new value of CPE
*/
public void setCpe(List<PropertyType> cpe) {
this.cpe = cpe;
}
/**
* Adds the CPE to the CPE list.
*
* @param cpe the CPE to add
*/
public void addCpe(PropertyType cpe) {
this.cpe.add(cpe);
}
/**
* Adds the CPE to the CPE list.
*
* @param name the vulnerability name to add
*/
public void addVulnerabilityName(PropertyType name) {
this.vulnerabilityNames.add(name);
}
/**
* Returns whether or not this suppression rule as CPE entries.
*
* @return whether or not this suppression rule as CPE entries
*/
public boolean hasCpe() {
return !cpe.isEmpty();
}
/**
* Get the value of cvssBelow.
*
* @return the value of cvssBelow
*/
public List<Float> getCvssBelow() {
return cvssBelow;
}
/**
* Set the value of cvssBelow.
*
* @param cvssBelow new value of cvssBelow
*/
public void setCvssBelow(List<Float> cvssBelow) {
this.cvssBelow = cvssBelow;
}
/**
* Adds the CVSS to the cvssBelow list.
*
* @param cvss the CVSS to add
*/
public void addCvssBelow(Float cvss) {
this.cvssBelow.add(cvss);
}
/**
* Returns whether or not this suppression rule has CVSS suppressions.
*
* @return whether or not this suppression rule has CVSS suppressions
*/
public boolean hasCvssBelow() {
return !cvssBelow.isEmpty();
}
/**
* Get the value of notes.
*
* @return the value of notes
*/
public String getNotes() {
return notes;
}
/**
* Set the value of notes.
*
* @param notes new value of cve
*/
public void setNotes(String notes) {
this.notes = notes;
}
/**
* Adds the notes to the cve list.
*
* @param notes the cve to add
*/
public void addNotes(String notes) {
this.notes = notes;
}
/**
* Returns whether this suppression rule has notes entries.
*
* @return whether this suppression rule has notes entries
*/
public boolean hasNotes() {
return !cve.isEmpty();
}
/**
* Get the value of CWE.
*
* @return the value of CWE
*/
public List<String> getCwe() {
return cwe;
}
/**
* Set the value of CWE.
*
* @param cwe new value of CWE
*/
public void setCwe(List<String> cwe) {
this.cwe = cwe;
}
/**
* Adds the CWE to the CWE list.
*
* @param cwe the CWE to add
*/
public void addCwe(String cwe) {
this.cwe.add(cwe);
}
/**
* Returns whether this suppression rule has CWE entries.
*
* @return whether this suppression rule has CWE entries
*/
public boolean hasCwe() {
return !cwe.isEmpty();
}
/**
* Get the value of CVE.
*
* @return the value of CVE
*/
public List<String> getCve() {
return cve;
}
/**
* Set the value of CVE.
*
* @param cve new value of CVE
*/
public void setCve(List<String> cve) {
this.cve = cve;
}
/**
* Adds the CVE to the CVE list.
*
* @param cve the CVE to add
*/
public void addCve(String cve) {
this.cve.add(cve);
}
/**
* Returns whether this suppression rule has CVE entries.
*
* @return whether this suppression rule has CVE entries
*/
public boolean hasCve() {
return !cve.isEmpty();
}
/**
* Returns whether this suppression rule has vulnerabilityName entries.
*
* @return whether this suppression rule has vulnerabilityName entries
*/
public boolean hasVulnerabilityName() {
return !vulnerabilityNames.isEmpty();
}
/**
* Get the value of Maven GAV.
*
* @return the value of GAV
*/
public PropertyType getGav() {
return gav;
}
/**
* Set the value of Maven GAV.
*
* @param gav new value of Maven GAV
*/
public void setGav(PropertyType gav) {
this.gav = gav;
}
/**
* Returns whether or not this suppression rule as GAV entries.
*
* @return whether or not this suppression rule as GAV entries
*/
public boolean hasGav() {
return gav != null;
}
/**
* Set the value of Package URL.
*
* @param purl new value of package URL
*/
public void setPackageUrl(PropertyType purl) {
this.packageUrl = purl;
}
/**
* Returns whether or not this suppression rule as packageUrl entries.
*
* @return whether or not this suppression rule as packageUrl entries
*/
public boolean hasPackageUrl() {
return packageUrl != null;
}
/**
* Get the value of base.
*
* @return the value of base
*/
public boolean isBase() {
return base;
}
/**
* Set the value of base.
*
* @param base new value of base
*/
public void setBase(boolean base) {
this.base = base;
}
/**
* Processes a given dependency to determine if any CPE, CVE, CWE, or CVSS
* scores should be suppressed. If any should be, they are removed from the
* dependency.
*
* @param dependency a project dependency to analyze
*/
public void process(Dependency dependency) {
if (filePath != null && !filePath.matches(dependency.getFilePath())) {
return;
}
if (sha1 != null && !sha1.equalsIgnoreCase(dependency.getSha1sum())) {
return;
}
if (hasGav()) {
final Iterator<Identifier> itr = dependency.getSoftwareIdentifiers().iterator();
boolean found = false;
while (itr.hasNext()) {
final Identifier i = itr.next();
if (identifierMatches(this.gav, i)) {
found = true;
break;
}
}
if (!found) {
return;
}
}
if (hasPackageUrl()) {
final Iterator<Identifier> itr = dependency.getSoftwareIdentifiers().iterator();
boolean found = false;
while (itr.hasNext()) {
final Identifier i = itr.next();
if (purlMatches(this.packageUrl, i)) {
found = true;
break;
}
}
if (!found) {
return;
}
}
if (this.hasCpe()) {
final Set<Identifier> removalList = new HashSet<>();
for (Identifier i : dependency.getVulnerableSoftwareIdentifiers()) {
for (PropertyType c : this.cpe) {
if (identifierMatches(c, i)) {
if (!isBase()) {
matched = true;
if (this.notes != null) {
i.setNotes(this.notes);
}
dependency.addSuppressedIdentifier(i);
}
removalList.add(i);
break;
}
}
}
removalList.forEach(dependency::removeVulnerableSoftwareIdentifier);
}
if (hasCve() || hasVulnerabilityName() || hasCwe() || hasCvssBelow()) {
final Set<Vulnerability> removeVulns = new HashSet<>();
for (Vulnerability v : dependency.getVulnerabilities()) {
boolean remove = false;
for (String entry : this.cve) {
if (entry.equalsIgnoreCase(v.getName())) {
removeVulns.add(v);
remove = true;
break;
}
}
if (!remove && this.cwe != null && !v.getCwes().isEmpty()) {
for (String entry : this.cwe) {
final String toMatch = String.format("CWE-%s", entry);
if (v.getCwes().stream().anyMatch(toTest -> toMatch.regionMatches(0, toTest, 0, toMatch.length()))) {
remove = true;
removeVulns.add(v);
break;
}
}
}
if (!remove && v.getName() != null) {
for (PropertyType entry : this.vulnerabilityNames) {
if (entry.matches(v.getName())) {
remove = true;
removeVulns.add(v);
break;
}
}
}
if (!remove) {
for (float cvss : this.cvssBelow) {
if (v.getCvssV2() != null && v.getCvssV2().getScore() < cvss) {
remove = true;
removeVulns.add(v);
break;
}
if (v.getCvssV3() != null && v.getCvssV3().getBaseScore() < cvss) {
remove = true;
removeVulns.add(v);
break;
}
}
}
if (remove && !isBase()) {
matched = true;
if (this.notes != null) {
v.setNotes(this.notes);
}
dependency.addSuppressedVulnerability(v);
}
}
removeVulns.forEach((v) -> {
dependency.removeVulnerability(v);
});
}
}
/**
* Identifies if the cpe specified by the cpe suppression rule does not
* specify a version.
*
* @param c a suppression rule identifier
* @return true if the property type does not specify a version; otherwise
* false
*/
protected boolean cpeHasNoVersion(PropertyType c) {
return !c.isRegex() && countCharacter(c.getValue(), ':') <= 3;
}
/**
* Counts the number of occurrences of the character found within the
* string.
*
* @param str the string to check
* @param c the character to count
* @return the number of times the character is found in the string
*/
private int countCharacter(String str, char c) {
int count = 0;
int pos = str.indexOf(c) + 1;
while (pos > 0) {
count += 1;
pos = str.indexOf(c, pos) + 1;
}
return count;
}
/**
* Determines if the cpeEntry specified as a PropertyType matches the given
* Identifier.
*
* @param suppressionEntry a suppression rule entry
* @param identifier a CPE identifier to check
* @return true if the entry matches; otherwise false
*/
protected boolean purlMatches(PropertyType suppressionEntry, Identifier identifier) {
if (identifier instanceof PurlIdentifier) {
final PurlIdentifier purl = (PurlIdentifier) identifier;
return suppressionEntry.matches(purl.toString());
}
return false;
}
/**
* Determines if the cpeEntry specified as a PropertyType matches the given
* Identifier.
*
* @param suppressionEntry a suppression rule entry
* @param identifier a CPE identifier to check
* @return true if the entry matches; otherwise false
*/
protected boolean identifierMatches(PropertyType suppressionEntry, Identifier identifier) {
if (identifier instanceof PurlIdentifier) {
final PurlIdentifier purl = (PurlIdentifier) identifier;
return suppressionEntry.matches(purl.toGav());
} else if (identifier instanceof CpeIdentifier) {
//TODO check for regex - not just type
final Cpe cpeId = ((CpeIdentifier) identifier).getCpe();
if (suppressionEntry.isRegex()) {
try {
return suppressionEntry.matches(cpeId.toCpe22Uri());
} catch (CpeEncodingException ex) {
LOGGER.debug("Unable to convert CPE to 22 URI?" + cpeId.toString());
}
} else if (suppressionEntry.isCaseSensitive()) {
try {
return cpeId.toCpe22Uri().startsWith(suppressionEntry.getValue());
} catch (CpeEncodingException ex) {
LOGGER.debug("Unable to convert CPE to 22 URI?" + cpeId.toString());
}
} else {
final String id;
try {
id = cpeId.toCpe22Uri().toLowerCase();
} catch (CpeEncodingException ex) {
LOGGER.debug("Unable to convert CPE to 22 URI?" + cpeId.toString());
return false;
}
final String check = suppressionEntry.getValue().toLowerCase();
return id.startsWith(check);
}
}
return suppressionEntry.matches(identifier.getValue());
}
/**
* Standard toString implementation.
*
* @return a string representation of this object
*/
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(64);
sb.append("SuppressionRule{");
if (until != null) {
final String dt = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(until);
sb.append("until=").append(dt).append(',');
}
if (filePath != null) {
sb.append("filePath=").append(filePath).append(',');
}
if (sha1 != null) {
sb.append("sha1=").append(sha1).append(',');
}
if (packageUrl != null) {
sb.append("packageUrl=").append(packageUrl).append(',');
}
if (gav != null) {
sb.append("gav=").append(gav).append(',');
}
if (cpe != null && !cpe.isEmpty()) {
sb.append("cpe={");
cpe.forEach((pt) -> {
sb.append(pt).append(',');
});
sb.append('}');
}
if (cwe != null && !cwe.isEmpty()) {
sb.append("cwe={");
cwe.forEach((s) -> {
sb.append(s).append(',');
});
sb.append('}');
}
if (cve != null && !cve.isEmpty()) {
sb.append("cve={");
cve.forEach((s) -> {
sb.append(s).append(',');
});
sb.append('}');
}
if (vulnerabilityNames != null && !vulnerabilityNames.isEmpty()) {
sb.append("vulnerabilityName={");
vulnerabilityNames.forEach((pt) -> {
sb.append(pt).append(',');
});
sb.append('}');
}
if (cvssBelow != null && !cvssBelow.isEmpty()) {
sb.append("cvssBelow={");
cvssBelow.forEach((s) -> {
sb.append(s).append(',');
});
sb.append('}');
}
sb.append('}');
return sb.toString();
}
}