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