| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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.logging.Level; |
| 27 | |
import java.util.logging.Logger; |
| 28 | |
import java.util.regex.Matcher; |
| 29 | |
import java.util.regex.Pattern; |
| 30 | |
import org.owasp.dependencycheck.Engine; |
| 31 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 32 | |
import org.owasp.dependencycheck.utils.DependencyVersion; |
| 33 | |
import org.owasp.dependencycheck.utils.DependencyVersionUtil; |
| 34 | |
import org.owasp.dependencycheck.utils.LogUtils; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 1 | public class DependencyBundlingAnalyzer extends AbstractAnalyzer implements Analyzer { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 1 | private static final Pattern STARTING_TEXT_PATTERN = Pattern.compile("^[a-zA-Z]*"); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 1 | private boolean analyzed = false; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | 1 | private static final Set<String> EXTENSIONS = null; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private static final String ANALYZER_NAME = "Dependency Bundling Analyzer"; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.PRE_FINDING_ANALYSIS; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public Set<String> getSupportedExtensions() { |
| 79 | 132 | return EXTENSIONS; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public String getName() { |
| 88 | 9 | return ANALYZER_NAME; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public boolean supportsExtension(String extension) { |
| 99 | 9 | return true; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public AnalysisPhase getAnalysisPhase() { |
| 108 | 6 | return ANALYSIS_PHASE; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
@Override |
| 123 | |
public void analyze(Dependency ignore, Engine engine) throws AnalysisException { |
| 124 | 9 | if (!analyzed) { |
| 125 | 1 | analyzed = true; |
| 126 | 1 | final Set<Dependency> dependenciesToRemove = new HashSet<Dependency>(); |
| 127 | 1 | final ListIterator<Dependency> mainIterator = engine.getDependencies().listIterator(); |
| 128 | |
|
| 129 | 4 | while (mainIterator.hasNext()) { |
| 130 | 3 | final Dependency dependency = mainIterator.next(); |
| 131 | 3 | if (mainIterator.hasNext()) { |
| 132 | 2 | final ListIterator<Dependency> subIterator = engine.getDependencies().listIterator(mainIterator.nextIndex()); |
| 133 | 5 | while (subIterator.hasNext()) { |
| 134 | 3 | final Dependency nextDependency = subIterator.next(); |
| 135 | |
|
| 136 | 3 | if (identifiersMatch(dependency, nextDependency) |
| 137 | |
&& hasSameBasePath(dependency, nextDependency) |
| 138 | |
&& fileNameMatch(dependency, nextDependency)) { |
| 139 | |
|
| 140 | 0 | if (isCore(dependency, nextDependency)) { |
| 141 | 0 | dependency.addRelatedDependency(nextDependency); |
| 142 | |
|
| 143 | 0 | final Iterator<Dependency> i = nextDependency.getRelatedDependencies().iterator(); |
| 144 | 0 | while (i.hasNext()) { |
| 145 | 0 | dependency.addRelatedDependency(i.next()); |
| 146 | 0 | i.remove(); |
| 147 | |
} |
| 148 | 0 | dependenciesToRemove.add(nextDependency); |
| 149 | 0 | } else { |
| 150 | 0 | nextDependency.addRelatedDependency(dependency); |
| 151 | |
|
| 152 | 0 | final Iterator<Dependency> i = dependency.getRelatedDependencies().iterator(); |
| 153 | 0 | while (i.hasNext()) { |
| 154 | 0 | nextDependency.addRelatedDependency(i.next()); |
| 155 | 0 | i.remove(); |
| 156 | |
} |
| 157 | 0 | dependenciesToRemove.add(dependency); |
| 158 | |
} |
| 159 | |
} |
| 160 | 3 | } |
| 161 | |
} |
| 162 | 3 | } |
| 163 | |
|
| 164 | |
|
| 165 | 1 | for (Dependency d : dependenciesToRemove) { |
| 166 | 0 | engine.getDependencies().remove(d); |
| 167 | |
} |
| 168 | |
} |
| 169 | 9 | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
private String getBaseRepoPath(final String path) { |
| 179 | 0 | int pos = path.indexOf("repository" + File.separator) + 11; |
| 180 | 0 | if (pos < 0) { |
| 181 | 0 | return path; |
| 182 | |
} |
| 183 | 0 | int tmp = path.indexOf(File.separator, pos); |
| 184 | 0 | if (tmp <= 0) { |
| 185 | 0 | return path; |
| 186 | |
} |
| 187 | 0 | if (tmp > 0) { |
| 188 | 0 | pos = tmp + 1; |
| 189 | |
} |
| 190 | 0 | tmp = path.indexOf(File.separator, pos); |
| 191 | 0 | if (tmp > 0) { |
| 192 | 0 | pos = tmp + 1; |
| 193 | |
} |
| 194 | 0 | return path.substring(0, pos); |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
private boolean fileNameMatch(Dependency dependency1, Dependency dependency2) { |
| 207 | 0 | if (dependency1 == null || dependency1.getFileName() == null |
| 208 | |
|| dependency2 == null || dependency2.getFileName() == null) { |
| 209 | 0 | return false; |
| 210 | |
} |
| 211 | 0 | String fileName1 = dependency1.getFileName(); |
| 212 | 0 | String fileName2 = dependency2.getFileName(); |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | 0 | final File one = new File(fileName1); |
| 217 | 0 | final File two = new File(fileName2); |
| 218 | 0 | final String oneParent = one.getParent(); |
| 219 | 0 | final String twoParent = two.getParent(); |
| 220 | 0 | if (oneParent != null) { |
| 221 | 0 | if (twoParent != null && oneParent.equals(twoParent)) { |
| 222 | 0 | fileName1 = one.getName(); |
| 223 | 0 | fileName2 = two.getName(); |
| 224 | |
} else { |
| 225 | 0 | return false; |
| 226 | |
} |
| 227 | 0 | } else if (twoParent != null) { |
| 228 | 0 | return false; |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | 0 | final DependencyVersion version1 = DependencyVersionUtil.parseVersion(fileName1); |
| 233 | 0 | final DependencyVersion version2 = DependencyVersionUtil.parseVersion(fileName2); |
| 234 | 0 | if (version1 != null && version2 != null) { |
| 235 | 0 | if (!version1.equals(version2)) { |
| 236 | 0 | return false; |
| 237 | |
} |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | 0 | final Matcher match1 = STARTING_TEXT_PATTERN.matcher(fileName1); |
| 242 | 0 | final Matcher match2 = STARTING_TEXT_PATTERN.matcher(fileName2); |
| 243 | 0 | if (match1.find() && match2.find()) { |
| 244 | 0 | return match1.group().equals(match2.group()); |
| 245 | |
} |
| 246 | |
|
| 247 | 0 | return false; |
| 248 | |
} |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
private boolean identifiersMatch(Dependency dependency1, Dependency dependency2) { |
| 260 | 3 | if (dependency1 == null || dependency1.getIdentifiers() == null |
| 261 | |
|| dependency2 == null || dependency2.getIdentifiers() == null) { |
| 262 | 0 | return false; |
| 263 | |
} |
| 264 | 3 | final boolean matches = dependency1.getIdentifiers().size() > 0 |
| 265 | |
&& dependency2.getIdentifiers().equals(dependency1.getIdentifiers()); |
| 266 | 3 | if (LogUtils.isVerboseLoggingEnabled()) { |
| 267 | 0 | final String msg = String.format("IdentifiersMatch=%s (%s, %s)", matches, dependency1.getFileName(), dependency2.getFileName()); |
| 268 | 0 | Logger.getLogger(DependencyBundlingAnalyzer.class.getName()).log(Level.FINE, msg); |
| 269 | |
} |
| 270 | 3 | return matches; |
| 271 | |
} |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
private boolean hasSameBasePath(Dependency dependency1, Dependency dependency2) { |
| 281 | 1 | if (dependency1 == null || dependency2 == null) { |
| 282 | 0 | return false; |
| 283 | |
} |
| 284 | 1 | final File lFile = new File(dependency1.getFilePath()); |
| 285 | 1 | String left = lFile.getParent(); |
| 286 | 1 | final File rFile = new File(dependency2.getFilePath()); |
| 287 | 1 | String right = rFile.getParent(); |
| 288 | 1 | if (left == null) { |
| 289 | 0 | if (right == null) { |
| 290 | 0 | return true; |
| 291 | |
} |
| 292 | 0 | return false; |
| 293 | |
} |
| 294 | 1 | if (left.equalsIgnoreCase(right)) { |
| 295 | 0 | return true; |
| 296 | |
} |
| 297 | 1 | if (left.matches(".*[/\\\\]repository[/\\\\].*") && right.matches(".*[/\\\\]repository[/\\\\].*")) { |
| 298 | 0 | left = getBaseRepoPath(left); |
| 299 | 0 | right = getBaseRepoPath(right); |
| 300 | |
} |
| 301 | 1 | return left.equalsIgnoreCase(right); |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
private boolean isCore(Dependency left, Dependency right) { |
| 314 | 0 | final String leftName = left.getFileName().toLowerCase(); |
| 315 | 0 | final String rightName = right.getFileName().toLowerCase(); |
| 316 | |
final boolean returnVal; |
| 317 | 0 | if (rightName.contains("core") && !leftName.contains("core")) { |
| 318 | 0 | returnVal = false; |
| 319 | 0 | } else if (!rightName.contains("core") && leftName.contains("core")) { |
| 320 | 0 | returnVal = true; |
| 321 | |
} else { |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | 0 | if (leftName.length() > rightName.length()) { |
| 332 | 0 | returnVal = false; |
| 333 | |
} else { |
| 334 | 0 | returnVal = true; |
| 335 | |
} |
| 336 | |
} |
| 337 | 0 | if (LogUtils.isVerboseLoggingEnabled()) { |
| 338 | 0 | final String msg = String.format("IsCore=%s (%s, %s)", returnVal, left.getFileName(), right.getFileName()); |
| 339 | 0 | Logger.getLogger(DependencyBundlingAnalyzer.class.getName()).log(Level.FINE, msg); |
| 340 | |
} |
| 341 | 0 | return returnVal; |
| 342 | |
} |
| 343 | |
} |