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