| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.update.nvd; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.FileInputStream; |
| 22 | |
import java.io.FileNotFoundException; |
| 23 | |
import java.io.FileOutputStream; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.net.URL; |
| 27 | |
import java.util.concurrent.Callable; |
| 28 | |
import java.util.concurrent.ExecutorService; |
| 29 | |
import java.util.concurrent.Future; |
| 30 | |
import java.util.zip.GZIPInputStream; |
| 31 | |
import org.apache.commons.io.FileUtils; |
| 32 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 33 | |
import org.owasp.dependencycheck.data.update.exception.UpdateException; |
| 34 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 35 | |
import org.owasp.dependencycheck.utils.Downloader; |
| 36 | |
import org.owasp.dependencycheck.utils.Settings; |
| 37 | |
import org.slf4j.Logger; |
| 38 | |
import org.slf4j.LoggerFactory; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class DownloadTask implements Callable<Future<ProcessTask>> { |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(DownloadTask.class); |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 1 | public DownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException { |
| 63 | 1 | this.nvdCveInfo = nvdCveInfo; |
| 64 | 1 | this.processorService = processor; |
| 65 | 1 | this.cveDB = cveDB; |
| 66 | 1 | this.settings = settings; |
| 67 | |
|
| 68 | |
final File file1; |
| 69 | |
final File file2; |
| 70 | |
|
| 71 | |
try { |
| 72 | 1 | file1 = File.createTempFile("cve" + nvdCveInfo.getId() + '_', ".xml", Settings.getTempDirectory()); |
| 73 | 1 | file2 = File.createTempFile("cve_1_2_" + nvdCveInfo.getId() + '_', ".xml", Settings.getTempDirectory()); |
| 74 | 0 | } catch (IOException ex) { |
| 75 | 0 | throw new UpdateException("Unable to create temporary files", ex); |
| 76 | 1 | } |
| 77 | 1 | this.first = file1; |
| 78 | 1 | this.second = file2; |
| 79 | |
|
| 80 | 1 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
private final CveDB cveDB; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private final ExecutorService processorService; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private NvdCveInfo nvdCveInfo; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private final Settings settings; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public NvdCveInfo getNvdCveInfo() { |
| 104 | 0 | return nvdCveInfo; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public void setNvdCveInfo(NvdCveInfo nvdCveInfo) { |
| 113 | 0 | this.nvdCveInfo = nvdCveInfo; |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private File first; |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public File getFirst() { |
| 126 | 0 | return first; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public void setFirst(File first) { |
| 135 | 0 | this.first = first; |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
private File second; |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public File getSecond() { |
| 148 | 0 | return second; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public void setSecond(File second) { |
| 157 | 0 | this.second = second; |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
@Override |
| 161 | |
public Future<ProcessTask> call() throws Exception { |
| 162 | |
try { |
| 163 | 1 | Settings.setInstance(settings); |
| 164 | 1 | final URL url1 = new URL(nvdCveInfo.getUrl()); |
| 165 | 1 | final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl()); |
| 166 | 1 | LOGGER.info("Download Started for NVD CVE - {}", nvdCveInfo.getId()); |
| 167 | 1 | final long startDownload = System.currentTimeMillis(); |
| 168 | |
try { |
| 169 | 1 | Downloader.fetchFile(url1, first); |
| 170 | 1 | Downloader.fetchFile(url2, second); |
| 171 | 0 | } catch (DownloadFailedException ex) { |
| 172 | 0 | LOGGER.warn("Download Failed for NVD CVE - {}\nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 173 | 0 | if (Settings.getString(Settings.KEYS.PROXY_SERVER) == null) { |
| 174 | 0 | LOGGER.info( |
| 175 | |
"If you are behind a proxy you may need to configure dependency-check to use the proxy."); |
| 176 | |
} |
| 177 | 0 | LOGGER.debug("", ex); |
| 178 | 0 | return null; |
| 179 | 1 | } |
| 180 | 1 | if (url1.toExternalForm().endsWith(".xml.gz") && !isXml(first)) { |
| 181 | 1 | extractGzip(first); |
| 182 | |
} |
| 183 | 1 | if (url2.toExternalForm().endsWith(".xml.gz") && !isXml(second)) { |
| 184 | 1 | extractGzip(second); |
| 185 | |
} |
| 186 | |
|
| 187 | 2 | LOGGER.info("Download Complete for NVD CVE - {} ({} ms)", nvdCveInfo.getId(), |
| 188 | 1 | System.currentTimeMillis() - startDownload); |
| 189 | 1 | if (this.processorService == null) { |
| 190 | 1 | return null; |
| 191 | |
} |
| 192 | 0 | final ProcessTask task = new ProcessTask(cveDB, this, settings); |
| 193 | 0 | return this.processorService.submit(task); |
| 194 | |
|
| 195 | 0 | } catch (Throwable ex) { |
| 196 | 0 | LOGGER.warn("An exception occurred downloading NVD CVE - {}\nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 197 | 0 | LOGGER.debug("Download Task Failed", ex); |
| 198 | |
} finally { |
| 199 | 1 | Settings.cleanup(false); |
| 200 | 0 | } |
| 201 | 0 | return null; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
public void cleanup() { |
| 208 | 0 | boolean deleted = false; |
| 209 | |
try { |
| 210 | 0 | if (first != null && first.exists()) { |
| 211 | 0 | deleted = first.delete(); |
| 212 | |
} |
| 213 | |
} finally { |
| 214 | 0 | if (first != null && (first.exists() || !deleted)) { |
| 215 | 0 | first.deleteOnExit(); |
| 216 | |
} |
| 217 | |
} |
| 218 | |
try { |
| 219 | 0 | deleted = false; |
| 220 | 0 | if (second != null && second.exists()) { |
| 221 | 0 | deleted = second.delete(); |
| 222 | |
} |
| 223 | |
} finally { |
| 224 | 0 | if (second != null && (second.exists() || !deleted)) { |
| 225 | 0 | second.deleteOnExit(); |
| 226 | |
} |
| 227 | |
} |
| 228 | 0 | } |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public static boolean isXml(File file) { |
| 237 | 4 | if (file == null || !file.isFile()) { |
| 238 | 0 | return false; |
| 239 | |
} |
| 240 | 4 | InputStream is = null; |
| 241 | |
try { |
| 242 | 4 | is = new FileInputStream(file); |
| 243 | |
|
| 244 | 4 | final byte[] buf = new byte[5]; |
| 245 | 4 | int read = 0; |
| 246 | |
try { |
| 247 | 4 | read = is.read(buf); |
| 248 | 0 | } catch (IOException ex) { |
| 249 | 0 | return false; |
| 250 | 4 | } |
| 251 | 4 | return read == 5 |
| 252 | |
&& buf[0] == '<' |
| 253 | |
&& (buf[1] == '?') |
| 254 | |
&& (buf[2] == 'x' || buf[2] == 'X') |
| 255 | |
&& (buf[3] == 'm' || buf[3] == 'M') |
| 256 | |
&& (buf[4] == 'l' || buf[4] == 'L'); |
| 257 | 0 | } catch (FileNotFoundException ex) { |
| 258 | 0 | return false; |
| 259 | |
} finally { |
| 260 | 4 | if (is != null) { |
| 261 | |
try { |
| 262 | 4 | is.close(); |
| 263 | 0 | } catch (IOException ex) { |
| 264 | 8 | } |
| 265 | |
} |
| 266 | |
} |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
private void extractGzip(File file) throws FileNotFoundException, IOException { |
| 277 | 2 | final String originalPath = file.getPath(); |
| 278 | 2 | final File gzip = new File(originalPath + ".gz"); |
| 279 | 2 | if (gzip.isFile() && !gzip.delete()) { |
| 280 | 0 | gzip.deleteOnExit(); |
| 281 | |
} |
| 282 | 2 | if (!file.renameTo(gzip)) { |
| 283 | 0 | throw new IOException("Unable to rename '" + file.getPath() + "'"); |
| 284 | |
} |
| 285 | 2 | final File newfile = new File(originalPath); |
| 286 | |
|
| 287 | 2 | final byte[] buffer = new byte[4096]; |
| 288 | |
|
| 289 | 2 | GZIPInputStream cin = null; |
| 290 | 2 | FileOutputStream out = null; |
| 291 | |
try { |
| 292 | 2 | cin = new GZIPInputStream(new FileInputStream(gzip)); |
| 293 | 2 | out = new FileOutputStream(newfile); |
| 294 | |
|
| 295 | |
int len; |
| 296 | 310 | while ((len = cin.read(buffer)) > 0) { |
| 297 | 308 | out.write(buffer, 0, len); |
| 298 | |
} |
| 299 | |
} finally { |
| 300 | 2 | if (cin != null) { |
| 301 | |
try { |
| 302 | 2 | cin.close(); |
| 303 | 0 | } catch (IOException ex) { |
| 304 | 0 | LOGGER.trace("ignore", ex); |
| 305 | 2 | } |
| 306 | |
} |
| 307 | 2 | if (out != null) { |
| 308 | |
try { |
| 309 | 2 | out.close(); |
| 310 | 0 | } catch (IOException ex) { |
| 311 | 0 | LOGGER.trace("ignore", ex); |
| 312 | 2 | } |
| 313 | |
} |
| 314 | 2 | if (gzip.isFile()) { |
| 315 | 2 | FileUtils.deleteQuietly(gzip); |
| 316 | |
} |
| 317 | |
} |
| 318 | 2 | } |
| 319 | |
} |