Coverage Report - org.owasp.dependencycheck.maven.DependencyCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyCheckMojo
0%
0/312
0%
0/114
3.37
 
 1  
 /*
 2  
  * This file is part of dependency-check-maven.
 3  
  *
 4  
  * Dependency-check-maven is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-maven is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-maven. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.maven;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.UnsupportedEncodingException;
 25  
 import java.net.URLEncoder;
 26  
 import java.text.DateFormat;
 27  
 import java.util.Date;
 28  
 import java.util.List;
 29  
 import java.util.Locale;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 import org.apache.maven.doxia.sink.SinkFactory;
 33  
 import org.apache.maven.plugin.AbstractMojo;
 34  
 import org.apache.maven.plugin.MojoExecutionException;
 35  
 import org.apache.maven.project.MavenProject;
 36  
 import java.util.Set;
 37  
 import org.apache.maven.artifact.Artifact;
 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.reporting.MavenMultiPageReport;
 44  
 import org.apache.maven.reporting.MavenReport;
 45  
 import org.apache.maven.reporting.MavenReportException;
 46  
 import org.apache.maven.doxia.sink.Sink;
 47  
 import org.apache.maven.plugin.MojoFailureException;
 48  
 import org.owasp.dependencycheck.Engine;
 49  
 import org.owasp.dependencycheck.dependency.Dependency;
 50  
 import org.owasp.dependencycheck.dependency.Evidence;
 51  
 import org.owasp.dependencycheck.dependency.Identifier;
 52  
 import org.owasp.dependencycheck.dependency.Reference;
 53  
 import org.owasp.dependencycheck.dependency.Vulnerability;
 54  
 import org.owasp.dependencycheck.dependency.VulnerableSoftware;
 55  
 import org.owasp.dependencycheck.reporting.ReportGenerator;
 56  
 import org.owasp.dependencycheck.utils.LogUtils;
 57  
 import org.owasp.dependencycheck.utils.Settings;
 58  
 
 59  
 /**
 60  
  * Maven Plugin that checks project dependencies to see if they have any known
 61  
  * published vulnerabilities.
 62  
  *
 63  
  * @author Jeremy Long (jeremy.long@owasp.org)
 64  
  */
 65  
 @Mojo(name = "check", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true,
 66  
         requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM,
 67  
         requiresOnline = true)
 68  0
 public class DependencyCheckMojo extends AbstractMojo implements MavenMultiPageReport {
 69  
 
 70  
     /**
 71  
      * The properties file location.
 72  
      */
 73  
     private static final String PROPERTIES_FILE = "mojo.properties";
 74  
     /**
 75  
      * Name of the logging properties file.
 76  
      */
 77  
     private static final String LOG_PROPERTIES_FILE = "log.properties";
 78  
     /**
 79  
      * The name of the test scope.
 80  
      */
 81  
     public static final String TEST_SCOPE = "test";
 82  
     // <editor-fold defaultstate="collapsed" desc="Maven bound parameters and components">
 83  
     /**
 84  
      * The Maven Project Object.
 85  
      */
 86  
     @Component
 87  
     private MavenProject project;
 88  
     /**
 89  
      * The name of the site report destination.
 90  
      */
 91  
     @Parameter(property = "report-name", defaultValue = "dependency-check-report")
 92  
     private String reportName;
 93  
     /**
 94  
      * The path to the verbose log
 95  
      */
 96  
     @Parameter(property = "logfile", defaultValue = "")
 97  
     private String logFile;
 98  
     /**
 99  
      * The name of the report to be displayed in the Maven Generated Reports
 100  
      * page
 101  
      */
 102  
     @Parameter(property = "name", defaultValue = "Dependency-Check")
 103  
     private String name;
 104  
     /**
 105  
      * The description of the Dependency-Check report to be displayed in the
 106  
      * Maven Generated Reports page
 107  
      */
 108  
     @Parameter(property = "description", defaultValue = "A report providing details on any published "
 109  
             + "vulnerabilities within project dependencies. This report is a best effort but may contain "
 110  
             + "false positives and false negatives.")
 111  
     private String description;
 112  
     /**
 113  
      * Specifies the destination directory for the generated Dependency-Check
 114  
      * report.
 115  
      */
 116  
     @Parameter(property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}", required = true)
 117  
     private File reportOutputDirectory;
 118  
     /**
 119  
      * Specifies if the build should be failed if a CVSS score above a specified
 120  
      * level is identified. The default is 11 which means since the CVSS scores
 121  
      * are 0-10, by default the build will never fail.
 122  
      */
 123  0
     @Parameter(property = "failBuildOnCVSS", defaultValue = "11", required = true)
 124  
     private float failBuildOnCVSS = 11;
 125  
     /**
 126  
      * The output directory.
 127  
      */
 128  
     @Parameter(defaultValue = "${project.build.directory}", required = true)
 129  
     private File outputDirectory;
 130  
     /**
 131  
      * Sets whether auto-updating of the NVD CVE/CPE data is enabled. It is not
 132  
      * recommended that this be turned to false. Default is true.
 133  
      */
 134  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 135  
     @Parameter(property = "autoupdate", defaultValue = "true", required = true)
 136  
     private boolean autoUpdate = true;
 137  
     /**
 138  
      * The report format to be generated (HTML, XML, VULN, ALL). This
 139  
      * configuration option has no affect if using this within the Site plugin
 140  
      * unless the externalReport is set to true. Default is HTML.
 141  
      */
 142  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 143  
     @Parameter(property = "format", defaultValue = "HTML", required = true)
 144  
     private String format = "HTML";
 145  
     /**
 146  
      * Sets whether or not the external report format should be used.
 147  
      */
 148  0
     @SuppressWarnings({"CanBeFinal", "FieldCanBeLocal"})
 149  
     @Parameter(property = "externalReport", defaultValue = "false", required = true)
 150  
     private boolean externalReport = false;
 151  
     /**
 152  
      * The Proxy URL.
 153  
      */
 154  0
     @SuppressWarnings("CanBeFinal")
 155  
     @Parameter(property = "proxyUrl", defaultValue = "", required = false)
 156  
     private String proxyUrl = null;
 157  
     /**
 158  
      * The Proxy Port.
 159  
      */
 160  0
     @SuppressWarnings("CanBeFinal")
 161  
     @Parameter(property = "proxyPort", defaultValue = "", required = false)
 162  
     private String proxyPort = null;
 163  
     /**
 164  
      * The Connection Timeout.
 165  
      */
 166  0
     @SuppressWarnings("CanBeFinal")
 167  
     @Parameter(property = "connectionTimeout", defaultValue = "", required = false)
 168  
     private String connectionTimeout = null;
 169  
 
 170  
     // </editor-fold>
 171  
     /**
 172  
      * Executes the Dependency-Check on the dependent libraries.
 173  
      *
 174  
      * @return the Engine used to scan the dependencies.
 175  
      */
 176  
     private Engine executeDependencyCheck() {
 177  
 
 178  0
         final InputStream in = DependencyCheckMojo.class.getClassLoader().getResourceAsStream(LOG_PROPERTIES_FILE);
 179  0
         LogUtils.prepareLogger(in, logFile);
 180  
 
 181  0
         populateSettings();
 182  0
         final Engine engine = new Engine();
 183  0
         final Set<Artifact> artifacts = project.getArtifacts();
 184  0
         for (Artifact a : artifacts) {
 185  0
             if (!TEST_SCOPE.equals(a.getScope())) {
 186  0
                 engine.scan(a.getFile().getAbsolutePath());
 187  
             }
 188  
         }
 189  0
         engine.analyzeDependencies();
 190  0
         return engine;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Generates the reports for a given dependency-check engine.
 195  
      *
 196  
      * @param engine a dependency-check engine
 197  
      */
 198  
     private void generateExternalReports(Engine engine) {
 199  0
         final ReportGenerator r = new ReportGenerator(project.getName(), engine.getDependencies(), engine.getAnalyzers());
 200  
         try {
 201  0
             r.generateReports(outputDirectory.getCanonicalPath(), format);
 202  0
         } catch (IOException ex) {
 203  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 204  0
         } catch (Exception ex) {
 205  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.SEVERE, null, ex);
 206  0
         }
 207  0
     }
 208  
 
 209  
     /**
 210  
      * Generates a dependency-check report using the Maven Site format.
 211  
      *
 212  
      * @param engine the engine used to scan the dependencies
 213  
      * @param sink the sink to write the data to
 214  
      */
 215  
     private void generateMavenSiteReport(final Engine engine, Sink sink) {
 216  0
         final List<Dependency> dependencies = engine.getDependencies();
 217  
 
 218  0
         writeSiteReportHeader(sink, project.getName());
 219  0
         writeSiteReportTOC(sink, dependencies);
 220  
 
 221  0
         int cnt = 0;
 222  0
         for (Dependency d : dependencies) {
 223  0
             writeSiteReportDependencyHeader(sink, d);
 224  0
             cnt = writeSiteReportDependencyAnalysisExceptions(d, cnt, sink);
 225  0
             cnt = writeSiteReportDependencyEvidenceUsed(d, cnt, sink);
 226  0
             cnt = writeSiteReportDependencyRelatedDependencies(d, cnt, sink);
 227  0
             writeSiteReportDependencyIdentifiers(d, sink);
 228  0
             writeSiteReportDependencyVulnerabilities(d, sink, cnt);
 229  
         }
 230  0
         sink.body_();
 231  0
     }
 232  
 
 233  
     // <editor-fold defaultstate="collapsed" desc="various writeXXXXX methods to generate the Site Report">
 234  
     /**
 235  
      * Writes the vulnerabilities to the site report.
 236  
      *
 237  
      * @param d the dependency
 238  
      * @param sink the sink to write the data to
 239  
      * @param collapsibleHeaderCount the collapsible header count
 240  
      */
 241  
     private void writeSiteReportDependencyVulnerabilities(Dependency d, Sink sink, int collapsibleHeaderCount) {
 242  0
         int cnt = collapsibleHeaderCount;
 243  0
         if (d.getVulnerabilities() != null && !d.getVulnerabilities().isEmpty()) {
 244  0
             for (Vulnerability v : d.getVulnerabilities()) {
 245  
 
 246  0
                 sink.paragraph();
 247  0
                 sink.bold();
 248  
                 try {
 249  0
                     sink.link("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + URLEncoder.encode(v.getName(), "US-ASCII"));
 250  0
                     sink.text(v.getName());
 251  0
                     sink.link_();
 252  0
                     sink.bold_();
 253  0
                 } catch (UnsupportedEncodingException ex) {
 254  0
                     sink.text(v.getName());
 255  0
                     sink.bold_();
 256  0
                     sink.lineBreak();
 257  0
                     sink.text("http://web.nvd.nist.gov/view/vuln/detail?vulnId=" + v.getName());
 258  0
                 }
 259  0
                 sink.paragraph_();
 260  0
                 sink.paragraph();
 261  0
                 sink.text("Severity: ");
 262  0
                 if (v.getCvssScore() < 4.0) {
 263  0
                     sink.text("Low");
 264  
                 } else {
 265  0
                     if (v.getCvssScore() >= 7.0) {
 266  0
                         sink.text("High");
 267  
                     } else {
 268  0
                         sink.text("Medium");
 269  
                     }
 270  
                 }
 271  0
                 sink.lineBreak();
 272  0
                 sink.text("CVSS Score: " + v.getCvssScore());
 273  0
                 if (v.getCwe() != null && !v.getCwe().isEmpty()) {
 274  0
                     sink.lineBreak();
 275  0
                     sink.text("CWE: ");
 276  0
                     sink.text(v.getCwe());
 277  
                 }
 278  0
                 sink.paragraph_();
 279  0
                 sink.paragraph();
 280  0
                 sink.text(v.getDescription());
 281  0
                 if (v.getReferences() != null && !v.getReferences().isEmpty()) {
 282  0
                     sink.list();
 283  0
                     for (Reference ref : v.getReferences()) {
 284  0
                         sink.listItem();
 285  0
                         sink.text(ref.getSource());
 286  0
                         sink.text(" - ");
 287  0
                         sink.link(ref.getUrl());
 288  0
                         sink.text(ref.getName());
 289  0
                         sink.link_();
 290  0
                         sink.listItem_();
 291  
                     }
 292  0
                     sink.list_();
 293  
                 }
 294  0
                 sink.paragraph_();
 295  0
                 if (v.getVulnerableSoftware() != null && !v.getVulnerableSoftware().isEmpty()) {
 296  0
                     sink.paragraph();
 297  
 
 298  0
                     cnt += 1;
 299  0
                     sink.rawText("Vulnerable Software <a href=\"javascript:toggleElement(this, 'vulnSoft" + cnt + "')\">[-]</a>");
 300  0
                     sink.rawText("<div id=\"vulnSoft" + cnt + "\" style=\"display:block\">");
 301  0
                     sink.list();
 302  0
                     for (VulnerableSoftware vs : v.getVulnerableSoftware()) {
 303  0
                         sink.listItem();
 304  
                         try {
 305  0
                             sink.link("http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + URLEncoder.encode(vs.getName(), "US-ASCII"));
 306  0
                             sink.text(vs.getName());
 307  0
                             sink.link_();
 308  0
                             if (vs.hasPreviousVersion()) {
 309  0
                                 sink.text(" and all previous versions.");
 310  
                             }
 311  0
                         } catch (UnsupportedEncodingException ex) {
 312  0
                             sink.text(vs.getName());
 313  0
                             if (vs.hasPreviousVersion()) {
 314  0
                                 sink.text(" and all previous versions.");
 315  
                             }
 316  0
                             sink.text(" (http://web.nvd.nist.gov/view/vuln/search-results?cpe=" + vs.getName() + ")");
 317  0
                         }
 318  
 
 319  0
                         sink.listItem_();
 320  
                     }
 321  0
                     sink.list_();
 322  0
                     sink.rawText("</div>");
 323  0
                     sink.paragraph_();
 324  
                 }
 325  
             }
 326  
         }
 327  0
     }
 328  
 
 329  
     /**
 330  
      * Writes the identifiers to the site report.
 331  
      *
 332  
      * @param d the dependency
 333  
      * @param sink the sink to write the data to
 334  
      */
 335  
     private void writeSiteReportDependencyIdentifiers(Dependency d, Sink sink) {
 336  0
         if (d.getIdentifiers() != null && !d.getIdentifiers().isEmpty()) {
 337  0
             sink.sectionTitle4();
 338  0
             sink.text("Identifiers");
 339  0
             sink.sectionTitle4_();
 340  0
             sink.list();
 341  0
             for (Identifier i : d.getIdentifiers()) {
 342  0
                 sink.listItem();
 343  0
                 sink.text(i.getType());
 344  0
                 sink.text(": ");
 345  0
                 if (i.getUrl() != null && i.getUrl().length() > 0) {
 346  0
                     sink.link(i.getUrl());
 347  0
                     sink.text(i.getValue());
 348  0
                     sink.link_();
 349  
                 } else {
 350  0
                     sink.text(i.getValue());
 351  
                 }
 352  0
                 if (i.getDescription() != null && i.getDescription().length() > 0) {
 353  0
                     sink.lineBreak();
 354  0
                     sink.text(i.getDescription());
 355  
                 }
 356  0
                 sink.listItem_();
 357  
             }
 358  0
             sink.list_();
 359  
         }
 360  0
     }
 361  
 
 362  
     /**
 363  
      * Writes the related dependencies to the site report.
 364  
      *
 365  
      * @param d the dependency
 366  
      * @param sink the sink to write the data to
 367  
      * @param collapsibleHeaderCount the collapsible header count
 368  
      * @return the collapsible header count
 369  
      */
 370  
     private int writeSiteReportDependencyRelatedDependencies(Dependency d, int collapsibleHeaderCount, Sink sink) {
 371  0
         int cnt = collapsibleHeaderCount;
 372  0
         if (d.getRelatedDependencies() != null && !d.getRelatedDependencies().isEmpty()) {
 373  0
             cnt += 1;
 374  0
             sink.sectionTitle4();
 375  0
             sink.rawText("Related Dependencies <a href=\"javascript:toggleElement(this, 'related" + cnt + "')\">[+]</a>");
 376  0
             sink.sectionTitle4_();
 377  0
             sink.rawText("<div id=\"related" + cnt + "\" style=\"display:none\">");
 378  0
             sink.list();
 379  0
             for (Dependency r : d.getRelatedDependencies()) {
 380  0
                 sink.listItem();
 381  0
                 sink.text(r.getFileName());
 382  0
                 sink.list();
 383  0
                 writeListItem(sink, "File Path: " + r.getFilePath());
 384  0
                 writeListItem(sink, "SHA1: " + r.getSha1sum());
 385  0
                 writeListItem(sink, "MD5: " + r.getMd5sum());
 386  0
                 sink.list_();
 387  0
                 sink.listItem_();
 388  
             }
 389  0
             sink.list_();
 390  0
             sink.rawText("</div>");
 391  
         }
 392  0
         return cnt;
 393  
     }
 394  
 
 395  
     /**
 396  
      * Writes the evidence used to the site report.
 397  
      *
 398  
      * @param d the dependency
 399  
      * @param sink the sink to write the data to
 400  
      * @param collapsibleHeaderCount the collapsible header count
 401  
      * @return the collapsible header count
 402  
      */
 403  
     private int writeSiteReportDependencyEvidenceUsed(Dependency d, int collapsibleHeaderCount, Sink sink) {
 404  0
         int cnt = collapsibleHeaderCount;
 405  0
         if (d.getEvidenceUsed() != null && d.getEvidenceUsed().size() > 0) {
 406  0
             cnt += 1;
 407  0
             sink.sectionTitle4();
 408  0
             sink.rawText("Evidence Collected <a href=\"javascript:toggleElement(this, 'evidence" + cnt + "')\">[+]</a>");
 409  0
             sink.sectionTitle4_();
 410  0
             sink.rawText("<div id=\"evidence" + cnt + "\" style=\"display:none\">");
 411  0
             sink.table();
 412  0
             sink.tableRow();
 413  0
             writeTableHeaderCell(sink, "Source");
 414  0
             writeTableHeaderCell(sink, "Name");
 415  0
             writeTableHeaderCell(sink, "Value");
 416  0
             sink.tableRow_();
 417  0
             for (Evidence e : d.getEvidenceUsed()) {
 418  0
                 sink.tableRow();
 419  0
                 writeTableCell(sink, e.getSource());
 420  0
                 writeTableCell(sink, e.getName());
 421  0
                 writeTableCell(sink, e.getValue());
 422  0
                 sink.tableRow_();
 423  
             }
 424  0
             sink.table_();
 425  0
             sink.rawText("</div>");
 426  
         }
 427  0
         return cnt;
 428  
     }
 429  
 
 430  
     /**
 431  
      * Writes the analysis exceptions generated during analysis to the site
 432  
      * report.
 433  
      *
 434  
      * @param d the dependency
 435  
      * @param sink the sink to write the data to
 436  
      * @param collapsibleHeaderCount the collapsible header count
 437  
      * @return the collapsible header count
 438  
      */
 439  
     private int writeSiteReportDependencyAnalysisExceptions(Dependency d, int collapsibleHeaderCount, Sink sink) {
 440  0
         int cnt = collapsibleHeaderCount;
 441  0
         if (d.getAnalysisExceptions() != null && !d.getAnalysisExceptions().isEmpty()) {
 442  0
             cnt += 1;
 443  0
             sink.sectionTitle4();
 444  0
             sink.rawText("<font style=\"color:red\">Errors occurred during analysis:</font> <a href=\"javascript:toggleElement(this, 'errors"
 445  
                     + cnt + "')\">[+]</a>");
 446  0
             sink.sectionTitle4_();
 447  0
             sink.rawText("<div id=\"errors" + cnt + "\">");
 448  0
             sink.list();
 449  0
             for (Exception e : d.getAnalysisExceptions()) {
 450  0
                 sink.listItem();
 451  0
                 sink.text(e.getMessage());
 452  0
                 sink.listItem_();
 453  
             }
 454  0
             sink.list_();
 455  0
             sink.rawText("</div>");
 456  
         }
 457  0
         return cnt;
 458  
     }
 459  
 
 460  
     /**
 461  
      * Writes the dependency header to the site report.
 462  
      *
 463  
      * @param d the dependency
 464  
      * @param sink the sink to write the data to
 465  
      */
 466  
     private void writeSiteReportDependencyHeader(Sink sink, Dependency d) {
 467  0
         sink.sectionTitle2();
 468  0
         sink.anchor("sha1" + d.getSha1sum());
 469  0
         sink.text(d.getFileName());
 470  0
         sink.anchor_();
 471  0
         sink.sectionTitle2_();
 472  0
         if (d.getDescription() != null && d.getDescription().length() > 0) {
 473  0
             sink.paragraph();
 474  0
             sink.bold();
 475  0
             sink.text("Description: ");
 476  0
             sink.bold_();
 477  0
             sink.text(d.getDescription());
 478  0
             sink.paragraph_();
 479  
         }
 480  0
         if (d.getLicense() != null && d.getLicense().length() > 0) {
 481  0
             sink.paragraph();
 482  0
             sink.bold();
 483  0
             sink.text("License: ");
 484  0
             sink.bold_();
 485  0
             if (d.getLicense().startsWith("http://") && !d.getLicense().contains(" ")) {
 486  0
                 sink.link(d.getLicense());
 487  0
                 sink.text(d.getLicense());
 488  0
                 sink.link_();
 489  
             } else {
 490  0
                 sink.text(d.getLicense());
 491  
             }
 492  0
             sink.paragraph_();
 493  
         }
 494  0
     }
 495  
 
 496  
     /**
 497  
      * Adds a list item to the site report.
 498  
      *
 499  
      * @param sink the sink to write the data to
 500  
      * @param text the text to write
 501  
      */
 502  
     private void writeListItem(Sink sink, String text) {
 503  0
         sink.listItem();
 504  0
         sink.text(text);
 505  0
         sink.listItem_();
 506  0
     }
 507  
 
 508  
     /**
 509  
      * Adds a table cell to the site report.
 510  
      *
 511  
      * @param sink the sink to write the data to
 512  
      * @param text the text to write
 513  
      */
 514  
     private void writeTableCell(Sink sink, String text) {
 515  0
         sink.tableCell();
 516  0
         sink.text(text);
 517  0
         sink.tableCell_();
 518  0
     }
 519  
 
 520  
     /**
 521  
      * Adds a table header cell to the site report.
 522  
      *
 523  
      * @param sink the sink to write the data to
 524  
      * @param text the text to write
 525  
      */
 526  
     private void writeTableHeaderCell(Sink sink, String text) {
 527  0
         sink.tableHeaderCell();
 528  0
         sink.text(text);
 529  0
         sink.tableHeaderCell_();
 530  0
     }
 531  
 
 532  
     /**
 533  
      * Writes the TOC for the site report.
 534  
      *
 535  
      * @param sink the sink to write the data to
 536  
      * @param dependencies the dependencies that are being reported on
 537  
      */
 538  
     private void writeSiteReportTOC(Sink sink, final List<Dependency> dependencies) {
 539  0
         sink.list();
 540  0
         for (Dependency d : dependencies) {
 541  0
             sink.listItem();
 542  0
             sink.link("#sha1" + d.getSha1sum());
 543  0
             sink.text(d.getFileName());
 544  0
             sink.link_();
 545  0
             if (!d.getVulnerabilities().isEmpty()) {
 546  0
                 sink.rawText(" <font style=\"color:red\">•</font>");
 547  
             }
 548  0
             if (!d.getRelatedDependencies().isEmpty()) {
 549  0
                 sink.list();
 550  0
                 for (Dependency r : d.getRelatedDependencies()) {
 551  0
                     writeListItem(sink, r.getFileName());
 552  
                 }
 553  0
                 sink.list_();
 554  
             }
 555  0
             sink.listItem_();
 556  
         }
 557  0
         sink.list_();
 558  0
     }
 559  
 
 560  
     /**
 561  
      * Writes the site report header.
 562  
      *
 563  
      * @param sink the sink to write the data to
 564  
      * @param projectName the name of the project
 565  
      */
 566  
     private void writeSiteReportHeader(Sink sink, String projectName) {
 567  0
         sink.head();
 568  0
         sink.title();
 569  0
         sink.text("Dependency-Check Report: " + projectName);
 570  0
         sink.title_();
 571  0
         sink.head_();
 572  0
         sink.body();
 573  0
         sink.rawText("<script type=\"text/javascript\">");
 574  0
         sink.rawText("function toggleElement(el, targetId) {");
 575  0
         sink.rawText("if (el.innerText == '[+]') {");
 576  0
         sink.rawText("    el.innerText = '[-]';");
 577  0
         sink.rawText("    document.getElementById(targetId).style.display='block';");
 578  0
         sink.rawText("} else {");
 579  0
         sink.rawText("    el.innerText = '[+]';");
 580  0
         sink.rawText("    document.getElementById(targetId).style.display='none';");
 581  0
         sink.rawText("}");
 582  
 
 583  0
         sink.rawText("}");
 584  0
         sink.rawText("</script>");
 585  0
         sink.section1();
 586  0
         sink.sectionTitle1();
 587  0
         sink.text("Project: " + projectName);
 588  0
         sink.sectionTitle1_();
 589  0
         sink.date();
 590  0
         final Date now = new Date();
 591  0
         sink.text(DateFormat.getDateTimeInstance().format(now));
 592  0
         sink.date_();
 593  0
         sink.section1_();
 594  0
     }
 595  
     // </editor-fold>
 596  
 
 597  
     /**
 598  
      * Takes the properties supplied and updates the dependency-check settings.
 599  
      * Additionally, this sets the system properties required to change the
 600  
      * proxy url, port, and connection timeout.
 601  
      */
 602  
     private void populateSettings() {
 603  0
         InputStream mojoProperties = null;
 604  
         try {
 605  0
             mojoProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
 606  0
             Settings.mergeProperties(mojoProperties);
 607  0
         } catch (IOException ex) {
 608  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.WARNING, "Unable to load the dependency-check ant task.properties file.");
 609  0
             Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINE, null, ex);
 610  
         } finally {
 611  0
             if (mojoProperties != null) {
 612  
                 try {
 613  0
                     mojoProperties.close();
 614  0
                 } catch (IOException ex) {
 615  0
                     Logger.getLogger(DependencyCheckMojo.class.getName()).log(Level.FINEST, null, ex);
 616  0
                 }
 617  
             }
 618  
         }
 619  
 
 620  0
         Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
 621  
 
 622  0
         if (proxyUrl != null && !proxyUrl.isEmpty()) {
 623  0
             Settings.setString(Settings.KEYS.PROXY_URL, proxyUrl);
 624  
         }
 625  0
         if (proxyPort != null && !proxyPort.isEmpty()) {
 626  0
             Settings.setString(Settings.KEYS.PROXY_PORT, proxyPort);
 627  
         }
 628  0
         if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
 629  0
             Settings.setString(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
 630  
         }
 631  0
     }
 632  
 
 633  
     /**
 634  
      * Executes the dependency-check and generates the report.
 635  
      *
 636  
      * @throws MojoExecutionException if a maven exception occurs
 637  
      * @throws MojoFailureException thrown if a CVSS score is found that is
 638  
      * higher then the configured level
 639  
      */
 640  
     public void execute() throws MojoExecutionException, MojoFailureException {
 641  0
         final Engine engine = executeDependencyCheck();
 642  0
         generateExternalReports(engine);
 643  0
         if (this.failBuildOnCVSS <= 10) {
 644  0
             checkForFailure(engine.getDependencies());
 645  
         }
 646  0
     }
 647  
 
 648  
     /**
 649  
      * Generates the Dependency-Check Site Report.
 650  
      *
 651  
      * @param sink the sink to write the report to
 652  
      * @param locale the locale to use when generating the report
 653  
      * @throws MavenReportException if a Maven report exception occurs
 654  
      */
 655  
     public void generate(@SuppressWarnings("deprecation") org.codehaus.doxia.sink.Sink sink,
 656  
             Locale locale) throws MavenReportException {
 657  0
         generate((Sink) sink, null, locale);
 658  0
     }
 659  
 
 660  
     /**
 661  
      * Generates the Dependency-Check Site Report.
 662  
      *
 663  
      * @param sink the sink to write the report to
 664  
      * @param sinkFactory the sink factory
 665  
      * @param locale the locale to use when generating the report
 666  
      * @throws MavenReportException if a maven report exception occurs
 667  
      */
 668  
     public void generate(Sink sink, SinkFactory sinkFactory, Locale locale) throws MavenReportException {
 669  0
         final Engine engine = executeDependencyCheck();
 670  0
         generateMavenSiteReport(engine, sink);
 671  0
     }
 672  
 
 673  
     // <editor-fold defaultstate="collapsed" desc="required setter/getter methods">
 674  
     /**
 675  
      * Returns the output name.
 676  
      *
 677  
      * @return the output name
 678  
      */
 679  
     public String getOutputName() {
 680  0
         return reportName;
 681  
     }
 682  
 
 683  
     /**
 684  
      * Returns the category name.
 685  
      *
 686  
      * @return the category name
 687  
      */
 688  
     public String getCategoryName() {
 689  0
         return MavenReport.CATEGORY_PROJECT_REPORTS;
 690  
     }
 691  
 
 692  
     /**
 693  
      * Returns the report name.
 694  
      *
 695  
      * @param locale the location
 696  
      * @return the report name
 697  
      */
 698  
     public String getName(Locale locale) {
 699  0
         return name;
 700  
     }
 701  
 
 702  
     /**
 703  
      * Sets the Reporting output directory.
 704  
      *
 705  
      * @param directory the output directory
 706  
      */
 707  
     public void setReportOutputDirectory(File directory) {
 708  0
         reportOutputDirectory = directory;
 709  0
     }
 710  
 
 711  
     /**
 712  
      * Returns the output directory.
 713  
      *
 714  
      * @return the output directory
 715  
      */
 716  
     public File getReportOutputDirectory() {
 717  0
         return reportOutputDirectory;
 718  
     }
 719  
 
 720  
     /**
 721  
      * Gets the description of the Dependency-Check report to be displayed in
 722  
      * the Maven Generated Reports page.
 723  
      *
 724  
      * @param locale The Locale to get the description for
 725  
      * @return the description
 726  
      */
 727  
     public String getDescription(Locale locale) {
 728  0
         return description;
 729  
     }
 730  
 
 731  
     /**
 732  
      * Returns whether this is an external report.
 733  
      *
 734  
      * @return true or false;
 735  
      */
 736  
     public boolean isExternalReport() {
 737  0
         return externalReport;
 738  
     }
 739  
 
 740  
     /**
 741  
      * Returns whether or not the plugin can generate a report.
 742  
      *
 743  
      * @return true
 744  
      */
 745  
     public boolean canGenerateReport() {
 746  0
         return true;
 747  
     }
 748  
     // </editor-fold>
 749  
 
 750  
     /**
 751  
      * Checks to see if a vulnerability has been identified with a CVSS score
 752  
      * that is above the threshold set in the configuration.
 753  
      *
 754  
      * @param dependencies the list of dependency objects
 755  
      * @throws MojoFailureException thrown if a CVSS score is found that is
 756  
      * higher then the threshold set
 757  
      */
 758  
     private void checkForFailure(List<Dependency> dependencies) throws MojoFailureException {
 759  0
         final StringBuilder ids = new StringBuilder();
 760  0
         for (Dependency d : dependencies) {
 761  0
             for (Vulnerability v : d.getVulnerabilities()) {
 762  0
                 if (v.getCvssScore() >= failBuildOnCVSS) {
 763  0
                     if (ids.length() == 0) {
 764  0
                         ids.append(v.getName());
 765  
                     } else {
 766  0
                         ids.append(", ").append(v.getName());
 767  
                     }
 768  
                 }
 769  
             }
 770  
         }
 771  0
         if (ids.length() > 0) {
 772  0
             final String msg = String.format("%n%nDependency-Check Failure:%n"
 773  
                     + "One or more dependencies were identified with vulnerabilities that have a CVSS score greater then '%.1f': %s%n"
 774  
                     + "See the dependency-check report for more details.%n%n", failBuildOnCVSS, ids.toString());
 775  0
             throw new MojoFailureException(msg);
 776  
         }
 777  0
     }
 778  
 }