| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.maven; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import java.io.UnsupportedEncodingException; |
| 24 | |
import java.net.URLEncoder; |
| 25 | |
import java.text.DateFormat; |
| 26 | |
import java.util.Date; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.Locale; |
| 29 | |
import java.util.Set; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
import org.apache.maven.artifact.Artifact; |
| 33 | |
import org.apache.maven.doxia.sink.Sink; |
| 34 | |
import org.apache.maven.doxia.sink.SinkFactory; |
| 35 | |
import org.apache.maven.plugin.AbstractMojo; |
| 36 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 37 | |
import org.apache.maven.plugin.MojoFailureException; |
| 38 | |
import org.apache.maven.plugins.annotations.Component; |
| 39 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 40 | |
import org.apache.maven.plugins.annotations.Mojo; |
| 41 | |
import org.apache.maven.plugins.annotations.Parameter; |
| 42 | |
import org.apache.maven.plugins.annotations.ResolutionScope; |
| 43 | |
import org.apache.maven.project.MavenProject; |
| 44 | |
import org.apache.maven.reporting.MavenMultiPageReport; |
| 45 | |
import org.apache.maven.reporting.MavenReport; |
| 46 | |
import org.apache.maven.reporting.MavenReportException; |
| 47 | |
import org.owasp.dependencycheck.Engine; |
| 48 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 49 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 50 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties; |
| 51 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 52 | |
import org.owasp.dependencycheck.dependency.Evidence; |
| 53 | |
import org.owasp.dependencycheck.dependency.Identifier; |
| 54 | |
import org.owasp.dependencycheck.dependency.Reference; |
| 55 | |
import org.owasp.dependencycheck.dependency.Vulnerability; |
| 56 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 57 | |
import org.owasp.dependencycheck.reporting.ReportGenerator; |
| 58 | |
import org.owasp.dependencycheck.utils.LogUtils; |
| 59 | |
import org.owasp.dependencycheck.utils.Settings; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
@Mojo(name = "check", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true, |
| 67 | |
requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM, |
| 68 | |
requiresOnline = true) |
| 69 | 0 | public class DependencyCheckMojo extends AbstractMojo implements MavenMultiPageReport { |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private static final String PROPERTIES_FILE = "mojo.properties"; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private static final String LOG_PROPERTIES_FILE = "log.properties"; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public static final String TEST_SCOPE = "test"; |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 0 | private static final String NEW_LINE = System.getProperty("line.separator", "\n").intern(); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
@Component |
| 92 | |
private MavenProject project; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
@Parameter(property = "report-name", defaultValue = "dependency-check-report") |
| 97 | |
private String reportName; |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Parameter(property = "logfile", defaultValue = "") |
| 102 | |
private String logFile; |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
@Parameter(property = "name", defaultValue = "Dependency-Check") |
| 107 | |
private String name; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
@Parameter(property = "description", defaultValue = "A report providing details on any published " |
| 112 | |
+ "vulnerabilities within project dependencies. This report is a best effort but may contain " |
| 113 | |
+ "false positives and false negatives.") |
| 114 | |
private String description; |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
@Parameter(property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}", required = true) |
| 119 | |
private File reportOutputDirectory; |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | 0 | @Parameter(property = "failBuildOnCVSS", defaultValue = "11", required = true) |
| 125 | |
private float failBuildOnCVSS = 11; |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
@Parameter(defaultValue = "${project.build.directory}", required = true) |
| 130 | |
private File outputDirectory; |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 136 | |
@Parameter(property = "autoupdate", defaultValue = "true", required = true) |
| 137 | |
private boolean autoUpdate = true; |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 143 | |
@Parameter(property = "format", defaultValue = "HTML", required = true) |
| 144 | |
private String format = "HTML"; |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 149 | |
@Parameter(property = "externalReport", defaultValue = "false", required = true) |
| 150 | |
private boolean externalReport = false; |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 155 | |
@Parameter(property = "proxyUrl", defaultValue = "", required = false) |
| 156 | |
private String proxyUrl = null; |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 161 | |
@Parameter(property = "proxyPort", defaultValue = "", required = false) |
| 162 | |
private String proxyPort = null; |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 167 | |
@Parameter(property = "proxyUsername", defaultValue = "", required = false) |
| 168 | |
private String proxyUsername = null; |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 173 | |
@Parameter(property = "proxyPassword", defaultValue = "", required = false) |
| 174 | |
private String proxyPassword = null; |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 179 | |
@Parameter(property = "connectionTimeout", defaultValue = "", required = false) |
| 180 | |
private String connectionTimeout = null; |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 185 | |
@Parameter(property = "suppressionFile", defaultValue = "", required = false) |
| 186 | |
private String suppressionFile = null; |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 191 | |
@Parameter(property = "showSummary", defaultValue = "true", required = false) |
| 192 | |
private boolean showSummary = true; |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | 0 | @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 197 | |
@Parameter(property = "nexusAnalyzerEnabled", defaultValue = "true", required = false) |
| 198 | |
private boolean nexusAnalyzerEnabled = true; |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 203 | |
@Parameter(property = "nexusUrl", defaultValue = "", required = false) |
| 204 | |
private String nexusUrl; |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 209 | |
@Parameter(property = "connectionString", defaultValue = "", required = false) |
| 210 | |
private String connectionString; |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 215 | |
@Parameter(property = "databaseDriverName", defaultValue = "", required = false) |
| 216 | |
private String databaseDriverName; |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 221 | |
@Parameter(property = "databaseDriverPath", defaultValue = "", required = false) |
| 222 | |
private String databaseDriverPath; |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 227 | |
@Parameter(property = "databaseUser", defaultValue = "", required = false) |
| 228 | |
private String databaseUser; |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
@SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"}) |
| 233 | |
@Parameter(property = "databasePassword", defaultValue = "", required = false) |
| 234 | |
private String databasePassword; |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
private Engine executeDependencyCheck() { |
| 243 | |
|
| 244 | 0 | final InputStream in = DependencyCheckMojo.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE); |
| 245 | 0 | LogUtils.prepareLogger(in, logFile); |
| 246 | |
|
| 247 | 0 | populateSettings(); |
| 248 | 0 | final Engine engine = new Engine(); |
| 249 | 0 | final Set<Artifact> artifacts = project.getArtifacts(); |
| 250 | 0 | for (Artifact a : artifacts) { |
| 251 | 0 | if (!TEST_SCOPE.equals(a.getScope())) { |
| 252 | 0 | engine.scan(a.getFile().getAbsolutePath()); |
| 253 | |
} |
| 254 | 0 | } |
| 255 | 0 | engine.analyzeDependencies(); |
| 256 | 0 | return engine; |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
private void generateExternalReports(Engine engine) { |
| 265 | 0 | DatabaseProperties prop = null; |
| 266 | 0 | CveDB cve = null; |
| 267 | |
try { |
| 268 | 0 | cve = new CveDB(); |
| 269 | 0 | cve.open(); |
| 270 | 0 | prop = cve.getDatabaseProperties(); |
| 271 | 0 | } catch (DatabaseException ex) { |
| 272 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, "Unable to retrieve DB Properties", ex); |
| 273 | |
} finally { |
| 274 | 0 | if (cve != null) { |
| 275 | 0 | cve.close(); |
| 276 | |
} |
| 277 | |
} |
| 278 | 0 | final ReportGenerator r = new ReportGenerator(project.getName(), engine.getDependencies(), engine.getAnalyzers(), prop); |
| 279 | |
try { |
| 280 | 0 | r.generateReports(outputDirectory.getCanonicalPath(), format); |
| 281 | 0 | } catch (IOException ex) { |
| 282 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, |
| 283 | |
"Unexpected exception occurred during analysis; please see the verbose error log for more details."); |
| 284 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, null, ex); |
| 285 | 0 | } catch (Exception ex) { |
| 286 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, |
| 287 | |
"Unexpected exception occurred during analysis; please see the verbose error log for more details."); |
| 288 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, null, ex); |
| 289 | 0 | } |
| 290 | 0 | } |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
private void generateMavenSiteReport(final Engine engine, Sink sink) { |
| 299 | 0 | final List<Dependency> dependencies = engine.getDependencies(); |
| 300 | |
|
| 301 | 0 | writeSiteReportHeader(sink, project.getName()); |
| 302 | 0 | writeSiteReportTOC(sink, dependencies); |
| 303 | |
|
| 304 | 0 | int cnt = 0; |
| 305 | 0 | for (Dependency d : dependencies) { |
| 306 | 0 | writeSiteReportDependencyHeader(sink, d); |
| 307 | 0 | cnt = writeSiteReportDependencyAnalysisExceptions(d, cnt, sink); |
| 308 | 0 | cnt = writeSiteReportDependencyEvidenceUsed(d, cnt, sink); |
| 309 | 0 | cnt = writeSiteReportDependencyRelatedDependencies(d, cnt, sink); |
| 310 | 0 | writeSiteReportDependencyIdentifiers(d, sink); |
| 311 | 0 | writeSiteReportDependencyVulnerabilities(d, sink, cnt); |
| 312 | 0 | } |
| 313 | 0 | sink.body_(); |
| 314 | 0 | } |
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
private void writeSiteReportDependencyVulnerabilities(Dependency d, Sink sink, int collapsibleHeaderCount) { |
| 325 | 0 | int cnt = collapsibleHeaderCount; |
| 326 | 0 | if (d.getVulnerabilities() != null && !d.getVulnerabilities().isEmpty()) { |
| 327 | 0 | for (Vulnerability v : d.getVulnerabilities()) { |
| 328 | |
|
| 329 | 0 | sink.paragraph(); |
| 330 | 0 | sink.bold(); |
| 331 | |
try { |
| 332 | 0 | sink.link("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + URLEncoder.encode(v.getName(), "US-ASCII")); |
| 333 | 0 | sink.text(v.getName()); |
| 334 | 0 | sink.link_(); |
| 335 | 0 | sink.bold_(); |
| 336 | 0 | } catch (UnsupportedEncodingException ex) { |
| 337 | 0 | sink.text(v.getName()); |
| 338 | 0 | sink.bold_(); |
| 339 | 0 | sink.lineBreak(); |
| 340 | 0 | sink.text("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + v.getName()); |
| 341 | 0 | } |
| 342 | 0 | sink.paragraph_(); |
| 343 | 0 | sink.paragraph(); |
| 344 | 0 | sink.text("Severity: "); |
| 345 | 0 | if (v.getCvssScore() < 4.0) { |
| 346 | 0 | sink.text("Low"); |
| 347 | |
} else { |
| 348 | 0 | if (v.getCvssScore() >= 7.0) { |
| 349 | 0 | sink.text("High"); |
| 350 | |
} else { |
| 351 | 0 | sink.text("Medium"); |
| 352 | |
} |
| 353 | |
} |
| 354 | 0 | sink.lineBreak(); |
| 355 | 0 | sink.text("CVSS Score: " + v.getCvssScore()); |
| 356 | 0 | if (v.getCwe() != null && !v.getCwe().isEmpty()) { |
| 357 | 0 | sink.lineBreak(); |
| 358 | 0 | sink.text("CWE: "); |
| 359 | 0 | sink.text(v.getCwe()); |
| 360 | |
} |
| 361 | 0 | sink.paragraph_(); |
| 362 | 0 | sink.paragraph(); |
| 363 | 0 | sink.text(v.getDescription()); |
| 364 | 0 | if (v.getReferences() != null && !v.getReferences().isEmpty()) { |
| 365 | 0 | sink.list(); |
| 366 | 0 | for (Reference ref : v.getReferences()) { |
| 367 | 0 | sink.listItem(); |
| 368 | 0 | sink.text(ref.getSource()); |
| 369 | 0 | sink.text(" - "); |
| 370 | 0 | sink.link(ref.getUrl()); |
| 371 | 0 | sink.text(ref.getName()); |
| 372 | 0 | sink.link_(); |
| 373 | 0 | sink.listItem_(); |
| 374 | 0 | } |
| 375 | 0 | sink.list_(); |
| 376 | |
} |
| 377 | 0 | sink.paragraph_(); |
| 378 | 0 | if (v.getVulnerableSoftware() != null && !v.getVulnerableSoftware().isEmpty()) { |
| 379 | 0 | sink.paragraph(); |
| 380 | |
|
| 381 | 0 | cnt += 1; |
| 382 | 0 | sink.rawText("Vulnerable Software <a href=\"javascript:toggleElement(this, 'vulnSoft" + cnt + "')\">[-]</a>"); |
| 383 | 0 | sink.rawText("<div id=\"vulnSoft" + cnt + "\" style=\"display:block\">"); |
| 384 | 0 | sink.list(); |
| 385 | 0 | for (VulnerableSoftware vs : v.getVulnerableSoftware()) { |
| 386 | 0 | sink.listItem(); |
| 387 | |
try { |
| 388 | 0 | sink.link("http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + URLEncoder.encode(vs.getName(), "US-ASCII")); |
| 389 | 0 | sink.text(vs.getName()); |
| 390 | 0 | sink.link_(); |
| 391 | 0 | if (vs.hasPreviousVersion()) { |
| 392 | 0 | sink.text(" and all previous versions."); |
| 393 | |
} |
| 394 | 0 | } catch (UnsupportedEncodingException ex) { |
| 395 | 0 | sink.text(vs.getName()); |
| 396 | 0 | if (vs.hasPreviousVersion()) { |
| 397 | 0 | sink.text(" and all previous versions."); |
| 398 | |
} |
| 399 | 0 | sink.text(" (http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + vs.getName() + ")"); |
| 400 | 0 | } |
| 401 | |
|
| 402 | 0 | sink.listItem_(); |
| 403 | 0 | } |
| 404 | 0 | sink.list_(); |
| 405 | 0 | sink.rawText("</div>"); |
| 406 | 0 | sink.paragraph_(); |
| 407 | |
} |
| 408 | 0 | } |
| 409 | |
} |
| 410 | 0 | } |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
private void writeSiteReportDependencyIdentifiers(Dependency d, Sink sink) { |
| 419 | 0 | if (d.getIdentifiers() != null && !d.getIdentifiers().isEmpty()) { |
| 420 | 0 | sink.sectionTitle4(); |
| 421 | 0 | sink.text("Identifiers"); |
| 422 | 0 | sink.sectionTitle4_(); |
| 423 | 0 | sink.list(); |
| 424 | 0 | for (Identifier i : d.getIdentifiers()) { |
| 425 | 0 | sink.listItem(); |
| 426 | 0 | sink.text(i.getType()); |
| 427 | 0 | sink.text(": "); |
| 428 | 0 | if (i.getUrl() != null && i.getUrl().length() > 0) { |
| 429 | 0 | sink.link(i.getUrl()); |
| 430 | 0 | sink.text(i.getValue()); |
| 431 | 0 | sink.link_(); |
| 432 | |
} else { |
| 433 | 0 | sink.text(i.getValue()); |
| 434 | |
} |
| 435 | 0 | if (i.getDescription() != null && i.getDescription().length() > 0) { |
| 436 | 0 | sink.lineBreak(); |
| 437 | 0 | sink.text(i.getDescription()); |
| 438 | |
} |
| 439 | 0 | sink.listItem_(); |
| 440 | 0 | } |
| 441 | 0 | sink.list_(); |
| 442 | |
} |
| 443 | 0 | } |
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
private int writeSiteReportDependencyRelatedDependencies(Dependency d, int collapsibleHeaderCount, Sink sink) { |
| 454 | 0 | int cnt = collapsibleHeaderCount; |
| 455 | 0 | if (d.getRelatedDependencies() != null && !d.getRelatedDependencies().isEmpty()) { |
| 456 | 0 | cnt += 1; |
| 457 | 0 | sink.sectionTitle4(); |
| 458 | 0 | sink.rawText("Related Dependencies <a href=\"javascript:toggleElement(this, 'related" + cnt + "')\">[+]</a>"); |
| 459 | 0 | sink.sectionTitle4_(); |
| 460 | 0 | sink.rawText("<div id=\"related" + cnt + "\" style=\"display:none\">"); |
| 461 | 0 | sink.list(); |
| 462 | 0 | for (Dependency r : d.getRelatedDependencies()) { |
| 463 | 0 | sink.listItem(); |
| 464 | 0 | sink.text(r.getFileName()); |
| 465 | 0 | sink.list(); |
| 466 | 0 | writeListItem(sink, "File Path: " + r.getFilePath()); |
| 467 | 0 | writeListItem(sink, "SHA1: " + r.getSha1sum()); |
| 468 | 0 | writeListItem(sink, "MD5: " + r.getMd5sum()); |
| 469 | 0 | sink.list_(); |
| 470 | 0 | sink.listItem_(); |
| 471 | 0 | } |
| 472 | 0 | sink.list_(); |
| 473 | 0 | sink.rawText("</div>"); |
| 474 | |
} |
| 475 | 0 | return cnt; |
| 476 | |
} |
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
private int writeSiteReportDependencyEvidenceUsed(Dependency d, int collapsibleHeaderCount, Sink sink) { |
| 487 | 0 | int cnt = collapsibleHeaderCount; |
| 488 | 0 | if (d.getEvidenceUsed() != null && d.getEvidenceUsed().size() > 0) { |
| 489 | 0 | cnt += 1; |
| 490 | 0 | sink.sectionTitle4(); |
| 491 | 0 | sink.rawText("Evidence Collected <a href=\"javascript:toggleElement(this, 'evidence" + cnt + "')\">[+]</a>"); |
| 492 | 0 | sink.sectionTitle4_(); |
| 493 | 0 | sink.rawText("<div id=\"evidence" + cnt + "\" style=\"display:none\">"); |
| 494 | 0 | sink.table(); |
| 495 | 0 | sink.tableRow(); |
| 496 | 0 | writeTableHeaderCell(sink, "Source"); |
| 497 | 0 | writeTableHeaderCell(sink, "Name"); |
| 498 | 0 | writeTableHeaderCell(sink, "Value"); |
| 499 | 0 | sink.tableRow_(); |
| 500 | 0 | for (Evidence e : d.getEvidenceUsed()) { |
| 501 | 0 | sink.tableRow(); |
| 502 | 0 | writeTableCell(sink, e.getSource()); |
| 503 | 0 | writeTableCell(sink, e.getName()); |
| 504 | 0 | writeTableCell(sink, e.getValue()); |
| 505 | 0 | sink.tableRow_(); |
| 506 | 0 | } |
| 507 | 0 | sink.table_(); |
| 508 | 0 | sink.rawText("</div>"); |
| 509 | |
} |
| 510 | 0 | return cnt; |
| 511 | |
} |
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
private int writeSiteReportDependencyAnalysisExceptions(Dependency d, int collapsibleHeaderCount, Sink sink) { |
| 522 | 0 | int cnt = collapsibleHeaderCount; |
| 523 | 0 | if (d.getAnalysisExceptions() != null && !d.getAnalysisExceptions().isEmpty()) { |
| 524 | 0 | cnt += 1; |
| 525 | 0 | sink.sectionTitle4(); |
| 526 | 0 | sink.rawText("<font style=\"color:red\">Errors occurred during analysis:</font> <a href=\"javascript:toggleElement(this, 'errors" |
| 527 | |
+ cnt + "')\">[+]</a>"); |
| 528 | 0 | sink.sectionTitle4_(); |
| 529 | 0 | sink.rawText("<div id=\"errors" + cnt + "\">"); |
| 530 | 0 | sink.list(); |
| 531 | 0 | for (Exception e : d.getAnalysisExceptions()) { |
| 532 | 0 | sink.listItem(); |
| 533 | 0 | sink.text(e.getMessage()); |
| 534 | 0 | sink.listItem_(); |
| 535 | 0 | } |
| 536 | 0 | sink.list_(); |
| 537 | 0 | sink.rawText("</div>"); |
| 538 | |
} |
| 539 | 0 | return cnt; |
| 540 | |
} |
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
private void writeSiteReportDependencyHeader(Sink sink, Dependency d) { |
| 549 | 0 | sink.sectionTitle2(); |
| 550 | 0 | sink.anchor("sha1" + d.getSha1sum()); |
| 551 | 0 | sink.text(d.getFileName()); |
| 552 | 0 | sink.anchor_(); |
| 553 | 0 | sink.sectionTitle2_(); |
| 554 | 0 | if (d.getDescription() != null && d.getDescription().length() > 0) { |
| 555 | 0 | sink.paragraph(); |
| 556 | 0 | sink.bold(); |
| 557 | 0 | sink.text("Description: "); |
| 558 | 0 | sink.bold_(); |
| 559 | 0 | sink.text(d.getDescription()); |
| 560 | 0 | sink.paragraph_(); |
| 561 | |
} |
| 562 | 0 | if (d.getLicense() != null && d.getLicense().length() > 0) { |
| 563 | 0 | sink.paragraph(); |
| 564 | 0 | sink.bold(); |
| 565 | 0 | sink.text("License: "); |
| 566 | 0 | sink.bold_(); |
| 567 | 0 | if (d.getLicense().startsWith("http://") && !d.getLicense().contains(" ")) { |
| 568 | 0 | sink.link(d.getLicense()); |
| 569 | 0 | sink.text(d.getLicense()); |
| 570 | 0 | sink.link_(); |
| 571 | |
} else { |
| 572 | 0 | sink.text(d.getLicense()); |
| 573 | |
} |
| 574 | 0 | sink.paragraph_(); |
| 575 | |
} |
| 576 | 0 | } |
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
private void writeListItem(Sink sink, String text) { |
| 585 | 0 | sink.listItem(); |
| 586 | 0 | sink.text(text); |
| 587 | 0 | sink.listItem_(); |
| 588 | 0 | } |
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
private void writeTableCell(Sink sink, String text) { |
| 597 | 0 | sink.tableCell(); |
| 598 | 0 | sink.text(text); |
| 599 | 0 | sink.tableCell_(); |
| 600 | 0 | } |
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
private void writeTableHeaderCell(Sink sink, String text) { |
| 609 | 0 | sink.tableHeaderCell(); |
| 610 | 0 | sink.text(text); |
| 611 | 0 | sink.tableHeaderCell_(); |
| 612 | 0 | } |
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
private void writeSiteReportTOC(Sink sink, final List<Dependency> dependencies) { |
| 621 | 0 | sink.list(); |
| 622 | 0 | for (Dependency d : dependencies) { |
| 623 | 0 | sink.listItem(); |
| 624 | 0 | sink.link("#sha1" + d.getSha1sum()); |
| 625 | 0 | sink.text(d.getFileName()); |
| 626 | 0 | sink.link_(); |
| 627 | 0 | if (!d.getVulnerabilities().isEmpty()) { |
| 628 | 0 | sink.rawText(" <font style=\"color:red\">•</font>"); |
| 629 | |
} |
| 630 | 0 | if (!d.getRelatedDependencies().isEmpty()) { |
| 631 | 0 | sink.list(); |
| 632 | 0 | for (Dependency r : d.getRelatedDependencies()) { |
| 633 | 0 | writeListItem(sink, r.getFileName()); |
| 634 | 0 | } |
| 635 | 0 | sink.list_(); |
| 636 | |
} |
| 637 | 0 | sink.listItem_(); |
| 638 | 0 | } |
| 639 | 0 | sink.list_(); |
| 640 | 0 | } |
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
|
| 648 | |
private void writeSiteReportHeader(Sink sink, String projectName) { |
| 649 | 0 | sink.head(); |
| 650 | 0 | sink.title(); |
| 651 | 0 | sink.text("Dependency-Check Report: " + projectName); |
| 652 | 0 | sink.title_(); |
| 653 | 0 | sink.head_(); |
| 654 | 0 | sink.body(); |
| 655 | 0 | sink.rawText("<script type=\"text/javascript\">"); |
| 656 | 0 | sink.rawText("function toggleElement(el, targetId) {"); |
| 657 | 0 | sink.rawText("if (el.innerText == '[+]') {"); |
| 658 | 0 | sink.rawText(" el.innerText = '[-]';"); |
| 659 | 0 | sink.rawText(" document.getElementById(targetId).style.display='block';"); |
| 660 | 0 | sink.rawText("} else {"); |
| 661 | 0 | sink.rawText(" el.innerText = '[+]';"); |
| 662 | 0 | sink.rawText(" document.getElementById(targetId).style.display='none';"); |
| 663 | 0 | sink.rawText("}"); |
| 664 | |
|
| 665 | 0 | sink.rawText("}"); |
| 666 | 0 | sink.rawText("</script>"); |
| 667 | 0 | sink.section1(); |
| 668 | 0 | sink.sectionTitle1(); |
| 669 | 0 | sink.text("Project: " + projectName); |
| 670 | 0 | sink.sectionTitle1_(); |
| 671 | 0 | sink.date(); |
| 672 | 0 | final Date now = new Date(); |
| 673 | 0 | sink.text(DateFormat.getDateTimeInstance().format(now)); |
| 674 | 0 | sink.date_(); |
| 675 | 0 | sink.section1_(); |
| 676 | 0 | } |
| 677 | |
|
| 678 | |
|
| 679 | |
|
| 680 | |
|
| 681 | |
|
| 682 | |
|
| 683 | |
private void populateSettings() { |
| 684 | 0 | InputStream mojoProperties = null; |
| 685 | |
try { |
| 686 | 0 | mojoProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE); |
| 687 | 0 | Settings.mergeProperties(mojoProperties); |
| 688 | 0 | } catch (IOException ex) { |
| 689 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.WARNING, "Unable to load the dependency-check ant task.properties file."); |
| 690 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, null, ex); |
| 691 | |
} finally { |
| 692 | 0 | if (mojoProperties != null) { |
| 693 | |
try { |
| 694 | 0 | mojoProperties.close(); |
| 695 | 0 | } catch (IOException ex) { |
| 696 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINEST, null, ex); |
| 697 | 0 | } |
| 698 | |
} |
| 699 | |
} |
| 700 | |
|
| 701 | 0 | Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate); |
| 702 | |
|
| 703 | 0 | if (proxyUrl != null && !proxyUrl.isEmpty()) { |
| 704 | 0 | Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl); |
| 705 | |
} |
| 706 | 0 | if (proxyPort != null && !proxyPort.isEmpty()) { |
| 707 | 0 | Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort); |
| 708 | |
} |
| 709 | 0 | if (proxyUsername != null && !proxyUsername.isEmpty()) { |
| 710 | 0 | Settings.setString(Settings.KEYS.PROXY_USERNAME, proxyUsername); |
| 711 | |
} |
| 712 | 0 | if (proxyPassword != null && !proxyPassword.isEmpty()) { |
| 713 | 0 | Settings.setString(Settings.KEYS.PROXY_PASSWORD, proxyPassword); |
| 714 | |
} |
| 715 | 0 | if (connectionTimeout != null && !connectionTimeout.isEmpty()) { |
| 716 | 0 | Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout); |
| 717 | |
} |
| 718 | 0 | if (suppressionFile != null && !suppressionFile.isEmpty()) { |
| 719 | 0 | Settings.setString(Settings.KEYS.SUPPRESSION_FILE, suppressionFile); |
| 720 | |
} |
| 721 | 0 | Settings.setBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED, nexusAnalyzerEnabled); |
| 722 | 0 | if (nexusUrl != null && !nexusUrl.isEmpty()) { |
| 723 | 0 | Settings.setString(Settings.KEYS.ANALYZER_NEXUS_URL, nexusUrl); |
| 724 | |
} |
| 725 | 0 | if (databaseDriverName != null && !databaseDriverName.isEmpty()) { |
| 726 | 0 | Settings.setString(Settings.KEYS.DB_DRIVER_NAME, databaseDriverName); |
| 727 | |
} |
| 728 | 0 | if (databaseDriverPath != null && !databaseDriverPath.isEmpty()) { |
| 729 | 0 | Settings.setString(Settings.KEYS.DB_DRIVER_PATH, databaseDriverPath); |
| 730 | |
} |
| 731 | 0 | if (connectionString != null && !connectionString.isEmpty()) { |
| 732 | 0 | Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString); |
| 733 | |
} |
| 734 | 0 | if (databaseUser != null && !databaseUser.isEmpty()) { |
| 735 | 0 | Settings.setString(Settings.KEYS.DB_USER, databaseUser); |
| 736 | |
} |
| 737 | 0 | if (databasePassword != null && !databasePassword.isEmpty()) { |
| 738 | 0 | Settings.setString(Settings.KEYS.DB_PASSWORD, databasePassword); |
| 739 | |
} |
| 740 | 0 | } |
| 741 | |
|
| 742 | |
|
| 743 | |
|
| 744 | |
|
| 745 | |
|
| 746 | |
|
| 747 | |
|
| 748 | |
public void execute() throws MojoExecutionException, MojoFailureException { |
| 749 | 0 | final Engine engine = executeDependencyCheck(); |
| 750 | 0 | generateExternalReports(engine); |
| 751 | 0 | if (this.failBuildOnCVSS <= 10) { |
| 752 | 0 | checkForFailure(engine.getDependencies()); |
| 753 | |
} |
| 754 | 0 | if (this.showSummary) { |
| 755 | 0 | showSummary(engine.getDependencies()); |
| 756 | |
} |
| 757 | 0 | } |
| 758 | |
|
| 759 | |
|
| 760 | |
|
| 761 | |
|
| 762 | |
|
| 763 | |
|
| 764 | |
|
| 765 | |
|
| 766 | |
public void generate(@SuppressWarnings("deprecation") org.codehaus.doxia.sink.Sink sink, |
| 767 | |
Locale locale) throws MavenReportException { |
| 768 | 0 | generate((Sink) sink, null, locale); |
| 769 | 0 | } |
| 770 | |
|
| 771 | |
|
| 772 | |
|
| 773 | |
|
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException { |
| 780 | 0 | final Engine engine = executeDependencyCheck(); |
| 781 | 0 | generateMavenSiteReport(engine, sink); |
| 782 | 0 | } |
| 783 | |
|
| 784 | |
|
| 785 | |
|
| 786 | |
|
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | |
public String getOutputName() { |
| 791 | 0 | return reportName; |
| 792 | |
} |
| 793 | |
|
| 794 | |
|
| 795 | |
|
| 796 | |
|
| 797 | |
|
| 798 | |
|
| 799 | |
public String getCategoryName() { |
| 800 | 0 | return MavenReport.CATEGORY_PROJECT_REPORTS; |
| 801 | |
} |
| 802 | |
|
| 803 | |
|
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
|
| 809 | |
public String getName(Locale locale) { |
| 810 | 0 | return name; |
| 811 | |
} |
| 812 | |
|
| 813 | |
|
| 814 | |
|
| 815 | |
|
| 816 | |
|
| 817 | |
|
| 818 | |
public void setReportOutputDirectory(File directory) { |
| 819 | 0 | reportOutputDirectory = directory; |
| 820 | 0 | } |
| 821 | |
|
| 822 | |
|
| 823 | |
|
| 824 | |
|
| 825 | |
|
| 826 | |
|
| 827 | |
public File getReportOutputDirectory() { |
| 828 | 0 | return reportOutputDirectory; |
| 829 | |
} |
| 830 | |
|
| 831 | |
|
| 832 | |
|
| 833 | |
|
| 834 | |
|
| 835 | |
|
| 836 | |
|
| 837 | |
public String getDescription(Locale locale) { |
| 838 | 0 | return description; |
| 839 | |
} |
| 840 | |
|
| 841 | |
|
| 842 | |
|
| 843 | |
|
| 844 | |
|
| 845 | |
|
| 846 | |
public boolean isExternalReport() { |
| 847 | 0 | return externalReport; |
| 848 | |
} |
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
|
| 853 | |
|
| 854 | |
|
| 855 | |
public boolean canGenerateReport() { |
| 856 | 0 | return true; |
| 857 | |
} |
| 858 | |
|
| 859 | |
|
| 860 | |
|
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
private void checkForFailure(List<Dependency> dependencies) throws MojoFailureException { |
| 868 | 0 | final StringBuilder ids = new StringBuilder(); |
| 869 | 0 | for (Dependency d : dependencies) { |
| 870 | 0 | for (Vulnerability v : d.getVulnerabilities()) { |
| 871 | 0 | if (v.getCvssScore() >= failBuildOnCVSS) { |
| 872 | 0 | if (ids.length() == 0) { |
| 873 | 0 | ids.append(v.getName()); |
| 874 | |
} else { |
| 875 | 0 | ids.append(", ").append(v.getName()); |
| 876 | |
} |
| 877 | |
} |
| 878 | 0 | } |
| 879 | 0 | } |
| 880 | 0 | if (ids.length() > 0) { |
| 881 | 0 | final String msg = String.format("%n%nDependency-Check Failure:%n" |
| 882 | |
+ "One or more dependencies were identified with vulnerabilities that have a CVSS score greater then '%.1f': %s%n" |
| 883 | |
+ "See the dependency-check report for more details.%n%n", failBuildOnCVSS, ids.toString()); |
| 884 | 0 | throw new MojoFailureException(msg); |
| 885 | |
} |
| 886 | 0 | } |
| 887 | |
|
| 888 | |
|
| 889 | |
|
| 890 | |
|
| 891 | |
|
| 892 | |
|
| 893 | |
private void showSummary(List<Dependency> dependencies) { |
| 894 | 0 | final StringBuilder summary = new StringBuilder(); |
| 895 | 0 | for (Dependency d : dependencies) { |
| 896 | 0 | boolean firstEntry = true; |
| 897 | 0 | final StringBuilder ids = new StringBuilder(); |
| 898 | 0 | for (Vulnerability v : d.getVulnerabilities()) { |
| 899 | 0 | if (firstEntry) { |
| 900 | 0 | firstEntry = false; |
| 901 | |
} else { |
| 902 | 0 | ids.append(", "); |
| 903 | |
} |
| 904 | 0 | ids.append(v.getName()); |
| 905 | 0 | } |
| 906 | 0 | if (ids.length() > 0) { |
| 907 | 0 | summary.append(d.getFileName()).append(" ("); |
| 908 | 0 | firstEntry = true; |
| 909 | 0 | for (Identifier id : d.getIdentifiers()) { |
| 910 | 0 | if (firstEntry) { |
| 911 | 0 | firstEntry = false; |
| 912 | |
} else { |
| 913 | 0 | summary.append(", "); |
| 914 | |
} |
| 915 | 0 | summary.append(id.getValue()); |
| 916 | 0 | } |
| 917 | 0 | summary.append(") : ").append(ids).append(NEW_LINE); |
| 918 | |
} |
| 919 | 0 | } |
| 920 | 0 | if (summary.length() > 0) { |
| 921 | 0 | final String msg = String.format("%n%n" |
| 922 | |
+ "One or more dependencies were identified with known vulnerabilities:%n%n%s" |
| 923 | |
+ "%n%nSee the dependency-check report for more details.%n%n", summary.toString()); |
| 924 | 0 | Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.WARNING, msg); |
| 925 | |
} |
| 926 | 0 | } |
| 927 | |
} |