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