Coverage Report - org.owasp.dependencycheck.analyzer.AbstractSuppressionAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSuppressionAnalyzer
75%
48/64
71%
10/14
4
 
 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.analyzer;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 import java.util.regex.Pattern;
 28  
 import org.owasp.dependencycheck.suppression.SuppressionParseException;
 29  
 import org.owasp.dependencycheck.suppression.SuppressionParser;
 30  
 import org.owasp.dependencycheck.suppression.SuppressionRule;
 31  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 32  
 import org.owasp.dependencycheck.utils.Downloader;
 33  
 import org.owasp.dependencycheck.utils.FileUtils;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 import org.slf4j.Logger;
 36  
 import org.slf4j.LoggerFactory;
 37  
 import org.xml.sax.SAXException;
 38  
 
 39  
 /**
 40  
  * Abstract base suppression analyzer that contains methods for parsing the
 41  
  * suppression xml file.
 42  
  *
 43  
  * @author Jeremy Long
 44  
  */
 45  32
 public abstract class AbstractSuppressionAnalyzer extends AbstractAnalyzer {
 46  
 
 47  
     /**
 48  
      * The Logger for use throughout the class
 49  
      */
 50  2
     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSuppressionAnalyzer.class);
 51  
 
 52  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 53  
     /**
 54  
      * Returns a list of file EXTENSIONS supported by this analyzer.
 55  
      *
 56  
      * @return a list of file EXTENSIONS supported by this analyzer.
 57  
      */
 58  
     public Set<String> getSupportedExtensions() {
 59  2
         return null;
 60  
     }
 61  
 
 62  
     //</editor-fold>
 63  
     /**
 64  
      * The initialize method loads the suppression XML file.
 65  
      *
 66  
      * @throws Exception thrown if there is an exception
 67  
      */
 68  
     @Override
 69  
     public void initialize() throws Exception {
 70  14
         super.initialize();
 71  14
         loadSuppressionData();
 72  12
     }
 73  
 
 74  
     /**
 75  
      * The list of suppression rules
 76  
      */
 77  
     private List<SuppressionRule> rules;
 78  
 
 79  
     /**
 80  
      * Get the value of rules.
 81  
      *
 82  
      * @return the value of rules
 83  
      */
 84  
     public List<SuppressionRule> getRules() {
 85  52
         return rules;
 86  
     }
 87  
 
 88  
     /**
 89  
      * Set the value of rules.
 90  
      *
 91  
      * @param rules new value of rules
 92  
      */
 93  
     public void setRules(List<SuppressionRule> rules) {
 94  0
         this.rules = rules;
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Loads the suppression rules file.
 99  
      *
 100  
      * @throws SuppressionParseException thrown if the XML cannot be parsed.
 101  
      */
 102  
     private void loadSuppressionData() throws SuppressionParseException {
 103  14
         final SuppressionParser parser = new SuppressionParser();
 104  14
         File file = null;
 105  
         try {
 106  14
             rules = parser.parseSuppressionRules(this.getClass().getClassLoader().getResourceAsStream("dependencycheck-base-suppression.xml"));
 107  0
         } catch (SuppressionParseException ex) {
 108  0
             LOGGER.error("Unable to parse the base suppression data file");
 109  0
             LOGGER.debug("Unable to parse the base suppression data file", ex);
 110  0
         } catch (SAXException ex) {
 111  0
             LOGGER.error("Unable to parse the base suppression data file");
 112  0
             LOGGER.debug("Unable to parse the base suppression data file", ex);
 113  14
         }
 114  14
         final String suppressionFilePath = Settings.getString(Settings.KEYS.SUPPRESSION_FILE);
 115  14
         if (suppressionFilePath == null) {
 116  8
             return;
 117  
         }
 118  6
         boolean deleteTempFile = false;
 119  
         try {
 120  6
             final Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE);
 121  6
             if (uriRx.matcher(suppressionFilePath).matches()) {
 122  2
                 deleteTempFile = true;
 123  2
                 file = FileUtils.getTempFile("suppression", "xml");
 124  2
                 final URL url = new URL(suppressionFilePath);
 125  
                 try {
 126  2
                     Downloader.fetchFile(url, file, false);
 127  0
                 } catch (DownloadFailedException ex) {
 128  0
                     Downloader.fetchFile(url, file, true);
 129  2
                 }
 130  2
             } else {
 131  4
                 file = new File(suppressionFilePath);
 132  4
                 if (!file.exists()) {
 133  4
                     final InputStream suppressionsFromClasspath = this.getClass().getClassLoader().getResourceAsStream(suppressionFilePath);
 134  4
                     if (suppressionsFromClasspath != null) {
 135  2
                         deleteTempFile = true;
 136  2
                         file = FileUtils.getTempFile("suppression", "xml");
 137  
                         try {
 138  2
                             org.apache.commons.io.FileUtils.copyInputStreamToFile(suppressionsFromClasspath, file);
 139  0
                         } catch (IOException ex) {
 140  0
                             throwSuppressionParseException("Unable to locate suppressions file in classpath", ex);
 141  2
                         }
 142  
                     }
 143  
                 }
 144  
             }
 145  
 
 146  6
             if (file != null) {
 147  
                 try {
 148  
                     //rules = parser.parseSuppressionRules(file);
 149  6
                     rules.addAll(parser.parseSuppressionRules(file));
 150  4
                     LOGGER.debug("{} suppression rules were loaded.", rules.size());
 151  2
                 } catch (SuppressionParseException ex) {
 152  2
                     LOGGER.warn("Unable to parse suppression xml file '{}'", file.getPath());
 153  2
                     LOGGER.warn(ex.getMessage());
 154  2
                     LOGGER.debug("", ex);
 155  2
                     throw ex;
 156  4
                 }
 157  
             }
 158  0
         } catch (DownloadFailedException ex) {
 159  0
             throwSuppressionParseException("Unable to fetch the configured suppression file", ex);
 160  0
         } catch (MalformedURLException ex) {
 161  0
             throwSuppressionParseException("Configured suppression file has an invalid URL", ex);
 162  2
         } catch (IOException ex) {
 163  2
             throwSuppressionParseException("Unable to create temp file for suppressions", ex);
 164  
         } finally {
 165  6
             if (deleteTempFile && file != null) {
 166  4
                 FileUtils.delete(file);
 167  
             }
 168  
         }
 169  4
     }
 170  
 
 171  
     /**
 172  
      * Utility method to throw parse exceptions.
 173  
      *
 174  
      * @param message the exception message
 175  
      * @param exception the cause of the exception
 176  
      * @throws SuppressionParseException throws the generated
 177  
      * SuppressionParseException
 178  
      */
 179  
     private void throwSuppressionParseException(String message, Exception exception) throws SuppressionParseException {
 180  2
         LOGGER.warn(message);
 181  2
         LOGGER.debug("", exception);
 182  2
         throw new SuppressionParseException(message, exception);
 183  
     }
 184  
 }