| 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 | 23 | public class SuppressionRule { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
private PropertyType filePath; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public PropertyType getFilePath() { |
| 44 | 1 | return filePath; |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public void setFilePath(PropertyType filePath) { |
| 53 | 8 | this.filePath = filePath; |
| 54 | 8 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private String sha1; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public String getSha1() { |
| 66 | 1 | return sha1; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public void setSha1(String sha1) { |
| 75 | 4 | this.sha1 = sha1; |
| 76 | 4 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | 23 | private List<PropertyType> cpe = new ArrayList<PropertyType>(); |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public List<PropertyType> getCpe() { |
| 88 | 1 | return cpe; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public void setCpe(List<PropertyType> cpe) { |
| 97 | 1 | this.cpe = cpe; |
| 98 | 1 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public void addCpe(PropertyType cpe) { |
| 106 | 8 | this.cpe.add(cpe); |
| 107 | 8 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public boolean hasCpe() { |
| 115 | 10 | return cpe.size() > 0; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 23 | private List<Float> cvssBelow = new ArrayList<Float>(); |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public List<Float> getCvssBelow() { |
| 128 | 1 | return cvssBelow; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public void setCvssBelow(List<Float> cvssBelow) { |
| 137 | 1 | this.cvssBelow = cvssBelow; |
| 138 | 1 | } |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void addCvssBelow(Float cvss) { |
| 146 | 5 | this.cvssBelow.add(cvss); |
| 147 | 5 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public boolean hasCvssBelow() { |
| 155 | 7 | return cvssBelow.size() > 0; |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 23 | private List<String> cwe = new ArrayList<String>(); |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public List<String> getCwe() { |
| 168 | 1 | return cwe; |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void setCwe(List<String> cwe) { |
| 177 | 1 | this.cwe = cwe; |
| 178 | 1 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public void addCwe(String cwe) { |
| 186 | 2 | this.cwe.add(cwe); |
| 187 | 2 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
public boolean hasCwe() { |
| 195 | 8 | return cwe.size() > 0; |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | 23 | private List<String> cve = new ArrayList<String>(); |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
public List<String> getCve() { |
| 208 | 1 | return cve; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public void setCve(List<String> cve) { |
| 217 | 1 | this.cve = cve; |
| 218 | 1 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public void addCve(String cve) { |
| 226 | 7 | this.cve.add(cve); |
| 227 | 7 | } |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public boolean hasCve() { |
| 235 | 10 | return cve.size() > 0; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void process(Dependency dependency) { |
| 245 | 9 | if (filePath != null && !filePath.matches(dependency.getFilePath())) { |
| 246 | 0 | return; |
| 247 | |
} |
| 248 | 9 | if (sha1 != null && !sha1.equalsIgnoreCase(dependency.getSha1sum())) { |
| 249 | 1 | return; |
| 250 | |
} |
| 251 | 8 | if (this.hasCpe()) { |
| 252 | 3 | final Iterator<Identifier> itr = dependency.getIdentifiers().iterator(); |
| 253 | 8 | while (itr.hasNext()) { |
| 254 | 5 | final Identifier i = itr.next(); |
| 255 | 5 | for (PropertyType c : this.cpe) { |
| 256 | 9 | if (cpeMatches(c, i)) { |
| 257 | 4 | itr.remove(); |
| 258 | 4 | break; |
| 259 | |
} |
| 260 | 5 | } |
| 261 | 5 | } |
| 262 | |
} |
| 263 | 8 | if (hasCve() || hasCwe() || hasCvssBelow()) { |
| 264 | 5 | final Iterator<Vulnerability> itr = dependency.getVulnerabilities().iterator(); |
| 265 | 10 | while (itr.hasNext()) { |
| 266 | 5 | boolean remove = false; |
| 267 | 5 | final Vulnerability v = itr.next(); |
| 268 | 5 | for (String entry : this.cve) { |
| 269 | 3 | if (entry.equalsIgnoreCase(v.getName())) { |
| 270 | 1 | remove = true; |
| 271 | 1 | break; |
| 272 | |
} |
| 273 | 2 | } |
| 274 | 5 | if (!remove) { |
| 275 | 4 | for (String entry : this.cwe) { |
| 276 | 1 | if (v.getCwe() != null) { |
| 277 | 1 | final String toMatch = String.format("CWE-%s ", entry); |
| 278 | 1 | final String toTest = v.getCwe().substring(0, toMatch.length()).toUpperCase(); |
| 279 | 1 | if (toTest.equals(toMatch)) { |
| 280 | 1 | remove = true; |
| 281 | 1 | break; |
| 282 | |
} |
| 283 | |
} |
| 284 | 0 | } |
| 285 | |
} |
| 286 | 5 | if (!remove) { |
| 287 | 3 | for (float cvss : this.cvssBelow) { |
| 288 | 3 | if (v.getCvssScore() < cvss) { |
| 289 | 1 | remove = true; |
| 290 | 1 | break; |
| 291 | |
} |
| 292 | 2 | } |
| 293 | |
} |
| 294 | 5 | if (remove) { |
| 295 | 3 | itr.remove(); |
| 296 | |
} |
| 297 | 5 | } |
| 298 | |
} |
| 299 | 8 | } |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
boolean cpeHasNoVersion(PropertyType c) { |
| 308 | 14 | if (c.isRegex()) { |
| 309 | 2 | return false; |
| 310 | |
} |
| 311 | 12 | if (countCharacter(c.getValue(), ':') == 3) { |
| 312 | 3 | return true; |
| 313 | |
} |
| 314 | 9 | return false; |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
int countCharacter(String str, char c) { |
| 325 | 15 | int count = 0; |
| 326 | 15 | int pos = str.indexOf(c) + 1; |
| 327 | 66 | while (pos > 0) { |
| 328 | 51 | count += 1; |
| 329 | 51 | pos = str.indexOf(c, pos) + 1; |
| 330 | |
} |
| 331 | 15 | return count; |
| 332 | |
} |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
boolean cpeMatches(PropertyType cpeEntry, Identifier identifier) { |
| 342 | 16 | if (cpeEntry.matches(identifier.getValue())) { |
| 343 | 5 | return true; |
| 344 | 11 | } else if (cpeHasNoVersion(cpeEntry)) { |
| 345 | 2 | if (cpeEntry.isCaseSensitive()) { |
| 346 | 0 | if (identifier.getValue().startsWith(cpeEntry.getValue())) { |
| 347 | 0 | return true; |
| 348 | |
} |
| 349 | |
} else { |
| 350 | 2 | final String id = identifier.getValue().toLowerCase(); |
| 351 | 2 | final String check = cpeEntry.getValue().toLowerCase(); |
| 352 | 2 | if (id.startsWith(check)) { |
| 353 | 2 | return true; |
| 354 | |
} |
| 355 | |
} |
| 356 | |
} |
| 357 | 9 | return false; |
| 358 | |
} |
| 359 | |
} |