forked from DependencyTrack/dependency-track
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycloneDXVexImporter.java
More file actions
266 lines (232 loc) · 12.4 KB
/
Copy pathCycloneDXVexImporter.java
File metadata and controls
266 lines (232 loc) · 12.4 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
/*
* This file is part of Dependency-Track.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.dependencytrack.parser.cyclonedx;
import alpine.common.logging.Logger;
import org.apache.commons.collections4.CollectionUtils;
import org.cyclonedx.model.Bom;
import org.cyclonedx.util.BomLink;
import org.dependencytrack.model.Analysis;
import org.dependencytrack.model.AnalysisJustification;
import org.dependencytrack.model.AnalysisResponse;
import org.dependencytrack.model.AnalysisState;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.ComponentIdentity;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Vulnerability;
import org.dependencytrack.parser.cyclonedx.util.ModelConverter;
import org.dependencytrack.persistence.QueryManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static java.util.Objects.requireNonNullElse;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToNull;
public class CycloneDXVexImporter {
private static final Logger LOGGER = Logger.getLogger(CycloneDXVexImporter.class);
private static final String COMMENTER = "CycloneDX VEX";
public void applyVex(final QueryManager qm, final Bom bom, final Project project) {
if (bom.getVulnerabilities() == null || bom.getVulnerabilities().isEmpty()) {
LOGGER.info("The uploaded VEX does not contain any vulnerabilities; Skipping VEX import");
return;
}
final List<org.cyclonedx.model.vulnerability.Vulnerability> vexVulns = getApplicableVexVulnerabilities(bom.getVulnerabilities());
if (vexVulns.isEmpty()) {
LOGGER.info("The uploaded VEX does not contain any applicable vulnerabilities; Skipping VEX import");
return;
}
if (!qm.hasVulnerabilities(project)) {
LOGGER.info("The project %s does not have any vulnerabilities; Skipping VEX import".formatted(project));
return;
}
final Map<String, BomRefTarget> targetByBomRef = indexComponents(bom);
final Map<String, List<Component>> componentsByBomRef = new HashMap<>();
for (final org.cyclonedx.model.vulnerability.Vulnerability vexVuln : vexVulns) {
final Vulnerability dtVuln = qm.getVulnerabilityByVulnId(vexVuln.getSource().getName(), vexVuln.getId());
if (dtVuln == null) {
LOGGER.warn("""
VEX contains analysis for vulnerability %s/%s, but the project is not affected by it. \
Analyses can currently only be applied to existing findings.\
""".formatted(vexVuln.getSource().getName(), vexVuln.getId()));
continue;
}
List<Component> vulnerableComponents = null;
for (org.cyclonedx.model.vulnerability.Vulnerability.Affect affect : vexVuln.getAffects()) {
final String affectedBomRef = affect.getRef();
final BomRefTarget affectedBomRefTarget = affectedBomRef != null
? targetByBomRef.get(affectedBomRef)
: null;
final boolean isProjectScoped =
(affectedBomRefTarget != null && affectedBomRefTarget.isMetadataComponent())
|| (affectedBomRefTarget == null && affectedBomRef != null && BomLink.isBomLink(affectedBomRef));
if (isProjectScoped) {
if (vulnerableComponents == null) {
vulnerableComponents = qm.getAllVulnerableComponents(project, dtVuln);
}
for (final Component component : vulnerableComponents) {
updateAnalysis(qm, component, dtVuln, vexVuln);
}
} else if (affectedBomRefTarget != null) {
final List<Component> components = componentsByBomRef.computeIfAbsent(affectedBomRef, ignored -> {
final var cid = new ComponentIdentity(affectedBomRefTarget.component());
return qm.matchIdentity(project, cid);
});
for (final Component component : components) {
updateAnalysis(qm, component, dtVuln, vexVuln);
}
} else {
LOGGER.warn("""
Unable to locate affected element (metadata.component or components[].component) \
based on the BOM reference %s. The vulnerability.affects[].ref \
node of %s/%s is not resolvable; Skipping it\
""".formatted(affectedBomRef, vexVuln.getSource().getName(), vexVuln.getId()));
}
}
}
}
private static List<org.cyclonedx.model.vulnerability.Vulnerability> getApplicableVexVulnerabilities(
final List<org.cyclonedx.model.vulnerability.Vulnerability> vexVulns) {
final var applicableVulns = new ArrayList<org.cyclonedx.model.vulnerability.Vulnerability>();
for (int vexVulnPos = 0; vexVulnPos < vexVulns.size(); vexVulnPos++) {
final var vexVuln = vexVulns.get(vexVulnPos);
if (isBlank(vexVuln.getId()) || vexVuln.getSource() == null || isBlank(vexVuln.getSource().getName())) {
LOGGER.warn("VEX vulnerability at position #%d does not have an ID and / or source; Skipping it".formatted(vexVulnPos));
continue;
}
final String vexVulnId = vexVuln.getId();
final String vexVulnSource = vexVuln.getSource().getName();
if (!Vulnerability.Source.isKnownSource(vexVulnSource)) {
LOGGER.warn("VEX vulnerability %s/%s at position #%d is from an unsupported source; Skipping it"
.formatted(vexVulnSource, vexVulnId, vexVulnPos));
continue;
}
if (CollectionUtils.isEmpty(vexVuln.getAffects())) {
LOGGER.debug("VEX vulnerability %s/%s at position #%d does not have an affects node; Skipping it"
.formatted(vexVulnSource, vexVulnId, vexVulnPos));
continue;
}
if (vexVuln.getAnalysis() == null) {
LOGGER.debug("VEX vulnerability %s/%s at position #%d does not have an analysis; Skipping it"
.formatted(vexVulnSource, vexVulnId, vexVulnPos));
continue;
}
applicableVulns.add(vexVuln);
}
return applicableVulns;
}
private record BomRefTarget(org.cyclonedx.model.Component component, boolean isMetadataComponent) {
}
private static Map<String, BomRefTarget> indexComponents(Bom bom) {
final Map<String, BomRefTarget> targetByBomRef = new HashMap<>();
if (bom == null) {
return targetByBomRef;
}
if (bom.getMetadata() != null && bom.getMetadata().getComponent() != null) {
indexComponents(List.of(bom.getMetadata().getComponent()), targetByBomRef, true);
}
indexComponents(bom.getComponents(), targetByBomRef, false);
return targetByBomRef;
}
private static void indexComponents(
List<org.cyclonedx.model.Component> components,
Map<String, BomRefTarget> targetByBomRef,
boolean metadataComponent) {
if (components == null) {
return;
}
for (final var component : components) {
if (component.getBomRef() != null) {
targetByBomRef.putIfAbsent(
component.getBomRef(),
new BomRefTarget(component, metadataComponent));
}
if (component.getComponents() != null && !component.getComponents().isEmpty()) {
indexComponents(component.getComponents(), targetByBomRef, false);
}
}
}
private static void updateAnalysis(final QueryManager qm, final Component component, final Vulnerability dtVuln,
final org.cyclonedx.model.vulnerability.Vulnerability cdxVuln) {
final org.cyclonedx.model.vulnerability.Vulnerability.Analysis cdxAnalysis = cdxVuln.getAnalysis();
final Analysis existing = qm.getAnalysis(component, dtVuln);
final AnalysisState oldState = existing != null
? requireNonNullElse(existing.getAnalysisState(), AnalysisState.NOT_SET)
: AnalysisState.NOT_SET;
final AnalysisJustification oldJustification = existing != null
? requireNonNullElse(existing.getAnalysisJustification(), AnalysisJustification.NOT_SET)
: AnalysisJustification.NOT_SET;
final AnalysisResponse oldResponse = existing != null
? requireNonNullElse(existing.getAnalysisResponse(), AnalysisResponse.NOT_SET)
: AnalysisResponse.NOT_SET;
final String oldDetails = existing != null
? requireNonNullElse(existing.getAnalysisDetails(), "")
: "";
AnalysisState newState = null;
boolean suppress = false;
if (cdxAnalysis.getState() != null) {
newState = ModelConverter.convertCdxVulnAnalysisStateToDtAnalysisState(cdxAnalysis.getState());
suppress = AnalysisState.FALSE_POSITIVE == newState
|| AnalysisState.NOT_AFFECTED == newState
|| AnalysisState.RESOLVED == newState;
}
AnalysisJustification newJustification = null;
if (cdxAnalysis.getJustification() != null) {
newJustification = ModelConverter.convertCdxVulnAnalysisJustificationToDtAnalysisJustification(cdxAnalysis.getJustification());
}
String newDetails = null;
if (trimToNull(cdxAnalysis.getDetail()) != null) {
newDetails = cdxAnalysis.getDetail().trim();
}
AnalysisResponse newResponse = null;
final List<AnalysisResponse> responseTrail;
if (cdxAnalysis.getResponses() != null && !cdxAnalysis.getResponses().isEmpty()) {
responseTrail = new ArrayList<>(cdxAnalysis.getResponses().size());
for (var cdxRes : cdxAnalysis.getResponses()) {
final AnalysisResponse response = ModelConverter.convertCdxVulnAnalysisResponseToDtAnalysisResponse(cdxRes);
responseTrail.add(response);
newResponse = response;
}
} else {
responseTrail = Collections.emptyList();
}
final Analysis updated;
if (existing != null) {
updated = qm.updateAnalysis(existing, newState, newJustification, newResponse, newDetails, suppress);
} else {
final AnalysisState createState = newState != null ? newState : AnalysisState.NOT_SET;
updated = qm.makeAnalysis(component, dtVuln, createState, newJustification, newResponse, newDetails, suppress);
}
if (newState != null && !Objects.equals(newState, oldState)) {
qm.makeAnalysisComment(updated, "Analysis: %s → %s".formatted(oldState, newState), COMMENTER);
}
if (newJustification != null && !Objects.equals(newJustification, oldJustification)) {
qm.makeAnalysisComment(updated, "Justification: %s → %s".formatted(oldJustification, newJustification), COMMENTER);
}
if (newDetails != null && !Objects.equals(newDetails, oldDetails)) {
qm.makeAnalysisComment(updated, "Details: %s".formatted(newDetails), COMMENTER);
}
for (final AnalysisResponse response : responseTrail) {
if (response != null && !Objects.equals(response, oldResponse)) {
qm.makeAnalysisComment(updated, "Vendor Response: %s → %s".formatted(oldResponse, response), COMMENTER);
}
}
}
}