| 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.Serializable; |
| 21 | |
import java.net.MalformedURLException; |
| 22 | |
import java.util.HashSet; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.Set; |
| 26 | |
import java.util.TreeSet; |
| 27 | |
import java.util.logging.Level; |
| 28 | |
import java.util.logging.Logger; |
| 29 | |
import org.apache.commons.lang.StringUtils; |
| 30 | |
import org.owasp.dependencycheck.utils.DependencyVersion; |
| 31 | |
import org.owasp.dependencycheck.utils.DependencyVersionUtil; |
| 32 | |
import org.owasp.dependencycheck.utils.Filter; |
| 33 | |
import org.owasp.dependencycheck.utils.UrlStringUtils; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class EvidenceCollection implements Serializable, Iterable<Evidence> { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 1 | private static final Logger LOGGER = Logger.getLogger(EvidenceCollection.class.getName()); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 59 | private static final Filter<Evidence> HIGHEST_CONFIDENCE = new Filter<Evidence>() { |
| 50 | |
public boolean passes(Evidence evidence) { |
| 51 | 58 | return evidence.getConfidence() == Confidence.HIGHEST; |
| 52 | |
} |
| 53 | |
}; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 49 | private static final Filter<Evidence> HIGH_CONFIDENCE = new Filter<Evidence>() { |
| 58 | |
public boolean passes(Evidence evidence) { |
| 59 | 48 | return evidence.getConfidence() == Confidence.HIGH; |
| 60 | |
} |
| 61 | |
}; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 1 | private static final Filter<Evidence> MEDIUM_CONFIDENCE = new Filter<Evidence>() { |
| 66 | |
public boolean passes(Evidence evidence) { |
| 67 | 32 | return evidence.getConfidence() == Confidence.MEDIUM; |
| 68 | |
} |
| 69 | |
}; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 1 | private static final Filter<Evidence> LOW_CONFIDENCE = new Filter<Evidence>() { |
| 74 | |
public boolean passes(Evidence evidence) { |
| 75 | 32 | return evidence.getConfidence() == Confidence.LOW; |
| 76 | |
} |
| 77 | |
}; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 1 | private static final Filter<Evidence> EVIDENCE_USED = new Filter<Evidence>() { |
| 82 | |
public boolean passes(Evidence evidence) { |
| 83 | 1057 | return evidence.isUsed(); |
| 84 | |
} |
| 85 | |
}; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public final Iterable<Evidence> iterator(Confidence confidence) { |
| 94 | 20 | if (confidence == Confidence.HIGHEST) { |
| 95 | 6 | return EvidenceCollection.HIGHEST_CONFIDENCE.filter(this.list); |
| 96 | 14 | } else if (confidence == Confidence.HIGH) { |
| 97 | 6 | return EvidenceCollection.HIGH_CONFIDENCE.filter(this.list); |
| 98 | 8 | } else if (confidence == Confidence.MEDIUM) { |
| 99 | 4 | return EvidenceCollection.MEDIUM_CONFIDENCE.filter(this.list); |
| 100 | |
} else { |
| 101 | 4 | return EvidenceCollection.LOW_CONFIDENCE.filter(this.list); |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private final Set<Evidence> list; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
private final Set<String> weightedStrings; |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | 132 | public EvidenceCollection() { |
| 117 | 132 | list = new TreeSet<Evidence>(); |
| 118 | 132 | weightedStrings = new HashSet<String>(); |
| 119 | 132 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public void addEvidence(Evidence e) { |
| 127 | 14702 | list.add(e); |
| 128 | 14702 | } |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public void addEvidence(String source, String name, String value, Confidence confidence) { |
| 139 | 14695 | final Evidence e = new Evidence(source, name, value, confidence); |
| 140 | 14695 | addEvidence(e); |
| 141 | 14695 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public void addWeighting(String str) { |
| 155 | 15 | weightedStrings.add(str); |
| 156 | 15 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public Set<String> getWeighting() { |
| 165 | 13 | return weightedStrings; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public Set<Evidence> getEvidence() { |
| 174 | 9 | return list; |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public Set<Evidence> getEvidence(String source) { |
| 184 | 0 | if (source == null) { |
| 185 | 0 | return null; |
| 186 | |
} |
| 187 | 0 | final Set<Evidence> ret = new HashSet<Evidence>(); |
| 188 | 0 | for (Evidence e : list) { |
| 189 | 0 | if (source.equals(e.getSource())) { |
| 190 | 0 | ret.add(e); |
| 191 | |
} |
| 192 | 0 | } |
| 193 | 0 | return ret; |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public Set<Evidence> getEvidence(String source, String name) { |
| 204 | 8 | if (source == null || name == null) { |
| 205 | 0 | return null; |
| 206 | |
} |
| 207 | 8 | final Set<Evidence> ret = new HashSet<Evidence>(); |
| 208 | 8 | for (Evidence e : list) { |
| 209 | 11 | if (source.equals(e.getSource()) && name.equals(e.getName())) { |
| 210 | 5 | ret.add(e); |
| 211 | |
} |
| 212 | 11 | } |
| 213 | 8 | return ret; |
| 214 | |
} |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
public Iterator<Evidence> iterator() { |
| 222 | 126 | return list.iterator(); |
| 223 | |
} |
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public boolean containsUsedString(String text) { |
| 232 | 121 | if (text == null) { |
| 233 | 0 | return false; |
| 234 | |
} |
| 235 | 121 | final String textToTest = text.toLowerCase(); |
| 236 | |
|
| 237 | 121 | for (Evidence e : EvidenceCollection.EVIDENCE_USED.filter(this)) { |
| 238 | |
|
| 239 | 643 | final String value = urlCorrection(e.getValue().toLowerCase()).replaceAll("[\\s_-]", ""); |
| 240 | 643 | if (value.contains(textToTest)) { |
| 241 | 48 | return true; |
| 242 | |
} |
| 243 | 595 | } |
| 244 | 73 | return false; |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public boolean containsUsedVersion(DependencyVersion version) { |
| 254 | 0 | if (version == null) { |
| 255 | 0 | return false; |
| 256 | |
} |
| 257 | |
|
| 258 | 0 | for (Evidence e : EvidenceCollection.EVIDENCE_USED.filter(this)) { |
| 259 | 0 | final DependencyVersion value = DependencyVersionUtil.parseVersion(e.getValue()); |
| 260 | 0 | if (value != null && value.matchesAtLeastThreeLevels(version)) { |
| 261 | 0 | return true; |
| 262 | |
} |
| 263 | 0 | } |
| 264 | 0 | return false; |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
public boolean contains(Confidence confidence) { |
| 274 | 14 | for (Evidence e : list) { |
| 275 | 63 | if (e.getConfidence().equals(confidence)) { |
| 276 | 12 | return true; |
| 277 | |
} |
| 278 | 51 | } |
| 279 | 2 | return false; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
public static EvidenceCollection mergeUsed(EvidenceCollection... ec) { |
| 289 | 1 | final EvidenceCollection ret = new EvidenceCollection(); |
| 290 | 4 | for (EvidenceCollection col : ec) { |
| 291 | 3 | for (Evidence e : col.list) { |
| 292 | 2 | if (e.isUsed()) { |
| 293 | 1 | ret.addEvidence(e); |
| 294 | |
} |
| 295 | 2 | } |
| 296 | |
} |
| 297 | 1 | return ret; |
| 298 | |
} |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public static EvidenceCollection merge(EvidenceCollection... ec) { |
| 307 | 11 | final EvidenceCollection ret = new EvidenceCollection(); |
| 308 | 44 | for (EvidenceCollection col : ec) { |
| 309 | 33 | ret.list.addAll(col.list); |
| 310 | 33 | ret.weightedStrings.addAll(col.weightedStrings); |
| 311 | |
} |
| 312 | 11 | return ret; |
| 313 | |
} |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
public static Set<Evidence> mergeForDisplay(EvidenceCollection... ec) { |
| 322 | 0 | final Set<Evidence> ret = new TreeSet<Evidence>(); |
| 323 | 0 | for (EvidenceCollection col : ec) { |
| 324 | 0 | for (Evidence e : col) { |
| 325 | 0 | if (e.isUsed()) { |
| 326 | 0 | final Evidence newEvidence = new Evidence(e.getSource(), e.getName(), e.getValue(), null); |
| 327 | 0 | newEvidence.setUsed(true); |
| 328 | 0 | ret.add(newEvidence); |
| 329 | |
} |
| 330 | 0 | } |
| 331 | |
} |
| 332 | 0 | return ret; |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
@Override |
| 341 | |
public String toString() { |
| 342 | 3 | final StringBuilder sb = new StringBuilder(); |
| 343 | 3 | for (Evidence e : this.list) { |
| 344 | 12 | sb.append(e.getValue()).append(' '); |
| 345 | 12 | } |
| 346 | 3 | return sb.toString(); |
| 347 | |
} |
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
public int size() { |
| 355 | 13 | return list.size(); |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
private String urlCorrection(String value) { |
| 377 | 643 | if (value == null || !UrlStringUtils.containsUrl(value)) { |
| 378 | 627 | return value; |
| 379 | |
} |
| 380 | 16 | final StringBuilder sb = new StringBuilder(value.length()); |
| 381 | 16 | final String[] parts = value.split("\\s"); |
| 382 | 32 | for (String part : parts) { |
| 383 | 16 | if (UrlStringUtils.isUrl(part)) { |
| 384 | |
try { |
| 385 | 16 | final List<String> data = UrlStringUtils.extractImportantUrlData(part); |
| 386 | 16 | sb.append(' ').append(StringUtils.join(data, ' ')); |
| 387 | 0 | } catch (MalformedURLException ex) { |
| 388 | 0 | LOGGER.log(Level.FINE, "error parsing " + part, ex); |
| 389 | 0 | sb.append(' ').append(part); |
| 390 | 16 | } |
| 391 | |
} else { |
| 392 | 0 | sb.append(' ').append(part); |
| 393 | |
} |
| 394 | |
} |
| 395 | 16 | return sb.toString().trim(); |
| 396 | |
} |
| 397 | |
} |