| 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 org.apache.commons.io.FileUtils; |
| 21 | |
import org.owasp.dependencycheck.Engine; |
| 22 | |
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; |
| 23 | |
import org.owasp.dependencycheck.data.central.CentralSearch; |
| 24 | |
import org.owasp.dependencycheck.data.nexus.MavenArtifact; |
| 25 | |
import org.owasp.dependencycheck.dependency.Confidence; |
| 26 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 27 | |
import org.owasp.dependencycheck.dependency.Evidence; |
| 28 | |
import org.owasp.dependencycheck.xml.pom.PomUtils; |
| 29 | |
import org.slf4j.Logger; |
| 30 | |
import org.slf4j.LoggerFactory; |
| 31 | |
|
| 32 | |
import java.io.File; |
| 33 | |
import java.io.FileFilter; |
| 34 | |
import java.io.FileNotFoundException; |
| 35 | |
import java.io.IOException; |
| 36 | |
import java.net.MalformedURLException; |
| 37 | |
import java.net.URL; |
| 38 | |
import java.util.List; |
| 39 | |
import org.owasp.dependencycheck.exception.InitializationException; |
| 40 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 41 | |
import org.owasp.dependencycheck.utils.Downloader; |
| 42 | |
import org.owasp.dependencycheck.utils.FileFilterBuilder; |
| 43 | |
import org.owasp.dependencycheck.utils.InvalidSettingException; |
| 44 | |
import org.owasp.dependencycheck.utils.Settings; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 8 | public class CentralAnalyzer extends AbstractFileTypeAnalyzer { |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(CentralAnalyzer.class); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private static final String ANALYZER_NAME = "Central Analyzer"; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private static final String SUPPORTED_EXTENSIONS = "jar"; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 8 | private volatile boolean errorFlag = false; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private CentralSearch searcher; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 8 | private final boolean enabled = checkEnabled(); |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public boolean isEnabled() { |
| 96 | 864 | return enabled; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
private boolean checkEnabled() { |
| 106 | 8 | boolean retval = false; |
| 107 | |
|
| 108 | |
try { |
| 109 | 8 | if (Settings.getBoolean(Settings.KEYS.ANALYZER_CENTRAL_ENABLED)) { |
| 110 | 6 | if (!Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED) |
| 111 | 0 | || NexusAnalyzer.DEFAULT_URL.equals(Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL))) { |
| 112 | 6 | LOGGER.debug("Enabling the Central analyzer"); |
| 113 | 6 | retval = true; |
| 114 | |
} else { |
| 115 | 0 | LOGGER.info("Nexus analyzer is enabled, disabling the Central Analyzer"); |
| 116 | |
} |
| 117 | |
} else { |
| 118 | 2 | LOGGER.info("Central analyzer disabled"); |
| 119 | |
} |
| 120 | 0 | } catch (InvalidSettingException ise) { |
| 121 | 0 | LOGGER.warn("Invalid setting. Disabling the Central analyzer"); |
| 122 | 8 | } |
| 123 | 8 | return retval; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public void initializeFileTypeAnalyzer() throws InitializationException { |
| 133 | 0 | LOGGER.debug("Initializing Central analyzer"); |
| 134 | 0 | LOGGER.debug("Central analyzer enabled: {}", isEnabled()); |
| 135 | 0 | if (isEnabled()) { |
| 136 | 0 | final String searchUrl = Settings.getString(Settings.KEYS.ANALYZER_CENTRAL_URL); |
| 137 | 0 | LOGGER.debug("Central Analyzer URL: {}", searchUrl); |
| 138 | |
try { |
| 139 | 0 | searcher = new CentralSearch(new URL(searchUrl)); |
| 140 | 0 | } catch (MalformedURLException ex) { |
| 141 | 0 | setEnabled(false); |
| 142 | 0 | throw new InitializationException("The configured URL to Maven Central is malformed: " + searchUrl, ex); |
| 143 | 0 | } |
| 144 | |
} |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public String getName() { |
| 154 | 20 | return ANALYZER_NAME; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
@Override |
| 164 | |
protected String getAnalyzerEnabledSettingKey() { |
| 165 | 2 | return Settings.KEYS.ANALYZER_CENTRAL_ENABLED; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public AnalysisPhase getAnalysisPhase() { |
| 175 | 6 | return ANALYSIS_PHASE; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 1 | private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(SUPPORTED_EXTENSIONS).build(); |
| 182 | |
|
| 183 | |
@Override |
| 184 | |
protected FileFilter getFileFilter() { |
| 185 | 858 | return FILTER; |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
@Override |
| 196 | |
public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException { |
| 197 | 0 | if (errorFlag || !isEnabled()) { |
| 198 | 0 | return; |
| 199 | |
} |
| 200 | |
|
| 201 | |
try { |
| 202 | 0 | final List<MavenArtifact> mas = searcher.searchSha1(dependency.getSha1sum()); |
| 203 | 0 | final Confidence confidence = mas.size() > 1 ? Confidence.HIGH : Confidence.HIGHEST; |
| 204 | 0 | for (MavenArtifact ma : mas) { |
| 205 | 0 | LOGGER.debug("Central analyzer found artifact ({}) for dependency ({})", ma, dependency.getFileName()); |
| 206 | 0 | dependency.addAsEvidence("central", ma, confidence); |
| 207 | 0 | boolean pomAnalyzed = false; |
| 208 | 0 | for (Evidence e : dependency.getVendorEvidence()) { |
| 209 | 0 | if ("pom".equals(e.getSource())) { |
| 210 | 0 | pomAnalyzed = true; |
| 211 | 0 | break; |
| 212 | |
} |
| 213 | 0 | } |
| 214 | 0 | if (!pomAnalyzed && ma.getPomUrl() != null) { |
| 215 | 0 | File pomFile = null; |
| 216 | |
try { |
| 217 | 0 | final File baseDir = Settings.getTempDirectory(); |
| 218 | 0 | pomFile = File.createTempFile("pom", ".xml", baseDir); |
| 219 | 0 | if (!pomFile.delete()) { |
| 220 | 0 | LOGGER.warn("Unable to fetch pom.xml for {} from Central; " |
| 221 | 0 | + "this could result in undetected CPE/CVEs.", dependency.getFileName()); |
| 222 | 0 | LOGGER.debug("Unable to delete temp file"); |
| 223 | |
} |
| 224 | 0 | LOGGER.debug("Downloading {}", ma.getPomUrl()); |
| 225 | 0 | Downloader.fetchFile(new URL(ma.getPomUrl()), pomFile); |
| 226 | 0 | PomUtils.analyzePOM(dependency, pomFile); |
| 227 | |
|
| 228 | 0 | } catch (DownloadFailedException ex) { |
| 229 | 0 | LOGGER.warn("Unable to download pom.xml for {} from Central; " |
| 230 | 0 | + "this could result in undetected CPE/CVEs.", dependency.getFileName()); |
| 231 | |
} finally { |
| 232 | 0 | if (pomFile != null && pomFile.exists() && !FileUtils.deleteQuietly(pomFile)) { |
| 233 | 0 | LOGGER.debug("Failed to delete temporary pom file {}", pomFile.toString()); |
| 234 | 0 | pomFile.deleteOnExit(); |
| 235 | |
} |
| 236 | |
} |
| 237 | |
} |
| 238 | |
|
| 239 | 0 | } |
| 240 | 0 | } catch (IllegalArgumentException iae) { |
| 241 | 0 | LOGGER.info("invalid sha1-hash on {}", dependency.getFileName()); |
| 242 | 0 | } catch (FileNotFoundException fnfe) { |
| 243 | 0 | LOGGER.debug("Artifact not found in repository: '{}", dependency.getFileName()); |
| 244 | 0 | } catch (IOException ioe) { |
| 245 | 0 | LOGGER.debug("Could not connect to Central search", ioe); |
| 246 | 0 | errorFlag = true; |
| 247 | 0 | } |
| 248 | 0 | } |
| 249 | |
} |