| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.dependency; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.security.NoSuchAlgorithmException; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.Set; |
| 26 | |
import java.util.SortedSet; |
| 27 | |
import java.util.TreeSet; |
| 28 | |
import java.util.logging.Level; |
| 29 | |
import java.util.logging.Logger; |
| 30 | |
import org.owasp.dependencycheck.utils.Checksum; |
| 31 | |
import org.owasp.dependencycheck.utils.FileUtils; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 32 | public class Dependency implements Comparable<Dependency> { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private String actualFilePath; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private String filePath; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private String fileName; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
private String fileExtension; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private String md5sum; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private String sha1sum; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private Set<Identifier> identifiers; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private final EvidenceCollection vendorEvidence; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final EvidenceCollection productEvidence; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
private final EvidenceCollection versionEvidence; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 69 | public Dependency() { |
| 87 | 69 | vendorEvidence = new EvidenceCollection(); |
| 88 | 69 | productEvidence = new EvidenceCollection(); |
| 89 | 69 | versionEvidence = new EvidenceCollection(); |
| 90 | 69 | identifiers = new TreeSet<Identifier>(); |
| 91 | 69 | vulnerabilities = new TreeSet<Vulnerability>(new VulnerabilityComparator()); |
| 92 | 69 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public Dependency(File file) { |
| 100 | 48 | this(); |
| 101 | 48 | this.actualFilePath = file.getPath(); |
| 102 | 48 | this.filePath = this.actualFilePath; |
| 103 | 48 | this.fileName = file.getName(); |
| 104 | 48 | this.fileExtension = FileUtils.getFileExtension(fileName); |
| 105 | 48 | determineHashes(file); |
| 106 | 48 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public String getFileName() { |
| 114 | 208 | return this.fileName; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public void setFileName(String fileName) { |
| 123 | 26 | this.fileName = fileName; |
| 124 | 26 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public void setActualFilePath(String actualFilePath) { |
| 132 | 2 | this.actualFilePath = actualFilePath; |
| 133 | 2 | if (this.sha1sum == null) { |
| 134 | 2 | final File file = new File(this.actualFilePath); |
| 135 | 2 | determineHashes(file); |
| 136 | |
} |
| 137 | 2 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public String getActualFilePath() { |
| 145 | 167 | return this.actualFilePath; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public File getActualFile() { |
| 154 | 0 | return new File(this.actualFilePath); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public void setFilePath(String filePath) { |
| 163 | 21 | this.filePath = filePath; |
| 164 | 21 | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public String getFilePath() { |
| 176 | 28 | return this.filePath; |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public void setFileExtension(String fileExtension) { |
| 185 | 2 | this.fileExtension = fileExtension; |
| 186 | 2 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public String getFileExtension() { |
| 194 | 125 | return this.fileExtension; |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public String getMd5sum() { |
| 203 | 5 | return this.md5sum; |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public void setMd5sum(String md5sum) { |
| 212 | 51 | this.md5sum = md5sum; |
| 213 | 51 | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public String getSha1sum() { |
| 221 | 20 | return this.sha1sum; |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public void setSha1sum(String sha1sum) { |
| 230 | 53 | this.sha1sum = sha1sum; |
| 231 | 53 | } |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public Set<Identifier> getIdentifiers() { |
| 239 | 142 | return this.identifiers; |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public void setIdentifiers(Set<Identifier> identifiers) { |
| 248 | 1 | this.identifiers = identifiers; |
| 249 | 1 | } |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public void addIdentifier(String type, String value, String url) { |
| 259 | 6 | final Identifier i = new Identifier(type, value, url); |
| 260 | 6 | this.identifiers.add(i); |
| 261 | 6 | } |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public void addIdentifier(String type, String value, String url, Confidence confidence) { |
| 272 | 0 | final Identifier i = new Identifier(type, value, url); |
| 273 | 0 | i.setConfidence(confidence); |
| 274 | 0 | this.identifiers.add(i); |
| 275 | 0 | } |
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
public void addIdentifier(Identifier identifier) { |
| 283 | 88 | this.identifiers.add(identifier); |
| 284 | 88 | } |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
public EvidenceCollection getEvidence() { |
| 292 | 1 | return EvidenceCollection.merge(this.productEvidence, this.vendorEvidence, this.versionEvidence); |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
public EvidenceCollection getEvidenceUsed() { |
| 301 | 4 | return EvidenceCollection.mergeUsed(this.productEvidence, this.vendorEvidence, this.versionEvidence); |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public EvidenceCollection getVendorEvidence() { |
| 310 | 451 | return this.vendorEvidence; |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
public EvidenceCollection getProductEvidence() { |
| 319 | 1087 | return this.productEvidence; |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
public EvidenceCollection getVersionEvidence() { |
| 328 | 460 | return this.versionEvidence; |
| 329 | |
} |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | 69 | private List<Exception> analysisExceptions = new ArrayList<Exception>(); |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
public List<Exception> getAnalysisExceptions() { |
| 341 | 0 | return analysisExceptions; |
| 342 | |
} |
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public void setAnalysisExceptions(List<Exception> analysisExceptions) { |
| 350 | 0 | this.analysisExceptions = analysisExceptions; |
| 351 | 0 | } |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
public void addAnalysisException(Exception ex) { |
| 359 | 0 | this.analysisExceptions.add(ex); |
| 360 | 0 | } |
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
private String description; |
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
public String getDescription() { |
| 372 | 15 | return description; |
| 373 | |
} |
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
public void setDescription(String description) { |
| 381 | 19 | this.description = description; |
| 382 | 19 | } |
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
private String license; |
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
public String getLicense() { |
| 394 | 11 | return license; |
| 395 | |
} |
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
public void setLicense(String license) { |
| 403 | 7 | this.license = license; |
| 404 | 7 | } |
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
private SortedSet<Vulnerability> vulnerabilities; |
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
public SortedSet<Vulnerability> getVulnerabilities() { |
| 416 | 28 | return vulnerabilities; |
| 417 | |
} |
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
public void setVulnerabilities(SortedSet<Vulnerability> vulnerabilities) { |
| 425 | 0 | this.vulnerabilities = vulnerabilities; |
| 426 | 0 | } |
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
private void determineHashes(File file) { |
| 434 | 50 | String md5 = null; |
| 435 | 50 | String sha1 = null; |
| 436 | |
try { |
| 437 | 50 | md5 = Checksum.getMD5Checksum(file); |
| 438 | 48 | sha1 = Checksum.getSHA1Checksum(file); |
| 439 | 2 | } catch (IOException ex) { |
| 440 | 2 | final String msg = String.format("Unable to read '%s' to determine hashes.", file.getName()); |
| 441 | 2 | Logger.getLogger(Dependency.class.getName()).log(Level.WARNING, msg); |
| 442 | 2 | Logger.getLogger(Dependency.class.getName()).log(Level.FINE, null, ex); |
| 443 | 0 | } catch (NoSuchAlgorithmException ex) { |
| 444 | 0 | final String msg = "Unable to use MD5 of SHA1 checksums."; |
| 445 | 0 | Logger.getLogger(Dependency.class.getName()).log(Level.WARNING, msg); |
| 446 | 0 | Logger.getLogger(Dependency.class.getName()).log(Level.FINE, null, ex); |
| 447 | 50 | } |
| 448 | 50 | this.setMd5sum(md5); |
| 449 | 50 | this.setSha1sum(sha1); |
| 450 | 50 | } |
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
public void addVulnerability(Vulnerability vulnerability) { |
| 458 | 3 | this.vulnerabilities.add(vulnerability); |
| 459 | 3 | } |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | 69 | private Set<Dependency> relatedDependencies = new TreeSet<Dependency>(); |
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
public Set<Dependency> getRelatedDependencies() { |
| 471 | 4 | return relatedDependencies; |
| 472 | |
} |
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
public void setRelatedDependencies(Set<Dependency> relatedDependencies) { |
| 480 | 0 | this.relatedDependencies = relatedDependencies; |
| 481 | 0 | } |
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
public void addRelatedDependency(Dependency dependency) { |
| 489 | 0 | relatedDependencies.add(dependency); |
| 490 | 0 | } |
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
public int compareTo(Dependency o) { |
| 499 | 32 | return this.getFileName().compareToIgnoreCase(o.getFileName()); |
| 500 | |
} |
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
@Override |
| 509 | |
public boolean equals(Object obj) { |
| 510 | 0 | if (obj == null) { |
| 511 | 0 | return false; |
| 512 | |
} |
| 513 | 0 | if (getClass() != obj.getClass()) { |
| 514 | 0 | return false; |
| 515 | |
} |
| 516 | 0 | final Dependency other = (Dependency) obj; |
| 517 | 0 | if ((this.actualFilePath == null) ? (other.actualFilePath != null) : !this.actualFilePath.equals(other.actualFilePath)) { |
| 518 | 0 | return false; |
| 519 | |
} |
| 520 | 0 | if ((this.filePath == null) ? (other.filePath != null) : !this.filePath.equals(other.filePath)) { |
| 521 | 0 | return false; |
| 522 | |
} |
| 523 | 0 | if ((this.fileName == null) ? (other.fileName != null) : !this.fileName.equals(other.fileName)) { |
| 524 | 0 | return false; |
| 525 | |
} |
| 526 | 0 | if ((this.fileExtension == null) ? (other.fileExtension != null) : !this.fileExtension.equals(other.fileExtension)) { |
| 527 | 0 | return false; |
| 528 | |
} |
| 529 | 0 | if ((this.md5sum == null) ? (other.md5sum != null) : !this.md5sum.equals(other.md5sum)) { |
| 530 | 0 | return false; |
| 531 | |
} |
| 532 | 0 | if ((this.sha1sum == null) ? (other.sha1sum != null) : !this.sha1sum.equals(other.sha1sum)) { |
| 533 | 0 | return false; |
| 534 | |
} |
| 535 | 0 | if (this.identifiers != other.identifiers && (this.identifiers == null || !this.identifiers.equals(other.identifiers))) { |
| 536 | 0 | return false; |
| 537 | |
} |
| 538 | 0 | if (this.vendorEvidence != other.vendorEvidence && (this.vendorEvidence == null || !this.vendorEvidence.equals(other.vendorEvidence))) { |
| 539 | 0 | return false; |
| 540 | |
} |
| 541 | 0 | if (this.productEvidence != other.productEvidence && (this.productEvidence == null || !this.productEvidence.equals(other.productEvidence))) { |
| 542 | 0 | return false; |
| 543 | |
} |
| 544 | 0 | if (this.versionEvidence != other.versionEvidence && (this.versionEvidence == null || !this.versionEvidence.equals(other.versionEvidence))) { |
| 545 | 0 | return false; |
| 546 | |
} |
| 547 | 0 | if (this.analysisExceptions != other.analysisExceptions |
| 548 | |
&& (this.analysisExceptions == null || !this.analysisExceptions.equals(other.analysisExceptions))) { |
| 549 | 0 | return false; |
| 550 | |
} |
| 551 | 0 | if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) { |
| 552 | 0 | return false; |
| 553 | |
} |
| 554 | 0 | if ((this.license == null) ? (other.license != null) : !this.license.equals(other.license)) { |
| 555 | 0 | return false; |
| 556 | |
} |
| 557 | 0 | if (this.vulnerabilities != other.vulnerabilities && (this.vulnerabilities == null || !this.vulnerabilities.equals(other.vulnerabilities))) { |
| 558 | 0 | return false; |
| 559 | |
} |
| 560 | 0 | if (this.relatedDependencies != other.relatedDependencies |
| 561 | |
&& (this.relatedDependencies == null || !this.relatedDependencies.equals(other.relatedDependencies))) { |
| 562 | 0 | return false; |
| 563 | |
} |
| 564 | 0 | return true; |
| 565 | |
} |
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | |
@Override |
| 573 | |
public int hashCode() { |
| 574 | 135 | int hash = 3; |
| 575 | 135 | hash = 47 * hash + (this.actualFilePath != null ? this.actualFilePath.hashCode() : 0); |
| 576 | 135 | hash = 47 * hash + (this.filePath != null ? this.filePath.hashCode() : 0); |
| 577 | 135 | hash = 47 * hash + (this.fileName != null ? this.fileName.hashCode() : 0); |
| 578 | 135 | hash = 47 * hash + (this.fileExtension != null ? this.fileExtension.hashCode() : 0); |
| 579 | 135 | hash = 47 * hash + (this.md5sum != null ? this.md5sum.hashCode() : 0); |
| 580 | 135 | hash = 47 * hash + (this.sha1sum != null ? this.sha1sum.hashCode() : 0); |
| 581 | 135 | hash = 47 * hash + (this.identifiers != null ? this.identifiers.hashCode() : 0); |
| 582 | 135 | hash = 47 * hash + (this.vendorEvidence != null ? this.vendorEvidence.hashCode() : 0); |
| 583 | 135 | hash = 47 * hash + (this.productEvidence != null ? this.productEvidence.hashCode() : 0); |
| 584 | 135 | hash = 47 * hash + (this.versionEvidence != null ? this.versionEvidence.hashCode() : 0); |
| 585 | 135 | hash = 47 * hash + (this.analysisExceptions != null ? this.analysisExceptions.hashCode() : 0); |
| 586 | 135 | hash = 47 * hash + (this.description != null ? this.description.hashCode() : 0); |
| 587 | 135 | hash = 47 * hash + (this.license != null ? this.license.hashCode() : 0); |
| 588 | 135 | hash = 47 * hash + (this.vulnerabilities != null ? this.vulnerabilities.hashCode() : 0); |
| 589 | 135 | hash = 47 * hash + (this.relatedDependencies != null ? this.relatedDependencies.hashCode() : 0); |
| 590 | 135 | return hash; |
| 591 | |
} |
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | |
@Override |
| 599 | |
public String toString() { |
| 600 | 0 | return "Dependency{ fileName='" + fileName + "', actualFilePath='" + actualFilePath + "', filePath='" + filePath + "'}"; |
| 601 | |
} |
| 602 | |
} |