Coverage Report - org.owasp.dependencycheck.xml.suppression.SuppressionRule
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressionRule
75%
114/152
67%
76/112
2.867
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.xml.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  
  * @author Jeremy Long
 30  
  */
 31  497
 public class SuppressionRule {
 32  
 
 33  
     /**
 34  
      * The file path for the suppression.
 35  
      */
 36  
     private PropertyType filePath;
 37  
 
 38  
     /**
 39  
      * Get the value of filePath.
 40  
      *
 41  
      * @return the value of filePath
 42  
      */
 43  
     public PropertyType getFilePath() {
 44  1
         return filePath;
 45  
     }
 46  
 
 47  
     /**
 48  
      * Set the value of filePath.
 49  
      *
 50  
      * @param filePath new value of filePath
 51  
      */
 52  
     public void setFilePath(PropertyType filePath) {
 53  91
         this.filePath = filePath;
 54  91
     }
 55  
     /**
 56  
      * The sha1 hash.
 57  
      */
 58  
     private String sha1;
 59  
 
 60  
     /**
 61  
      * Get the value of sha1.
 62  
      *
 63  
      * @return the value of sha1
 64  
      */
 65  
     public String getSha1() {
 66  1
         return sha1;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Set the value of sha1.
 71  
      *
 72  
      * @param sha1 new value of sha1
 73  
      */
 74  
     public void setSha1(String sha1) {
 75  6
         this.sha1 = sha1;
 76  6
     }
 77  
     /**
 78  
      * A list of CPEs to suppression
 79  
      */
 80  497
     private List<PropertyType> cpe = new ArrayList<PropertyType>();
 81  
 
 82  
     /**
 83  
      * Get the value of cpe.
 84  
      *
 85  
      * @return the value of cpe
 86  
      */
 87  
     public List<PropertyType> getCpe() {
 88  1
         return cpe;
 89  
     }
 90  
 
 91  
     /**
 92  
      * Set the value of cpe.
 93  
      *
 94  
      * @param cpe new value of cpe
 95  
      */
 96  
     public void setCpe(List<PropertyType> cpe) {
 97  1
         this.cpe = cpe;
 98  1
     }
 99  
 
 100  
     /**
 101  
      * Adds the cpe to the cpe list.
 102  
      *
 103  
      * @param cpe the cpe to add
 104  
      */
 105  
     public void addCpe(PropertyType cpe) {
 106  708
         this.cpe.add(cpe);
 107  708
     }
 108  
 
 109  
     /**
 110  
      * Returns whether or not this suppression rule as CPE entries.
 111  
      *
 112  
      * @return whether or not this suppression rule as CPE entries
 113  
      */
 114  
     public boolean hasCpe() {
 115  43
         return !cpe.isEmpty();
 116  
     }
 117  
     /**
 118  
      * The list of cvssBelow scores.
 119  
      */
 120  497
     private List<Float> cvssBelow = new ArrayList<Float>();
 121  
 
 122  
     /**
 123  
      * Get the value of cvssBelow.
 124  
      *
 125  
      * @return the value of cvssBelow
 126  
      */
 127  
     public List<Float> getCvssBelow() {
 128  1
         return cvssBelow;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Set the value of cvssBelow.
 133  
      *
 134  
      * @param cvssBelow new value of cvssBelow
 135  
      */
 136  
     public void setCvssBelow(List<Float> cvssBelow) {
 137  1
         this.cvssBelow = cvssBelow;
 138  1
     }
 139  
 
 140  
     /**
 141  
      * Adds the cvss to the cvssBelow list.
 142  
      *
 143  
      * @param cvss the cvss to add
 144  
      */
 145  
     public void addCvssBelow(Float cvss) {
 146  7
         this.cvssBelow.add(cvss);
 147  7
     }
 148  
 
 149  
     /**
 150  
      * Returns whether or not this suppression rule has cvss suppressions.
 151  
      *
 152  
      * @return whether or not this suppression rule has cvss suppressions
 153  
      */
 154  
     public boolean hasCvssBelow() {
 155  32
         return !cvssBelow.isEmpty();
 156  
     }
 157  
     /**
 158  
      * The list of cwe entries to suppress.
 159  
      */
 160  497
     private List<String> cwe = new ArrayList<String>();
 161  
 
 162  
     /**
 163  
      * Get the value of cwe.
 164  
      *
 165  
      * @return the value of cwe
 166  
      */
 167  
     public List<String> getCwe() {
 168  1
         return cwe;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Set the value of cwe.
 173  
      *
 174  
      * @param cwe new value of cwe
 175  
      */
 176  
     public void setCwe(List<String> cwe) {
 177  1
         this.cwe = cwe;
 178  1
     }
 179  
 
 180  
     /**
 181  
      * Adds the cwe to the cwe list.
 182  
      *
 183  
      * @param cwe the cwe to add
 184  
      */
 185  
     public void addCwe(String cwe) {
 186  2
         this.cwe.add(cwe);
 187  2
     }
 188  
 
 189  
     /**
 190  
      * Returns whether this suppression rule has CWE entries.
 191  
      *
 192  
      * @return whether this suppression rule has CWE entries
 193  
      */
 194  
     public boolean hasCwe() {
 195  33
         return !cwe.isEmpty();
 196  
     }
 197  
     /**
 198  
      * The list of cve entries to suppress.
 199  
      */
 200  497
     private List<String> cve = new ArrayList<String>();
 201  
 
 202  
     /**
 203  
      * Get the value of cve.
 204  
      *
 205  
      * @return the value of cve
 206  
      */
 207  
     public List<String> getCve() {
 208  1
         return cve;
 209  
     }
 210  
 
 211  
     /**
 212  
      * Set the value of cve.
 213  
      *
 214  
      * @param cve new value of cve
 215  
      */
 216  
     public void setCve(List<String> cve) {
 217  1
         this.cve = cve;
 218  1
     }
 219  
 
 220  
     /**
 221  
      * Adds the cve to the cve list.
 222  
      *
 223  
      * @param cve the cve to add
 224  
      */
 225  
     public void addCve(String cve) {
 226  32
         this.cve.add(cve);
 227  32
     }
 228  
 
 229  
     /**
 230  
      * Returns whether this suppression rule has CVE entries.
 231  
      *
 232  
      * @return whether this suppression rule has CVE entries
 233  
      */
 234  
     public boolean hasCve() {
 235  43
         return !cve.isEmpty();
 236  
     }
 237  
     /**
 238  
      * A Maven GAV to suppression.
 239  
      */
 240  497
     private PropertyType gav = null;
 241  
 
 242  
     /**
 243  
      * Get the value of Maven GAV.
 244  
      *
 245  
      * @return the value of gav
 246  
      */
 247  
     public PropertyType getGav() {
 248  0
         return gav;
 249  
     }
 250  
 
 251  
     /**
 252  
      * Set the value of Maven GAV.
 253  
      *
 254  
      * @param gav new value of Maven gav
 255  
      */
 256  
     public void setGav(PropertyType gav) {
 257  386
         this.gav = gav;
 258  386
     }
 259  
 
 260  
     /**
 261  
      * Returns whether or not this suppression rule as GAV entries.
 262  
      *
 263  
      * @return whether or not this suppression rule as GAV entries
 264  
      */
 265  
     public boolean hasGav() {
 266  0
         return gav != null;
 267  
     }
 268  
 
 269  
     /**
 270  
      * A flag indicating whether or not the suppression rule is a core/base rule that should not be included in the resulting
 271  
      * report in the "suppressed" section.
 272  
      */
 273  
     private boolean base;
 274  
 
 275  
     /**
 276  
      * Get the value of base.
 277  
      *
 278  
      * @return the value of base
 279  
      */
 280  
     public boolean isBase() {
 281  17
         return base;
 282  
     }
 283  
 
 284  
     /**
 285  
      * Set the value of base.
 286  
      *
 287  
      * @param base new value of base
 288  
      */
 289  
     public void setBase(boolean base) {
 290  484
         this.base = base;
 291  484
     }
 292  
 
 293  
     /**
 294  
      * Processes a given dependency to determine if any CPE, CVE, CWE, or CVSS scores should be suppressed. If any should be, they
 295  
      * are removed from the dependency.
 296  
      *
 297  
      * @param dependency a project dependency to analyze
 298  
      */
 299  
     public void process(Dependency dependency) {
 300  538
         if (filePath != null && !filePath.matches(dependency.getFilePath())) {
 301  56
             return;
 302  
         }
 303  482
         if (sha1 != null && !sha1.equalsIgnoreCase(dependency.getSha1sum())) {
 304  1
             return;
 305  
         }
 306  481
         if (gav != null) {
 307  441
             final Iterator<Identifier> itr = dependency.getIdentifiers().iterator();
 308  441
             boolean gavFound = false;
 309  885
             while (itr.hasNext()) {
 310  445
                 final Identifier i = itr.next();
 311  445
                 if (identifierMatches("maven", this.gav, i)) {
 312  1
                     gavFound = true;
 313  1
                     break;
 314  
                 }
 315  444
             }
 316  441
             if (!gavFound) {
 317  440
                 return;
 318  
             }
 319  
         }
 320  
 
 321  41
         if (this.hasCpe()) {
 322  28
             final Iterator<Identifier> itr = dependency.getIdentifiers().iterator();
 323  86
             while (itr.hasNext()) {
 324  58
                 final Identifier i = itr.next();
 325  58
                 for (PropertyType c : this.cpe) {
 326  90
                     if (identifierMatches("cpe", c, i)) {
 327  7
                         if (!isBase()) {
 328  4
                             dependency.addSuppressedIdentifier(i);
 329  
                         }
 330  7
                         itr.remove();
 331  7
                         break;
 332  
                     }
 333  83
                 }
 334  58
             }
 335  
         }
 336  41
         if (hasCve() || hasCwe() || hasCvssBelow()) {
 337  13
             final Iterator<Vulnerability> itr = dependency.getVulnerabilities().iterator();
 338  27
             while (itr.hasNext()) {
 339  14
                 boolean remove = false;
 340  14
                 final Vulnerability v = itr.next();
 341  14
                 for (String entry : this.cve) {
 342  12
                     if (entry.equalsIgnoreCase(v.getName())) {
 343  1
                         remove = true;
 344  1
                         break;
 345  
                     }
 346  11
                 }
 347  14
                 if (!remove) {
 348  13
                     for (String entry : this.cwe) {
 349  1
                         if (v.getCwe() != null) {
 350  1
                             final String toMatch = String.format("CWE-%s ", entry);
 351  1
                             final String toTest = v.getCwe().substring(0, toMatch.length()).toUpperCase();
 352  1
                             if (toTest.equals(toMatch)) {
 353  1
                                 remove = true;
 354  1
                                 break;
 355  
                             }
 356  
                         }
 357  0
                     }
 358  
                 }
 359  14
                 if (!remove) {
 360  12
                     for (float cvss : this.cvssBelow) {
 361  3
                         if (v.getCvssScore() < cvss) {
 362  1
                             remove = true;
 363  1
                             break;
 364  
                         }
 365  2
                     }
 366  
                 }
 367  14
                 if (remove) {
 368  3
                     if (!isBase()) {
 369  3
                         dependency.addSuppressedVulnerability(v);
 370  
                     }
 371  3
                     itr.remove();
 372  
                 }
 373  14
             }
 374  
         }
 375  41
     }
 376  
 
 377  
     /**
 378  
      * Identifies if the cpe specified by the cpe suppression rule does not specify a version.
 379  
      *
 380  
      * @param c a suppression rule identifier
 381  
      * @return true if the property type does not specify a version; otherwise false
 382  
      */
 383  
     boolean cpeHasNoVersion(PropertyType c) {
 384  94
         return !c.isRegex() && countCharacter(c.getValue(), ':') <= 3;
 385  
     }
 386  
 
 387  
     /**
 388  
      * Counts the number of occurrences of the character found within the string.
 389  
      *
 390  
      * @param str the string to check
 391  
      * @param c the character to count
 392  
      * @return the number of times the character is found in the string
 393  
      */
 394  
     int countCharacter(String str, char c) {
 395  92
         int count = 0;
 396  92
         int pos = str.indexOf(c) + 1;
 397  374
         while (pos > 0) {
 398  282
             count += 1;
 399  282
             pos = str.indexOf(c, pos) + 1;
 400  
         }
 401  92
         return count;
 402  
     }
 403  
 
 404  
     /**
 405  
      * Determines if the cpeEntry specified as a PropertyType matches the given Identifier.
 406  
      *
 407  
      * @param identifierType the type of identifier ("cpe", "maven", etc.)
 408  
      * @param suppressionEntry a suppression rule entry
 409  
      * @param identifier a CPE identifier to check
 410  
      * @return true if the entry matches; otherwise false
 411  
      */
 412  
     boolean identifierMatches(String identifierType, PropertyType suppressionEntry, Identifier identifier) {
 413  544
         if (identifierType.equals(identifier.getType())) {
 414  97
             if (suppressionEntry.matches(identifier.getValue())) {
 415  5
                 return true;
 416  92
             } else if ("cpe".equals(identifierType) && cpeHasNoVersion(suppressionEntry)) {
 417  85
                 if (suppressionEntry.isCaseSensitive()) {
 418  0
                     return identifier.getValue().startsWith(suppressionEntry.getValue());
 419  
                 } else {
 420  85
                     final String id = identifier.getValue().toLowerCase();
 421  85
                     final String check = suppressionEntry.getValue().toLowerCase();
 422  85
                     return id.startsWith(check);
 423  
                 }
 424  
             }
 425  
         }
 426  454
         return false;
 427  
     }
 428  
 
 429  
     /**
 430  
      * Standard toString implementation.
 431  
      *
 432  
      * @return a string representation of this object
 433  
      */
 434  
     @Override
 435  
     public String toString() {
 436  0
         final StringBuilder sb = new StringBuilder(64);
 437  0
         sb.append("SuppressionRule{");
 438  0
         if (filePath != null) {
 439  0
             sb.append("filePath=").append(filePath).append(',');
 440  
         }
 441  0
         if (sha1 != null) {
 442  0
             sb.append("sha1=").append(sha1).append(',');
 443  
         }
 444  0
         if (gav != null) {
 445  0
             sb.append("gav=").append(gav).append(',');
 446  
         }
 447  0
         if (cpe != null && !cpe.isEmpty()) {
 448  0
             sb.append("cpe={");
 449  0
             for (PropertyType pt : cpe) {
 450  0
                 sb.append(pt).append(',');
 451  0
             }
 452  0
             sb.append('}');
 453  
         }
 454  0
         if (cwe != null && !cwe.isEmpty()) {
 455  0
             sb.append("cwe={");
 456  0
             for (String s : cwe) {
 457  0
                 sb.append(s).append(',');
 458  0
             }
 459  0
             sb.append('}');
 460  
         }
 461  0
         if (cve != null && !cve.isEmpty()) {
 462  0
             sb.append("cve={");
 463  0
             for (String s : cve) {
 464  0
                 sb.append(s).append(',');
 465  0
             }
 466  0
             sb.append('}');
 467  
         }
 468  0
         if (cvssBelow != null && !cvssBelow.isEmpty()) {
 469  0
             sb.append("cvssBelow={");
 470  0
             for (Float s : cvssBelow) {
 471  0
                 sb.append(s).append(',');
 472  0
             }
 473  0
             sb.append('}');
 474  
         }
 475  0
         sb.append('}');
 476  0
         return sb.toString();
 477  
     }
 478  
 }