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