1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.nvd.ecosystem;
19
20 import io.github.jeremylong.openvulnerability.client.nvd.Config;
21 import io.github.jeremylong.openvulnerability.client.nvd.CpeMatch;
22 import io.github.jeremylong.openvulnerability.client.nvd.Node;
23 import io.github.jeremylong.openvulnerability.client.nvd.DefCveItem;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import javax.annotation.concurrent.NotThreadSafe;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 @NotThreadSafe
43 public class CveEcosystemMapper {
44
45
46
47
48 private final DescriptionEcosystemMapper descriptionEcosystemMapper = new DescriptionEcosystemMapper();
49
50
51
52 private final UrlEcosystemMapper urlEcosystemMapper = new UrlEcosystemMapper();
53
54
55
56
57
58
59
60
61
62
63 public String getEcosystem(DefCveItem cve) {
64
65
66 if (hasMultipleVendorProductConfigurations(cve)) {
67 return null;
68 }
69 final String ecosystem = descriptionEcosystemMapper.getEcosystem(cve);
70 if (ecosystem != null) {
71 return ecosystem;
72 }
73 return urlEcosystemMapper.getEcosystem(cve);
74 }
75
76
77
78
79
80
81
82
83
84 private boolean hasMultipleVendorProductConfigurations(DefCveItem cve) {
85 if (cve.getCve().getConfigurations() != null && !cve.getCve().getConfigurations().isEmpty()) {
86 final List<CpeMatch> cpeEntries = cve.getCve().getConfigurations().stream()
87 .map(Config::getNodes)
88 .flatMap(List::stream)
89 .filter(cpe -> cpe.getCpeMatch() != null)
90 .map(Node::getCpeMatch)
91 .flatMap(List::stream)
92 .filter(match -> match.getCriteria() != null)
93 .collect(Collectors.toList());
94 if (!cpeEntries.isEmpty() && cpeEntries.size() > 1) {
95 final CpeMatch firstMatch = cpeEntries.get(0);
96 final String uri = firstMatch.getCriteria();
97 final int pos = uri.indexOf(":", uri.indexOf(":", 10) + 1);
98 final String match = uri.substring(0, pos + 1);
99 return !cpeEntries.stream().allMatch(e -> e.getCriteria().startsWith(match));
100 }
101 }
102 return false;
103 }
104 }