| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.suppression; |
| 19 | |
|
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.List; |
| 23 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 24 | |
import org.owasp.dependencycheck.dependency.Identifier; |
| 25 | |
import org.owasp.dependencycheck.dependency.Vulnerability; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 176 | public class SuppressionRule { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
private PropertyType filePath; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public PropertyType getFilePath() { |
| 44 | |
return filePath; |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public void setFilePath(PropertyType filePath) { |
| 53 | |
this.filePath = filePath; |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private String sha1; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public String getSha1() { |
| 66 | |
return sha1; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public void setSha1(String sha1) { |
| 75 | |
this.sha1 = sha1; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | 176 | private List<PropertyType> cpe = new ArrayList<PropertyType>(); |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public List<PropertyType> getCpe() { |
| 88 | |
return cpe; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public void setCpe(List<PropertyType> cpe) { |
| 97 | |
this.cpe = cpe; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public void addCpe(PropertyType cpe) { |
| 106 | 140 | this.cpe.add(cpe); |
| 107 | 140 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public boolean hasCpe() { |
| 115 | 44 | return cpe.size() > 0; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 176 | private List<Float> cvssBelow = new ArrayList<Float>(); |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public List<Float> getCvssBelow() { |
| 128 | |
return cvssBelow; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public void setCvssBelow(List<Float> cvssBelow) { |
| 137 | |
this.cvssBelow = cvssBelow; |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void addCvssBelow(Float cvss) { |
| 146 | 28 | this.cvssBelow.add(cvss); |
| 147 | 28 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public boolean hasCvssBelow() { |
| 155 | 32 | return cvssBelow.size() > 0; |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 176 | private List<String> cwe = new ArrayList<String>(); |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public List<String> getCwe() { |
| 168 | |
return cwe; |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void setCwe(List<String> cwe) { |
| 177 | |
this.cwe = cwe; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public void addCwe(String cwe) { |
| 186 | 8 | this.cwe.add(cwe); |
| 187 | 8 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
public boolean hasCwe() { |
| 195 | 36 | return cwe.size() > 0; |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | 176 | private List<String> cve = new ArrayList<String>(); |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
public List<String> getCve() { |
| 208 | |
return cve; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public void setCve(List<String> cve) { |
| 217 | |
this.cve = cve; |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public void addCve(String cve) { |
| 226 | 44 | this.cve.add(cve); |
| 227 | 44 | } |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public boolean hasCve() { |
| 235 | 44 | return cve.size() > 0; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | 176 | private PropertyType gav = null; |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public PropertyType getGav() { |
| 248 | |
return gav; |
| 249 | |
} |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
public void setGav(PropertyType gav) { |
| 257 | |
this.gav = gav; |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
public boolean hasGav() { |
| 266 | 0 | return gav != null; |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public void process(Dependency dependency) { |
| 276 | 72 | if (filePath != null && !filePath.matches(dependency.getFilePath())) { |
| 277 | 0 | return; |
| 278 | |
} |
| 279 | 72 | if (sha1 != null && !sha1.equalsIgnoreCase(dependency.getSha1sum())) { |
| 280 | 4 | return; |
| 281 | |
} |
| 282 | 68 | if (gav != null) { |
| 283 | 36 | final Iterator<Identifier> itr = dependency.getIdentifiers().iterator(); |
| 284 | 36 | boolean gavFound = false; |
| 285 | 84 | while (itr.hasNext()) { |
| 286 | 52 | final Identifier i = itr.next(); |
| 287 | 52 | if (identifierMatches("maven", this.gav, i)) { |
| 288 | 4 | gavFound = true; |
| 289 | 4 | break; |
| 290 | |
} |
| 291 | 48 | } |
| 292 | 36 | if (!gavFound) { |
| 293 | 32 | return; |
| 294 | |
} |
| 295 | |
} |
| 296 | |
|
| 297 | 36 | if (this.hasCpe()) { |
| 298 | 16 | final Iterator<Identifier> itr = dependency.getIdentifiers().iterator(); |
| 299 | 56 | while (itr.hasNext()) { |
| 300 | 40 | final Identifier i = itr.next(); |
| 301 | 40 | for (PropertyType c : this.cpe) { |
| 302 | 84 | if (identifierMatches("cpe", c, i)) { |
| 303 | 28 | dependency.addSuppressedIdentifier(i); |
| 304 | 28 | itr.remove(); |
| 305 | 28 | break; |
| 306 | |
} |
| 307 | 56 | } |
| 308 | 40 | } |
| 309 | |
} |
| 310 | 36 | if (hasCve() || hasCwe() || hasCvssBelow()) { |
| 311 | 20 | final Iterator<Vulnerability> itr = dependency.getVulnerabilities().iterator(); |
| 312 | 40 | while (itr.hasNext()) { |
| 313 | 20 | boolean remove = false; |
| 314 | 20 | final Vulnerability v = itr.next(); |
| 315 | 20 | for (String entry : this.cve) { |
| 316 | 12 | if (entry.equalsIgnoreCase(v.getName())) { |
| 317 | 4 | remove = true; |
| 318 | 4 | break; |
| 319 | |
} |
| 320 | 8 | } |
| 321 | 20 | if (!remove) { |
| 322 | 16 | for (String entry : this.cwe) { |
| 323 | 4 | if (v.getCwe() != null) { |
| 324 | 4 | final String toMatch = String.format("CWE-%s ", entry); |
| 325 | 4 | final String toTest = v.getCwe().substring(0, toMatch.length()).toUpperCase(); |
| 326 | 4 | if (toTest.equals(toMatch)) { |
| 327 | 4 | remove = true; |
| 328 | 4 | break; |
| 329 | |
} |
| 330 | |
} |
| 331 | 0 | } |
| 332 | |
} |
| 333 | 20 | if (!remove) { |
| 334 | 12 | for (float cvss : this.cvssBelow) { |
| 335 | 12 | if (v.getCvssScore() < cvss) { |
| 336 | 4 | remove = true; |
| 337 | 4 | break; |
| 338 | |
} |
| 339 | 8 | } |
| 340 | |
} |
| 341 | 20 | if (remove) { |
| 342 | 12 | dependency.addSuppressedVulnerability(v); |
| 343 | 12 | itr.remove(); |
| 344 | |
} |
| 345 | 20 | } |
| 346 | |
} |
| 347 | 36 | } |
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
boolean cpeHasNoVersion(PropertyType c) { |
| 356 | 92 | if (c.isRegex()) { |
| 357 | 8 | return false; |
| 358 | |
} |
| 359 | 84 | if (countCharacter(c.getValue(), ':') == 3) { |
| 360 | 48 | return true; |
| 361 | |
} |
| 362 | 36 | return false; |
| 363 | |
} |
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
int countCharacter(String str, char c) { |
| 373 | 96 | int count = 0; |
| 374 | 96 | int pos = str.indexOf(c) + 1; |
| 375 | 408 | while (pos > 0) { |
| 376 | 312 | count += 1; |
| 377 | 312 | pos = str.indexOf(c, pos) + 1; |
| 378 | |
} |
| 379 | 96 | return count; |
| 380 | |
} |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
boolean identifierMatches(String identifierType, PropertyType suppressionEntry, Identifier identifier) { |
| 391 | 172 | if (identifierType.equals(identifier.getType())) { |
| 392 | 112 | if (suppressionEntry.matches(identifier.getValue())) { |
| 393 | 28 | return true; |
| 394 | 84 | } else if ("cpe".equals(identifierType) && cpeHasNoVersion(suppressionEntry)) { |
| 395 | 44 | if (suppressionEntry.isCaseSensitive()) { |
| 396 | 0 | return identifier.getValue().startsWith(suppressionEntry.getValue()); |
| 397 | |
} else { |
| 398 | 44 | final String id = identifier.getValue().toLowerCase(); |
| 399 | 44 | final String check = suppressionEntry.getValue().toLowerCase(); |
| 400 | 44 | return id.startsWith(check); |
| 401 | |
} |
| 402 | |
} |
| 403 | |
} |
| 404 | 100 | return false; |
| 405 | |
} |
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
@Override |
| 413 | |
public String toString() { |
| 414 | 0 | final StringBuilder sb = new StringBuilder(); |
| 415 | 0 | sb.append("SuppressionRule{"); |
| 416 | 0 | if (filePath != null) { |
| 417 | 0 | sb.append("filePath=").append(filePath).append(","); |
| 418 | |
} |
| 419 | 0 | if (sha1 != null) { |
| 420 | 0 | sb.append("sha1=").append(sha1).append(","); |
| 421 | |
} |
| 422 | 0 | if (gav != null) { |
| 423 | 0 | sb.append("gav=").append(gav).append(","); |
| 424 | |
} |
| 425 | 0 | if (cpe != null && cpe.size() > 0) { |
| 426 | 0 | sb.append("cpe={"); |
| 427 | 0 | for (PropertyType pt : cpe) { |
| 428 | 0 | sb.append(pt).append(","); |
| 429 | 0 | } |
| 430 | 0 | sb.append("}"); |
| 431 | |
} |
| 432 | 0 | if (cwe != null && cwe.size() > 0) { |
| 433 | 0 | sb.append("cwe={"); |
| 434 | 0 | for (String s : cwe) { |
| 435 | 0 | sb.append(s).append(","); |
| 436 | 0 | } |
| 437 | 0 | sb.append("}"); |
| 438 | |
} |
| 439 | 0 | if (cve != null && cve.size() > 0) { |
| 440 | 0 | sb.append("cve={"); |
| 441 | 0 | for (String s : cve) { |
| 442 | 0 | sb.append(s).append(","); |
| 443 | 0 | } |
| 444 | 0 | sb.append("}"); |
| 445 | |
} |
| 446 | 0 | if (cvssBelow != null && cvssBelow.size() > 0) { |
| 447 | 0 | sb.append("cvssBelow={"); |
| 448 | 0 | for (Float s : cvssBelow) { |
| 449 | 0 | sb.append(s).append(","); |
| 450 | 0 | } |
| 451 | 0 | sb.append("}"); |
| 452 | |
} |
| 453 | 0 | sb.append("}"); |
| 454 | 0 | return sb.toString(); |
| 455 | |
} |
| 456 | |
} |