Coverage Report - org.owasp.dependencycheck.analyzer.DependencyBundlingAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyBundlingAnalyzer
34%
48/141
28%
43/150
9.091
 
 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.File;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.ListIterator;
 24  
 import java.util.Set;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 import java.util.regex.Matcher;
 28  
 import java.util.regex.Pattern;
 29  
 import org.owasp.dependencycheck.Engine;
 30  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 31  
 import org.owasp.dependencycheck.dependency.Dependency;
 32  
 import org.owasp.dependencycheck.dependency.Identifier;
 33  
 import org.owasp.dependencycheck.utils.DependencyVersion;
 34  
 import org.owasp.dependencycheck.utils.DependencyVersionUtil;
 35  
 import org.owasp.dependencycheck.utils.LogUtils;
 36  
 
 37  
 /**
 38  
  * <p>
 39  
  * This analyzer ensures dependencies that should be grouped together, to remove excess noise from the report, are
 40  
  * grouped. An example would be Spring, Spring Beans, Spring MVC, etc. If they are all for the same version and have the
 41  
  * same relative path then these should be grouped into a single dependency under the core/main library.</p>
 42  
  * <p>
 43  
  * Note, this grouping only works on dependencies with identified CVE entries</p>
 44  
  *
 45  
  * @author Jeremy Long <jeremy.long@owasp.org>
 46  
  */
 47  
 public class DependencyBundlingAnalyzer extends AbstractAnalyzer implements Analyzer {
 48  
 
 49  
     /**
 50  
      * The Logger.
 51  
      */
 52  1
     private static final Logger LOGGER = Logger.getLogger(DependencyBundlingAnalyzer.class.getName());
 53  
 
 54  
     //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables">
 55  
     /**
 56  
      * A pattern for obtaining the first part of a filename.
 57  
      */
 58  1
     private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z]*");
 59  
     /**
 60  
      * a flag indicating if this analyzer has run. This analyzer only runs once.
 61  
      */
 62  
     private boolean analyzed = false;
 63  
     //</editor-fold>
 64  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 65  
     /**
 66  
      * The name of the analyzer.
 67  
      */
 68  
     private static final String ANALYZER_NAME = "Dependency Bundling Analyzer";
 69  
     /**
 70  
      * The phase that this analyzer is intended to run in.
 71  
      */
 72  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.PRE_FINDING_ANALYSIS;
 73  
 
 74  
     /**
 75  
      * Returns the name of the analyzer.
 76  
      *
 77  
      * @return the name of the analyzer.
 78  
      */
 79  
     public String getName() {
 80  5
         return ANALYZER_NAME;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Returns the phase that the analyzer is intended to run in.
 85  
      *
 86  
      * @return the phase that the analyzer is intended to run in.
 87  
      */
 88  
     public AnalysisPhase getAnalysisPhase() {
 89  2
         return ANALYSIS_PHASE;
 90  
     }
 91  
     //</editor-fold>
 92  
 
 93  
     /**
 94  
      * Analyzes a set of dependencies. If they have been found to have the same base path and the same set of
 95  
      * identifiers they are likely related. The related dependencies are bundled into a single reportable item.
 96  
      *
 97  
      * @param ignore this analyzer ignores the dependency being analyzed
 98  
      * @param engine the engine that is scanning the dependencies
 99  
      * @throws AnalysisException is thrown if there is an error reading the JAR file.
 100  
      */
 101  
     @Override
 102  
     public void analyze(Dependency ignore, Engine engine) throws AnalysisException {
 103  2
         if (!analyzed) {
 104  1
             analyzed = true;
 105  1
             final Set<Dependency> dependenciesToRemove = new HashSet<Dependency>();
 106  1
             final ListIterator<Dependency> mainIterator = engine.getDependencies().listIterator();
 107  
             //for (Dependency nextDependency : engine.getDependencies()) {
 108  3
             while (mainIterator.hasNext()) {
 109  2
                 final Dependency dependency = mainIterator.next();
 110  2
                 if (mainIterator.hasNext()) {
 111  1
                     final ListIterator<Dependency> subIterator = engine.getDependencies().listIterator(mainIterator.nextIndex());
 112  2
                     while (subIterator.hasNext()) {
 113  1
                         final Dependency nextDependency = subIterator.next();
 114  1
                         if (hashesMatch(dependency, nextDependency)) {
 115  0
                             if (isCore(dependency, nextDependency)) {
 116  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 117  
                             } else {
 118  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 119  
                             }
 120  1
                         } else if (isShadedJar(dependency, nextDependency)) {
 121  0
                             if (dependency.getFileName().toLowerCase().endsWith("pom.xml")) {
 122  0
                                 dependenciesToRemove.add(dependency);
 123  
                             } else {
 124  0
                                 dependenciesToRemove.add(nextDependency);
 125  
                             }
 126  1
                         } else if (cpeIdentifiersMatch(dependency, nextDependency)
 127  
                                 && hasSameBasePath(dependency, nextDependency)
 128  
                                 && fileNameMatch(dependency, nextDependency)) {
 129  
 
 130  0
                             if (isCore(dependency, nextDependency)) {
 131  0
                                 mergeDependencies(dependency, nextDependency, dependenciesToRemove);
 132  
                             } else {
 133  0
                                 mergeDependencies(nextDependency, dependency, dependenciesToRemove);
 134  
                             }
 135  
                         }
 136  1
                     }
 137  
                 }
 138  2
             }
 139  
             //removing dependencies here as ensuring correctness and avoiding ConcurrentUpdateExceptions
 140  
             // was difficult because of the inner iterator.
 141  1
             for (Dependency d : dependenciesToRemove) {
 142  0
                 engine.getDependencies().remove(d);
 143  0
             }
 144  
         }
 145  2
     }
 146  
 
 147  
     /**
 148  
      * Adds the relatedDependency to the dependency's related dependencies.
 149  
      *
 150  
      * @param dependency the main dependency
 151  
      * @param relatedDependency a collection of dependencies to be removed from the main analysis loop, this is the
 152  
      * source of dependencies to remove
 153  
      * @param dependenciesToRemove a collection of dependencies that will be removed from the main analysis loop, this
 154  
      * function adds to this collection
 155  
      */
 156  
     private void mergeDependencies(final Dependency dependency, final Dependency relatedDependency, final Set<Dependency> dependenciesToRemove) {
 157  0
         dependency.addRelatedDependency(relatedDependency);
 158  0
         final Iterator<Dependency> i = relatedDependency.getRelatedDependencies().iterator();
 159  0
         while (i.hasNext()) {
 160  0
             dependency.addRelatedDependency(i.next());
 161  0
             i.remove();
 162  
         }
 163  0
         dependenciesToRemove.add(relatedDependency);
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Attempts to trim a maven repo to a common base path. This is typically
 168  
      * [drive]\[repo_location]\repository\[path1]\[path2].
 169  
      *
 170  
      * @param path the path to trim
 171  
      * @return a string representing the base path.
 172  
      */
 173  
     private String getBaseRepoPath(final String path) {
 174  0
         int pos = path.indexOf("repository" + File.separator) + 11;
 175  0
         if (pos < 0) {
 176  0
             return path;
 177  
         }
 178  0
         int tmp = path.indexOf(File.separator, pos);
 179  0
         if (tmp <= 0) {
 180  0
             return path;
 181  
         }
 182  0
         if (tmp > 0) {
 183  0
             pos = tmp + 1;
 184  
         }
 185  0
         tmp = path.indexOf(File.separator, pos);
 186  0
         if (tmp > 0) {
 187  0
             pos = tmp + 1;
 188  
         }
 189  0
         return path.substring(0, pos);
 190  
     }
 191  
 
 192  
     /**
 193  
      * Returns true if the file names (and version if it exists) of the two dependencies are sufficiently similar.
 194  
      *
 195  
      * @param dependency1 a dependency2 to compare
 196  
      * @param dependency2 a dependency2 to compare
 197  
      * @return true if the identifiers in the two supplied dependencies are equal
 198  
      */
 199  
     private boolean fileNameMatch(Dependency dependency1, Dependency dependency2) {
 200  0
         if (dependency1 == null || dependency1.getFileName() == null
 201  
                 || dependency2 == null || dependency2.getFileName() == null) {
 202  0
             return false;
 203  
         }
 204  0
         String fileName1 = dependency1.getFileName();
 205  0
         String fileName2 = dependency2.getFileName();
 206  
 
 207  
         //update to deal with archive analyzer, the starting name maybe the same
 208  
         // as this is incorrectly looking at the starting path
 209  0
         final File one = new File(fileName1);
 210  0
         final File two = new File(fileName2);
 211  0
         final String oneParent = one.getParent();
 212  0
         final String twoParent = two.getParent();
 213  0
         if (oneParent != null) {
 214  0
             if (oneParent.equals(twoParent)) {
 215  0
                 fileName1 = one.getName();
 216  0
                 fileName2 = two.getName();
 217  
             } else {
 218  0
                 return false;
 219  
             }
 220  0
         } else if (twoParent != null) {
 221  0
             return false;
 222  
         }
 223  
 
 224  
         //version check
 225  0
         final DependencyVersion version1 = DependencyVersionUtil.parseVersion(fileName1);
 226  0
         final DependencyVersion version2 = DependencyVersionUtil.parseVersion(fileName2);
 227  0
         if (version1 != null && version2 != null) {
 228  0
             if (!version1.equals(version2)) {
 229  0
                 return false;
 230  
             }
 231  
         }
 232  
 
 233  
         //filename check
 234  0
         final Matcher match1 = STARTING_TEXT_PATTERN.matcher(fileName1);
 235  0
         final Matcher match2 = STARTING_TEXT_PATTERN.matcher(fileName2);
 236  0
         if (match1.find() && match2.find()) {
 237  0
             return match1.group().equals(match2.group());
 238  
         }
 239  
 
 240  0
         return false;
 241  
     }
 242  
 
 243  
     /**
 244  
      * Returns true if the CPE identifiers in the two supplied dependencies are equal.
 245  
      *
 246  
      * @param dependency1 a dependency2 to compare
 247  
      * @param dependency2 a dependency2 to compare
 248  
      * @return true if the identifiers in the two supplied dependencies are equal
 249  
      */
 250  
     private boolean cpeIdentifiersMatch(Dependency dependency1, Dependency dependency2) {
 251  1
         if (dependency1 == null || dependency1.getIdentifiers() == null
 252  
                 || dependency2 == null || dependency2.getIdentifiers() == null) {
 253  0
             return false;
 254  
         }
 255  1
         boolean matches = false;
 256  1
         int cpeCount1 = 0;
 257  1
         int cpeCount2 = 0;
 258  1
         for (Identifier i : dependency1.getIdentifiers()) {
 259  0
             if ("cpe".equals(i.getType())) {
 260  0
                 cpeCount1 += 1;
 261  
             }
 262  0
         }
 263  1
         for (Identifier i : dependency2.getIdentifiers()) {
 264  2
             if ("cpe".equals(i.getType())) {
 265  2
                 cpeCount2 += 1;
 266  
             }
 267  2
         }
 268  1
         if (cpeCount1 > 0 && cpeCount1 == cpeCount2) {
 269  0
             for (Identifier i : dependency1.getIdentifiers()) {
 270  0
                 matches |= dependency2.getIdentifiers().contains(i);
 271  0
                 if (!matches) {
 272  0
                     break;
 273  
                 }
 274  0
             }
 275  
         }
 276  1
         if (LogUtils.isVerboseLoggingEnabled()) {
 277  0
             final String msg = String.format("IdentifiersMatch=%s (%s, %s)", matches, dependency1.getFileName(), dependency2.getFileName());
 278  0
             LOGGER.log(Level.FINE, msg);
 279  
         }
 280  1
         return matches;
 281  
     }
 282  
 
 283  
     /**
 284  
      * Determines if the two dependencies have the same base path.
 285  
      *
 286  
      * @param dependency1 a Dependency object
 287  
      * @param dependency2 a Dependency object
 288  
      * @return true if the base paths of the dependencies are identical
 289  
      */
 290  
     private boolean hasSameBasePath(Dependency dependency1, Dependency dependency2) {
 291  0
         if (dependency1 == null || dependency2 == null) {
 292  0
             return false;
 293  
         }
 294  0
         final File lFile = new File(dependency1.getFilePath());
 295  0
         String left = lFile.getParent();
 296  0
         final File rFile = new File(dependency2.getFilePath());
 297  0
         String right = rFile.getParent();
 298  0
         if (left == null) {
 299  0
             return right == null;
 300  
         }
 301  0
         if (left.equalsIgnoreCase(right)) {
 302  0
             return true;
 303  
         }
 304  0
         if (left.matches(".*[/\\\\]repository[/\\\\].*") && right.matches(".*[/\\\\]repository[/\\\\].*")) {
 305  0
             left = getBaseRepoPath(left);
 306  0
             right = getBaseRepoPath(right);
 307  
         }
 308  0
         if (left.equalsIgnoreCase(right)) {
 309  0
             return true;
 310  
         }
 311  
         //new code
 312  0
         for (Dependency child : dependency2.getRelatedDependencies()) {
 313  0
             if (hasSameBasePath(dependency1, child)) {
 314  0
                 return true;
 315  
             }
 316  0
         }
 317  0
         return false;
 318  
     }
 319  
 
 320  
     /**
 321  
      * This is likely a very broken attempt at determining if the 'left' dependency is the 'core' library in comparison
 322  
      * to the 'right' library.
 323  
      *
 324  
      * @param left the dependency to test
 325  
      * @param right the dependency to test against
 326  
      * @return a boolean indicating whether or not the left dependency should be considered the "core" version.
 327  
      */
 328  
     boolean isCore(Dependency left, Dependency right) {
 329  2
         final String leftName = left.getFileName().toLowerCase();
 330  2
         final String rightName = right.getFileName().toLowerCase();
 331  
 
 332  
         final boolean returnVal;
 333  2
         if (!rightName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+") && leftName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+")
 334  
                 || rightName.contains("core") && !leftName.contains("core")
 335  
                 || rightName.contains("kernel") && !leftName.contains("kernel")) {
 336  0
             returnVal = false;
 337  2
         } else if (rightName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+") && !leftName.matches(".*\\.(tar|tgz|gz|zip|ear|war).+")
 338  
                 || !rightName.contains("core") && leftName.contains("core")
 339  
                 || !rightName.contains("kernel") && leftName.contains("kernel")) {
 340  2
             returnVal = true;
 341  
         } else {
 342  
             /*
 343  
              * considered splitting the names up and comparing the components,
 344  
              * but decided that the file name length should be sufficient as the
 345  
              * "core" component, if this follows a normal naming protocol should
 346  
              * be shorter:
 347  
              * axis2-saaj-1.4.1.jar
 348  
              * axis2-1.4.1.jar       <-----
 349  
              * axis2-kernel-1.4.1.jar
 350  
              */
 351  0
             returnVal = leftName.length() <= rightName.length();
 352  
         }
 353  2
         if (LogUtils.isVerboseLoggingEnabled()) {
 354  0
             final String msg = String.format("IsCore=%s (%s, %s)", returnVal, left.getFileName(), right.getFileName());
 355  0
             LOGGER.log(Level.FINE, msg);
 356  
         }
 357  2
         return returnVal;
 358  
     }
 359  
 
 360  
     /**
 361  
      * Compares the SHA1 hashes of two dependencies to determine if they are equal.
 362  
      *
 363  
      * @param dependency1 a dependency object to compare
 364  
      * @param dependency2 a dependency object to compare
 365  
      * @return true if the sha1 hashes of the two dependencies match; otherwise false
 366  
      */
 367  
     private boolean hashesMatch(Dependency dependency1, Dependency dependency2) {
 368  1
         if (dependency1 == null || dependency2 == null || dependency1.getSha1sum() == null || dependency2.getSha1sum() == null) {
 369  0
             return false;
 370  
         }
 371  1
         return dependency1.getSha1sum().equals(dependency2.getSha1sum());
 372  
     }
 373  
 
 374  
     /**
 375  
      * Determines if the jar is shaded and the created pom.xml identified the same CPE as the jar - if so, the pom.xml
 376  
      * dependency should be removed.
 377  
      *
 378  
      * @param dependency a dependency to check
 379  
      * @param nextDependency another dependency to check
 380  
      * @return true if on of the dependencies is a pom.xml and the identifiers between the two collections match;
 381  
      * otherwise false
 382  
      */
 383  
     private boolean isShadedJar(Dependency dependency, Dependency nextDependency) {
 384  1
         final String mainName = dependency.getFileName().toLowerCase();
 385  1
         final String nextName = nextDependency.getFileName().toLowerCase();
 386  1
         if (mainName.endsWith(".jar") && nextName.endsWith("pom.xml")) {
 387  0
             return dependency.getIdentifiers().containsAll(nextDependency.getIdentifiers());
 388  1
         } else if (nextName.endsWith(".jar") && mainName.endsWith("pom.xml")) {
 389  0
             return nextDependency.getIdentifiers().containsAll(dependency.getIdentifiers());
 390  
         }
 391  1
         return false;
 392  
     }
 393  
 }