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