| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 12 | public abstract class AbstractSuppressionAnalyzer extends AbstractAnalyzer { |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSuppressionAnalyzer.class); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public Set<String> getSupportedExtensions() { |
| 57 | 1 | return null; |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
@Override |
| 67 | |
public void initialize() throws Exception { |
| 68 | 5 | super.initialize(); |
| 69 | 5 | loadSuppressionData(); |
| 70 | 4 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private List<SuppressionRule> rules; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public List<SuppressionRule> getRules() { |
| 83 | 14 | return rules; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public void setRules(List<SuppressionRule> rules) { |
| 92 | 0 | this.rules = rules; |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
private void loadSuppressionData() throws SuppressionParseException { |
| 101 | 5 | final SuppressionParser parser = new SuppressionParser(); |
| 102 | 5 | File file = null; |
| 103 | |
try { |
| 104 | 5 | rules = parser.parseSuppressionRules(this.getClass().getClassLoader().getResourceAsStream("dependencycheck-base-suppression.xml")); |
| 105 | 0 | } catch (SuppressionParseException ex) { |
| 106 | 0 | LOGGER.debug("Unable to parse the base suppression data file", ex); |
| 107 | 5 | } |
| 108 | 5 | final String suppressionFilePath = Settings.getString(Settings.KEYS.SUPPRESSION_FILE); |
| 109 | 5 | if (suppressionFilePath == null) { |
| 110 | 2 | return; |
| 111 | |
} |
| 112 | 3 | boolean deleteTempFile = false; |
| 113 | |
try { |
| 114 | 3 | final Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE); |
| 115 | 3 | if (uriRx.matcher(suppressionFilePath).matches()) { |
| 116 | 1 | deleteTempFile = true; |
| 117 | 1 | file = FileUtils.getTempFile("suppression", "xml"); |
| 118 | 1 | final URL url = new URL(suppressionFilePath); |
| 119 | |
try { |
| 120 | 1 | Downloader.fetchFile(url, file, false); |
| 121 | 0 | } catch (DownloadFailedException ex) { |
| 122 | 0 | Downloader.fetchFile(url, file, true); |
| 123 | 1 | } |
| 124 | 1 | } else { |
| 125 | 2 | file = new File(suppressionFilePath); |
| 126 | 2 | if (!file.exists()) { |
| 127 | 2 | final InputStream suppressionsFromClasspath = this.getClass().getClassLoader().getResourceAsStream(suppressionFilePath); |
| 128 | 2 | if (suppressionsFromClasspath != null) { |
| 129 | 1 | deleteTempFile = true; |
| 130 | 1 | file = FileUtils.getTempFile("suppression", "xml"); |
| 131 | |
try { |
| 132 | 1 | org.apache.commons.io.FileUtils.copyInputStreamToFile(suppressionsFromClasspath, file); |
| 133 | 0 | } catch (IOException ex) { |
| 134 | 0 | throwSuppressionParseException("Unable to locate suppressions file in classpath", ex); |
| 135 | 1 | } |
| 136 | |
} |
| 137 | |
} |
| 138 | |
} |
| 139 | |
|
| 140 | 3 | if (file != null) { |
| 141 | |
try { |
| 142 | |
|
| 143 | 3 | rules.addAll(parser.parseSuppressionRules(file)); |
| 144 | 2 | LOGGER.debug("{} suppression rules were loaded.", rules.size()); |
| 145 | 1 | } catch (SuppressionParseException ex) { |
| 146 | 1 | LOGGER.warn("Unable to parse suppression xml file '{}'", file.getPath()); |
| 147 | 1 | LOGGER.warn(ex.getMessage()); |
| 148 | 1 | LOGGER.debug("", ex); |
| 149 | 1 | throw ex; |
| 150 | 2 | } |
| 151 | |
} |
| 152 | 0 | } catch (DownloadFailedException ex) { |
| 153 | 0 | throwSuppressionParseException("Unable to fetch the configured suppression file", ex); |
| 154 | 0 | } catch (MalformedURLException ex) { |
| 155 | 0 | throwSuppressionParseException("Configured suppression file has an invalid URL", ex); |
| 156 | 1 | } catch (IOException ex) { |
| 157 | 1 | throwSuppressionParseException("Unable to create temp file for suppressions", ex); |
| 158 | |
} finally { |
| 159 | 3 | if (deleteTempFile && file != null) { |
| 160 | 2 | FileUtils.delete(file); |
| 161 | |
} |
| 162 | |
} |
| 163 | 2 | } |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
private void throwSuppressionParseException(String message, Exception exception) throws SuppressionParseException { |
| 173 | 1 | LOGGER.warn(message); |
| 174 | 1 | LOGGER.debug("", exception); |
| 175 | 1 | throw new SuppressionParseException(message, exception); |
| 176 | |
} |
| 177 | |
} |