| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class DependencyBundlingAnalyzer extends AbstractAnalyzer implements Analyzer { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 1 | private static final Logger LOGGER = Logger.getLogger(DependencyBundlingAnalyzer.class.getName()); |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 1 | private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z]*"); |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private boolean analyzed = false; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private static final String ANALYZER_NAME = "Dependency Bundling Analyzer"; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.PRE_FINDING_ANALYSIS; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public String getName() { |
| 80 | 5 | return ANALYZER_NAME; |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public AnalysisPhase getAnalysisPhase() { |
| 89 | 2 | return ANALYSIS_PHASE; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 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 | |
|
| 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 (firstPathIsShortest(dependency.getFilePath(), nextDependency.getFilePath())) { |
| 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 | |
|
| 140 | |
|
| 141 | 1 | for (Dependency d : dependenciesToRemove) { |
| 142 | 0 | engine.getDependencies().remove(d); |
| 143 | 0 | } |
| 144 | |
} |
| 145 | 2 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 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 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 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 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 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 | |
|
| 208 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 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 | 1 | if ("cpe".equals(i.getType())) { |
| 260 | 0 | cpeCount1 += 1; |
| 261 | |
} |
| 262 | 1 | } |
| 263 | 1 | for (Identifier i : dependency2.getIdentifiers()) { |
| 264 | 3 | if ("cpe".equals(i.getType())) { |
| 265 | 2 | cpeCount2 += 1; |
| 266 | |
} |
| 267 | 3 | } |
| 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 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 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 | |
|
| 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 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 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 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 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 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 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 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 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 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
protected boolean firstPathIsShortest(String left, String right) { |
| 403 | 5 | final String leftPath = left.replace('\\', '/'); |
| 404 | 5 | final String rightPath = right.replace('\\', '/'); |
| 405 | |
|
| 406 | 5 | final int leftCount = countChar(leftPath, '/'); |
| 407 | 5 | final int rightCount = countChar(rightPath, '/'); |
| 408 | 5 | if (leftCount == rightCount) { |
| 409 | 3 | return leftPath.compareTo(rightPath) <= 0; |
| 410 | |
} else { |
| 411 | 2 | return leftCount < rightCount; |
| 412 | |
} |
| 413 | |
} |
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
private int countChar(String string, char c) { |
| 423 | 10 | int count = 0; |
| 424 | 10 | final int max = string.length(); |
| 425 | 116 | for (int i = 0; i < max; i++) { |
| 426 | 106 | if (c == string.charAt(i)) { |
| 427 | 28 | count++; |
| 428 | |
} |
| 429 | |
} |
| 430 | 10 | return count; |
| 431 | |
} |
| 432 | |
} |