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