| 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.BufferedOutputStream; |
| 21 | |
import java.io.File; |
| 22 | |
import java.io.FileOutputStream; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.io.InputStream; |
| 25 | |
import java.io.InputStreamReader; |
| 26 | |
import java.io.OutputStream; |
| 27 | |
import java.io.Reader; |
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.Collections; |
| 30 | |
import java.util.Enumeration; |
| 31 | |
import java.util.HashMap; |
| 32 | |
import java.util.List; |
| 33 | |
import java.util.Map; |
| 34 | |
import java.util.Map.Entry; |
| 35 | |
import java.util.Properties; |
| 36 | |
import java.util.Set; |
| 37 | |
import java.util.StringTokenizer; |
| 38 | |
import java.util.jar.Attributes; |
| 39 | |
import java.util.jar.JarEntry; |
| 40 | |
import java.util.jar.JarFile; |
| 41 | |
import java.util.jar.Manifest; |
| 42 | |
import java.util.logging.Level; |
| 43 | |
import java.util.logging.Logger; |
| 44 | |
import java.util.regex.Pattern; |
| 45 | |
import java.util.zip.ZipEntry; |
| 46 | |
import org.jsoup.Jsoup; |
| 47 | |
import org.owasp.dependencycheck.Engine; |
| 48 | |
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; |
| 49 | |
import org.owasp.dependencycheck.dependency.Confidence; |
| 50 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 51 | |
import org.owasp.dependencycheck.dependency.EvidenceCollection; |
| 52 | |
import org.owasp.dependencycheck.xml.pom.License; |
| 53 | |
import org.owasp.dependencycheck.xml.pom.PomUtils; |
| 54 | |
import org.owasp.dependencycheck.xml.pom.Model; |
| 55 | |
import org.owasp.dependencycheck.utils.FileUtils; |
| 56 | |
import org.owasp.dependencycheck.utils.Settings; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public class JarAnalyzer extends AbstractFileTypeAnalyzer { |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 1 | private static final Logger LOGGER = Logger.getLogger(JarAnalyzer.class.getName()); |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private static final int BUFFER_SIZE = 4096; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 1 | private static int dirCount = 0; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 1 | private static final String NEWLINE = System.getProperty("line.separator"); |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 1 | private static final Set<String> IGNORE_VALUES = newHashSet( |
| 86 | |
"Sun Java System Application Server"); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | 1 | private static final Set<String> IGNORE_KEYS = newHashSet( |
| 91 | |
"built-by", |
| 92 | |
"created-by", |
| 93 | |
"builtby", |
| 94 | |
"createdby", |
| 95 | |
"build-jdk", |
| 96 | |
"buildjdk", |
| 97 | |
"ant-version", |
| 98 | |
"antversion", |
| 99 | |
"dynamicimportpackage", |
| 100 | |
"dynamicimport-package", |
| 101 | |
"dynamic-importpackage", |
| 102 | |
"dynamic-import-package", |
| 103 | |
"import-package", |
| 104 | |
"ignore-package", |
| 105 | |
"export-package", |
| 106 | |
"importpackage", |
| 107 | |
"ignorepackage", |
| 108 | |
"exportpackage", |
| 109 | |
"sealed", |
| 110 | |
"manifest-version", |
| 111 | |
"archiver-version", |
| 112 | |
"manifestversion", |
| 113 | |
"archiverversion", |
| 114 | |
"classpath", |
| 115 | |
"class-path", |
| 116 | |
"tool", |
| 117 | |
"bundle-manifestversion", |
| 118 | |
"bundlemanifestversion", |
| 119 | |
"include-resource", |
| 120 | |
"embed-dependency", |
| 121 | |
"ipojo-components", |
| 122 | |
"ipojo-extension", |
| 123 | |
"eclipse-sourcereferences"); |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
private static final String BUNDLE_VERSION = "Bundle-Version"; |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
private static final String BUNDLE_DESCRIPTION = "Bundle-Description"; |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
private static final String BUNDLE_NAME = "Bundle-Name"; |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private static final String BUNDLE_VENDOR = "Bundle-Vendor"; |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | 1 | private static final Pattern HTML_DETECTION_PATTERN = Pattern.compile("\\<[a-z]+.*/?\\>", Pattern.CASE_INSENSITIVE); |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | 6 | public JarAnalyzer() { |
| 150 | 6 | } |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
private static final String ANALYZER_NAME = "Jar Analyzer"; |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION; |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | 1 | private static final Set<String> EXTENSIONS = newHashSet("jar", "war"); |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
@Override |
| 172 | |
public Set<String> getSupportedExtensions() { |
| 173 | 852 | return EXTENSIONS; |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
@Override |
| 182 | |
public String getName() { |
| 183 | 5 | return ANALYZER_NAME; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public AnalysisPhase getAnalysisPhase() { |
| 192 | 1 | return ANALYSIS_PHASE; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
@Override |
| 202 | |
protected String getAnalyzerEnabledSettingKey() { |
| 203 | 6 | return Settings.KEYS.ANALYZER_JAR_ENABLED; |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
@Override |
| 215 | |
public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException { |
| 216 | |
try { |
| 217 | 5 | final List<ClassNameInformation> classNames = collectClassNames(dependency); |
| 218 | 5 | final String fileName = dependency.getFileName().toLowerCase(); |
| 219 | 5 | if (classNames.isEmpty() |
| 220 | |
&& (fileName.endsWith("-sources.jar") |
| 221 | |
|| fileName.endsWith("-javadoc.jar") |
| 222 | |
|| fileName.endsWith("-src.jar") |
| 223 | |
|| fileName.endsWith("-doc.jar"))) { |
| 224 | 0 | engine.getDependencies().remove(dependency); |
| 225 | |
} |
| 226 | 5 | final boolean hasManifest = parseManifest(dependency, classNames); |
| 227 | 5 | final boolean hasPOM = analyzePOM(dependency, classNames, engine); |
| 228 | 5 | final boolean addPackagesAsEvidence = !(hasManifest && hasPOM); |
| 229 | 5 | analyzePackageNames(classNames, dependency, addPackagesAsEvidence); |
| 230 | 0 | } catch (IOException ex) { |
| 231 | 0 | throw new AnalysisException("Exception occurred reading the JAR file.", ex); |
| 232 | 5 | } |
| 233 | 5 | } |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
protected boolean analyzePOM(Dependency dependency, List<ClassNameInformation> classes, Engine engine) throws AnalysisException { |
| 246 | 5 | boolean foundSomething = false; |
| 247 | |
final JarFile jar; |
| 248 | |
try { |
| 249 | 5 | jar = new JarFile(dependency.getActualFilePath()); |
| 250 | 0 | } catch (IOException ex) { |
| 251 | 0 | final String msg = String.format("Unable to read JarFile '%s'.", dependency.getActualFilePath()); |
| 252 | |
|
| 253 | 0 | LOGGER.log(Level.WARNING, msg); |
| 254 | 0 | LOGGER.log(Level.FINE, "", ex); |
| 255 | 0 | return false; |
| 256 | 5 | } |
| 257 | |
List<String> pomEntries; |
| 258 | |
try { |
| 259 | 5 | pomEntries = retrievePomListing(jar); |
| 260 | 0 | } catch (IOException ex) { |
| 261 | 0 | final String msg = String.format("Unable to read Jar file entries in '%s'.", dependency.getActualFilePath()); |
| 262 | |
|
| 263 | 0 | LOGGER.log(Level.WARNING, msg); |
| 264 | 0 | LOGGER.log(Level.FINE, msg, ex); |
| 265 | 0 | return false; |
| 266 | 5 | } |
| 267 | 5 | File externalPom = null; |
| 268 | 5 | if (pomEntries.isEmpty()) { |
| 269 | 4 | String pomPath = dependency.getActualFilePath(); |
| 270 | 4 | pomPath = pomPath.substring(0, pomPath.lastIndexOf('.')) + ".pom"; |
| 271 | 4 | externalPom = new File(pomPath); |
| 272 | 4 | if (externalPom.isFile()) { |
| 273 | 0 | pomEntries.add(pomPath); |
| 274 | |
} else { |
| 275 | 4 | return false; |
| 276 | |
} |
| 277 | |
} |
| 278 | 1 | for (String path : pomEntries) { |
| 279 | 1 | LOGGER.fine(String.format("Reading pom entry: %s", path)); |
| 280 | 1 | Properties pomProperties = null; |
| 281 | |
try { |
| 282 | 1 | if (externalPom == null) { |
| 283 | 1 | pomProperties = retrievePomProperties(path, jar); |
| 284 | |
} |
| 285 | 0 | } catch (IOException ex) { |
| 286 | 0 | LOGGER.log(Level.FINEST, "ignore this, failed reading a non-existent pom.properties", ex); |
| 287 | 1 | } |
| 288 | 1 | Model pom = null; |
| 289 | |
try { |
| 290 | 1 | if (pomEntries.size() > 1) { |
| 291 | |
|
| 292 | 0 | final Dependency newDependency = new Dependency(); |
| 293 | 0 | pom = extractPom(path, jar, newDependency); |
| 294 | |
|
| 295 | 0 | final String displayPath = String.format("%s%s%s", |
| 296 | |
dependency.getFilePath(), |
| 297 | |
File.separator, |
| 298 | |
path); |
| 299 | 0 | final String displayName = String.format("%s%s%s", |
| 300 | |
dependency.getFileName(), |
| 301 | |
File.separator, |
| 302 | |
path); |
| 303 | |
|
| 304 | 0 | newDependency.setFileName(displayName); |
| 305 | 0 | newDependency.setFilePath(displayPath); |
| 306 | 0 | pom.processProperties(pomProperties); |
| 307 | 0 | setPomEvidence(newDependency, pom, null); |
| 308 | 0 | engine.getDependencies().add(newDependency); |
| 309 | 0 | Collections.sort(engine.getDependencies()); |
| 310 | 0 | } else { |
| 311 | 1 | if (externalPom == null) { |
| 312 | 1 | pom = PomUtils.readPom(path, jar); |
| 313 | |
} else { |
| 314 | 0 | pom = PomUtils.readPom(externalPom); |
| 315 | |
} |
| 316 | 1 | pom.processProperties(pomProperties); |
| 317 | 1 | foundSomething |= setPomEvidence(dependency, pom, classes); |
| 318 | |
} |
| 319 | 0 | } catch (AnalysisException ex) { |
| 320 | 0 | final String msg = String.format("An error occured while analyzing '%s'.", dependency.getActualFilePath()); |
| 321 | 0 | LOGGER.log(Level.WARNING, msg); |
| 322 | 0 | LOGGER.log(Level.FINE, "", ex); |
| 323 | 1 | } |
| 324 | 1 | } |
| 325 | 1 | return foundSomething; |
| 326 | |
} |
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
private Properties retrievePomProperties(String path, final JarFile jar) throws IOException { |
| 337 | 1 | Properties pomProperties = null; |
| 338 | 1 | final String propPath = path.substring(0, path.length() - 7) + "pom.properies"; |
| 339 | 1 | final ZipEntry propEntry = jar.getEntry(propPath); |
| 340 | 1 | if (propEntry != null) { |
| 341 | 0 | Reader reader = null; |
| 342 | |
try { |
| 343 | 0 | reader = new InputStreamReader(jar.getInputStream(propEntry), "UTF-8"); |
| 344 | 0 | pomProperties = new Properties(); |
| 345 | 0 | pomProperties.load(reader); |
| 346 | 0 | LOGGER.fine(String.format("Read pom.properties: %s", propPath)); |
| 347 | |
} finally { |
| 348 | 0 | if (reader != null) { |
| 349 | |
try { |
| 350 | 0 | reader.close(); |
| 351 | 0 | } catch (IOException ex) { |
| 352 | 0 | LOGGER.log(Level.FINEST, "close error", ex); |
| 353 | 0 | } |
| 354 | |
} |
| 355 | |
} |
| 356 | |
} |
| 357 | 1 | return pomProperties; |
| 358 | |
} |
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
private List<String> retrievePomListing(final JarFile jar) throws IOException { |
| 368 | 5 | final List<String> pomEntries = new ArrayList<String>(); |
| 369 | 5 | final Enumeration<JarEntry> entries = jar.entries(); |
| 370 | 1848 | while (entries.hasMoreElements()) { |
| 371 | 1843 | final JarEntry entry = entries.nextElement(); |
| 372 | 1843 | final String entryName = (new File(entry.getName())).getName().toLowerCase(); |
| 373 | 1843 | if (!entry.isDirectory() && "pom.xml".equals(entryName)) { |
| 374 | 1 | LOGGER.fine(String.format("POM Entry found: %s", entry.getName())); |
| 375 | 1 | pomEntries.add(entry.getName()); |
| 376 | |
} |
| 377 | 1843 | } |
| 378 | 5 | return pomEntries; |
| 379 | |
} |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
private Model extractPom(String path, JarFile jar, Dependency dependency) throws AnalysisException { |
| 392 | 0 | InputStream input = null; |
| 393 | 0 | FileOutputStream fos = null; |
| 394 | 0 | BufferedOutputStream bos = null; |
| 395 | 0 | final File tmpDir = getNextTempDirectory(); |
| 396 | 0 | final File file = new File(tmpDir, "pom.xml"); |
| 397 | |
try { |
| 398 | 0 | final ZipEntry entry = jar.getEntry(path); |
| 399 | 0 | input = jar.getInputStream(entry); |
| 400 | 0 | fos = new FileOutputStream(file); |
| 401 | 0 | bos = new BufferedOutputStream(fos, BUFFER_SIZE); |
| 402 | |
int count; |
| 403 | 0 | final byte[] data = new byte[BUFFER_SIZE]; |
| 404 | 0 | while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) { |
| 405 | 0 | bos.write(data, 0, count); |
| 406 | |
} |
| 407 | 0 | bos.flush(); |
| 408 | 0 | dependency.setActualFilePath(file.getAbsolutePath()); |
| 409 | 0 | } catch (IOException ex) { |
| 410 | 0 | final String msg = String.format("An error occurred reading '%s' from '%s'.", path, dependency.getFilePath()); |
| 411 | 0 | LOGGER.warning(msg); |
| 412 | 0 | LOGGER.log(Level.SEVERE, "", ex); |
| 413 | |
} finally { |
| 414 | 0 | closeStream(bos); |
| 415 | 0 | closeStream(fos); |
| 416 | 0 | closeStream(input); |
| 417 | 0 | } |
| 418 | 0 | return PomUtils.readPom(file); |
| 419 | |
} |
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
private void closeStream(InputStream stream) { |
| 427 | 0 | if (stream != null) { |
| 428 | |
try { |
| 429 | 0 | stream.close(); |
| 430 | 0 | } catch (IOException ex) { |
| 431 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 432 | 0 | } |
| 433 | |
} |
| 434 | 0 | } |
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
private void closeStream(OutputStream stream) { |
| 442 | 0 | if (stream != null) { |
| 443 | |
try { |
| 444 | 0 | stream.close(); |
| 445 | 0 | } catch (IOException ex) { |
| 446 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 447 | 0 | } |
| 448 | |
} |
| 449 | 0 | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
public static boolean setPomEvidence(Dependency dependency, Model pom, List<ClassNameInformation> classes) { |
| 461 | 1 | boolean foundSomething = false; |
| 462 | 1 | boolean addAsIdentifier = true; |
| 463 | 1 | if (pom == null) { |
| 464 | 0 | return foundSomething; |
| 465 | |
} |
| 466 | 1 | String groupid = pom.getGroupId(); |
| 467 | 1 | String parentGroupId = pom.getParentGroupId(); |
| 468 | 1 | String artifactid = pom.getArtifactId(); |
| 469 | 1 | String parentArtifactId = pom.getParentArtifactId(); |
| 470 | 1 | String version = pom.getVersion(); |
| 471 | 1 | String parentVersion = pom.getParentVersion(); |
| 472 | |
|
| 473 | 1 | if ("org.sonatype.oss".equals(parentGroupId) && "oss-parent".equals(parentArtifactId)) { |
| 474 | 0 | parentGroupId = null; |
| 475 | 0 | parentArtifactId = null; |
| 476 | 0 | parentVersion = null; |
| 477 | |
} |
| 478 | |
|
| 479 | 1 | if ((groupid == null || groupid.isEmpty()) && parentGroupId != null && !parentGroupId.isEmpty()) { |
| 480 | 0 | groupid = parentGroupId; |
| 481 | |
} |
| 482 | |
|
| 483 | 1 | final String originalGroupID = groupid; |
| 484 | 1 | if (groupid.startsWith("org.") || groupid.startsWith("com.")) { |
| 485 | 1 | groupid = groupid.substring(4); |
| 486 | |
} |
| 487 | |
|
| 488 | 1 | if ((artifactid == null || artifactid.isEmpty()) && parentArtifactId != null && !parentArtifactId.isEmpty()) { |
| 489 | 0 | artifactid = parentArtifactId; |
| 490 | |
} |
| 491 | |
|
| 492 | 1 | final String originalArtifactID = artifactid; |
| 493 | 1 | if (artifactid.startsWith("org.") || artifactid.startsWith("com.")) { |
| 494 | 0 | artifactid = artifactid.substring(4); |
| 495 | |
} |
| 496 | |
|
| 497 | 1 | if ((version == null || version.isEmpty()) && parentVersion != null && !parentVersion.isEmpty()) { |
| 498 | 1 | version = parentVersion; |
| 499 | |
} |
| 500 | |
|
| 501 | 1 | if (groupid != null && !groupid.isEmpty()) { |
| 502 | 1 | foundSomething = true; |
| 503 | 1 | dependency.getVendorEvidence().addEvidence("pom", "groupid", groupid, Confidence.HIGHEST); |
| 504 | 1 | dependency.getProductEvidence().addEvidence("pom", "groupid", groupid, Confidence.LOW); |
| 505 | 1 | addMatchingValues(classes, groupid, dependency.getVendorEvidence()); |
| 506 | 1 | addMatchingValues(classes, groupid, dependency.getProductEvidence()); |
| 507 | 1 | if (parentGroupId != null && !parentGroupId.isEmpty() && !parentGroupId.equals(groupid)) { |
| 508 | 1 | dependency.getVendorEvidence().addEvidence("pom", "parent-groupid", parentGroupId, Confidence.MEDIUM); |
| 509 | 1 | dependency.getProductEvidence().addEvidence("pom", "parent-groupid", parentGroupId, Confidence.LOW); |
| 510 | 1 | addMatchingValues(classes, parentGroupId, dependency.getVendorEvidence()); |
| 511 | 1 | addMatchingValues(classes, parentGroupId, dependency.getProductEvidence()); |
| 512 | |
} |
| 513 | |
} else { |
| 514 | 0 | addAsIdentifier = false; |
| 515 | |
} |
| 516 | |
|
| 517 | 1 | if (artifactid != null && !artifactid.isEmpty()) { |
| 518 | 1 | foundSomething = true; |
| 519 | 1 | dependency.getProductEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.HIGHEST); |
| 520 | 1 | dependency.getVendorEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.LOW); |
| 521 | 1 | addMatchingValues(classes, artifactid, dependency.getVendorEvidence()); |
| 522 | 1 | addMatchingValues(classes, artifactid, dependency.getProductEvidence()); |
| 523 | 1 | if (parentArtifactId != null && !parentArtifactId.isEmpty() && !parentArtifactId.equals(artifactid)) { |
| 524 | 1 | dependency.getProductEvidence().addEvidence("pom", "parent-artifactid", parentArtifactId, Confidence.MEDIUM); |
| 525 | 1 | dependency.getVendorEvidence().addEvidence("pom", "parent-artifactid", parentArtifactId, Confidence.LOW); |
| 526 | 1 | addMatchingValues(classes, parentArtifactId, dependency.getVendorEvidence()); |
| 527 | 1 | addMatchingValues(classes, parentArtifactId, dependency.getProductEvidence()); |
| 528 | |
} |
| 529 | |
} else { |
| 530 | 0 | addAsIdentifier = false; |
| 531 | |
} |
| 532 | |
|
| 533 | 1 | if (version != null && !version.isEmpty()) { |
| 534 | 1 | foundSomething = true; |
| 535 | 1 | dependency.getVersionEvidence().addEvidence("pom", "version", version, Confidence.HIGHEST); |
| 536 | 1 | if (parentVersion != null && !parentVersion.isEmpty() && !parentVersion.equals(version)) { |
| 537 | 0 | dependency.getVersionEvidence().addEvidence("pom", "parent-version", version, Confidence.LOW); |
| 538 | |
} |
| 539 | |
} else { |
| 540 | 0 | addAsIdentifier = false; |
| 541 | |
} |
| 542 | |
|
| 543 | 1 | if (addAsIdentifier) { |
| 544 | 1 | dependency.addIdentifier("maven", String.format("%s:%s:%s", originalGroupID, originalArtifactID, version), null, Confidence.HIGH); |
| 545 | |
} |
| 546 | |
|
| 547 | |
|
| 548 | 1 | final String org = pom.getOrganization(); |
| 549 | 1 | if (org != null && !org.isEmpty()) { |
| 550 | 0 | dependency.getVendorEvidence().addEvidence("pom", "organization name", org, Confidence.HIGH); |
| 551 | 0 | dependency.getProductEvidence().addEvidence("pom", "organization name", org, Confidence.LOW); |
| 552 | 0 | addMatchingValues(classes, org, dependency.getVendorEvidence()); |
| 553 | 0 | addMatchingValues(classes, org, dependency.getProductEvidence()); |
| 554 | |
} |
| 555 | |
|
| 556 | 1 | final String pomName = pom.getName(); |
| 557 | 1 | if (pomName |
| 558 | |
!= null && !pomName.isEmpty()) { |
| 559 | 1 | foundSomething = true; |
| 560 | 1 | dependency.getProductEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH); |
| 561 | 1 | dependency.getVendorEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH); |
| 562 | 1 | addMatchingValues(classes, pomName, dependency.getVendorEvidence()); |
| 563 | 1 | addMatchingValues(classes, pomName, dependency.getProductEvidence()); |
| 564 | |
} |
| 565 | |
|
| 566 | |
|
| 567 | 1 | final String description = pom.getDescription(); |
| 568 | 1 | if (description != null && !description.isEmpty()) { |
| 569 | 0 | foundSomething = true; |
| 570 | 0 | final String trimmedDescription = addDescription(dependency, description, "pom", "description"); |
| 571 | 0 | addMatchingValues(classes, trimmedDescription, dependency.getVendorEvidence()); |
| 572 | 0 | addMatchingValues(classes, trimmedDescription, dependency.getProductEvidence()); |
| 573 | |
} |
| 574 | |
|
| 575 | 1 | extractLicense(pom, dependency); |
| 576 | 1 | return foundSomething; |
| 577 | |
} |
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
protected void analyzePackageNames(List<ClassNameInformation> classNames, |
| 588 | |
Dependency dependency, boolean addPackagesAsEvidence) { |
| 589 | 5 | final Map<String, Integer> vendorIdentifiers = new HashMap<String, Integer>(); |
| 590 | 5 | final Map<String, Integer> productIdentifiers = new HashMap<String, Integer>(); |
| 591 | 5 | analyzeFullyQualifiedClassNames(classNames, vendorIdentifiers, productIdentifiers); |
| 592 | |
|
| 593 | 5 | final int classCount = classNames.size(); |
| 594 | 5 | final EvidenceCollection vendor = dependency.getVendorEvidence(); |
| 595 | 5 | final EvidenceCollection product = dependency.getProductEvidence(); |
| 596 | |
|
| 597 | 5 | for (Map.Entry<String, Integer> entry : vendorIdentifiers.entrySet()) { |
| 598 | 48 | final float ratio = entry.getValue() / (float) classCount; |
| 599 | 48 | if (ratio > 0.5) { |
| 600 | |
|
| 601 | 10 | vendor.addWeighting(entry.getKey()); |
| 602 | 10 | if (addPackagesAsEvidence && entry.getKey().length() > 1) { |
| 603 | 8 | vendor.addEvidence("jar", "package name", entry.getKey(), Confidence.LOW); |
| 604 | |
} |
| 605 | |
} |
| 606 | 48 | } |
| 607 | 5 | for (Map.Entry<String, Integer> entry : productIdentifiers.entrySet()) { |
| 608 | 985 | final float ratio = entry.getValue() / (float) classCount; |
| 609 | 985 | if (ratio > 0.5) { |
| 610 | 5 | product.addWeighting(entry.getKey()); |
| 611 | 5 | if (addPackagesAsEvidence && entry.getKey().length() > 1) { |
| 612 | 4 | product.addEvidence("jar", "package name", entry.getKey(), Confidence.LOW); |
| 613 | |
} |
| 614 | |
} |
| 615 | 985 | } |
| 616 | 5 | } |
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
|
| 624 | |
|
| 625 | |
|
| 626 | |
|
| 627 | |
|
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
protected boolean parseManifest(Dependency dependency, List<ClassNameInformation> classInformation) throws IOException { |
| 633 | 5 | boolean foundSomething = false; |
| 634 | 5 | JarFile jar = null; |
| 635 | |
try { |
| 636 | 5 | jar = new JarFile(dependency.getActualFilePath()); |
| 637 | |
|
| 638 | 5 | final Manifest manifest = jar.getManifest(); |
| 639 | |
|
| 640 | 5 | if (manifest == null) { |
| 641 | |
|
| 642 | 0 | if (!dependency.getFileName().toLowerCase().endsWith("-sources.jar") |
| 643 | |
&& !dependency.getFileName().toLowerCase().endsWith("-javadoc.jar") |
| 644 | |
&& !dependency.getFileName().toLowerCase().endsWith("-src.jar") |
| 645 | |
&& !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) { |
| 646 | 0 | LOGGER.log(Level.FINE, |
| 647 | |
String.format("Jar file '%s' does not contain a manifest.", |
| 648 | |
dependency.getFileName())); |
| 649 | |
} |
| 650 | 0 | return false; |
| 651 | |
} |
| 652 | 5 | final Attributes atts = manifest.getMainAttributes(); |
| 653 | |
|
| 654 | 5 | final EvidenceCollection vendorEvidence = dependency.getVendorEvidence(); |
| 655 | 5 | final EvidenceCollection productEvidence = dependency.getProductEvidence(); |
| 656 | 5 | final EvidenceCollection versionEvidence = dependency.getVersionEvidence(); |
| 657 | |
|
| 658 | 5 | final String source = "Manifest"; |
| 659 | |
|
| 660 | 5 | for (Entry<Object, Object> entry : atts.entrySet()) { |
| 661 | 65 | String key = entry.getKey().toString(); |
| 662 | 65 | String value = atts.getValue(key); |
| 663 | 65 | if (HTML_DETECTION_PATTERN.matcher(value).find()) { |
| 664 | 0 | value = Jsoup.parse(value).text(); |
| 665 | |
} |
| 666 | 65 | if (IGNORE_VALUES.contains(value)) { |
| 667 | 0 | continue; |
| 668 | 65 | } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) { |
| 669 | 1 | foundSomething = true; |
| 670 | 1 | productEvidence.addEvidence(source, key, value, Confidence.HIGH); |
| 671 | 1 | addMatchingValues(classInformation, value, productEvidence); |
| 672 | 64 | } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) { |
| 673 | 2 | foundSomething = true; |
| 674 | 2 | versionEvidence.addEvidence(source, key, value, Confidence.HIGH); |
| 675 | 62 | } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) { |
| 676 | 1 | foundSomething = true; |
| 677 | 1 | vendorEvidence.addEvidence(source, key, value, Confidence.HIGH); |
| 678 | 1 | addMatchingValues(classInformation, value, vendorEvidence); |
| 679 | 61 | } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR_ID.toString())) { |
| 680 | 0 | foundSomething = true; |
| 681 | 0 | vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 682 | 0 | addMatchingValues(classInformation, value, vendorEvidence); |
| 683 | 61 | } else if (key.equalsIgnoreCase(BUNDLE_DESCRIPTION)) { |
| 684 | 2 | foundSomething = true; |
| 685 | 2 | addDescription(dependency, value, "manifest", key); |
| 686 | |
|
| 687 | 2 | addMatchingValues(classInformation, value, productEvidence); |
| 688 | 59 | } else if (key.equalsIgnoreCase(BUNDLE_NAME)) { |
| 689 | 3 | foundSomething = true; |
| 690 | 3 | productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 691 | 3 | addMatchingValues(classInformation, value, productEvidence); |
| 692 | 56 | } else if (key.equalsIgnoreCase(BUNDLE_VENDOR)) { |
| 693 | 3 | foundSomething = true; |
| 694 | 3 | vendorEvidence.addEvidence(source, key, value, Confidence.HIGH); |
| 695 | 3 | addMatchingValues(classInformation, value, vendorEvidence); |
| 696 | 53 | } else if (key.equalsIgnoreCase(BUNDLE_VERSION)) { |
| 697 | 3 | foundSomething = true; |
| 698 | 3 | versionEvidence.addEvidence(source, key, value, Confidence.HIGH); |
| 699 | 50 | } else if (key.equalsIgnoreCase(Attributes.Name.MAIN_CLASS.toString())) { |
| 700 | 2 | continue; |
| 701 | |
|
| 702 | |
|
| 703 | |
|
| 704 | |
|
| 705 | |
|
| 706 | |
|
| 707 | |
|
| 708 | |
|
| 709 | |
} else { |
| 710 | 48 | key = key.toLowerCase(); |
| 711 | |
|
| 712 | 48 | if (!IGNORE_KEYS.contains(key) |
| 713 | |
&& !key.endsWith("jdk") |
| 714 | |
&& !key.contains("lastmodified") |
| 715 | |
&& !key.endsWith("package") |
| 716 | |
&& !key.endsWith("classpath") |
| 717 | |
&& !key.endsWith("class-path") |
| 718 | |
&& !key.endsWith("-scm") |
| 719 | |
&& !key.startsWith("scm-") |
| 720 | |
&& !value.trim().startsWith("scm:") |
| 721 | |
&& !isImportPackage(key, value) |
| 722 | |
&& !isPackage(key, value)) { |
| 723 | |
|
| 724 | 14 | foundSomething = true; |
| 725 | 14 | if (key.contains("version")) { |
| 726 | 1 | if (key.contains("specification")) { |
| 727 | 1 | versionEvidence.addEvidence(source, key, value, Confidence.LOW); |
| 728 | |
} else { |
| 729 | 0 | versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 730 | |
} |
| 731 | 13 | } else if ("build-id".equals(key)) { |
| 732 | 0 | int pos = value.indexOf('('); |
| 733 | 0 | if (pos >= 0) { |
| 734 | 0 | value = value.substring(0, pos - 1); |
| 735 | |
} |
| 736 | 0 | pos = value.indexOf('['); |
| 737 | 0 | if (pos >= 0) { |
| 738 | 0 | value = value.substring(0, pos - 1); |
| 739 | |
} |
| 740 | 0 | versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 741 | 0 | } else if (key.contains("title")) { |
| 742 | 1 | productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 743 | 1 | addMatchingValues(classInformation, value, productEvidence); |
| 744 | 12 | } else if (key.contains("vendor")) { |
| 745 | 0 | if (key.contains("specification")) { |
| 746 | 0 | vendorEvidence.addEvidence(source, key, value, Confidence.LOW); |
| 747 | |
} else { |
| 748 | 0 | vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 749 | 0 | addMatchingValues(classInformation, value, vendorEvidence); |
| 750 | |
} |
| 751 | 12 | } else if (key.contains("name")) { |
| 752 | 3 | productEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 753 | 3 | vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM); |
| 754 | 3 | addMatchingValues(classInformation, value, vendorEvidence); |
| 755 | 3 | addMatchingValues(classInformation, value, productEvidence); |
| 756 | 9 | } else if (key.contains("license")) { |
| 757 | 2 | addLicense(dependency, value); |
| 758 | |
} else { |
| 759 | 7 | if (key.contains("description")) { |
| 760 | 0 | addDescription(dependency, value, "manifest", key); |
| 761 | |
} else { |
| 762 | 7 | productEvidence.addEvidence(source, key, value, Confidence.LOW); |
| 763 | 7 | vendorEvidence.addEvidence(source, key, value, Confidence.LOW); |
| 764 | 7 | addMatchingValues(classInformation, value, vendorEvidence); |
| 765 | 7 | addMatchingValues(classInformation, value, productEvidence); |
| 766 | 7 | if (value.matches(".*\\d.*")) { |
| 767 | 3 | final StringTokenizer tokenizer = new StringTokenizer(value, " "); |
| 768 | 15 | while (tokenizer.hasMoreElements()) { |
| 769 | 12 | final String s = tokenizer.nextToken(); |
| 770 | 12 | if (s.matches("^[0-9.]+$")) { |
| 771 | 1 | versionEvidence.addEvidence(source, key, s, Confidence.LOW); |
| 772 | |
} |
| 773 | 12 | } |
| 774 | |
} |
| 775 | |
} |
| 776 | |
} |
| 777 | |
} |
| 778 | |
} |
| 779 | 63 | } |
| 780 | |
} finally { |
| 781 | 5 | if (jar != null) { |
| 782 | 5 | jar.close(); |
| 783 | |
} |
| 784 | |
} |
| 785 | 5 | return foundSomething; |
| 786 | |
} |
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | |
|
| 791 | |
|
| 792 | |
|
| 793 | |
|
| 794 | |
|
| 795 | |
|
| 796 | |
|
| 797 | |
|
| 798 | |
|
| 799 | |
public static String addDescription(Dependency dependency, String description, String source, String key) { |
| 800 | 9 | if (dependency.getDescription() == null) { |
| 801 | 9 | dependency.setDescription(description); |
| 802 | |
} |
| 803 | |
String desc; |
| 804 | 9 | if (HTML_DETECTION_PATTERN.matcher(description).find()) { |
| 805 | 0 | desc = Jsoup.parse(description).text(); |
| 806 | |
} else { |
| 807 | 9 | desc = description; |
| 808 | |
} |
| 809 | 9 | dependency.setDescription(desc); |
| 810 | 9 | if (desc.length() > 100) { |
| 811 | 0 | desc = desc.replaceAll("\\s\\s+", " "); |
| 812 | 0 | final int posSuchAs = desc.toLowerCase().indexOf("such as ", 100); |
| 813 | 0 | final int posLike = desc.toLowerCase().indexOf("like ", 100); |
| 814 | 0 | final int posWillUse = desc.toLowerCase().indexOf("will use ", 100); |
| 815 | 0 | final int posUses = desc.toLowerCase().indexOf(" uses ", 100); |
| 816 | 0 | int pos = -1; |
| 817 | 0 | pos = Math.max(pos, posSuchAs); |
| 818 | 0 | if (pos >= 0 && posLike >= 0) { |
| 819 | 0 | pos = Math.min(pos, posLike); |
| 820 | |
} else { |
| 821 | 0 | pos = Math.max(pos, posLike); |
| 822 | |
} |
| 823 | 0 | if (pos >= 0 && posWillUse >= 0) { |
| 824 | 0 | pos = Math.min(pos, posWillUse); |
| 825 | |
} else { |
| 826 | 0 | pos = Math.max(pos, posWillUse); |
| 827 | |
} |
| 828 | 0 | if (pos >= 0 && posUses >= 0) { |
| 829 | 0 | pos = Math.min(pos, posUses); |
| 830 | |
} else { |
| 831 | 0 | pos = Math.max(pos, posUses); |
| 832 | |
} |
| 833 | |
|
| 834 | 0 | if (pos > 0) { |
| 835 | 0 | final StringBuilder sb = new StringBuilder(pos + 3); |
| 836 | 0 | sb.append(desc.substring(0, pos)); |
| 837 | 0 | sb.append("..."); |
| 838 | 0 | desc = sb.toString(); |
| 839 | |
} |
| 840 | 0 | dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.LOW); |
| 841 | 0 | dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.LOW); |
| 842 | 0 | } else { |
| 843 | 9 | dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.MEDIUM); |
| 844 | 9 | dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.MEDIUM); |
| 845 | |
} |
| 846 | 9 | return desc; |
| 847 | |
} |
| 848 | |
|
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
|
| 853 | |
|
| 854 | |
|
| 855 | |
private void addLicense(Dependency d, String license) { |
| 856 | 2 | if (d.getLicense() == null) { |
| 857 | 2 | d.setLicense(license); |
| 858 | 0 | } else if (!d.getLicense().contains(license)) { |
| 859 | 0 | d.setLicense(d.getLicense() + NEWLINE + license); |
| 860 | |
} |
| 861 | 2 | } |
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | 6 | private File tempFileLocation = null; |
| 867 | |
|
| 868 | |
|
| 869 | |
|
| 870 | |
|
| 871 | |
|
| 872 | |
|
| 873 | |
@Override |
| 874 | |
public void initializeFileTypeAnalyzer() throws Exception { |
| 875 | 1 | final File baseDir = Settings.getTempDirectory(); |
| 876 | 1 | tempFileLocation = File.createTempFile("check", "tmp", baseDir); |
| 877 | 1 | if (!tempFileLocation.delete()) { |
| 878 | 0 | final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath()); |
| 879 | 0 | throw new AnalysisException(msg); |
| 880 | |
} |
| 881 | 1 | if (!tempFileLocation.mkdirs()) { |
| 882 | 0 | final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath()); |
| 883 | 0 | throw new AnalysisException(msg); |
| 884 | |
} |
| 885 | 1 | } |
| 886 | |
|
| 887 | |
|
| 888 | |
|
| 889 | |
|
| 890 | |
@Override |
| 891 | |
public void close() { |
| 892 | 1 | if (tempFileLocation != null && tempFileLocation.exists()) { |
| 893 | 1 | LOGGER.log(Level.FINE, "Attempting to delete temporary files"); |
| 894 | 1 | final boolean success = FileUtils.delete(tempFileLocation); |
| 895 | 1 | if (!success) { |
| 896 | 0 | LOGGER.log(Level.WARNING, |
| 897 | |
"Failed to delete some temporary files, see the log for more details"); |
| 898 | |
} |
| 899 | |
} |
| 900 | 1 | } |
| 901 | |
|
| 902 | |
|
| 903 | |
|
| 904 | |
|
| 905 | |
|
| 906 | |
|
| 907 | |
|
| 908 | |
|
| 909 | |
private boolean isImportPackage(String key, String value) { |
| 910 | 15 | final Pattern packageRx = Pattern.compile("^([a-zA-Z0-9_#\\$\\*\\.]+\\s*[,;]\\s*)+([a-zA-Z0-9_#\\$\\*\\.]+\\s*)?$"); |
| 911 | 15 | final boolean matches = packageRx.matcher(value).matches(); |
| 912 | 15 | return matches && (key.contains("import") || key.contains("include") || value.length() > 10); |
| 913 | |
} |
| 914 | |
|
| 915 | |
|
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
|
| 922 | |
private List<ClassNameInformation> collectClassNames(Dependency dependency) { |
| 923 | 5 | final List<ClassNameInformation> classNames = new ArrayList<ClassNameInformation>(); |
| 924 | 5 | JarFile jar = null; |
| 925 | |
try { |
| 926 | 5 | jar = new JarFile(dependency.getActualFilePath()); |
| 927 | 5 | final Enumeration entries = jar.entries(); |
| 928 | 1848 | while (entries.hasMoreElements()) { |
| 929 | 1843 | final JarEntry entry = (JarEntry) entries.nextElement(); |
| 930 | 1843 | final String name = entry.getName().toLowerCase(); |
| 931 | |
|
| 932 | 1843 | if (name.endsWith(".class") && !name.matches("^javax?\\..*$")) { |
| 933 | 1535 | final ClassNameInformation className = new ClassNameInformation(name.substring(0, name.length() - 6)); |
| 934 | 1535 | classNames.add(className); |
| 935 | |
} |
| 936 | 1843 | } |
| 937 | 0 | } catch (IOException ex) { |
| 938 | 0 | final String msg = String.format("Unable to open jar file '%s'.", dependency.getFileName()); |
| 939 | 0 | LOGGER.log(Level.WARNING, msg); |
| 940 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 941 | |
} finally { |
| 942 | 5 | if (jar != null) { |
| 943 | |
try { |
| 944 | 5 | jar.close(); |
| 945 | 0 | } catch (IOException ex) { |
| 946 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 947 | 5 | } |
| 948 | |
} |
| 949 | |
} |
| 950 | 5 | return classNames; |
| 951 | |
} |
| 952 | |
|
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
|
| 958 | |
|
| 959 | |
|
| 960 | |
|
| 961 | |
private void analyzeFullyQualifiedClassNames(List<ClassNameInformation> classNames, |
| 962 | |
Map<String, Integer> vendor, Map<String, Integer> product) { |
| 963 | 5 | for (ClassNameInformation entry : classNames) { |
| 964 | 1535 | final List<String> list = entry.getPackageStructure(); |
| 965 | 1535 | addEntry(vendor, list.get(0)); |
| 966 | |
|
| 967 | 1535 | if (list.size() == 2) { |
| 968 | 0 | addEntry(product, list.get(1)); |
| 969 | |
} |
| 970 | 1535 | if (list.size() == 3) { |
| 971 | 345 | addEntry(vendor, list.get(1)); |
| 972 | 345 | addEntry(product, list.get(1)); |
| 973 | 345 | addEntry(product, list.get(2)); |
| 974 | |
} |
| 975 | 1535 | if (list.size() >= 4) { |
| 976 | 1190 | addEntry(vendor, list.get(1)); |
| 977 | 1190 | addEntry(vendor, list.get(2)); |
| 978 | 1190 | addEntry(product, list.get(1)); |
| 979 | 1190 | addEntry(product, list.get(2)); |
| 980 | 1190 | addEntry(product, list.get(3)); |
| 981 | |
} |
| 982 | 1535 | } |
| 983 | 5 | } |
| 984 | |
|
| 985 | |
|
| 986 | |
|
| 987 | |
|
| 988 | |
|
| 989 | |
|
| 990 | |
|
| 991 | |
|
| 992 | |
private void addEntry(Map<String, Integer> collection, String key) { |
| 993 | 8520 | if (collection.containsKey(key)) { |
| 994 | 7487 | collection.put(key, collection.get(key) + 1); |
| 995 | |
} else { |
| 996 | 1033 | collection.put(key, 1); |
| 997 | |
} |
| 998 | 8520 | } |
| 999 | |
|
| 1000 | |
|
| 1001 | |
|
| 1002 | |
|
| 1003 | |
|
| 1004 | |
|
| 1005 | |
|
| 1006 | |
|
| 1007 | |
|
| 1008 | |
|
| 1009 | |
private static void addMatchingValues(List<ClassNameInformation> classes, String value, EvidenceCollection evidence) { |
| 1010 | 41 | if (value == null || value.isEmpty() || classes == null || classes.isEmpty()) { |
| 1011 | 0 | return; |
| 1012 | |
} |
| 1013 | 41 | final String text = value.toLowerCase(); |
| 1014 | 41 | for (ClassNameInformation cni : classes) { |
| 1015 | 16500 | for (String key : cni.getPackageStructure()) { |
| 1016 | 63820 | if (text.contains(key)) { |
| 1017 | 15400 | evidence.addEvidence("jar", "package name", key, Confidence.HIGHEST); |
| 1018 | |
} |
| 1019 | 63820 | } |
| 1020 | 16500 | } |
| 1021 | 41 | } |
| 1022 | |
|
| 1023 | |
|
| 1024 | |
|
| 1025 | |
|
| 1026 | |
|
| 1027 | |
|
| 1028 | |
|
| 1029 | |
|
| 1030 | |
private boolean isPackage(String key, String value) { |
| 1031 | |
|
| 1032 | 15 | return !key.matches(".*(version|title|vendor|name|license|description).*") |
| 1033 | |
&& value.matches("^([a-zA-Z_][a-zA-Z0-9_\\$]*(\\.[a-zA-Z_][a-zA-Z0-9_\\$]*)*)?$"); |
| 1034 | |
|
| 1035 | |
} |
| 1036 | |
|
| 1037 | |
|
| 1038 | |
|
| 1039 | |
|
| 1040 | |
|
| 1041 | |
|
| 1042 | |
|
| 1043 | |
public static void extractLicense(Model pom, Dependency dependency) { |
| 1044 | |
|
| 1045 | 1 | if (pom.getLicenses() != null) { |
| 1046 | 1 | String license = null; |
| 1047 | 1 | for (License lic : pom.getLicenses()) { |
| 1048 | 0 | String tmp = null; |
| 1049 | 0 | if (lic.getName() != null) { |
| 1050 | 0 | tmp = lic.getName(); |
| 1051 | |
} |
| 1052 | 0 | if (lic.getUrl() != null) { |
| 1053 | 0 | if (tmp == null) { |
| 1054 | 0 | tmp = lic.getUrl(); |
| 1055 | |
} else { |
| 1056 | 0 | tmp += ": " + lic.getUrl(); |
| 1057 | |
} |
| 1058 | |
} |
| 1059 | 0 | if (tmp == null) { |
| 1060 | 0 | continue; |
| 1061 | |
} |
| 1062 | 0 | if (HTML_DETECTION_PATTERN.matcher(tmp).find()) { |
| 1063 | 0 | tmp = Jsoup.parse(tmp).text(); |
| 1064 | |
} |
| 1065 | 0 | if (license == null) { |
| 1066 | 0 | license = tmp; |
| 1067 | |
} else { |
| 1068 | 0 | license += "\n" + tmp; |
| 1069 | |
} |
| 1070 | 0 | } |
| 1071 | 1 | if (license != null) { |
| 1072 | 0 | dependency.setLicense(license); |
| 1073 | |
|
| 1074 | |
} |
| 1075 | |
} |
| 1076 | 1 | } |
| 1077 | |
|
| 1078 | |
|
| 1079 | |
|
| 1080 | |
|
| 1081 | |
protected static class ClassNameInformation { |
| 1082 | |
|
| 1083 | |
|
| 1084 | |
|
| 1085 | |
|
| 1086 | |
|
| 1087 | |
|
| 1088 | |
|
| 1089 | |
|
| 1090 | |
|
| 1091 | |
|
| 1092 | |
|
| 1093 | |
|
| 1094 | |
|
| 1095 | |
|
| 1096 | |
|
| 1097 | |
|
| 1098 | |
|
| 1099 | |
|
| 1100 | |
|
| 1101 | |
|
| 1102 | |
|
| 1103 | 1535 | ClassNameInformation(String className) { |
| 1104 | 1535 | name = className; |
| 1105 | 1535 | if (name.contains("/")) { |
| 1106 | 1535 | final String[] tmp = className.toLowerCase().split("/"); |
| 1107 | 1535 | int start = 0; |
| 1108 | 1535 | int end = 3; |
| 1109 | 1535 | if ("com".equals(tmp[0]) || "org".equals(tmp[0])) { |
| 1110 | 1535 | start = 1; |
| 1111 | 1535 | end = 4; |
| 1112 | |
} |
| 1113 | 1535 | if (tmp.length <= end) { |
| 1114 | 345 | end = tmp.length - 1; |
| 1115 | |
} |
| 1116 | 7330 | for (int i = start; i <= end; i++) { |
| 1117 | 5795 | packageStructure.add(tmp[i]); |
| 1118 | |
} |
| 1119 | 1535 | } else { |
| 1120 | 0 | packageStructure.add(name); |
| 1121 | |
} |
| 1122 | 1535 | } |
| 1123 | |
|
| 1124 | |
|
| 1125 | |
|
| 1126 | |
private String name; |
| 1127 | |
|
| 1128 | |
|
| 1129 | |
|
| 1130 | |
|
| 1131 | |
|
| 1132 | |
|
| 1133 | |
public String getName() { |
| 1134 | 0 | return name; |
| 1135 | |
} |
| 1136 | |
|
| 1137 | |
|
| 1138 | |
|
| 1139 | |
|
| 1140 | |
|
| 1141 | |
|
| 1142 | |
public void setName(String name) { |
| 1143 | 0 | this.name = name; |
| 1144 | 0 | } |
| 1145 | |
|
| 1146 | |
|
| 1147 | |
|
| 1148 | 1535 | private final ArrayList<String> packageStructure = new ArrayList<String>(); |
| 1149 | |
|
| 1150 | |
|
| 1151 | |
|
| 1152 | |
|
| 1153 | |
|
| 1154 | |
|
| 1155 | |
public ArrayList<String> getPackageStructure() { |
| 1156 | 18035 | return packageStructure; |
| 1157 | |
} |
| 1158 | |
} |
| 1159 | |
|
| 1160 | |
|
| 1161 | |
|
| 1162 | |
|
| 1163 | |
|
| 1164 | |
|
| 1165 | |
|
| 1166 | |
private File getNextTempDirectory() throws AnalysisException { |
| 1167 | 0 | dirCount += 1; |
| 1168 | 0 | final File directory = new File(tempFileLocation, String.valueOf(dirCount)); |
| 1169 | |
|
| 1170 | 0 | if (directory.exists()) { |
| 1171 | 0 | return getNextTempDirectory(); |
| 1172 | |
} |
| 1173 | 0 | if (!directory.mkdirs()) { |
| 1174 | 0 | final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath()); |
| 1175 | 0 | throw new AnalysisException(msg); |
| 1176 | |
} |
| 1177 | 0 | return directory; |
| 1178 | |
} |
| 1179 | |
} |