Coverage Report - org.owasp.dependencycheck.analyzer.JarAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
JarAnalyzer
60%
349/578
50%
177/350
7.486
JarAnalyzer$ClassNameInformation
94%
16/17
90%
9/10
7.486
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import java.io.BufferedOutputStream;
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.io.InputStreamReader;
 28  
 import java.io.OutputStream;
 29  
 import java.io.Reader;
 30  
 import java.io.UnsupportedEncodingException;
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collections;
 33  
 import java.util.Enumeration;
 34  
 import java.util.HashMap;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 import java.util.Map.Entry;
 38  
 import java.util.Properties;
 39  
 import java.util.Set;
 40  
 import java.util.StringTokenizer;
 41  
 import java.util.jar.Attributes;
 42  
 import java.util.jar.JarEntry;
 43  
 import java.util.jar.JarFile;
 44  
 import java.util.jar.Manifest;
 45  
 import java.util.logging.Level;
 46  
 import java.util.logging.Logger;
 47  
 import java.util.regex.Pattern;
 48  
 import java.util.zip.ZipEntry;
 49  
 import javax.xml.bind.JAXBContext;
 50  
 import javax.xml.bind.JAXBElement;
 51  
 import javax.xml.bind.JAXBException;
 52  
 import javax.xml.bind.Unmarshaller;
 53  
 import javax.xml.parsers.ParserConfigurationException;
 54  
 import javax.xml.parsers.SAXParser;
 55  
 import javax.xml.parsers.SAXParserFactory;
 56  
 import javax.xml.transform.sax.SAXSource;
 57  
 import org.jsoup.Jsoup;
 58  
 import org.owasp.dependencycheck.Engine;
 59  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 60  
 import org.owasp.dependencycheck.dependency.Confidence;
 61  
 import org.owasp.dependencycheck.dependency.Dependency;
 62  
 import org.owasp.dependencycheck.dependency.EvidenceCollection;
 63  
 import org.owasp.dependencycheck.jaxb.pom.MavenNamespaceFilter;
 64  
 import org.owasp.dependencycheck.jaxb.pom.generated.License;
 65  
 import org.owasp.dependencycheck.jaxb.pom.generated.Model;
 66  
 import org.owasp.dependencycheck.jaxb.pom.generated.Organization;
 67  
 import org.owasp.dependencycheck.jaxb.pom.generated.Parent;
 68  
 import org.owasp.dependencycheck.utils.FileUtils;
 69  
 import org.owasp.dependencycheck.utils.NonClosingStream;
 70  
 import org.owasp.dependencycheck.utils.Settings;
 71  
 import org.xml.sax.InputSource;
 72  
 import org.xml.sax.SAXException;
 73  
 import org.xml.sax.XMLFilter;
 74  
 import org.xml.sax.XMLReader;
 75  
 
 76  
 /**
 77  
  *
 78  
  * Used to load a JAR file and collect information that can be used to determine the associated CPE.
 79  
  *
 80  
  * @author Jeremy Long <jeremy.long@owasp.org>
 81  
  */
 82  
 public class JarAnalyzer extends AbstractAnalyzer implements Analyzer {
 83  
 
 84  
     //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables">
 85  
     /**
 86  
      * The buffer size to use when extracting files from the archive.
 87  
      */
 88  
     private static final int BUFFER_SIZE = 4096;
 89  
     /**
 90  
      * The count of directories created during analysis. This is used for creating temporary directories.
 91  
      */
 92  1
     private static int dirCount = 0;
 93  
     /**
 94  
      * The system independent newline character.
 95  
      */
 96  1
     private static final String NEWLINE = System.getProperty("line.separator");
 97  
     /**
 98  
      * A list of values in the manifest to ignore as they only result in false positives.
 99  
      */
 100  1
     private static final Set<String> IGNORE_VALUES = newHashSet(
 101  
             "Sun Java System Application Server");
 102  
     /**
 103  
      * A list of elements in the manifest to ignore.
 104  
      */
 105  1
     private static final Set<String> IGNORE_KEYS = newHashSet(
 106  
             "built-by",
 107  
             "created-by",
 108  
             "builtby",
 109  
             "createdby",
 110  
             "build-jdk",
 111  
             "buildjdk",
 112  
             "ant-version",
 113  
             "antversion",
 114  
             "import-package",
 115  
             "export-package",
 116  
             "importpackage",
 117  
             "exportpackage",
 118  
             "sealed",
 119  
             "manifest-version",
 120  
             "archiver-version",
 121  
             "manifestversion",
 122  
             "archiverversion",
 123  
             "classpath",
 124  
             "class-path",
 125  
             "tool",
 126  
             "bundle-manifestversion",
 127  
             "bundlemanifestversion",
 128  
             "include-resource");
 129  
     /**
 130  
      * item in some manifest, should be considered medium confidence.
 131  
      */
 132  
     private static final String BUNDLE_VERSION = "Bundle-Version"; //: 2.1.2
 133  
     /**
 134  
      * item in some manifest, should be considered medium confidence.
 135  
      */
 136  
     private static final String BUNDLE_DESCRIPTION = "Bundle-Description"; //: Apache Struts 2
 137  
     /**
 138  
      * item in some manifest, should be considered medium confidence.
 139  
      */
 140  
     private static final String BUNDLE_NAME = "Bundle-Name"; //: Struts 2 Core
 141  
     /**
 142  
      * item in some manifest, should be considered medium confidence.
 143  
      */
 144  
     private static final String BUNDLE_VENDOR = "Bundle-Vendor"; //: Apache Software Foundation
 145  
     /**
 146  
      * A pattern to detect HTML within text.
 147  
      */
 148  1
     private static final Pattern HTML_DETECTION_PATTERN = Pattern.compile("\\<[a-z]+.*/?\\>", Pattern.CASE_INSENSITIVE);
 149  
     /**
 150  
      * The unmarshaller used to parse the pom.xml from a JAR file.
 151  
      */
 152  
     private Unmarshaller pomUnmarshaller;
 153  
     //</editor-fold>
 154  
 
 155  
     /**
 156  
      * Constructs a new JarAnalyzer.
 157  
      */
 158  13
     public JarAnalyzer() {
 159  
         try {
 160  13
             final JAXBContext jaxbContext = JAXBContext.newInstance("org.owasp.dependencycheck.jaxb.pom.generated");
 161  13
             pomUnmarshaller = jaxbContext.createUnmarshaller();
 162  0
         } catch (JAXBException ex) { //guess we will just have a null pointer exception later...
 163  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.SEVERE, "Unable to load parser. See the log for more details.");
 164  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 165  13
         }
 166  13
     }
 167  
     //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
 168  
     /**
 169  
      * The name of the analyzer.
 170  
      */
 171  
     private static final String ANALYZER_NAME = "Jar Analyzer";
 172  
     /**
 173  
      * The phase that this analyzer is intended to run in.
 174  
      */
 175  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 176  
     /**
 177  
      * The set of file extensions supported by this analyzer.
 178  
      */
 179  1
     private static final Set<String> EXTENSIONS = newHashSet("jar", "war");
 180  
 
 181  
     /**
 182  
      * Returns a list of file EXTENSIONS supported by this analyzer.
 183  
      *
 184  
      * @return a list of file EXTENSIONS supported by this analyzer.
 185  
      */
 186  
     public Set<String> getSupportedExtensions() {
 187  155
         return EXTENSIONS;
 188  
     }
 189  
 
 190  
     /**
 191  
      * Returns the name of the analyzer.
 192  
      *
 193  
      * @return the name of the analyzer.
 194  
      */
 195  
     public String getName() {
 196  10
         return ANALYZER_NAME;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Returns whether or not this analyzer can process the given extension.
 201  
      *
 202  
      * @param extension the file extension to test for support.
 203  
      * @return whether or not the specified file extension is supported by this analyzer.
 204  
      */
 205  
     public boolean supportsExtension(String extension) {
 206  151
         return EXTENSIONS.contains(extension);
 207  
     }
 208  
 
 209  
     /**
 210  
      * Returns the phase that the analyzer is intended to run in.
 211  
      *
 212  
      * @return the phase that the analyzer is intended to run in.
 213  
      */
 214  
     public AnalysisPhase getAnalysisPhase() {
 215  6
         return ANALYSIS_PHASE;
 216  
     }
 217  
     //</editor-fold>
 218  
 
 219  
     /**
 220  
      * Loads a specified JAR file and collects information from the manifest and checksums to identify the correct CPE
 221  
      * information.
 222  
      *
 223  
      * @param dependency the dependency to analyze.
 224  
      * @param engine the engine that is scanning the dependencies
 225  
      * @throws AnalysisException is thrown if there is an error reading the JAR file.
 226  
      */
 227  
     @Override
 228  
     public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 229  
         try {
 230  18
             final ArrayList<ClassNameInformation> classNames = collectClassNames(dependency);
 231  18
             final String fileName = dependency.getFileName().toLowerCase();
 232  18
             if (classNames.isEmpty()
 233  
                     && (fileName.endsWith("-sources.jar")
 234  
                     || fileName.endsWith("-javadoc.jar")
 235  
                     || fileName.endsWith("-src.jar")
 236  
                     || fileName.endsWith("-doc.jar"))) {
 237  0
                 engine.getDependencies().remove(dependency);
 238  
             }
 239  18
             final boolean hasManifest = parseManifest(dependency, classNames);
 240  18
             final boolean hasPOM = analyzePOM(dependency, classNames, engine);
 241  18
             final boolean addPackagesAsEvidence = !(hasManifest && hasPOM);
 242  18
             analyzePackageNames(classNames, dependency, addPackagesAsEvidence);
 243  0
         } catch (IOException ex) {
 244  0
             throw new AnalysisException("Exception occurred reading the JAR file.", ex);
 245  18
         }
 246  18
     }
 247  
 
 248  
     /**
 249  
      * Attempts to find a pom.xml within the JAR file. If found it extracts information and adds it to the evidence.
 250  
      * This will attempt to interpolate the strings contained within the pom.properties if one exists.
 251  
      *
 252  
      * @param dependency the dependency being analyzed
 253  
      * @param classes a collection of class name information
 254  
      * @param engine the analysis engine, used to add additional dependencies
 255  
      * @throws AnalysisException is thrown if there is an exception parsing the pom
 256  
      * @return whether or not evidence was added to the dependency
 257  
      */
 258  
     protected boolean analyzePOM(Dependency dependency, ArrayList<ClassNameInformation> classes, Engine engine) throws AnalysisException {
 259  18
         boolean foundSomething = false;
 260  
         final JarFile jar;
 261  
         try {
 262  18
             jar = new JarFile(dependency.getActualFilePath());
 263  0
         } catch (IOException ex) {
 264  0
             final String msg = String.format("Unable to read JarFile '%s'.", dependency.getActualFilePath());
 265  
             //final AnalysisException ax = new AnalysisException(msg, ex);
 266  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 267  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 268  0
             return false;
 269  18
         }
 270  
         List<String> pomEntries;
 271  
         try {
 272  18
             pomEntries = retrievePomListing(jar);
 273  0
         } catch (IOException ex) {
 274  0
             final String msg = String.format("Unable to read Jar file entries in '%s'.", dependency.getActualFilePath());
 275  
             //final AnalysisException ax = new AnalysisException(msg, ex);
 276  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 277  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, msg, ex);
 278  0
             return false;
 279  18
         }
 280  18
         if (pomEntries.isEmpty()) {
 281  11
             return false;
 282  
         }
 283  7
         for (String path : pomEntries) {
 284  7
             Properties pomProperties = null;
 285  
             try {
 286  7
                 pomProperties = retrievePomProperties(path, jar);
 287  0
             } catch (IOException ex) {
 288  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINEST, "ignore this, failed reading a non-existent pom.properties", ex);
 289  7
             }
 290  7
             Model pom = null;
 291  
             try {
 292  7
                 if (pomEntries.size() > 1) {
 293  
                     //extract POM to its own directory and add it as its own dependency
 294  0
                     final Dependency newDependency = new Dependency();
 295  0
                     pom = extractPom(path, jar, newDependency);
 296  
 
 297  0
                     final String displayPath = String.format("%s%s%s",
 298  
                             dependency.getFilePath(),
 299  
                             File.separator,
 300  
                             path); //.replaceAll("[\\/]", File.separator));
 301  0
                     final String displayName = String.format("%s%s%s",
 302  
                             dependency.getFileName(),
 303  
                             File.separator,
 304  
                             path); //.replaceAll("[\\/]", File.separator));
 305  
 
 306  0
                     newDependency.setFileName(displayName);
 307  0
                     newDependency.setFilePath(displayPath);
 308  0
                     addPomEvidence(newDependency, pom, pomProperties);
 309  0
                     engine.getDependencies().add(newDependency);
 310  0
                     Collections.sort(engine.getDependencies());
 311  0
                 } else {
 312  7
                     pom = retrievePom(path, jar);
 313  7
                     foundSomething |= setPomEvidence(dependency, pom, pomProperties, classes);
 314  
                 }
 315  0
             } catch (AnalysisException ex) {
 316  0
                 final String msg = String.format("An error occured while analyzing '%s'.", dependency.getActualFilePath());
 317  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 318  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, "", ex);
 319  7
             }
 320  7
         }
 321  7
         return foundSomething;
 322  
     }
 323  
 
 324  
     /**
 325  
      * Given a path to a pom.xml within a JarFile, this method attempts to load a sibling pom.properties if one exists.
 326  
      *
 327  
      * @param path the path to the pom.xml within the JarFile
 328  
      * @param jar the JarFile to load the pom.properties from
 329  
      * @return a Properties object or null if no pom.properties was found
 330  
      * @throws IOException thrown if there is an exception reading the pom.properties
 331  
      */
 332  
     @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "OS_OPEN_STREAM",
 333  
             justification = "The reader is closed by closing the zipEntry")
 334  
     private Properties retrievePomProperties(String path, final JarFile jar) throws IOException {
 335  7
         Properties pomProperties = null;
 336  7
         final String propPath = path.substring(0, path.length() - 7) + "pom.properies";
 337  7
         final ZipEntry propEntry = jar.getEntry(propPath);
 338  7
         if (propEntry != null) {
 339  0
             final Reader reader = new InputStreamReader(jar.getInputStream(propEntry), "UTF-8");
 340  0
             pomProperties = new Properties();
 341  0
             pomProperties.load(reader);
 342  
         }
 343  7
         return pomProperties;
 344  
     }
 345  
 
 346  
     /**
 347  
      * Searches a JarFile for pom.xml entries and returns a listing of these entries.
 348  
      *
 349  
      * @param jar the JarFile to search
 350  
      * @return a list of pom.xml entries
 351  
      * @throws IOException thrown if there is an exception reading a JarEntryf
 352  
      */
 353  
     private List<String> retrievePomListing(final JarFile jar) throws IOException {
 354  18
         final List<String> pomEntries = new ArrayList<String>();
 355  18
         final Enumeration<JarEntry> entries = jar.entries();
 356  7564
         while (entries.hasMoreElements()) {
 357  7546
             final JarEntry entry = entries.nextElement();
 358  7546
             final String entryName = (new File(entry.getName())).getName().toLowerCase();
 359  7546
             if (!entry.isDirectory() && "pom.xml".equals(entryName)) {
 360  7
                 pomEntries.add(entry.getName());
 361  
             }
 362  7546
         }
 363  18
         return pomEntries;
 364  
     }
 365  
 
 366  
     /**
 367  
      * Retrieves the specified POM from a jar file and converts it to a Model.
 368  
      *
 369  
      * @param path the path to the pom.xml file within the jar file
 370  
      * @param jar the jar file to extract the pom from
 371  
      * @param dependency the dependency being analyzed
 372  
      * @return returns the POM object
 373  
      * @throws AnalysisException is thrown if there is an exception extracting or parsing the POM
 374  
      * {@link org.owasp.dependencycheck.jaxb.pom.generated.Model} object
 375  
      */
 376  
     private Model extractPom(String path, JarFile jar, Dependency dependency) throws AnalysisException {
 377  0
         InputStream input = null;
 378  0
         FileOutputStream fos = null;
 379  0
         BufferedOutputStream bos = null;
 380  0
         final File tmpDir = getNextTempDirectory();
 381  0
         final File file = new File(tmpDir, "pom.xml");
 382  
         try {
 383  0
             final ZipEntry entry = jar.getEntry(path);
 384  0
             input = jar.getInputStream(entry);
 385  0
             fos = new FileOutputStream(file);
 386  0
             bos = new BufferedOutputStream(fos, BUFFER_SIZE);
 387  
             int count;
 388  0
             final byte data[] = new byte[BUFFER_SIZE];
 389  0
             while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
 390  0
                 bos.write(data, 0, count);
 391  
             }
 392  0
             bos.flush();
 393  0
             dependency.setActualFilePath(file.getAbsolutePath());
 394  0
         } catch (IOException ex) {
 395  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.SEVERE, null, ex);
 396  
         } finally {
 397  0
             closeStream(bos);
 398  0
             closeStream(fos);
 399  0
             closeStream(input);
 400  0
         }
 401  0
         Model model = null;
 402  0
         FileInputStream fis = null;
 403  
         try {
 404  0
             fis = new FileInputStream(file);
 405  0
             final InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
 406  0
             final InputSource xml = new InputSource(reader);
 407  0
             final SAXSource source = new SAXSource(xml);
 408  0
             model = readPom(source);
 409  0
         } catch (FileNotFoundException ex) {
 410  0
             final String msg = String.format("Unable to parse pom '%s' in jar '%s' (File Not Found)", path, jar.getName());
 411  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 412  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 413  0
             throw new AnalysisException(ex);
 414  0
         } catch (UnsupportedEncodingException ex) {
 415  0
             final String msg = String.format("Unable to parse pom '%s' in jar '%s' (IO Exception)", path, jar.getName());
 416  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 417  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 418  0
             throw new AnalysisException(ex);
 419  0
         } catch (AnalysisException ex) {
 420  0
             final String msg = String.format("Unable to parse pom '%s' in jar '%s'", path, jar.getName());
 421  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 422  0
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 423  0
             throw ex;
 424  
         } finally {
 425  0
             closeStream(fis);
 426  0
         }
 427  0
         return model;
 428  
     }
 429  
 
 430  
     /**
 431  
      * Silently closes an input stream ignoring errors.
 432  
      *
 433  
      * @param stream an input stream to close
 434  
      */
 435  
     private void closeStream(InputStream stream) {
 436  0
         if (stream != null) {
 437  
             try {
 438  0
                 stream.close();
 439  0
             } catch (IOException ex) {
 440  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINEST, null, ex);
 441  0
             }
 442  
         }
 443  0
     }
 444  
 
 445  
     /**
 446  
      * Silently closes an output stream ignoring errors.
 447  
      *
 448  
      * @param stream an output stream to close
 449  
      */
 450  
     private void closeStream(OutputStream stream) {
 451  0
         if (stream != null) {
 452  
             try {
 453  0
                 stream.close();
 454  0
             } catch (IOException ex) {
 455  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINEST, null, ex);
 456  0
             }
 457  
         }
 458  0
     }
 459  
 
 460  
     /**
 461  
      * Retrieves the specified POM from a jar file and converts it to a Model.
 462  
      *
 463  
      * @param path the path to the pom.xml file within the jar file
 464  
      * @param jar the jar file to extract the pom from
 465  
      * @return returns a
 466  
      * @throws AnalysisException is thrown if there is an exception extracting or parsing the POM
 467  
      * {@link org.owasp.dependencycheck.jaxb.pom.generated.Model} object
 468  
      */
 469  
     private Model retrievePom(String path, JarFile jar) throws AnalysisException {
 470  7
         final ZipEntry entry = jar.getEntry(path);
 471  7
         Model model = null;
 472  7
         if (entry != null) { //should never be null
 473  
             try {
 474  7
                 final NonClosingStream stream = new NonClosingStream(jar.getInputStream(entry));
 475  7
                 final InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
 476  7
                 final InputSource xml = new InputSource(reader);
 477  7
                 final SAXSource source = new SAXSource(xml);
 478  7
                 model = readPom(source);
 479  0
             } catch (SecurityException ex) {
 480  0
                 final String msg = String.format("Unable to parse pom '%s' in jar '%s'; invalid signature", path, jar.getName());
 481  0
                 Logger
 482  
                         .getLogger(JarAnalyzer.class
 483  
                                 .getName()).log(Level.WARNING, msg);
 484  0
                 Logger.getLogger(JarAnalyzer.class
 485  
                         .getName()).log(Level.FINE, null, ex);
 486  0
                 throw new AnalysisException(ex);
 487  0
             } catch (IOException ex) {
 488  0
                 final String msg = String.format("Unable to parse pom '%s' in jar '%s' (IO Exception)", path, jar.getName());
 489  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 490  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 491  0
                 throw new AnalysisException(ex);
 492  0
             } catch (Throwable ex) {
 493  0
                 final String msg = String.format("Unexpected error during parsing of the pom '%s' in jar '%s'", path, jar.getName());
 494  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING, msg);
 495  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, null, ex);
 496  0
                 throw new AnalysisException(ex);
 497  7
             }
 498  
         }
 499  7
         return model;
 500  
     }
 501  
 
 502  
     /**
 503  
      * Retrieves the specified POM from a jar file and converts it to a Model.
 504  
      *
 505  
      * @param source the SAXSource input stream to read the POM from
 506  
      * @return returns the POM object
 507  
      * @throws AnalysisException is thrown if there is an exception extracting or parsing the POM
 508  
      * {@link org.owasp.dependencycheck.jaxb.pom.generated.Model} object
 509  
      */
 510  
     private Model readPom(SAXSource source) throws AnalysisException {
 511  7
         Model model = null;
 512  
         try {
 513  7
             final XMLFilter filter = new MavenNamespaceFilter();
 514  7
             final SAXParserFactory spf = SAXParserFactory.newInstance();
 515  7
             final SAXParser sp = spf.newSAXParser();
 516  7
             final XMLReader xr = sp.getXMLReader();
 517  7
             filter.setParent(xr);
 518  7
             final JAXBElement<Model> el = pomUnmarshaller.unmarshal(source, Model.class);
 519  7
             model = el.getValue();
 520  0
         } catch (SecurityException ex) {
 521  0
             throw new AnalysisException(ex);
 522  0
         } catch (ParserConfigurationException ex) {
 523  0
             throw new AnalysisException(ex);
 524  0
         } catch (SAXException ex) {
 525  0
             throw new AnalysisException(ex);
 526  0
         } catch (JAXBException ex) {
 527  0
             throw new AnalysisException(ex);
 528  0
         } catch (Throwable ex) {
 529  0
             throw new AnalysisException(ex);
 530  7
         }
 531  7
         return model;
 532  
     }
 533  
 
 534  
     /**
 535  
      * Sets evidence from the pom on the supplied dependency.
 536  
      *
 537  
      * @param dependency the dependency to set data on
 538  
      * @param pom the information from the pom
 539  
      * @param pomProperties the pom properties file (null if none exists)
 540  
      * @param classes a collection of ClassNameInformation - containing data about the fully qualified class names
 541  
      * within the JAR file being analyzed
 542  
      * @return true if there was evidence within the pom that we could use; otherwise false
 543  
      */
 544  
     private boolean setPomEvidence(Dependency dependency, Model pom, Properties pomProperties, ArrayList<ClassNameInformation> classes) {
 545  7
         boolean foundSomething = false;
 546  7
         if (pom == null) {
 547  0
             return foundSomething;
 548  
         }
 549  7
         String groupid = interpolateString(pom.getGroupId(), pomProperties);
 550  7
         if (groupid != null && !groupid.isEmpty()) {
 551  4
             if (groupid.startsWith("org.") || groupid.startsWith("com.")) {
 552  3
                 groupid = groupid.substring(4);
 553  
             }
 554  4
             foundSomething = true;
 555  4
             dependency.getVendorEvidence().addEvidence("pom", "groupid", groupid, Confidence.HIGH);
 556  4
             dependency.getProductEvidence().addEvidence("pom", "groupid", groupid, Confidence.LOW);
 557  4
             addMatchingValues(classes, groupid, dependency.getVendorEvidence());
 558  4
             addMatchingValues(classes, groupid, dependency.getProductEvidence());
 559  
         }
 560  7
         String artifactid = interpolateString(pom.getArtifactId(), pomProperties);
 561  7
         if (artifactid != null && !artifactid.isEmpty()) {
 562  7
             if (artifactid.startsWith("org.") || artifactid.startsWith("com.")) {
 563  0
                 artifactid = artifactid.substring(4);
 564  
             }
 565  7
             foundSomething = true;
 566  7
             dependency.getProductEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.HIGH);
 567  7
             dependency.getVendorEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.LOW);
 568  7
             addMatchingValues(classes, artifactid, dependency.getVendorEvidence());
 569  7
             addMatchingValues(classes, artifactid, dependency.getProductEvidence());
 570  
         }
 571  
         //version
 572  7
         final String version = interpolateString(pom.getVersion(), pomProperties);
 573  7
         if (version != null && !version.isEmpty()) {
 574  2
             foundSomething = true;
 575  2
             dependency.getVersionEvidence().addEvidence("pom", "version", version, Confidence.HIGHEST);
 576  
         }
 577  
         // org name
 578  7
         final Organization org = pom.getOrganization();
 579  7
         if (org != null && org.getName() != null) {
 580  0
             foundSomething = true;
 581  0
             final String orgName = interpolateString(org.getName(), pomProperties);
 582  0
             if (orgName != null && !orgName.isEmpty()) {
 583  0
                 dependency.getVendorEvidence().addEvidence("pom", "organization name", orgName, Confidence.HIGH);
 584  0
                 addMatchingValues(classes, orgName, dependency.getVendorEvidence());
 585  
             }
 586  
         }
 587  
         //pom name
 588  7
         final String pomName = interpolateString(pom.getName(), pomProperties);
 589  7
         if (pomName != null && !pomName.isEmpty()) {
 590  7
             foundSomething = true;
 591  7
             dependency.getProductEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
 592  7
             dependency.getVendorEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
 593  7
             addMatchingValues(classes, pomName, dependency.getVendorEvidence());
 594  7
             addMatchingValues(classes, pomName, dependency.getProductEvidence());
 595  
         }
 596  
 
 597  
         //Description
 598  7
         if (pom.getDescription() != null) {
 599  3
             foundSomething = true;
 600  3
             final String description = interpolateString(pom.getDescription(), pomProperties);
 601  3
             if (description != null && !description.isEmpty()) {
 602  3
                 final String trimmedDescription = addDescription(dependency, description, "pom", "description");
 603  3
                 addMatchingValues(classes, trimmedDescription, dependency.getVendorEvidence());
 604  3
                 addMatchingValues(classes, trimmedDescription, dependency.getProductEvidence());
 605  
             }
 606  
         }
 607  7
         extractLicense(pom, pomProperties, dependency);
 608  7
         return foundSomething;
 609  
     }
 610  
 
 611  
     /**
 612  
      * Analyzes the path information of the classes contained within the JarAnalyzer to try and determine possible
 613  
      * vendor or product names. If any are found they are stored in the packageVendor and packageProduct hashSets.
 614  
      *
 615  
      * @param classNames a list of class names
 616  
      * @param dependency a dependency to analyze
 617  
      * @param addPackagesAsEvidence a flag indicating whether or not package names should be added as evidence.
 618  
      */
 619  
     protected void analyzePackageNames(ArrayList<ClassNameInformation> classNames,
 620  
             Dependency dependency, boolean addPackagesAsEvidence) {
 621  18
         final HashMap<String, Integer> vendorIdentifiers = new HashMap<String, Integer>();
 622  18
         final HashMap<String, Integer> productIdentifiers = new HashMap<String, Integer>();
 623  18
         analyzeFullyQualifiedClassNames(classNames, vendorIdentifiers, productIdentifiers);
 624  
 
 625  18
         final int classCount = classNames.size();
 626  18
         final EvidenceCollection vendor = dependency.getVendorEvidence();
 627  18
         final EvidenceCollection product = dependency.getProductEvidence();
 628  
 
 629  18
         for (Map.Entry<String, Integer> entry : vendorIdentifiers.entrySet()) {
 630  182
             final float ratio = entry.getValue() / (float) classCount;
 631  182
             if (ratio > 0.5) {
 632  
                 //TODO remove weighting
 633  36
                 vendor.addWeighting(entry.getKey());
 634  36
                 if (addPackagesAsEvidence && entry.getKey().length() > 1) {
 635  22
                     vendor.addEvidence("jar", "package", entry.getKey(), Confidence.LOW);
 636  
                 }
 637  
             }
 638  182
         }
 639  18
         for (Map.Entry<String, Integer> entry : productIdentifiers.entrySet()) {
 640  3772
             final float ratio = entry.getValue() / (float) classCount;
 641  3772
             if (ratio > 0.5) {
 642  21
                 product.addWeighting(entry.getKey());
 643  21
                 if (addPackagesAsEvidence && entry.getKey().length() > 1) {
 644  11
                     product.addEvidence("jar", "package", entry.getKey(), Confidence.LOW);
 645  
                 }
 646  
             }
 647  3772
         }
 648  18
     }
 649  
 
 650  
     /**
 651  
      * <p>
 652  
      * Reads the manifest from the JAR file and collects the entries. Some vendorKey entries are:</p>
 653  
      * <ul><li>Implementation Title</li>
 654  
      * <li>Implementation Version</li> <li>Implementation Vendor</li>
 655  
      * <li>Implementation VendorId</li> <li>Bundle Name</li> <li>Bundle Version</li> <li>Bundle Vendor</li> <li>Bundle
 656  
      * Description</li> <li>Main Class</li> </ul>
 657  
      * However, all but a handful of specific entries are read in.
 658  
      *
 659  
      * @param dependency A reference to the dependency
 660  
      * @param classInformation a collection of class information
 661  
      * @return whether evidence was identified parsing the manifest
 662  
      * @throws IOException if there is an issue reading the JAR file
 663  
      */
 664  
     protected boolean parseManifest(Dependency dependency, ArrayList<ClassNameInformation> classInformation) throws IOException {
 665  18
         boolean foundSomething = false;
 666  18
         JarFile jar = null;
 667  
         try {
 668  18
             jar = new JarFile(dependency.getActualFilePath());
 669  
 
 670  18
             final Manifest manifest = jar.getManifest();
 671  
 
 672  18
             if (manifest == null) {
 673  
                 //don't log this for javadoc or sources jar files
 674  0
                 if (!dependency.getFileName().toLowerCase().endsWith("-sources.jar")
 675  
                         && !dependency.getFileName().toLowerCase().endsWith("-javadoc.jar")
 676  
                         && !dependency.getFileName().toLowerCase().endsWith("-src.jar")
 677  
                         && !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) {
 678  0
                     Logger.getLogger(JarAnalyzer.class
 679  
                             .getName()).log(Level.INFO,
 680  
                                     String.format("Jar file '%s' does not contain a manifest.",
 681  
                                             dependency.getFileName()));
 682  
                 }
 683  0
                 return false;
 684  
             }
 685  18
             final Attributes atts = manifest.getMainAttributes();
 686  
 
 687  18
             final EvidenceCollection vendorEvidence = dependency.getVendorEvidence();
 688  18
             final EvidenceCollection productEvidence = dependency.getProductEvidence();
 689  18
             final EvidenceCollection versionEvidence = dependency.getVersionEvidence();
 690  
 
 691  18
             final String source = "Manifest";
 692  
 
 693  18
             for (Entry<Object, Object> entry : atts.entrySet()) {
 694  260
                 String key = entry.getKey().toString();
 695  260
                 String value = atts.getValue(key);
 696  260
                 if (HTML_DETECTION_PATTERN.matcher(value).find()) {
 697  0
                     value = Jsoup.parse(value).text();
 698  
                 }
 699  260
                 if (IGNORE_VALUES.contains(value)) {
 700  0
                     continue;
 701  260
                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) {
 702  9
                     foundSomething = true;
 703  9
                     productEvidence.addEvidence(source, key, value, Confidence.HIGH);
 704  9
                     addMatchingValues(classInformation, value, productEvidence);
 705  251
                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) {
 706  12
                     foundSomething = true;
 707  12
                     versionEvidence.addEvidence(source, key, value, Confidence.HIGH);
 708  239
                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) {
 709  8
                     foundSomething = true;
 710  8
                     vendorEvidence.addEvidence(source, key, value, Confidence.HIGH);
 711  8
                     addMatchingValues(classInformation, value, vendorEvidence);
 712  231
                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR_ID.toString())) {
 713  5
                     foundSomething = true;
 714  5
                     vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 715  5
                     addMatchingValues(classInformation, value, vendorEvidence);
 716  226
                 } else if (key.equalsIgnoreCase(BUNDLE_DESCRIPTION)) {
 717  7
                     foundSomething = true;
 718  7
                     addDescription(dependency, value, "manifest", key);
 719  
                     //productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 720  7
                     addMatchingValues(classInformation, value, productEvidence);
 721  219
                 } else if (key.equalsIgnoreCase(BUNDLE_NAME)) {
 722  10
                     foundSomething = true;
 723  10
                     productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 724  10
                     addMatchingValues(classInformation, value, productEvidence);
 725  209
                 } else if (key.equalsIgnoreCase(BUNDLE_VENDOR)) {
 726  8
                     foundSomething = true;
 727  8
                     vendorEvidence.addEvidence(source, key, value, Confidence.HIGH);
 728  8
                     addMatchingValues(classInformation, value, vendorEvidence);
 729  201
                 } else if (key.equalsIgnoreCase(BUNDLE_VERSION)) {
 730  10
                     foundSomething = true;
 731  10
                     versionEvidence.addEvidence(source, key, value, Confidence.HIGH);
 732  191
                 } else if (key.equalsIgnoreCase(Attributes.Name.MAIN_CLASS.toString())) {
 733  5
                     continue;
 734  
                     //skipping main class as if this has important information to add
 735  
                     // it will be added during class name analysis...  if other fields
 736  
                     // have the information from the class name then they will get added...
 737  
 //                    foundSomething = true;
 738  
 //                    productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 739  
 //                    vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 740  
 //                    addMatchingValues(classInformation, value, vendorEvidence);
 741  
 //                    addMatchingValues(classInformation, value, productEvidence);
 742  
                 } else {
 743  186
                     key = key.toLowerCase();
 744  
 
 745  186
                     if (!IGNORE_KEYS.contains(key)
 746  
                             && !key.endsWith("jdk")
 747  
                             && !key.contains("lastmodified")
 748  
                             && !key.endsWith("package")
 749  
                             && !key.endsWith("classpath")
 750  
                             && !key.endsWith("class-path")
 751  
                             && !key.endsWith("-scm") //todo change this to a regex?
 752  
                             && !key.startsWith("scm-")
 753  
                             && !isImportPackage(key, value)
 754  
                             && !isPackage(key, value)) {
 755  
 
 756  59
                         foundSomething = true;
 757  59
                         if (key.contains("version")) {
 758  10
                             if (key.contains("specification")) {
 759  8
                                 versionEvidence.addEvidence(source, key, value, Confidence.LOW);
 760  
                             } else {
 761  2
                                 versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 762  
                             }
 763  49
                         } else if ("build-id".equals(key)) {
 764  0
                             int pos = value.indexOf('(');
 765  0
                             if (pos >= 0) {
 766  0
                                 value = value.substring(0, pos - 1);
 767  
                             }
 768  0
                             pos = value.indexOf('[');
 769  0
                             if (pos >= 0) {
 770  0
                                 value = value.substring(0, pos - 1);
 771  
                             }
 772  0
                             versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 773  0
                         } else if (key.contains("title")) {
 774  8
                             productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 775  8
                             addMatchingValues(classInformation, value, productEvidence);
 776  41
                         } else if (key.contains("vendor")) {
 777  5
                             if (key.contains("specification")) {
 778  5
                                 vendorEvidence.addEvidence(source, key, value, Confidence.LOW);
 779  
                             } else {
 780  0
                                 vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 781  0
                                 addMatchingValues(classInformation, value, vendorEvidence);
 782  
                             }
 783  36
                         } else if (key.contains("name")) {
 784  13
                             productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 785  13
                             vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
 786  13
                             addMatchingValues(classInformation, value, vendorEvidence);
 787  13
                             addMatchingValues(classInformation, value, productEvidence);
 788  23
                         } else if (key.contains("license")) {
 789  6
                             addLicense(dependency, value);
 790  
                         } else {
 791  17
                             if (key.contains("description")) {
 792  0
                                 addDescription(dependency, value, "manifest", key);
 793  
                             } else {
 794  17
                                 productEvidence.addEvidence(source, key, value, Confidence.LOW);
 795  17
                                 vendorEvidence.addEvidence(source, key, value, Confidence.LOW);
 796  17
                                 addMatchingValues(classInformation, value, vendorEvidence);
 797  17
                                 addMatchingValues(classInformation, value, productEvidence);
 798  17
                                 if (value.matches(".*\\d.*")) {
 799  6
                                     final StringTokenizer tokenizer = new StringTokenizer(value, " ");
 800  30
                                     while (tokenizer.hasMoreElements()) {
 801  24
                                         final String s = tokenizer.nextToken();
 802  24
                                         if (s.matches("^[0-9.]+$")) {
 803  0
                                             versionEvidence.addEvidence(source, key, s, Confidence.LOW);
 804  
                                         }
 805  24
                                     }
 806  
                                 }
 807  
                             }
 808  
                         }
 809  
                     }
 810  
                 }
 811  255
             }
 812  
         } finally {
 813  18
             if (jar != null) {
 814  18
                 jar.close();
 815  
             }
 816  
         }
 817  18
         return foundSomething;
 818  
     }
 819  
 
 820  
     /**
 821  
      * Adds a description to the given dependency. If the description contains one of the following strings beyond 100
 822  
      * characters, then the description used will be trimmed to that position:
 823  
      * <ul><li>"such as"</li><li>"like "</li><li>"will use "</li><li>"* uses "</li></ul>
 824  
      *
 825  
      * @param dependency a dependency
 826  
      * @param description the description
 827  
      * @param source the source of the evidence
 828  
      * @param key the "name" of the evidence
 829  
      * @return if the description is trimmed, the trimmed version is returned; otherwise the original description is
 830  
      * returned
 831  
      */
 832  
     private String addDescription(Dependency dependency, String description, String source, String key) {
 833  10
         if (dependency.getDescription() == null) {
 834  9
             dependency.setDescription(description);
 835  
         }
 836  
         String desc;
 837  10
         if (HTML_DETECTION_PATTERN.matcher(description).find()) {
 838  0
             desc = Jsoup.parse(description).text();
 839  
         } else {
 840  10
             desc = description;
 841  
         }
 842  10
         dependency.setDescription(desc);
 843  10
         if (desc.length() > 100) {
 844  2
             desc = desc.replaceAll("\\s\\s+", " ");
 845  2
             final int posSuchAs = desc.toLowerCase().indexOf("such as ", 100);
 846  2
             final int posLike = desc.toLowerCase().indexOf("like ", 100);
 847  2
             final int posWillUse = desc.toLowerCase().indexOf("will use ", 100);
 848  2
             final int posUses = desc.toLowerCase().indexOf(" uses ", 100);
 849  2
             int pos = -1;
 850  2
             pos = Math.max(pos, posSuchAs);
 851  2
             if (pos >= 0 && posLike >= 0) {
 852  0
                 pos = Math.min(pos, posLike);
 853  
             } else {
 854  2
                 pos = Math.max(pos, posLike);
 855  
             }
 856  2
             if (pos >= 0 && posWillUse >= 0) {
 857  0
                 pos = Math.min(pos, posWillUse);
 858  
             } else {
 859  2
                 pos = Math.max(pos, posWillUse);
 860  
             }
 861  2
             if (pos >= 0 && posUses >= 0) {
 862  0
                 pos = Math.min(pos, posUses);
 863  
             } else {
 864  2
                 pos = Math.max(pos, posUses);
 865  
             }
 866  
 
 867  2
             if (pos > 0) {
 868  2
                 final StringBuilder sb = new StringBuilder(pos + 3);
 869  2
                 sb.append(desc.substring(0, pos));
 870  2
                 sb.append("...");
 871  2
                 desc = sb.toString();
 872  
             }
 873  2
             dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.LOW);
 874  2
             dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.LOW);
 875  2
         } else {
 876  8
             dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.MEDIUM);
 877  8
             dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.MEDIUM);
 878  
         }
 879  10
         return desc;
 880  
     }
 881  
 
 882  
     /**
 883  
      * Adds a license to the given dependency.
 884  
      *
 885  
      * @param d a dependency
 886  
      * @param license the license
 887  
      */
 888  
     private void addLicense(Dependency d, String license) {
 889  6
         if (d.getLicense() == null) {
 890  6
             d.setLicense(license);
 891  0
         } else if (!d.getLicense().contains(license)) {
 892  0
             d.setLicense(d.getLicense() + NEWLINE + license);
 893  
         }
 894  6
     }
 895  
 
 896  
     /**
 897  
      * The parent directory for the individual directories per archive.
 898  
      */
 899  13
     private File tempFileLocation = null;
 900  
 
 901  
     /**
 902  
      * The initialize method does nothing for this Analyzer.
 903  
      *
 904  
      * @throws Exception is thrown if there is an exception creating a temporary directory
 905  
      */
 906  
     @Override
 907  
     public void initialize() throws Exception {
 908  3
         final File baseDir = Settings.getTempDirectory();
 909  3
         if (!baseDir.exists()) {
 910  0
             if (!baseDir.mkdirs()) {
 911  0
                 final String msg = String.format("Unable to make a temporary folder '%s'", baseDir.getPath());
 912  0
                 throw new AnalysisException(msg);
 913  
             }
 914  
         }
 915  3
         tempFileLocation = File.createTempFile("check", "tmp", baseDir);
 916  3
         if (!tempFileLocation.delete()) {
 917  0
             final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
 918  0
             throw new AnalysisException(msg);
 919  
         }
 920  3
         if (!tempFileLocation.mkdirs()) {
 921  0
             final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
 922  0
             throw new AnalysisException(msg);
 923  
         }
 924  3
     }
 925  
 
 926  
     /**
 927  
      * Deletes any files extracted from the JAR during analysis.
 928  
      */
 929  
     @Override
 930  
     public void close() {
 931  3
         if (tempFileLocation != null && tempFileLocation.exists()) {
 932  3
             Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINE, "Attempting to delete temporary files");
 933  3
             final boolean success = FileUtils.delete(tempFileLocation);
 934  3
             if (!success) {
 935  0
                 Logger.getLogger(JarAnalyzer.class.getName()).log(Level.WARNING,
 936  
                         "Failed to delete some temporary files, see the log for more details");
 937  
             }
 938  
         }
 939  3
     }
 940  
 
 941  
     /**
 942  
      * <p>
 943  
      * A utility function that will interpolate strings based on values given in the properties file. It will also
 944  
      * interpolate the strings contained within the properties file so that properties can reference other
 945  
      * properties.</p>
 946  
      * <p>
 947  
      * <b>Note:</b> if there is no property found the reference will be removed. In other words, if the interpolated
 948  
      * string will be replaced with an empty string.
 949  
      * </p>
 950  
      * <p>
 951  
      * Example:</p>
 952  
      * <code>
 953  
      * Properties p = new Properties();
 954  
      * p.setProperty("key", "value");
 955  
      * String s = interpolateString("'${key}' and '${nothing}'", p);
 956  
      * System.out.println(s);
 957  
      * </code>
 958  
      * <p>
 959  
      * Will result in:</p>
 960  
      * <code>
 961  
      * 'value' and ''
 962  
      * </code>
 963  
      *
 964  
      * @param text the string that contains references to properties.
 965  
      * @param properties a collection of properties that may be referenced within the text.
 966  
      * @return the interpolated text.
 967  
      */
 968  
     protected String interpolateString(String text, Properties properties) {
 969  40
         Properties props = properties;
 970  40
         if (text == null) {
 971  8
             return text;
 972  
         }
 973  32
         if (props == null) {
 974  25
             props = new Properties();
 975  
         }
 976  
 
 977  32
         final int pos = text.indexOf("${");
 978  32
         if (pos < 0) {
 979  29
             return text;
 980  
         }
 981  3
         final int end = text.indexOf("}");
 982  3
         if (end < pos) {
 983  0
             return text;
 984  
         }
 985  
 
 986  3
         final String propName = text.substring(pos + 2, end);
 987  3
         String propValue = interpolateString(props.getProperty(propName), props);
 988  3
         if (propValue == null) {
 989  0
             propValue = "";
 990  
         }
 991  3
         final StringBuilder sb = new StringBuilder(propValue.length() + text.length());
 992  3
         sb.append(text.subSequence(0, pos));
 993  3
         sb.append(propValue);
 994  3
         sb.append(text.substring(end + 1));
 995  3
         return interpolateString(sb.toString(), props); //yes yes, this should be a loop...
 996  
     }
 997  
 
 998  
     /**
 999  
      * Determines if the key value pair from the manifest is for an "import" type entry for package names.
 1000  
      *
 1001  
      * @param key the key from the manifest
 1002  
      * @param value the value from the manifest
 1003  
      * @return true or false depending on if it is believed the entry is an "import" entry
 1004  
      */
 1005  
     private boolean isImportPackage(String key, String value) {
 1006  61
         final Pattern packageRx = Pattern.compile("^((([a-zA-Z_#\\$0-9]\\.)+)\\s*\\;\\s*)+$");
 1007  61
         if (packageRx.matcher(value).matches()) {
 1008  0
             return (key.contains("import") || key.contains("include"));
 1009  
         }
 1010  61
         return false;
 1011  
     }
 1012  
 
 1013  
     /**
 1014  
      * Cycles through an enumeration of JarEntries, contained within the dependency, and returns a list of the class
 1015  
      * names. This does not include core Java package names (i.e. java.* or javax.*).
 1016  
      *
 1017  
      * @param dependency the dependency being analyzed
 1018  
      * @return an list of fully qualified class names
 1019  
      */
 1020  
     private ArrayList<ClassNameInformation> collectClassNames(Dependency dependency) {
 1021  18
         final ArrayList<ClassNameInformation> classNames = new ArrayList<ClassNameInformation>();
 1022  18
         JarFile jar = null;
 1023  
         try {
 1024  18
             jar = new JarFile(dependency.getActualFilePath());
 1025  18
             final Enumeration entries = jar.entries();
 1026  7564
             while (entries.hasMoreElements()) {
 1027  7546
                 final JarEntry entry = (JarEntry) entries.nextElement();
 1028  7546
                 final String name = entry.getName().toLowerCase();
 1029  
                 //no longer stripping "|com\\.sun" - there are some com.sun jar files with CVEs.
 1030  7546
                 if (name.endsWith(".class") && !name.matches("^javax?\\..*$")) {
 1031  6375
                     final ClassNameInformation className = new ClassNameInformation(name.substring(0, name.length() - 6));
 1032  6375
                     classNames.add(className);
 1033  
                 }
 1034  7546
             }
 1035  0
         } catch (IOException ex) {
 1036  0
             final String msg = String.format("Unable to open jar file '%s'.", dependency.getFileName());
 1037  0
             Logger
 1038  
                     .getLogger(JarAnalyzer.class
 1039  
                             .getName()).log(Level.WARNING, msg);
 1040  0
             Logger.getLogger(JarAnalyzer.class
 1041  
                     .getName()).log(Level.FINE, null, ex);
 1042  
         } finally {
 1043  18
             if (jar != null) {
 1044  
                 try {
 1045  18
                     jar.close();
 1046  0
                 } catch (IOException ex) {
 1047  0
                     Logger.getLogger(JarAnalyzer.class.getName()).log(Level.FINEST, null, ex);
 1048  18
                 }
 1049  
             }
 1050  
         }
 1051  18
         return classNames;
 1052  
     }
 1053  
 
 1054  
     /**
 1055  
      * Cycles through the list of class names and places the package levels 0-3 into the provided maps for vendor and
 1056  
      * product. This is helpful when analyzing vendor/product as many times this is included in the package name.
 1057  
      *
 1058  
      * @param classNames a list of class names
 1059  
      * @param vendor HashMap of possible vendor names from package names (e.g. owasp)
 1060  
      * @param product HashMap of possible product names from package names (e.g. dependencycheck)
 1061  
      */
 1062  
     private void analyzeFullyQualifiedClassNames(ArrayList<ClassNameInformation> classNames,
 1063  
             HashMap<String, Integer> vendor, HashMap<String, Integer> product) {
 1064  18
         for (ClassNameInformation entry : classNames) {
 1065  6375
             final ArrayList<String> list = entry.getPackageStructure();
 1066  6375
             addEntry(vendor, list.get(0));
 1067  
 
 1068  6375
             if (list.size() == 2) {
 1069  0
                 addEntry(product, list.get(1));
 1070  
             }
 1071  6375
             if (list.size() == 3) {
 1072  1991
                 addEntry(vendor, list.get(1));
 1073  1991
                 addEntry(product, list.get(1));
 1074  1991
                 addEntry(product, list.get(2));
 1075  
             }
 1076  6375
             if (list.size() >= 4) {
 1077  4384
                 addEntry(vendor, list.get(1));
 1078  4384
                 addEntry(vendor, list.get(2));
 1079  4384
                 addEntry(product, list.get(1));
 1080  4384
                 addEntry(product, list.get(2));
 1081  4384
                 addEntry(product, list.get(3));
 1082  
             }
 1083  6375
         }
 1084  18
     }
 1085  
 
 1086  
     /**
 1087  
      * Adds an entry to the specified collection and sets the Integer (e.g. the count) to 1. If the entry already exists
 1088  
      * in the collection then the Integer is incremented by 1.
 1089  
      *
 1090  
      * @param collection a collection of strings and their occurrence count
 1091  
      * @param key the key to add to the collection
 1092  
      */
 1093  
     private void addEntry(HashMap<String, Integer> collection, String key) {
 1094  34268
         if (collection.containsKey(key)) {
 1095  30314
             collection.put(key, collection.get(key) + 1);
 1096  
         } else {
 1097  3954
             collection.put(key, 1);
 1098  
         }
 1099  34268
     }
 1100  
 
 1101  
     /**
 1102  
      * Cycles through the collection of class name information to see if parts of the package names are contained in the
 1103  
      * provided value. If found, it will be added as the HIGHEST confidence evidence because we have more then one
 1104  
      * source corroborating the value.
 1105  
      *
 1106  
      * @param classes a collection of class name information
 1107  
      * @param value the value to check to see if it contains a package name
 1108  
      * @param evidence the evidence collection to add new entries too
 1109  
      */
 1110  
     private void addMatchingValues(ArrayList<ClassNameInformation> classes, String value, EvidenceCollection evidence) {
 1111  157
         if (value == null || value.isEmpty()) {
 1112  0
             return;
 1113  
         }
 1114  157
         final String text = value.toLowerCase();
 1115  157
         for (ClassNameInformation cni : classes) {
 1116  63174
             for (String key : cni.getPackageStructure()) {
 1117  231862
                 if (text.contains(key)) { //note, package structure elements are already lowercase.
 1118  70531
                     evidence.addEvidence("jar", "package name", key, Confidence.HIGHEST);
 1119  
                 }
 1120  231862
             }
 1121  63174
         }
 1122  157
     }
 1123  
 
 1124  
     /**
 1125  
      * Simple check to see if the attribute from a manifest is just a package name.
 1126  
      *
 1127  
      * @param key the key of the value to check
 1128  
      * @param value the value to check
 1129  
      * @return true if the value looks like a java package name, otherwise false
 1130  
      */
 1131  
     private boolean isPackage(String key, String value) {
 1132  
 
 1133  61
         return !key.matches(".*(version|title|vendor|name|license|description).*")
 1134  
                 && value.matches("^([a-zA-Z_][a-zA-Z0-9_\\$]*(\\.[a-zA-Z_][a-zA-Z0-9_\\$]*)*)?$");
 1135  
 
 1136  
     }
 1137  
 
 1138  
     /**
 1139  
      * Adds evidence from the POM to the dependency. This includes the GAV and in some situations the parent GAV if
 1140  
      * specified.
 1141  
      *
 1142  
      * @param dependency the dependency being analyzed
 1143  
      * @param pom the POM data
 1144  
      * @param pomProperties the properties file associated with the pom
 1145  
      */
 1146  
     private void addPomEvidence(Dependency dependency, Model pom, Properties pomProperties) {
 1147  0
         if (pom == null) {
 1148  0
             return;
 1149  
         }
 1150  0
         String groupid = interpolateString(pom.getGroupId(), pomProperties);
 1151  0
         if (groupid != null && !groupid.isEmpty()) {
 1152  0
             if (groupid.startsWith("org.") || groupid.startsWith("com.")) {
 1153  0
                 groupid = groupid.substring(4);
 1154  
             }
 1155  0
             dependency.getVendorEvidence().addEvidence("pom", "groupid", groupid, Confidence.HIGH);
 1156  0
             dependency.getProductEvidence().addEvidence("pom", "groupid", groupid, Confidence.LOW);
 1157  
         }
 1158  0
         String artifactid = interpolateString(pom.getArtifactId(), pomProperties);
 1159  0
         if (artifactid != null && !artifactid.isEmpty()) {
 1160  0
             if (artifactid.startsWith("org.") || artifactid.startsWith("com.")) {
 1161  0
                 artifactid = artifactid.substring(4);
 1162  
             }
 1163  0
             dependency.getProductEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.HIGH);
 1164  0
             dependency.getVendorEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.LOW);
 1165  
         }
 1166  0
         final String version = interpolateString(pom.getVersion(), pomProperties);
 1167  0
         if (version != null && !version.isEmpty()) {
 1168  0
             dependency.getVersionEvidence().addEvidence("pom", "version", version, Confidence.HIGHEST);
 1169  
         }
 1170  
 
 1171  0
         final Parent parent = pom.getParent(); //grab parent GAV
 1172  0
         if (parent != null) {
 1173  0
             final String parentGroupId = interpolateString(parent.getGroupId(), pomProperties);
 1174  0
             if (parentGroupId != null && !parentGroupId.isEmpty()) {
 1175  0
                 if (groupid == null || groupid.isEmpty()) {
 1176  0
                     dependency.getVendorEvidence().addEvidence("pom", "parent.groupid", parentGroupId, Confidence.HIGH);
 1177  
                 } else {
 1178  0
                     dependency.getVendorEvidence().addEvidence("pom", "parent.groupid", parentGroupId, Confidence.MEDIUM);
 1179  
                 }
 1180  0
                 dependency.getProductEvidence().addEvidence("pom", "parent.groupid", parentGroupId, Confidence.LOW);
 1181  
             }
 1182  0
             final String parentArtifactId = interpolateString(parent.getArtifactId(), pomProperties);
 1183  0
             if (parentArtifactId != null && !parentArtifactId.isEmpty()) {
 1184  0
                 if (artifactid == null || artifactid.isEmpty()) {
 1185  0
                     dependency.getProductEvidence().addEvidence("pom", "parent.artifactid", parentArtifactId, Confidence.HIGH);
 1186  
                 } else {
 1187  0
                     dependency.getProductEvidence().addEvidence("pom", "parent.artifactid", parentArtifactId, Confidence.MEDIUM);
 1188  
                 }
 1189  0
                 dependency.getVendorEvidence().addEvidence("pom", "parent.artifactid", parentArtifactId, Confidence.LOW);
 1190  
             }
 1191  0
             final String parentVersion = interpolateString(parent.getVersion(), pomProperties);
 1192  0
             if (parentVersion != null && !parentVersion.isEmpty()) {
 1193  0
                 if (version == null || version.isEmpty()) {
 1194  0
                     dependency.getVersionEvidence().addEvidence("pom", "parent.version", parentVersion, Confidence.HIGH);
 1195  
                 } else {
 1196  0
                     dependency.getVersionEvidence().addEvidence("pom", "parent.version", parentVersion, Confidence.LOW);
 1197  
                 }
 1198  
             }
 1199  
         }
 1200  
         // org name
 1201  0
         final Organization org = pom.getOrganization();
 1202  0
         if (org != null && org.getName() != null) {
 1203  0
             final String orgName = interpolateString(org.getName(), pomProperties);
 1204  0
             if (orgName != null && !orgName.isEmpty()) {
 1205  0
                 dependency.getVendorEvidence().addEvidence("pom", "organization name", orgName, Confidence.HIGH);
 1206  
             }
 1207  
         }
 1208  
         //pom name
 1209  0
         final String pomName = interpolateString(pom.getName(), pomProperties);
 1210  0
         if (pomName != null && !pomName.isEmpty()) {
 1211  0
             dependency.getProductEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
 1212  0
             dependency.getVendorEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
 1213  
         }
 1214  
 
 1215  
         //Description
 1216  0
         if (pom.getDescription() != null) {
 1217  0
             final String description = interpolateString(pom.getDescription(), pomProperties);
 1218  0
             if (description != null && !description.isEmpty()) {
 1219  0
                 addDescription(dependency, description, "pom", "description");
 1220  
             }
 1221  
         }
 1222  0
         extractLicense(pom, pomProperties, dependency);
 1223  0
     }
 1224  
 
 1225  
     /**
 1226  
      * Extracts the license information from the pom and adds it to the dependency.
 1227  
      *
 1228  
      * @param pom the pom object
 1229  
      * @param pomProperties the properties, used for string interpolation
 1230  
      * @param dependency the dependency to add license information too
 1231  
      */
 1232  
     private void extractLicense(Model pom, Properties pomProperties, Dependency dependency) {
 1233  
         //license
 1234  7
         if (pom.getLicenses() != null) {
 1235  1
             String license = null;
 1236  1
             for (License lic : pom.getLicenses().getLicense()) {
 1237  1
                 String tmp = null;
 1238  1
                 if (lic.getName() != null) {
 1239  1
                     tmp = interpolateString(lic.getName(), pomProperties);
 1240  
                 }
 1241  1
                 if (lic.getUrl() != null) {
 1242  1
                     if (tmp == null) {
 1243  0
                         tmp = interpolateString(lic.getUrl(), pomProperties);
 1244  
                     } else {
 1245  1
                         tmp += ": " + interpolateString(lic.getUrl(), pomProperties);
 1246  
                     }
 1247  
                 }
 1248  1
                 if (tmp == null) {
 1249  0
                     continue;
 1250  
                 }
 1251  1
                 if (HTML_DETECTION_PATTERN.matcher(tmp).find()) {
 1252  0
                     tmp = Jsoup.parse(tmp).text();
 1253  
                 }
 1254  1
                 if (license == null) {
 1255  1
                     license = tmp;
 1256  
                 } else {
 1257  0
                     license += "\n" + tmp;
 1258  
                 }
 1259  1
             }
 1260  1
             if (license != null) {
 1261  1
                 dependency.setLicense(license);
 1262  
             }
 1263  
         }
 1264  7
     }
 1265  
 
 1266  
     /**
 1267  
      * Stores information about a class name.
 1268  
      */
 1269  
     protected static class ClassNameInformation {
 1270  
 
 1271  
         /**
 1272  
          * <p>
 1273  
          * Stores information about a given class name. This class will keep the fully qualified class name and a list
 1274  
          * of the important parts of the package structure. Up to the first four levels of the package structure are
 1275  
          * stored, excluding a leading "org" or "com". Example:</p>
 1276  
          * <code>ClassNameInformation obj = new ClassNameInformation("org.owasp.dependencycheck.analyzer.JarAnalyzer");
 1277  
          * System.out.println(obj.getName());
 1278  
          * for (String p : obj.getPackageStructure())
 1279  
          *     System.out.println(p);
 1280  
          * </code>
 1281  
          * <p>
 1282  
          * Would result in:</p>
 1283  
          * <code>org.owasp.dependencycheck.analyzer.JarAnalyzer
 1284  
          * owasp
 1285  
          * dependencycheck
 1286  
          * analyzer
 1287  
          * jaranalyzer</code>
 1288  
          *
 1289  
          * @param className a fully qualified class name
 1290  
          */
 1291  6375
         ClassNameInformation(String className) {
 1292  6375
             name = className;
 1293  6375
             if (name.contains("/")) {
 1294  6375
                 final String[] tmp = className.toLowerCase().split("/");
 1295  6375
                 int start = 0;
 1296  6375
                 int end = 3;
 1297  6375
                 if ("com".equals(tmp[0]) || "org".equals(tmp[0])) {
 1298  5948
                     start = 1;
 1299  5948
                     end = 4;
 1300  
                 }
 1301  6375
                 if (tmp.length <= end) {
 1302  1991
                     end = tmp.length - 1;
 1303  
                 }
 1304  29884
                 for (int i = start; i <= end; i++) {
 1305  23509
                     packageStructure.add(tmp[i]);
 1306  
                 }
 1307  6375
             } else {
 1308  0
                 packageStructure.add(name);
 1309  
             }
 1310  6375
         }
 1311  
         /**
 1312  
          * The fully qualified class name.
 1313  
          */
 1314  
         private String name;
 1315  
 
 1316  
         /**
 1317  
          * Get the value of name
 1318  
          *
 1319  
          * @return the value of name
 1320  
          */
 1321  
         public String getName() {
 1322  
             return name;
 1323  
         }
 1324  
 
 1325  
         /**
 1326  
          * Set the value of name
 1327  
          *
 1328  
          * @param name new value of name
 1329  
          */
 1330  
         public void setName(String name) {
 1331  
             this.name = name;
 1332  
         }
 1333  
         /**
 1334  
          * Up to the first four levels of the package structure, excluding a leading "org" or "com".
 1335  
          */
 1336  6375
         private final ArrayList<String> packageStructure = new ArrayList<String>();
 1337  
 
 1338  
         /**
 1339  
          * Get the value of packageStructure
 1340  
          *
 1341  
          * @return the value of packageStructure
 1342  
          */
 1343  
         public ArrayList<String> getPackageStructure() {
 1344  
             return packageStructure;
 1345  
         }
 1346  
     }
 1347  
 
 1348  
     /**
 1349  
      * Retrieves the next temporary directory to extract an archive too.
 1350  
      *
 1351  
      * @return a directory
 1352  
      * @throws AnalysisException thrown if unable to create temporary directory
 1353  
      */
 1354  
     private File getNextTempDirectory() throws AnalysisException {
 1355  0
         dirCount += 1;
 1356  0
         final File directory = new File(tempFileLocation, String.valueOf(dirCount));
 1357  
         //getting an exception for some directories not being able to be created; might be because the directory already exists?
 1358  0
         if (directory.exists()) {
 1359  0
             return getNextTempDirectory();
 1360  
         }
 1361  0
         if (!directory.mkdirs()) {
 1362  0
             final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath());
 1363  0
             throw new AnalysisException(msg);
 1364  
         }
 1365  0
         return directory;
 1366  
     }
 1367  
 }