| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DependencyBundlingAnalyzer |
|
| 7.1;7.1 |
| 1 | /* | |
| 2 | * This file is part of dependency-check-core. | |
| 3 | * | |
| 4 | * Dependency-check-core is free software: you can redistribute it and/or modify it | |
| 5 | * under the terms of the GNU General Public License as published by the Free | |
| 6 | * Software Foundation, either version 3 of the License, or (at your option) any | |
| 7 | * later version. | |
| 8 | * | |
| 9 | * Dependency-check-core is distributed in the hope that it will be useful, but | |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
| 12 | * details. | |
| 13 | * | |
| 14 | * You should have received a copy of the GNU General Public License along with | |
| 15 | * dependency-check-core. If not, see http://www.gnu.org/licenses/. | |
| 16 | * | |
| 17 | * Copyright (c) 2012 Jeremy Long. All Rights Reserved. | |
| 18 | */ | |
| 19 | package org.owasp.dependencycheck.analyzer; | |
| 20 | ||
| 21 | import java.io.File; | |
| 22 | import java.util.HashSet; | |
| 23 | import java.util.Iterator; | |
| 24 | import java.util.ListIterator; | |
| 25 | import java.util.Set; | |
| 26 | import java.util.regex.Matcher; | |
| 27 | import java.util.regex.Pattern; | |
| 28 | import org.owasp.dependencycheck.Engine; | |
| 29 | import org.owasp.dependencycheck.dependency.Dependency; | |
| 30 | import org.owasp.dependencycheck.utils.DependencyVersion; | |
| 31 | import org.owasp.dependencycheck.utils.DependencyVersionUtil; | |
| 32 | ||
| 33 | /** | |
| 34 | * <p>This analyzer ensures dependencies that should be grouped together, to | |
| 35 | * remove excess noise from the report, are grouped. An example would be Spring, | |
| 36 | * Spring Beans, Spring MVC, etc. If they are all for the same version and have | |
| 37 | * the same relative path then these should be grouped into a single dependency | |
| 38 | * under the core/main library.</p> | |
| 39 | * <p>Note, this grouping only works on dependencies with identified CVE | |
| 40 | * entries</p> | |
| 41 | * | |
| 42 | * @author Jeremy Long (jeremy.long@owasp.org) | |
| 43 | */ | |
| 44 | 3 | public class DependencyBundlingAnalyzer extends AbstractAnalyzer implements Analyzer { |
| 45 | ||
| 46 | //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables"> | |
| 47 | /** | |
| 48 | * A pattern for obtaining the first part of a filename. | |
| 49 | */ | |
| 50 | 3 | private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z]*"); |
| 51 | /** | |
| 52 | * a flag indicating if this analyzer has run. This analyzer only runs once. | |
| 53 | */ | |
| 54 | 3 | private boolean analyzed = false; |
| 55 | //</editor-fold> | |
| 56 | //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer"> | |
| 57 | /** | |
| 58 | * The set of file extensions supported by this analyzer. | |
| 59 | */ | |
| 60 | 3 | private static final Set<String> EXTENSIONS = null; |
| 61 | /** | |
| 62 | * The name of the analyzer. | |
| 63 | */ | |
| 64 | private static final String ANALYZER_NAME = "Dependency Bundling Analyzer"; | |
| 65 | /** | |
| 66 | * The phase that this analyzer is intended to run in. | |
| 67 | */ | |
| 68 | 3 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.PRE_FINDING_ANALYSIS; |
| 69 | ||
| 70 | /** | |
| 71 | * Returns a list of file EXTENSIONS supported by this analyzer. | |
| 72 | * | |
| 73 | * @return a list of file EXTENSIONS supported by this analyzer. | |
| 74 | */ | |
| 75 | public Set<String> getSupportedExtensions() { | |
| 76 | 17289 | return EXTENSIONS; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Returns the name of the analyzer. | |
| 81 | * | |
| 82 | * @return the name of the analyzer. | |
| 83 | */ | |
| 84 | public String getName() { | |
| 85 | 0 | return ANALYZER_NAME; |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Returns whether or not this analyzer can process the given extension. | |
| 90 | * | |
| 91 | * @param extension the file extension to test for support | |
| 92 | * @return whether or not the specified file extension is supported by this | |
| 93 | * analyzer. | |
| 94 | */ | |
| 95 | public boolean supportsExtension(String extension) { | |
| 96 | 0 | return true; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Returns the phase that the analyzer is intended to run in. | |
| 101 | * | |
| 102 | * @return the phase that the analyzer is intended to run in. | |
| 103 | */ | |
| 104 | public AnalysisPhase getAnalysisPhase() { | |
| 105 | 3 | return ANALYSIS_PHASE; |
| 106 | } | |
| 107 | //</editor-fold> | |
| 108 | ||
| 109 | /** | |
| 110 | * Analyzes a set of dependencies. If they have been found to have the same | |
| 111 | * base path and the same set of identifiers they are likely related. The | |
| 112 | * related dependencies are bundled into a single reportable item. | |
| 113 | * | |
| 114 | * @param ignore this analyzer ignores the dependency being analyzed | |
| 115 | * @param engine the engine that is scanning the dependencies | |
| 116 | * @throws AnalysisException is thrown if there is an error reading the JAR | |
| 117 | * file. | |
| 118 | */ | |
| 119 | @Override | |
| 120 | public void analyze(Dependency ignore, Engine engine) throws AnalysisException { | |
| 121 | 0 | if (!analyzed) { |
| 122 | 0 | analyzed = true; |
| 123 | 0 | final Set<Dependency> dependenciesToRemove = new HashSet<Dependency>(); |
| 124 | 0 | final ListIterator<Dependency> mainIterator = engine.getDependencies().listIterator(); |
| 125 | //for (Dependency nextDependency : engine.getDependencies()) { | |
| 126 | 0 | while (mainIterator.hasNext()) { |
| 127 | 0 | final Dependency dependency = mainIterator.next(); |
| 128 | 0 | if (mainIterator.hasNext()) { |
| 129 | 0 | final ListIterator<Dependency> subIterator = engine.getDependencies().listIterator(mainIterator.nextIndex()); |
| 130 | 0 | while (subIterator.hasNext()) { |
| 131 | 0 | final Dependency nextDependency = subIterator.next(); |
| 132 | ||
| 133 | 0 | if (identifiersMatch(dependency, nextDependency) |
| 134 | && hasSameBasePath(dependency, nextDependency) | |
| 135 | && fileNameMatch(dependency, nextDependency)) { | |
| 136 | ||
| 137 | 0 | if (isCore(dependency, nextDependency)) { |
| 138 | 0 | dependency.addRelatedDependency(nextDependency); |
| 139 | //move any "related dependencies" to the new "parent" dependency | |
| 140 | 0 | final Iterator<Dependency> i = nextDependency.getRelatedDependencies().iterator(); |
| 141 | 0 | while (i.hasNext()) { |
| 142 | 0 | dependency.addRelatedDependency(i.next()); |
| 143 | 0 | i.remove(); |
| 144 | } | |
| 145 | 0 | dependenciesToRemove.add(nextDependency); |
| 146 | 0 | } else { |
| 147 | 0 | if (isCore(nextDependency, dependency)) { |
| 148 | 0 | nextDependency.addRelatedDependency(dependency); |
| 149 | //move any "related dependencies" to the new "parent" dependency | |
| 150 | 0 | final Iterator<Dependency> i = dependency.getRelatedDependencies().iterator(); |
| 151 | 0 | while (i.hasNext()) { |
| 152 | 0 | nextDependency.addRelatedDependency(i.next()); |
| 153 | 0 | i.remove(); |
| 154 | } | |
| 155 | 0 | dependenciesToRemove.add(dependency); |
| 156 | } | |
| 157 | } | |
| 158 | } | |
| 159 | 0 | } |
| 160 | } | |
| 161 | 0 | } |
| 162 | //removing dependencies here as ensuring correctness and avoiding ConcurrentUpdateExceptions | |
| 163 | // was difficult because of the inner iterator. | |
| 164 | 0 | for (Dependency d : dependenciesToRemove) { |
| 165 | 0 | engine.getDependencies().remove(d); |
| 166 | } | |
| 167 | } | |
| 168 | 0 | } |
| 169 | ||
| 170 | /** | |
| 171 | * Attempts to trim a maven repo to a common base path. This is typically | |
| 172 | * [drive]\[repo_location]\repository\[path1]\[path2]. | |
| 173 | * | |
| 174 | * @param path the path to trim | |
| 175 | * @return a string representing the base path. | |
| 176 | */ | |
| 177 | private String getBaseRepoPath(final String path) { | |
| 178 | 0 | int pos = path.indexOf("repository" + File.separator) + 11; |
| 179 | 0 | if (pos < 0) { |
| 180 | 0 | return path; |
| 181 | } | |
| 182 | 0 | int tmp = path.indexOf(File.separator, pos); |
| 183 | 0 | if (tmp <= 0) { |
| 184 | 0 | return path; |
| 185 | } | |
| 186 | 0 | if (tmp > 0) { |
| 187 | 0 | pos = tmp + 1; |
| 188 | } | |
| 189 | 0 | tmp = path.indexOf(File.separator, pos); |
| 190 | 0 | if (tmp > 0) { |
| 191 | 0 | pos = tmp + 1; |
| 192 | } | |
| 193 | 0 | return path.substring(0, pos); |
| 194 | } | |
| 195 | ||
| 196 | /** | |
| 197 | * Returns true if the file names (and version if it exists) of the two | |
| 198 | * dependencies are sufficiently similiar. | |
| 199 | * | |
| 200 | * @param dependency1 a dependency2 to compare | |
| 201 | * @param dependency2 a dependency2 to compare | |
| 202 | * @return true if the identifiers in the two supplied dependencies are | |
| 203 | * equal | |
| 204 | */ | |
| 205 | private boolean fileNameMatch(Dependency dependency1, Dependency dependency2) { | |
| 206 | 0 | if (dependency1 == null || dependency1.getFileName() == null |
| 207 | || dependency2 == null || dependency2.getFileName() == null) { | |
| 208 | 0 | return false; |
| 209 | } | |
| 210 | 0 | String fileName1 = dependency1.getFileName(); |
| 211 | 0 | String fileName2 = dependency2.getFileName(); |
| 212 | ||
| 213 | //update to deal with archive analyzer, the starting name maybe the same | |
| 214 | // as this is incorrectly looking at the starting path | |
| 215 | 0 | final File one = new File(fileName1); |
| 216 | 0 | final File two = new File(fileName2); |
| 217 | 0 | final String oneParent = one.getParent(); |
| 218 | 0 | final String twoParent = two.getParent(); |
| 219 | 0 | if (oneParent != null) { |
| 220 | 0 | if (twoParent != null && oneParent.equals(twoParent)) { |
| 221 | 0 | fileName1 = one.getName(); |
| 222 | 0 | fileName2 = two.getName(); |
| 223 | } else { | |
| 224 | 0 | return false; |
| 225 | } | |
| 226 | 0 | } else if (twoParent != null) { |
| 227 | 0 | return false; |
| 228 | } | |
| 229 | ||
| 230 | //version check | |
| 231 | 0 | final DependencyVersion version1 = DependencyVersionUtil.parseVersion(fileName1); |
| 232 | 0 | final DependencyVersion version2 = DependencyVersionUtil.parseVersion(fileName2); |
| 233 | 0 | if (version1 != null && version2 != null) { |
| 234 | 0 | if (!version1.equals(version2)) { |
| 235 | 0 | return false; |
| 236 | } | |
| 237 | } | |
| 238 | ||
| 239 | //filename check | |
| 240 | 0 | final Matcher match1 = STARTING_TEXT_PATTERN.matcher(fileName1); |
| 241 | 0 | final Matcher match2 = STARTING_TEXT_PATTERN.matcher(fileName2); |
| 242 | 0 | if (match1.find() && match2.find()) { |
| 243 | 0 | return match1.group().equals(match2.group()); |
| 244 | } | |
| 245 | ||
| 246 | 0 | return false; |
| 247 | } | |
| 248 | ||
| 249 | /** | |
| 250 | * Returns true if the identifiers in the two supplied dependencies are | |
| 251 | * equal. | |
| 252 | * | |
| 253 | * @param dependency1 a dependency2 to compare | |
| 254 | * @param dependency2 a dependency2 to compare | |
| 255 | * @return true if the identifiers in the two supplied dependencies are | |
| 256 | * equal | |
| 257 | */ | |
| 258 | private boolean identifiersMatch(Dependency dependency1, Dependency dependency2) { | |
| 259 | 0 | if (dependency1 == null || dependency1.getIdentifiers() == null |
| 260 | || dependency2 == null || dependency2.getIdentifiers() == null) { | |
| 261 | 0 | return false; |
| 262 | } | |
| 263 | 0 | return dependency1.getIdentifiers().size() > 0 |
| 264 | && dependency2.getIdentifiers().equals(dependency1.getIdentifiers()); | |
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * Determines if the two dependencies have the same base path. | |
| 269 | * | |
| 270 | * @param dependency1 a Dependency object | |
| 271 | * @param dependency2 a Dependency object | |
| 272 | * @return true if the base paths of the dependencies are identical | |
| 273 | */ | |
| 274 | private boolean hasSameBasePath(Dependency dependency1, Dependency dependency2) { | |
| 275 | 0 | if (dependency1 == null || dependency2 == null) { |
| 276 | 0 | return false; |
| 277 | } | |
| 278 | 0 | final File lFile = new File(dependency1.getFilePath()); |
| 279 | 0 | String left = lFile.getParent(); |
| 280 | 0 | final File rFile = new File(dependency2.getFilePath()); |
| 281 | 0 | String right = rFile.getParent(); |
| 282 | 0 | if (left == null) { |
| 283 | 0 | if (right == null) { |
| 284 | 0 | return true; |
| 285 | } | |
| 286 | 0 | return false; |
| 287 | } | |
| 288 | 0 | if (left.equalsIgnoreCase(right)) { |
| 289 | 0 | return true; |
| 290 | } | |
| 291 | 0 | if (left.matches(".*[/\\\\]repository[/\\\\].*") && right.matches(".*[/\\\\]repository[/\\\\].*")) { |
| 292 | 0 | left = getBaseRepoPath(left); |
| 293 | 0 | right = getBaseRepoPath(right); |
| 294 | } | |
| 295 | 0 | return left.equalsIgnoreCase(right); |
| 296 | } | |
| 297 | ||
| 298 | /** | |
| 299 | * This is likely a very broken attempt at determining if the 'left' | |
| 300 | * dependency is the 'core' library in comparison to the 'right' library. | |
| 301 | * | |
| 302 | * TODO - consider splitting on /\._-\s/ and checking if all of one side is | |
| 303 | * fully contained in the other With the exception of the word "core". This | |
| 304 | * might work even on groups when we don't have a CVE. | |
| 305 | * | |
| 306 | * @param left the dependency to test | |
| 307 | * @param right the dependency to test against | |
| 308 | * @return a boolean indicating whether or not the left dependency should be | |
| 309 | * considered the "core" version. | |
| 310 | */ | |
| 311 | private boolean isCore(Dependency left, Dependency right) { | |
| 312 | 0 | final String leftName = left.getFileName().toLowerCase(); |
| 313 | 0 | final String rightName = right.getFileName().toLowerCase(); |
| 314 | ||
| 315 | 0 | if (rightName.contains("core") && !leftName.contains("core")) { |
| 316 | 0 | return false; |
| 317 | 0 | } else if (!rightName.contains("core") && leftName.contains("core")) { |
| 318 | 0 | return true; |
| 319 | } else { | |
| 320 | //TODO should we be splitting the name on [-_(.\d)+] and seeing if the | |
| 321 | // parts are contained in the other side? | |
| 322 | 0 | if (leftName.length() > rightName.length()) { |
| 323 | 0 | return false; |
| 324 | } | |
| 325 | 0 | return true; |
| 326 | } | |
| 327 | } | |
| 328 | } |