View Javadoc
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 com.github.packageurl.MalformedPackageURLException;
21  import com.github.packageurl.PackageURL;
22  import com.github.packageurl.PackageURLBuilder;
23  import com.google.common.base.Strings;
24  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25  import java.io.File;
26  import java.io.FileFilter;
27  import java.io.FileInputStream;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.io.Reader;
33  import java.io.UnsupportedEncodingException;
34  import java.nio.charset.StandardCharsets;
35  import java.nio.file.Paths;
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.Enumeration;
39  import java.util.HashMap;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Map.Entry;
44  import java.util.Properties;
45  import java.util.Set;
46  import java.util.StringTokenizer;
47  import java.util.concurrent.atomic.AtomicInteger;
48  import java.util.jar.Attributes;
49  import java.util.jar.JarEntry;
50  import java.util.jar.JarFile;
51  import java.util.jar.Manifest;
52  import java.util.regex.Pattern;
53  import java.util.zip.ZipEntry;
54  
55  import org.apache.commons.io.FilenameUtils;
56  import org.apache.commons.lang3.StringUtils;
57  import org.apache.commons.io.IOUtils;
58  import org.jsoup.Jsoup;
59  import org.owasp.dependencycheck.Engine;
60  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
61  import org.owasp.dependencycheck.data.nvd.ecosystem.Ecosystem;
62  import org.owasp.dependencycheck.dependency.Confidence;
63  import org.owasp.dependencycheck.dependency.Dependency;
64  import org.owasp.dependencycheck.dependency.EvidenceType;
65  import org.owasp.dependencycheck.dependency.naming.GenericIdentifier;
66  import org.owasp.dependencycheck.dependency.naming.Identifier;
67  import org.owasp.dependencycheck.dependency.naming.PurlIdentifier;
68  import org.owasp.dependencycheck.exception.InitializationException;
69  import org.owasp.dependencycheck.utils.FileFilterBuilder;
70  import org.owasp.dependencycheck.utils.FileUtils;
71  import org.owasp.dependencycheck.utils.Settings;
72  import org.owasp.dependencycheck.xml.pom.Developer;
73  import org.owasp.dependencycheck.xml.pom.License;
74  import org.owasp.dependencycheck.xml.pom.Model;
75  import org.owasp.dependencycheck.xml.pom.PomUtils;
76  import org.slf4j.Logger;
77  import org.slf4j.LoggerFactory;
78  
79  /**
80   * Used to load a JAR file and collect information that can be used to determine
81   * the associated CPE.
82   *
83   * @author Jeremy Long
84   */
85  public class JarAnalyzer extends AbstractFileTypeAnalyzer {
86  
87      //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables">
88      /**
89       * A descriptor for the type of dependencies processed or added by this
90       * analyzer.
91       */
92      public static final String DEPENDENCY_ECOSYSTEM = Ecosystem.JAVA;
93      /**
94       * The logger.
95       */
96      private static final Logger LOGGER = LoggerFactory.getLogger(JarAnalyzer.class);
97      /**
98       * The count of directories created during analysis. This is used for
99       * creating temporary directories.
100      */
101     private static final AtomicInteger DIR_COUNT = new AtomicInteger(0);
102     /**
103      * The system independent newline character.
104      */
105     private static final String NEWLINE = System.getProperty("line.separator");
106     /**
107      * A list of values in the manifest to ignore as they only result in false
108      * positives.
109      */
110     private static final Set<String> IGNORE_VALUES = newHashSet(
111             "Sun Java System Application Server");
112     /**
113      * A list of elements in the manifest to ignore.
114      */
115     private static final Set<String> IGNORE_KEYS = newHashSet(
116             "built-by",
117             "created-by",
118             "builtby",
119             "built-with",
120             "builtwith",
121             "createdby",
122             "build-jdk",
123             "buildjdk",
124             "ant-version",
125             "antversion",
126             "dynamicimportpackage",
127             "dynamicimport-package",
128             "dynamic-importpackage",
129             "dynamic-import-package",
130             "import-package",
131             "ignore-package",
132             "export-package",
133             "importpackage",
134             "import-template",
135             "importtemplate",
136             "java-vendor",
137             "export-template",
138             "exporttemplate",
139             "ignorepackage",
140             "exportpackage",
141             "sealed",
142             "manifest-version",
143             "archiver-version",
144             "manifestversion",
145             "archiverversion",
146             "classpath",
147             "class-path",
148             "tool",
149             "bundle-manifestversion",
150             "bundlemanifestversion",
151             "bundle-vendor",
152             "include-resource",
153             "embed-dependency",
154             "embedded-artifacts",
155             "ipojo-components",
156             "ipojo-extension",
157             "plugin-dependencies",
158             "today",
159             "tstamp",
160             "dstamp",
161             "eclipse-sourcereferences",
162             "kotlin-version",
163             "require-capability");
164     /**
165      * Deprecated Jar manifest attribute, that is, nonetheless, useful for
166      * analysis.
167      */
168     @SuppressWarnings("deprecation")
169     private static final String IMPLEMENTATION_VENDOR_ID = Attributes.Name.IMPLEMENTATION_VENDOR_ID
170             .toString();
171     /**
172      * item in some manifest, should be considered medium confidence.
173      */
174     private static final String BUNDLE_VERSION = "Bundle-Version"; //: 2.1.2
175     /**
176      * item in some manifest, should be considered medium confidence.
177      */
178     private static final String BUNDLE_DESCRIPTION = "Bundle-Description"; //: Apache Struts 2
179     /**
180      * item in some manifest, should be considered medium confidence.
181      */
182     private static final String BUNDLE_NAME = "Bundle-Name"; //: Struts 2 Core
183     /**
184      * A pattern to detect HTML within text.
185      */
186     private static final Pattern HTML_DETECTION_PATTERN = Pattern.compile("\\<[a-z]+.*/?\\>", Pattern.CASE_INSENSITIVE);
187     /**
188      * The name of the analyzer.
189      */
190     private static final String ANALYZER_NAME = "Jar Analyzer";
191     /**
192      * The phase that this analyzer is intended to run in.
193      */
194     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
195     /**
196      * The set of jar files to exclude from analysis.
197      */
198     private static final List<String> EXCLUDE_JARS = Arrays.asList("-doc.jar", "-src.jar", "-javadoc.jar", "-sources.jar");
199     /**
200      * The set of file extensions supported by this analyzer.
201      */
202     private static final String[] EXTENSIONS = {"jar", "war", "aar"};
203     /**
204      * The file filter used to determine which files this analyzer supports.
205      */
206     private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();
207 
208     /**
209      * The expected first bytes when reading a zip file.
210      */
211     private static final byte[] ZIP_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x03, 0x04};
212 
213     /**
214      * The expected first bytes when reading an empty zip file.
215      */
216     private static final byte[] ZIP_EMPTY_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x05, 0x06};
217 
218     /**
219      * The expected first bytes when reading a spanned zip file.
220      */
221     private static final byte[] ZIP_SPANNED_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x07, 0x08};
222 
223     //</editor-fold>
224     /**
225      * The parent directory for the individual directories per archive.
226      */
227     private File tempFileLocation = null;
228     /**
229      * Maven group id and artifact ids must match the regex to be considered
230      * valid. In some cases ODC cannot interpolate a variable and it produced
231      * invalid names.
232      */
233     private static final String VALID_NAME = "^[A-Za-z0-9_\\-.]+$";
234 
235     //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
236     /**
237      * Returns the FileFilter.
238      *
239      * @return the FileFilter
240      */
241     @Override
242     protected FileFilter getFileFilter() {
243         return FILTER;
244     }
245 
246     /**
247      * Returns the name of the analyzer.
248      *
249      * @return the name of the analyzer.
250      */
251     @Override
252     public String getName() {
253         return ANALYZER_NAME;
254     }
255 
256     /**
257      * Returns the phase that the analyzer is intended to run in.
258      *
259      * @return the phase that the analyzer is intended to run in.
260      */
261     @Override
262     public AnalysisPhase getAnalysisPhase() {
263         return ANALYSIS_PHASE;
264     }
265 
266     @Override
267     public boolean accept(File pathname) {
268         final boolean accepted = super.accept(pathname);
269         return accepted && !isExcludedJar(pathname);
270     }
271 
272     /**
273      * Returns true if the JAR is a `*-sources.jar` or `*-javadoc.jar`;
274      * otherwise false.
275      *
276      * @param path the path to the dependency
277      * @return true if the JAR is a `*-sources.jar` or `*-javadoc.jar`;
278      * otherwise false.
279      */
280     private boolean isExcludedJar(File path) {
281         final String fileName = path.getName().toLowerCase();
282         return EXCLUDE_JARS.stream().anyMatch(fileName::endsWith);
283     }
284     //</editor-fold>
285 
286     /**
287      * Returns the key used in the properties file to reference the analyzer's
288      * enabled property.
289      *
290      * @return the analyzer's enabled property setting key
291      */
292     @Override
293     protected String getAnalyzerEnabledSettingKey() {
294         return Settings.KEYS.ANALYZER_JAR_ENABLED;
295     }
296 
297     /**
298      * Loads a specified JAR file and collects information from the manifest and
299      * checksums to identify the correct CPE information.
300      *
301      * @param dependency the dependency to analyze.
302      * @param engine the engine that is scanning the dependencies
303      * @throws AnalysisException is thrown if there is an error reading the JAR
304      * file.
305      */
306     @Override
307     public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
308         final List<ClassNameInformation> classNames = collectClassNames(dependency);
309         final String fileName = dependency.getFileName().toLowerCase();
310         if ((classNames.isEmpty()
311                 && (fileName.endsWith("-sources.jar")
312                 || fileName.endsWith("-javadoc.jar")
313                 || fileName.endsWith("-src.jar")
314                 || fileName.endsWith("-doc.jar")
315                 || isMacOSMetaDataFile(dependency, engine)))
316                 || !isZipFile(dependency)) {
317             engine.removeDependency(dependency);
318             return;
319         }
320         Exception exception = null;
321         boolean hasManifest = false;
322         try {
323             hasManifest = parseManifest(dependency, classNames);
324         } catch (IOException ex) {
325             LOGGER.debug("Invalid Manifest", ex);
326             exception = ex;
327         }
328         boolean hasPOM = false;
329         try {
330             hasPOM = analyzePOM(dependency, classNames, engine);
331         } catch (AnalysisException ex) {
332             LOGGER.debug("Error parsing pom.xml", ex);
333             exception = ex;
334         }
335         final boolean addPackagesAsEvidence = !(hasManifest && hasPOM);
336         analyzePackageNames(classNames, dependency, addPackagesAsEvidence);
337         dependency.setEcosystem(DEPENDENCY_ECOSYSTEM);
338 
339         if (exception != null) {
340             throw new AnalysisException(String.format("An error occurred extracting evidence from "
341                     + "%s, analysis may be incomplete; please see the log for more details.",
342                     dependency.getDisplayFileName()), exception);
343         }
344     }
345 
346     /**
347      * Checks if the given dependency appears to be a macOS meta-data file,
348      * returning true if its filename starts with a ._ prefix and if there is
349      * another dependency with the same filename minus the ._ prefix, otherwise
350      * it returns false.
351      *
352      * @param dependency the dependency to check if it's a macOS meta-data file
353      * @param engine the engine that is scanning the dependencies
354      * @return whether or not the given dependency appears to be a macOS
355      * meta-data file
356      */
357     @SuppressFBWarnings(justification = "If actual file path is not null the path will have elements and getFileName will not be called on a null",
358             value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"})
359     private boolean isMacOSMetaDataFile(final Dependency dependency, final Engine engine) {
360         if (dependency.getActualFilePath() != null) {
361             final String fileName = Paths.get(dependency.getActualFilePath()).getFileName().toString();
362             return fileName.startsWith("._") && hasDependencyWithFilename(engine.getDependencies(), fileName.substring(2));
363         }
364         return false;
365     }
366 
367     /**
368      * Iterates through the given list of dependencies and returns true when it
369      * finds a dependency with a filename matching the given filename, otherwise
370      * returns false.
371      *
372      * @param dependencies the dependencies to search within
373      * @param fileName the filename to search for
374      * @return whether or not the given dependencies contain a dependency with
375      * the given filename
376      */
377     @SuppressFBWarnings(justification = "If actual file path is not null the path will have elements and getFileName will not be called on a null",
378             value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"})
379     private boolean hasDependencyWithFilename(final Dependency[] dependencies, final String fileName) {
380         for (final Dependency dependency : dependencies) {
381             if (dependency.getActualFilePath() != null
382                     && Paths.get(dependency.getActualFilePath()).getFileName().toString().equalsIgnoreCase(fileName)) {
383                 return true;
384             }
385         }
386         return false;
387     }
388 
389     /**
390      * Attempts to read the first bytes of the given dependency (using its
391      * actual file path) and returns true if they match the expected first bytes
392      * of a zip file, which may be empty or spanned. If they don't match, or if
393      * the file could not be read, then it returns false.
394      *
395      * @param dependency the dependency to check if it's a zip file
396      * @return whether or not the given dependency appears to be a zip file from
397      * its first bytes
398      */
399     @SuppressFBWarnings(justification = "try with resources will clean up the output stream", value = {"OBL_UNSATISFIED_OBLIGATION"})
400     private boolean isZipFile(final Dependency dependency) {
401         final byte[] buffer = new byte[4];
402         try (FileInputStream fileInputStream = new FileInputStream(dependency.getActualFilePath())) {
403             if (fileInputStream.read(buffer) > 0
404                     && (Arrays.equals(buffer, ZIP_FIRST_BYTES)
405                     || Arrays.equals(buffer, ZIP_EMPTY_FIRST_BYTES)
406                     || Arrays.equals(buffer, ZIP_SPANNED_FIRST_BYTES))) {
407                 return true;
408             }
409         } catch (Exception e) {
410             LOGGER.warn("Unable to check if '{}' is a zip file", dependency.getActualFilePath());
411             LOGGER.trace("", e);
412         }
413         return false;
414     }
415 
416     /**
417      * Attempts to find a pom.xml within the JAR file. If found it extracts
418      * information and adds it to the evidence. This will attempt to interpolate
419      * the strings contained within the pom.properties if one exists.
420      *
421      * @param dependency the dependency being analyzed
422      * @param classes a collection of class name information
423      * @param engine the analysis engine, used to add additional dependencies
424      * @throws AnalysisException is thrown if there is an exception parsing the
425      * pom
426      * @return whether or not evidence was added to the dependency
427      */
428     protected boolean analyzePOM(Dependency dependency, List<ClassNameInformation> classes, Engine engine) throws AnalysisException {
429 
430         //TODO add breakpoint on groov-all to find out why commons-cli is not added as a new dependency?
431         boolean evidenceAdded = false;
432         try (JarFile jar = new JarFile(dependency.getActualFilePath(), false)) {
433             //check if we are scanning in a repo directory - so the pom is adjacent to the jar
434             final String repoPomName = FilenameUtils.removeExtension(dependency.getActualFilePath()) + ".pom";
435             final File repoPom = new File(repoPomName);
436             if (repoPom.isFile()) {
437                 final Model pom = PomUtils.readPom(repoPom);
438                 evidenceAdded |= setPomEvidence(dependency, pom, classes, true);
439             }
440 
441             final List<String> pomEntries = retrievePomListing(jar);
442 
443             for (String path : pomEntries) {
444                 LOGGER.debug("Reading pom entry: {}", path);
445                 try {
446                     //extract POM to its own directory and add it as its own dependency
447                     final Properties pomProperties = retrievePomProperties(path, jar);
448                     final File pomFile = extractPom(path, jar);
449                     final Model pom = PomUtils.readPom(pomFile);
450                     pom.setGAVFromPomDotProperties(pomProperties);
451                     pom.processProperties(pomProperties);
452 
453                     final String artifactId = new File(path).getParentFile().getName();
454                     if (dependency.getActualFile().getName().startsWith(artifactId)) {
455                         evidenceAdded |= setPomEvidence(dependency, pom, classes, true);
456                     } else {
457                         final String displayPath = String.format("%s%s%s",
458                                 dependency.getFilePath(),
459                                 File.separator,
460                                 path);
461                         final String displayName = String.format("%s%s%s",
462                                 dependency.getFileName(),
463                                 File.separator,
464                                 path);
465                         final Dependency newDependency = new Dependency();
466                         newDependency.setActualFilePath(pomFile.getAbsolutePath());
467                         newDependency.setFileName(displayName);
468                         newDependency.setFilePath(displayPath);
469                         newDependency.setEcosystem(DEPENDENCY_ECOSYSTEM);
470                         String groupId = pom.getGroupId();
471                         String version = pom.getVersion();
472                         if (groupId == null) {
473                             groupId = pom.getParentGroupId();
474                         }
475                         if (version == null) {
476                             version = pom.getParentVersion();
477                         }
478                         if (groupId == null) {
479                             newDependency.setName(pom.getArtifactId());
480                             newDependency.setPackagePath(String.format("%s:%s", pom.getArtifactId(), version));
481                         } else {
482                             newDependency.setName(String.format("%s:%s", groupId, pom.getArtifactId()));
483                             newDependency.setPackagePath(String.format("%s:%s:%s", groupId, pom.getArtifactId(), version));
484                         }
485                         newDependency.setDisplayFileName(String.format("%s (shaded: %s)",
486                                 dependency.getDisplayFileName(), newDependency.getPackagePath()));
487                         newDependency.setVersion(version);
488                         setPomEvidence(newDependency, pom, null, true);
489                         if (dependency.getProjectReferences().size() > 0) {
490                             newDependency.addAllProjectReferences(dependency.getProjectReferences());
491                         }
492                         engine.addDependency(newDependency);
493                     }
494                 } catch (AnalysisException ex) {
495                     LOGGER.warn("An error occurred while analyzing '{}'.", dependency.getActualFilePath());
496                     LOGGER.trace("", ex);
497                 }
498             }
499         } catch (IOException ex) {
500             LOGGER.warn("Unable to read JarFile '{}'.", dependency.getActualFilePath());
501             LOGGER.trace("", ex);
502         }
503         return evidenceAdded;
504     }
505 
506     /**
507      * Given a path to a pom.xml within a JarFile, this method attempts to load
508      * a sibling pom.properties if one exists.
509      *
510      * @param path the path to the pom.xml within the JarFile
511      * @param jar the JarFile to load the pom.properties from
512      * @return a Properties object or null if no pom.properties was found
513      */
514     private Properties retrievePomProperties(String path, final JarFile jar) {
515         final Properties pomProperties = new Properties();
516         final String propPath = path.substring(0, path.length() - 7) + "pom.properties";
517         final ZipEntry propEntry = jar.getEntry(propPath);
518         if (propEntry != null) {
519             try (Reader reader = new InputStreamReader(jar.getInputStream(propEntry), StandardCharsets.UTF_8)) {
520                 pomProperties.load(reader);
521                 LOGGER.debug("Read pom.properties: {}", propPath);
522             } catch (UnsupportedEncodingException ex) {
523                 LOGGER.trace("UTF-8 is not supported", ex);
524             } catch (IOException ex) {
525                 LOGGER.trace("Unable to read the POM properties", ex);
526             }
527         }
528         return pomProperties;
529     }
530 
531     /**
532      * Searches a JarFile for pom.xml entries and returns a listing of these
533      * entries.
534      *
535      * @param jar the JarFile to search
536      * @return a list of pom.xml entries
537      * @throws IOException thrown if there is an exception reading a JarEntry
538      */
539     private List<String> retrievePomListing(final JarFile jar) throws IOException {
540         final List<String> pomEntries = new ArrayList<>();
541         final Enumeration<JarEntry> entries = jar.entries();
542         while (entries.hasMoreElements()) {
543             final JarEntry entry = entries.nextElement();
544             final String entryName = new File(entry.getName()).getName().toLowerCase();
545             if (!entry.isDirectory() && "pom.xml".equals(entryName)
546                     && entry.getName().toUpperCase().startsWith("META-INF")) {
547                 pomEntries.add(entry.getName());
548             }
549         }
550         return pomEntries;
551     }
552 
553     /**
554      * Retrieves the specified POM from a jar.
555      *
556      * @param path the path to the pom.xml file within the jar file
557      * @param jar the jar file to extract the pom from
558      * @return returns the POM file
559      * @throws AnalysisException is thrown if there is an exception extracting
560      * the file
561      */
562     private File extractPom(String path, JarFile jar) throws AnalysisException {
563         final File tmpDir = getNextTempDirectory();
564         final File file = new File(tmpDir, "pom.xml");
565         final ZipEntry entry = jar.getEntry(path);
566         if (entry == null) {
567             throw new AnalysisException(String.format("Pom (%s) does not exist in %s", path, jar.getName()));
568         }
569         try (InputStream input = jar.getInputStream(entry);
570                 FileOutputStream fos = new FileOutputStream(file)) {
571             IOUtils.copy(input, fos);
572         } catch (IOException ex) {
573             LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName());
574             LOGGER.error("", ex);
575         }
576         return file;
577     }
578 
579     /**
580      * Sets evidence from the pom on the supplied dependency.
581      *
582      * @param dependency the dependency to set data on
583      * @param pom the information from the pom
584      * @param classes a collection of ClassNameInformation - containing data
585      * about the fully qualified class names within the JAR file being analyzed
586      * @param isMainPom a flag indicating if this is the primary pom.
587      * @return true if there was evidence within the pom that we could use;
588      * otherwise false
589      */
590     public static boolean setPomEvidence(Dependency dependency, Model pom,
591             List<ClassNameInformation> classes, boolean isMainPom) {
592         if (pom == null) {
593             return false;
594         }
595         boolean foundSomething = false;
596         boolean addAsIdentifier = true;
597         String groupid = intepolationFailCheck(pom.getGroupId());
598         String parentGroupId = intepolationFailCheck(pom.getParentGroupId());
599         String artifactid = intepolationFailCheck(pom.getArtifactId());
600         String parentArtifactId = intepolationFailCheck(pom.getParentArtifactId());
601         String version = intepolationFailCheck(pom.getVersion());
602         String parentVersion = intepolationFailCheck(pom.getParentVersion());
603 
604         if (("org.sonatype.oss".equals(parentGroupId) && "oss-parent".equals(parentArtifactId))
605                 || ("org.springframework.boot".equals(parentGroupId) && "spring-boot-starter-parent".equals(parentArtifactId))) {
606             parentGroupId = null;
607             parentArtifactId = null;
608             parentVersion = null;
609         }
610 
611         if ((groupid == null || groupid.isEmpty()) && parentGroupId != null && !parentGroupId.isEmpty()) {
612             groupid = parentGroupId;
613         }
614 
615         final String originalGroupID = groupid;
616 
617         if ((artifactid == null || artifactid.isEmpty()) && parentArtifactId != null && !parentArtifactId.isEmpty()) {
618             artifactid = parentArtifactId;
619         }
620 
621         final String originalArtifactID = artifactid;
622         if (artifactid != null && (artifactid.startsWith("org.") || artifactid.startsWith("com."))) {
623             artifactid = artifactid.substring(4);
624         }
625 
626         if ((version == null || version.isEmpty()) && parentVersion != null && !parentVersion.isEmpty()) {
627             version = parentVersion;
628         }
629 
630         if (isMainPom && dependency.getName() == null && originalArtifactID != null && !originalArtifactID.isEmpty()) {
631             if (originalGroupID != null && !originalGroupID.isEmpty()) {
632                 dependency.setName(String.format("%s:%s", originalGroupID, originalArtifactID));
633             } else {
634                 dependency.setName(originalArtifactID);
635             }
636         }
637         if (isMainPom && dependency.getVersion() == null && version != null && !version.isEmpty()) {
638             dependency.setVersion(version);
639         }
640 
641         if (groupid != null && !groupid.isEmpty()) {
642             foundSomething = true;
643             dependency.addEvidence(EvidenceType.VENDOR, "pom", "groupid", groupid, Confidence.HIGHEST);
644             //In several cases we are seeing the product name at the end of the group identifier.
645             // This may cause several FP on products that have a collection of dependencies (e.g. jetty).
646             //dependency.addEvidence(EvidenceType.PRODUCT, "pom", "groupid", groupid, Confidence.LOW);
647             dependency.addEvidence(EvidenceType.PRODUCT, "pom", "groupid", groupid, Confidence.HIGHEST);
648             addMatchingValues(classes, groupid, dependency, EvidenceType.VENDOR);
649             addMatchingValues(classes, groupid, dependency, EvidenceType.PRODUCT);
650             if (parentGroupId != null && !parentGroupId.isEmpty() && !parentGroupId.equals(groupid)) {
651                 dependency.addEvidence(EvidenceType.VENDOR, "pom", "parent-groupid", parentGroupId, Confidence.MEDIUM);
652                 //see note above for groupid
653                 //dependency.addEvidence(EvidenceType.PRODUCT, "pom", "parent-groupid", parentGroupId, Confidence.LOW);
654                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "parent-groupid", parentGroupId, Confidence.MEDIUM);
655                 addMatchingValues(classes, parentGroupId, dependency, EvidenceType.VENDOR);
656                 addMatchingValues(classes, parentGroupId, dependency, EvidenceType.PRODUCT);
657             }
658         } else {
659             addAsIdentifier = false;
660         }
661 
662         if (artifactid != null && !artifactid.isEmpty()) {
663             foundSomething = true;
664             dependency.addEvidence(EvidenceType.PRODUCT, "pom", "artifactid", artifactid, Confidence.HIGHEST);
665             dependency.addEvidence(EvidenceType.VENDOR, "pom", "artifactid", artifactid, Confidence.LOW);
666             addMatchingValues(classes, artifactid, dependency, EvidenceType.VENDOR);
667             addMatchingValues(classes, artifactid, dependency, EvidenceType.PRODUCT);
668             if (parentArtifactId != null && !parentArtifactId.isEmpty() && !parentArtifactId.equals(artifactid)) {
669                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "parent-artifactid", parentArtifactId, Confidence.MEDIUM);
670                 dependency.addEvidence(EvidenceType.VENDOR, "pom", "parent-artifactid", parentArtifactId, Confidence.LOW);
671                 addMatchingValues(classes, parentArtifactId, dependency, EvidenceType.VENDOR);
672                 addMatchingValues(classes, parentArtifactId, dependency, EvidenceType.PRODUCT);
673             }
674         } else {
675             addAsIdentifier = false;
676         }
677 
678         if (version != null && !version.isEmpty()) {
679             foundSomething = true;
680             dependency.addEvidence(EvidenceType.VERSION, "pom", "version", version, Confidence.HIGHEST);
681             if (parentVersion != null && !parentVersion.isEmpty() && !parentVersion.equals(version)) {
682                 dependency.addEvidence(EvidenceType.VERSION, "pom", "parent-version", version, Confidence.LOW);
683             }
684         } else {
685             addAsIdentifier = false;
686         }
687 
688         if (addAsIdentifier && isMainPom) {
689             Identifier id = null;
690             try {
691                 if (originalArtifactID != null && originalArtifactID.matches(VALID_NAME)
692                         && originalGroupID != null && originalGroupID.matches(VALID_NAME)) {
693                     final PackageURL purl = PackageURLBuilder.aPackageURL().withType("maven").withNamespace(originalGroupID)
694                             .withName(originalArtifactID).withVersion(version).build();
695                     id = new PurlIdentifier(purl, Confidence.HIGH);
696                 } else {
697                     LOGGER.debug("Invalid maven identifier identified: " + originalGroupID + ":" + originalArtifactID);
698                 }
699             } catch (MalformedPackageURLException ex) {
700                 final String gav = String.format("%s:%s:%s", originalGroupID, originalArtifactID, version);
701                 LOGGER.debug("Error building package url for " + gav + "; using generic identifier instead.", ex);
702                 id = new GenericIdentifier("maven:" + gav, Confidence.HIGH);
703             }
704             if (id != null) {
705                 dependency.addSoftwareIdentifier(id);
706             }
707         }
708 
709         // org name
710         final String org = pom.getOrganization();
711         if (org != null && !org.isEmpty()) {
712             dependency.addEvidence(EvidenceType.VENDOR, "pom", "organization name", org, Confidence.HIGH);
713             dependency.addEvidence(EvidenceType.PRODUCT, "pom", "organization name", org, Confidence.LOW);
714             addMatchingValues(classes, org, dependency, EvidenceType.VENDOR);
715             addMatchingValues(classes, org, dependency, EvidenceType.PRODUCT);
716         }
717         // org name
718         String orgUrl = pom.getOrganizationUrl();
719         if (orgUrl != null && !orgUrl.isEmpty()) {
720             if (orgUrl.startsWith("https://github.com/") || orgUrl.startsWith("https://gitlab.com/")) {
721                 orgUrl = orgUrl.substring(19);
722                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "url", orgUrl, Confidence.HIGH);
723             } else {
724                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "organization url", orgUrl, Confidence.LOW);
725             }
726             dependency.addEvidence(EvidenceType.VENDOR, "pom", "organization url", orgUrl, Confidence.MEDIUM);
727         }
728         //pom name
729         final String pomName = pom.getName();
730         if (pomName != null && !pomName.isEmpty() && !"${project.groupId}:${project.artifactId}".equals(pomName)) {
731             foundSomething = true;
732             dependency.addEvidence(EvidenceType.PRODUCT, "pom", "name", pomName, Confidence.HIGH);
733             dependency.addEvidence(EvidenceType.VENDOR, "pom", "name", pomName, Confidence.HIGH);
734             addMatchingValues(classes, pomName, dependency, EvidenceType.VENDOR);
735             addMatchingValues(classes, pomName, dependency, EvidenceType.PRODUCT);
736         }
737 
738         //Description
739         final String description = pom.getDescription();
740         if (description != null && !description.isEmpty()
741                 && !description.startsWith("POM was created by")
742                 && !description.startsWith("Sonatype helps open source projects")
743                 && !description.endsWith("project for Spring Boot")) {
744             foundSomething = true;
745             final String trimmedDescription = addDescription(dependency, description, "pom", "description");
746             addMatchingValues(classes, trimmedDescription, dependency, EvidenceType.VENDOR);
747             addMatchingValues(classes, trimmedDescription, dependency, EvidenceType.PRODUCT);
748         }
749 
750         String projectURL = pom.getProjectURL();
751         if (projectURL != null && !projectURL.trim().isEmpty()) {
752             if (projectURL.startsWith("https://github.com/") || projectURL.startsWith("https://gitlab.com/")) {
753                 projectURL = projectURL.substring(19);
754                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "url", projectURL, Confidence.HIGH);
755             } else {
756                 dependency.addEvidence(EvidenceType.PRODUCT, "pom", "url", projectURL, Confidence.MEDIUM);
757             }
758             dependency.addEvidence(EvidenceType.VENDOR, "pom", "url", projectURL, Confidence.HIGHEST);
759 
760         }
761 
762         if (pom.getDevelopers() != null && !pom.getDevelopers().isEmpty()) {
763             for (Developer dev : pom.getDevelopers()) {
764                 if (!Strings.isNullOrEmpty(dev.getId())) {
765                     dependency.addEvidence(EvidenceType.VENDOR, "pom", "developer id", dev.getId(), Confidence.MEDIUM);
766                     dependency.addEvidence(EvidenceType.PRODUCT, "pom", "developer id", dev.getId(), Confidence.LOW);
767                 }
768                 if (!Strings.isNullOrEmpty(dev.getName())) {
769                     dependency.addEvidence(EvidenceType.VENDOR, "pom", "developer name", dev.getName(), Confidence.MEDIUM);
770                     dependency.addEvidence(EvidenceType.PRODUCT, "pom", "developer name", dev.getName(), Confidence.LOW);
771                 }
772                 if (!Strings.isNullOrEmpty(dev.getEmail())) {
773                     dependency.addEvidence(EvidenceType.VENDOR, "pom", "developer email", dev.getEmail(), Confidence.LOW);
774                     dependency.addEvidence(EvidenceType.PRODUCT, "pom", "developer email", dev.getEmail(), Confidence.LOW);
775                 }
776                 if (!Strings.isNullOrEmpty(dev.getOrganizationUrl())) {
777                     dependency.addEvidence(EvidenceType.VENDOR, "pom", "developer org URL", dev.getOrganizationUrl(), Confidence.MEDIUM);
778                     dependency.addEvidence(EvidenceType.PRODUCT, "pom", "developer org URL", dev.getOrganizationUrl(), Confidence.LOW);
779                 }
780                 final String devOrg = dev.getOrganization();
781                 if (!Strings.isNullOrEmpty(devOrg)) {
782                     dependency.addEvidence(EvidenceType.VENDOR, "pom", "developer org", devOrg, Confidence.MEDIUM);
783                     dependency.addEvidence(EvidenceType.PRODUCT, "pom", "developer org", devOrg, Confidence.LOW);
784                     addMatchingValues(classes, devOrg, dependency, EvidenceType.VENDOR);
785                     addMatchingValues(classes, devOrg, dependency, EvidenceType.PRODUCT);
786                 }
787             }
788         }
789 
790         extractLicense(pom, dependency);
791         return foundSomething;
792     }
793 
794     /**
795      * Analyzes the path information of the classes contained within the
796      * JarAnalyzer to try and determine possible vendor or product names. If any
797      * are found they are stored in the packageVendor and packageProduct
798      * hashSets.
799      *
800      * @param classNames a list of class names
801      * @param dependency a dependency to analyze
802      * @param addPackagesAsEvidence a flag indicating whether or not package
803      * names should be added as evidence.
804      */
805     protected void analyzePackageNames(List<ClassNameInformation> classNames,
806             Dependency dependency, boolean addPackagesAsEvidence) {
807         final Map<String, Integer> vendorIdentifiers = new HashMap<>();
808         final Map<String, Integer> productIdentifiers = new HashMap<>();
809         analyzeFullyQualifiedClassNames(classNames, vendorIdentifiers, productIdentifiers);
810 
811         final int classCount = classNames.size();
812 
813         vendorIdentifiers.forEach((key, value) -> {
814             final float ratio = value / (float) classCount;
815             if (ratio > 0.5) {
816                 //TODO remove weighting?
817                 dependency.addVendorWeighting(key);
818                 if (addPackagesAsEvidence && key.length() > 1) {
819                     dependency.addEvidence(EvidenceType.VENDOR, "jar", "package name", key, Confidence.LOW);
820                 }
821             }
822         });
823         productIdentifiers.forEach((key, value) -> {
824             final float ratio = value / (float) classCount;
825             if (ratio > 0.5) {
826                 //todo remove weighting
827                 dependency.addProductWeighting(key);
828                 if (addPackagesAsEvidence && key.length() > 1) {
829                     dependency.addEvidence(EvidenceType.PRODUCT, "jar", "package name", key, Confidence.LOW);
830                 }
831             }
832         });
833     }
834 
835     /**
836      * <p>
837      * Reads the manifest from the JAR file and collects the entries. Some
838      * vendorKey entries are:</p>
839      * <ul><li>Implementation Title</li>
840      * <li>Implementation Version</li> <li>Implementation Vendor</li>
841      * <li>Implementation VendorId</li> <li>Bundle Name</li> <li>Bundle
842      * Version</li> <li>Bundle Vendor</li> <li>Bundle Description</li> <li>Main
843      * Class</li> </ul>
844      * However, all but a handful of specific entries are read in.
845      *
846      * @param dependency A reference to the dependency
847      * @param classInformation a collection of class information
848      * @return whether evidence was identified parsing the manifest
849      * @throws IOException if there is an issue reading the JAR file
850      */
851     //CSOFF: MethodLength
852     protected boolean parseManifest(Dependency dependency, List<ClassNameInformation> classInformation)
853             throws IOException {
854         boolean foundSomething = false;
855         try (JarFile jar = new JarFile(dependency.getActualFilePath(), false)) {
856             final Manifest manifest = jar.getManifest();
857             if (manifest == null) {
858                 if (!dependency.getFileName().toLowerCase().endsWith("-sources.jar")
859                         && !dependency.getFileName().toLowerCase().endsWith("-javadoc.jar")
860                         && !dependency.getFileName().toLowerCase().endsWith("-src.jar")
861                         && !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) {
862                     LOGGER.debug("Jar file '{}' does not contain a manifest.", dependency.getFileName());
863                 }
864                 return false;
865             }
866             String source = "Manifest";
867             String specificationVersion = null;
868             boolean hasImplementationVersion = false;
869             Attributes atts = manifest.getMainAttributes();
870             for (Entry<Object, Object> entry : atts.entrySet()) {
871                 String key = entry.getKey().toString();
872                 String value = atts.getValue(key);
873                 if (HTML_DETECTION_PATTERN.matcher(value).find()) {
874                     value = Jsoup.parse(value).text();
875                 }
876                 if (value.startsWith("git@github.com:") || value.startsWith("git@gitlab.com:")) {
877                     value = value.substring(15);
878                 }
879                 if (IGNORE_VALUES.contains(value)) {
880                     continue;
881                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) {
882                     foundSomething = true;
883                     dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.HIGH);
884                     addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
885                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) {
886                     hasImplementationVersion = true;
887                     foundSomething = true;
888                     dependency.addEvidence(EvidenceType.VERSION, source, key, value, Confidence.HIGH);
889                 } else if ("specification-version".equalsIgnoreCase(key)) {
890                     specificationVersion = value;
891                 } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) {
892                     foundSomething = true;
893                     dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.HIGH);
894                     addMatchingValues(classInformation, value, dependency, EvidenceType.VENDOR);
895                 } else if (key.equalsIgnoreCase(IMPLEMENTATION_VENDOR_ID)) {
896                     foundSomething = true;
897                     dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.MEDIUM);
898                     addMatchingValues(classInformation, value, dependency, EvidenceType.VENDOR);
899                 } else if (key.equalsIgnoreCase(BUNDLE_DESCRIPTION)) {
900                     if (!value.startsWith("Sonatype helps open source projects")) {
901                         foundSomething = true;
902                         addDescription(dependency, value, "manifest", key);
903                         addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
904                     }
905                 } else if (key.equalsIgnoreCase(BUNDLE_NAME)) {
906                     foundSomething = true;
907                     dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.MEDIUM);
908                     addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
909 //                //the following caused false positives.
910 //                } else if (key.equalsIgnoreCase(BUNDLE_VENDOR)) {
911                 } else if (key.equalsIgnoreCase(BUNDLE_VERSION)) {
912                     foundSomething = true;
913                     dependency.addEvidence(EvidenceType.VERSION, source, key, value, Confidence.HIGH);
914                 } else if (key.equalsIgnoreCase(Attributes.Name.MAIN_CLASS.toString())) {
915                     //noinspection UnnecessaryContinue
916                     continue;
917                     //skipping main class as if this has important information to add it will be added during class name analysis...
918                 } else if ("implementation-url".equalsIgnoreCase(key)
919                         && value != null
920                         && value.startsWith("https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/parent/")) {
921                     continue;
922                 } else {
923                     key = key.toLowerCase();
924                     if (!IGNORE_KEYS.contains(key)
925                             && !key.endsWith("jdk")
926                             && !key.contains("lastmodified")
927                             && !key.endsWith("package")
928                             && !key.endsWith("classpath")
929                             && !key.endsWith("class-path")
930                             && !key.endsWith("-scm") //todo change this to a regex?
931                             && !key.startsWith("scm-")
932                             && !value.trim().startsWith("scm:")
933                             && !isImportPackage(key, value)
934                             && !isPackage(key, value)) {
935                         foundSomething = true;
936                         if (key.contains("version")) {
937                             if (!key.contains("specification")) {
938                                 dependency.addEvidence(EvidenceType.VERSION, source, key, value, Confidence.MEDIUM);
939                             }
940                         } else if ("build-id".equals(key)) {
941                             int pos = value.indexOf('(');
942                             if (pos > 0) {
943                                 value = value.substring(0, pos - 1);
944                             }
945                             pos = value.indexOf('[');
946                             if (pos > 0) {
947                                 value = value.substring(0, pos - 1);
948                             }
949                             dependency.addEvidence(EvidenceType.VERSION, source, key, value, Confidence.MEDIUM);
950                         } else if (key.contains("title")) {
951                             dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.MEDIUM);
952                             addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
953                         } else if (key.contains("vendor")) {
954                             if (key.contains("specification")) {
955                                 dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.LOW);
956                             } else {
957                                 dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.MEDIUM);
958                                 addMatchingValues(classInformation, value, dependency, EvidenceType.VENDOR);
959                             }
960                         } else if (key.contains("name")) {
961                             dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.MEDIUM);
962                             dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.MEDIUM);
963                             addMatchingValues(classInformation, value, dependency, EvidenceType.VENDOR);
964                             addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
965                         } else if (key.contains("license")) {
966                             addLicense(dependency, value);
967                         } else if (key.contains("description")) {
968                             if (!value.startsWith("Sonatype helps open source projects")) {
969                                 final String trimmedDescription = addDescription(dependency, value, "manifest", key);
970                                 addMatchingValues(classInformation, trimmedDescription, dependency, EvidenceType.VENDOR);
971                                 addMatchingValues(classInformation, trimmedDescription, dependency, EvidenceType.PRODUCT);
972                             }
973                         } else {
974                             dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.LOW);
975                             dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.LOW);
976                             addMatchingValues(classInformation, value, dependency, EvidenceType.VERSION);
977                             addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
978                             if (value.matches(".*\\d.*")) {
979                                 final StringTokenizer tokenizer = new StringTokenizer(value, " ");
980                                 while (tokenizer.hasMoreElements()) {
981                                     final String s = tokenizer.nextToken();
982                                     if (s.matches("^[0-9.]+$")) {
983                                         dependency.addEvidence(EvidenceType.VERSION, source, key, s, Confidence.LOW);
984                                     }
985                                 }
986                             }
987                         }
988                     }
989                 }
990             }
991             for (Map.Entry<String, Attributes> item : manifest.getEntries().entrySet()) {
992                 final String name = item.getKey();
993                 source = "manifest: " + name;
994                 atts = item.getValue();
995                 for (Entry<Object, Object> entry : atts.entrySet()) {
996                     final String key = entry.getKey().toString();
997                     final String value = atts.getValue(key);
998                     if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) {
999                         foundSomething = true;
1000                         dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.MEDIUM);
1001                         addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
1002                     } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) {
1003                         foundSomething = true;
1004                         dependency.addEvidence(EvidenceType.VERSION, source, key, value, Confidence.MEDIUM);
1005                     } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) {
1006                         foundSomething = true;
1007                         dependency.addEvidence(EvidenceType.VENDOR, source, key, value, Confidence.MEDIUM);
1008                         addMatchingValues(classInformation, value, dependency, EvidenceType.VENDOR);
1009                     } else if (key.equalsIgnoreCase(Attributes.Name.SPECIFICATION_TITLE.toString())) {
1010                         foundSomething = true;
1011                         dependency.addEvidence(EvidenceType.PRODUCT, source, key, value, Confidence.MEDIUM);
1012                         addMatchingValues(classInformation, value, dependency, EvidenceType.PRODUCT);
1013                     }
1014                 }
1015             }
1016             if (specificationVersion != null && !hasImplementationVersion) {
1017                 foundSomething = true;
1018                 dependency.addEvidence(EvidenceType.VERSION, source, "specification-version", specificationVersion, Confidence.HIGH);
1019             }
1020         }
1021         return foundSomething;
1022     }
1023     //CSON: MethodLength
1024 
1025     /**
1026      * Adds a description to the given dependency. If the description contains
1027      * one of the following strings beyond 100 characters, then the description
1028      * used will be trimmed to that position:
1029      * <ul><li>"such as"</li><li>"like "</li><li>"will use "</li><li>"* uses
1030      * "</li></ul>
1031      *
1032      * @param dependency a dependency
1033      * @param description the description
1034      * @param source the source of the evidence
1035      * @param key the "name" of the evidence
1036      * @return if the description is trimmed, the trimmed version is returned;
1037      * otherwise the original description is returned
1038      */
1039     public static String addDescription(Dependency dependency, String description, String source, String key) {
1040         if (dependency.getDescription() == null) {
1041             dependency.setDescription(description);
1042         }
1043         String desc;
1044         if (HTML_DETECTION_PATTERN.matcher(description).find()) {
1045             desc = Jsoup.parse(description).text();
1046         } else {
1047             desc = description;
1048         }
1049         dependency.setDescription(desc);
1050         if (desc.length() > 100) {
1051             desc = desc.replaceAll("\\s\\s+", " ");
1052             final int posSuchAs = desc.toLowerCase().indexOf("such as ", 100);
1053             final int posLike = desc.toLowerCase().indexOf("like ", 100);
1054             final int posWillUse = desc.toLowerCase().indexOf("will use ", 100);
1055             final int posUses = desc.toLowerCase().indexOf(" uses ", 100);
1056 
1057             int pos = -1;
1058             pos = Math.max(pos, posSuchAs);
1059             if (pos >= 0 && posLike >= 0) {
1060                 pos = Math.min(pos, posLike);
1061             } else {
1062                 pos = Math.max(pos, posLike);
1063             }
1064             if (pos >= 0 && posWillUse >= 0) {
1065                 pos = Math.min(pos, posWillUse);
1066             } else {
1067                 pos = Math.max(pos, posWillUse);
1068             }
1069             if (pos >= 0 && posUses >= 0) {
1070                 pos = Math.min(pos, posUses);
1071             } else {
1072                 pos = Math.max(pos, posUses);
1073             }
1074             if (pos > 0) {
1075                 desc = desc.substring(0, pos) + "...";
1076             }
1077 //            //no longer add description directly. Use matching terms in other parts of the evidence collection
1078 //            //but description adds too many FP
1079 //            dependency.addEvidence(EvidenceType.PRODUCT, source, key, desc, Confidence.LOW);
1080 //            dependency.addEvidence(EvidenceType.VENDOR, source, key, desc, Confidence.LOW);
1081 //        } else {
1082 //            dependency.addEvidence(EvidenceType.PRODUCT, source, key, desc, Confidence.MEDIUM);
1083 //            dependency.addEvidence(EvidenceType.VENDOR, source, key, desc, Confidence.MEDIUM);
1084         }
1085         return desc;
1086     }
1087 
1088     /**
1089      * Adds a license to the given dependency.
1090      *
1091      * @param d a dependency
1092      * @param license the license
1093      */
1094     private void addLicense(Dependency d, String license) {
1095         if (d.getLicense() == null) {
1096             d.setLicense(license);
1097         } else if (!d.getLicense().contains(license)) {
1098             d.setLicense(d.getLicense() + NEWLINE + license);
1099         }
1100     }
1101 
1102     /**
1103      * Initializes the JarAnalyzer.
1104      *
1105      * @param engine a reference to the dependency-check engine
1106      * @throws InitializationException is thrown if there is an exception
1107      * creating a temporary directory
1108      */
1109     @Override
1110     public void prepareFileTypeAnalyzer(Engine engine) throws InitializationException {
1111         try {
1112             final File baseDir = getSettings().getTempDirectory();
1113             tempFileLocation = File.createTempFile("check", "tmp", baseDir);
1114             if (!tempFileLocation.delete()) {
1115                 final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
1116                 setEnabled(false);
1117                 throw new InitializationException(msg);
1118             }
1119             if (!tempFileLocation.mkdirs()) {
1120                 final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
1121                 setEnabled(false);
1122                 throw new InitializationException(msg);
1123             }
1124         } catch (IOException ex) {
1125             setEnabled(false);
1126             throw new InitializationException("Unable to create a temporary file", ex);
1127         }
1128     }
1129 
1130     /**
1131      * Deletes any files extracted from the JAR during analysis.
1132      */
1133     @Override
1134     public void closeAnalyzer() {
1135         if (tempFileLocation != null && tempFileLocation.exists()) {
1136             LOGGER.debug("Attempting to delete temporary files from `{}`", tempFileLocation.toString());
1137             final boolean success = FileUtils.delete(tempFileLocation);
1138             if (!success && tempFileLocation.exists()) {
1139                 final String[] l = tempFileLocation.list();
1140                 if (l != null && l.length > 0) {
1141                     LOGGER.warn("Failed to delete the JAR Analyzder's temporary files from `{}`, "
1142                             + "see the log for more details", tempFileLocation.getAbsolutePath());
1143                 }
1144             }
1145         }
1146     }
1147 
1148     /**
1149      * Determines if the key value pair from the manifest is for an "import"
1150      * type entry for package names.
1151      *
1152      * @param key the key from the manifest
1153      * @param value the value from the manifest
1154      * @return true or false depending on if it is believed the entry is an
1155      * "import" entry
1156      */
1157     private boolean isImportPackage(String key, String value) {
1158         final Pattern packageRx = Pattern.compile("^(\\s*[a-zA-Z0-9_#\\$\\*\\.]+\\s*[,;])+(\\s*[a-zA-Z0-9_#\\$\\*\\.]+\\s*)?$");
1159         final boolean matches = packageRx.matcher(value).matches();
1160         return matches && (key.contains("import") || key.contains("include") || value.length() > 10);
1161     }
1162 
1163     /**
1164      * Cycles through an enumeration of JarEntries, contained within the
1165      * dependency, and returns a list of the class names. This does not include
1166      * core Java package names (i.e. java.* or javax.*).
1167      *
1168      * @param dependency the dependency being analyzed
1169      * @return an list of fully qualified class names
1170      */
1171     protected List<ClassNameInformation> collectClassNames(Dependency dependency) {
1172         final List<ClassNameInformation> classNames = new ArrayList<>();
1173         try (JarFile jar = new JarFile(dependency.getActualFilePath(), false)) {
1174             final Enumeration<JarEntry> entries = jar.entries();
1175             while (entries.hasMoreElements()) {
1176                 final JarEntry entry = entries.nextElement();
1177                 final String name = entry.getName().toLowerCase();
1178                 //no longer stripping "|com\\.sun" - there are some com.sun jar files with CVEs.
1179                 if (name.endsWith(".class") && !name.matches("^javax?\\..*$")) {
1180                     final ClassNameInformation className = new ClassNameInformation(name.substring(0, name.length() - 6));
1181                     classNames.add(className);
1182                 }
1183             }
1184         } catch (IOException ex) {
1185             LOGGER.warn("Unable to open jar file '{}'.", dependency.getFileName());
1186             LOGGER.debug("", ex);
1187         }
1188         return classNames;
1189     }
1190 
1191     /**
1192      * Cycles through the list of class names and places the package levels 0-3
1193      * into the provided maps for vendor and product. This is helpful when
1194      * analyzing vendor/product as many times this is included in the package
1195      * name.
1196      *
1197      * @param classNames a list of class names
1198      * @param vendor HashMap of possible vendor names from package names (e.g.
1199      * owasp)
1200      * @param product HashMap of possible product names from package names (e.g.
1201      * dependencycheck)
1202      */
1203     private void analyzeFullyQualifiedClassNames(List<ClassNameInformation> classNames,
1204             Map<String, Integer> vendor, Map<String, Integer> product) {
1205         for (ClassNameInformation entry : classNames) {
1206             final List<String> list = entry.getPackageStructure();
1207             addEntry(vendor, list.get(0));
1208 
1209             if (list.size() == 2) {
1210                 addEntry(product, list.get(1));
1211             } else if (list.size() == 3) {
1212                 addEntry(vendor, list.get(1));
1213                 addEntry(product, list.get(1));
1214                 addEntry(product, list.get(2));
1215             } else if (list.size() >= 4) {
1216                 addEntry(vendor, list.get(1));
1217                 addEntry(vendor, list.get(2));
1218                 addEntry(product, list.get(1));
1219                 addEntry(product, list.get(2));
1220                 addEntry(product, list.get(3));
1221             }
1222         }
1223     }
1224 
1225     /**
1226      * Adds an entry to the specified collection and sets the Integer (e.g. the
1227      * count) to 1. If the entry already exists in the collection then the
1228      * Integer is incremented by 1.
1229      *
1230      * @param collection a collection of strings and their occurrence count
1231      * @param key the key to add to the collection
1232      */
1233     private void addEntry(Map<String, Integer> collection, String key) {
1234         if (collection.containsKey(key)) {
1235             collection.put(key, collection.get(key) + 1);
1236         } else {
1237             collection.put(key, 1);
1238         }
1239     }
1240 
1241     /**
1242      * Cycles through the collection of class name information to see if parts
1243      * of the package names are contained in the provided value. If found, it
1244      * will be added as the HIGHEST confidence evidence because we have more
1245      * then one source corroborating the value.
1246      *
1247      * @param classes a collection of class name information
1248      * @param value the value to check to see if it contains a package name
1249      * @param dep the dependency to add new entries too
1250      * @param type the type of evidence (vendor, product, or version)
1251      */
1252     protected static void addMatchingValues(List<ClassNameInformation> classes, String value, Dependency dep, EvidenceType type) {
1253         if (value == null || value.isEmpty() || classes == null || classes.isEmpty()) {
1254             return;
1255         }
1256         final HashSet<String> tested = new HashSet<>();
1257         //TODO add a hashSet and only analyze any given key once.
1258         for (ClassNameInformation cni : classes) {
1259             //classes.forEach((cni) -> {
1260             for (String key : cni.getPackageStructure()) {
1261                 //cni.getPackageStructure().forEach((key) -> {
1262                 if (!tested.contains(key)) {
1263                     tested.add(key);
1264                     final int pos = StringUtils.indexOfIgnoreCase(value, key);
1265                     if ((pos == 0 && (key.length() == value.length() || (key.length() < value.length()
1266                             && !Character.isLetterOrDigit(value.charAt(key.length())))))
1267                             || (pos > 0 && !Character.isLetterOrDigit(value.charAt(pos - 1))
1268                             && (pos + key.length() == value.length() || (key.length() < value.length()
1269                             && !Character.isLetterOrDigit(value.charAt(pos + key.length())))))) {
1270                         dep.addEvidence(type, "jar", "package name", key, Confidence.HIGHEST);
1271                     }
1272                 }
1273             }
1274         }
1275     }
1276 
1277     /**
1278      * Simple check to see if the attribute from a manifest is just a package
1279      * name.
1280      *
1281      * @param key the key of the value to check
1282      * @param value the value to check
1283      * @return true if the value looks like a java package name, otherwise false
1284      */
1285     private boolean isPackage(String key, String value) {
1286 
1287         return !key.matches(".*(version|title|vendor|name|license|description).*")
1288                 && value.matches("^[a-zA-Z_][a-zA-Z0-9_\\$]*\\.([a-zA-Z_][a-zA-Z0-9_\\$]*\\.)*([a-zA-Z_][a-zA-Z0-9_\\$]*)$");
1289 
1290     }
1291 
1292     /**
1293      * Returns null if the value starts with `${` and ends with `}`.
1294      *
1295      * @param value the value to check
1296      * @return the correct value which may be null
1297      */
1298     private static String intepolationFailCheck(String value) {
1299         if (value != null && value.contains("${")) {
1300             return null;
1301         }
1302         return value;
1303     }
1304 
1305     /**
1306      * Extracts the license information from the pom and adds it to the
1307      * dependency.
1308      *
1309      * @param pom the pom object
1310      * @param dependency the dependency to add license information too
1311      */
1312     public static void extractLicense(Model pom, Dependency dependency) {
1313         //license
1314         if (pom.getLicenses() != null) {
1315             StringBuilder license = null;
1316             for (License lic : pom.getLicenses()) {
1317                 String tmp = null;
1318                 if (lic.getName() != null) {
1319                     tmp = lic.getName();
1320                 }
1321                 if (lic.getUrl() != null) {
1322                     if (tmp == null) {
1323                         tmp = lic.getUrl();
1324                     } else {
1325                         tmp += ": " + lic.getUrl();
1326                     }
1327                 }
1328                 if (tmp == null) {
1329                     continue;
1330                 }
1331                 if (HTML_DETECTION_PATTERN.matcher(tmp).find()) {
1332                     tmp = Jsoup.parse(tmp).text();
1333                 }
1334                 if (license == null) {
1335                     license = new StringBuilder(tmp);
1336                 } else {
1337                     license.append("\n").append(tmp);
1338                 }
1339             }
1340             if (license != null) {
1341                 dependency.setLicense(license.toString());
1342 
1343             }
1344         }
1345     }
1346 
1347     /**
1348      * Stores information about a class name.
1349      */
1350     protected static class ClassNameInformation {
1351 
1352         /**
1353          * The fully qualified class name.
1354          */
1355         private String name;
1356         /**
1357          * Up to the first four levels of the package structure, excluding a
1358          * leading "org" or "com".
1359          */
1360         private final ArrayList<String> packageStructure = new ArrayList<>();
1361 
1362         /**
1363          * <p>
1364          * Stores information about a given class name. This class will keep the
1365          * fully qualified class name and a list of the important parts of the
1366          * package structure. Up to the first four levels of the package
1367          * structure are stored, excluding a leading "org" or "com".
1368          * Example:</p>
1369          * <code>ClassNameInformation obj = new ClassNameInformation("org/owasp/dependencycheck/analyzer/JarAnalyzer");
1370          * System.out.println(obj.getName());
1371          * for (String p : obj.getPackageStructure())
1372          *     System.out.println(p);
1373          * </code>
1374          * <p>
1375          * Would result in:</p>
1376          * <code>org.owasp.dependencycheck.analyzer.JarAnalyzer
1377          * owasp
1378          * dependencycheck
1379          * analyzer
1380          * jaranalyzer</code>
1381          *
1382          * @param className a fully qualified class name
1383          */
1384         ClassNameInformation(String className) {
1385             name = className;
1386             if (name.contains("/")) {
1387                 final String[] tmp = StringUtils.split(className.toLowerCase(), '/');
1388                 int start = 0;
1389                 int end = 3;
1390                 if ("com".equals(tmp[0]) || "org".equals(tmp[0])) {
1391                     start = 1;
1392                     end = 4;
1393                 }
1394                 if (tmp.length <= end) {
1395                     end = tmp.length - 1;
1396                 }
1397                 packageStructure.addAll(Arrays.asList(tmp).subList(start, end + 1));
1398             } else {
1399                 packageStructure.add(name);
1400             }
1401         }
1402 
1403         /**
1404          * Get the value of name
1405          *
1406          * @return the value of name
1407          */
1408         public String getName() {
1409             return name;
1410         }
1411 
1412         /**
1413          * Set the value of name
1414          *
1415          * @param name new value of name
1416          */
1417         public void setName(String name) {
1418             this.name = name;
1419         }
1420 
1421         /**
1422          * Get the value of packageStructure
1423          *
1424          * @return the value of packageStructure
1425          */
1426         public ArrayList<String> getPackageStructure() {
1427             return packageStructure;
1428         }
1429     }
1430 
1431     /**
1432      * Retrieves the next temporary directory to extract an archive too.
1433      *
1434      * @return a directory
1435      * @throws AnalysisException thrown if unable to create temporary directory
1436      */
1437     private File getNextTempDirectory() throws AnalysisException {
1438         final int dirCount = DIR_COUNT.incrementAndGet();
1439         final File directory = new File(tempFileLocation, String.valueOf(dirCount));
1440         //getting an exception for some directories not being able to be created; might be because the directory already exists?
1441         if (directory.exists()) {
1442             return getNextTempDirectory();
1443         }
1444         if (!directory.mkdirs()) {
1445             final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath());
1446             throw new AnalysisException(msg);
1447         }
1448         return directory;
1449     }
1450 }