From 2190c0229c5df9182978bcf70dd43b946985cc01 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 6 Feb 2016 08:11:24 -0500 Subject: [PATCH] added check to see if the file is xml prior to unzipping it per issue #441 --- .../data/update/nvd/DownloadTask.java | 47 +++++++++++++++++-- .../data/update/nvd/DownloadTaskTest.java | 21 +++++++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/nvd/DownloadTask.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/nvd/DownloadTask.java index 32cb44e81..ab6ea3a8f 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/nvd/DownloadTask.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/update/nvd/DownloadTask.java @@ -22,10 +22,12 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.logging.Level; import java.util.zip.GZIPInputStream; import org.apache.commons.io.FileUtils; import org.owasp.dependencycheck.data.nvdcve.CveDB; @@ -176,15 +178,15 @@ public class DownloadTask implements Callable> { LOGGER.debug("", ex); return null; } - if (url1.toExternalForm().endsWith(".xml.gz")) { + if (url1.toExternalForm().endsWith(".xml.gz") && !isXml(first)) { extractGzip(first); } - if (url2.toExternalForm().endsWith(".xml.gz")) { + if (url2.toExternalForm().endsWith(".xml.gz") && !isXml(second)) { extractGzip(second); } LOGGER.info("Download Complete for NVD CVE - {} ({} ms)", nvdCveInfo.getId(), - System.currentTimeMillis() - startDownload); + System.currentTimeMillis() - startDownload); if (this.processorService == null) { return null; } @@ -226,6 +228,45 @@ public class DownloadTask implements Callable> { } } + /** + * Checks the file header to see if it is an XML file. + * + * @param file the file to check + * @return true if the file is XML + */ + public static boolean isXml(File file) { + if (file == null || !file.isFile()) { + return false; + } + InputStream is = null; + try { + is = new FileInputStream(file); + + byte[] buf = new byte[5]; + int read = 0; + try { + read = is.read(buf); + } catch (IOException ex) { + return false; + } + return read == 5 + && buf[0] == '<' + && (buf[1] == '?') + && (buf[2] == 'x' || buf[2] == 'X') + && (buf[3] == 'm' || buf[3] == 'M') + && (buf[4] == 'l' || buf[4] == 'L'); + } catch (FileNotFoundException ex) { + return false; + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ex) { + } + } + } + } + /** * Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified. * diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/update/nvd/DownloadTaskTest.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/update/nvd/DownloadTaskTest.java index 573f0739e..dbea57fcd 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/update/nvd/DownloadTaskTest.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/update/nvd/DownloadTaskTest.java @@ -17,25 +17,26 @@ */ package org.owasp.dependencycheck.data.update.nvd; -import org.owasp.dependencycheck.data.update.nvd.ProcessTask; -import org.owasp.dependencycheck.data.update.nvd.DownloadTask; +import java.io.File; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import org.junit.After; import org.junit.AfterClass; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.owasp.dependencycheck.BaseTest; import org.owasp.dependencycheck.data.nvdcve.CveDB; -import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo; import org.owasp.dependencycheck.utils.Settings; /** * * @author Jeremy Long */ -public class DownloadTaskTest { +public class DownloadTaskTest extends BaseTest { public DownloadTaskTest() { } @@ -74,4 +75,16 @@ public class DownloadTaskTest { Future result = instance.call(); assertNull(result); } + + /** + * Test of isXml(file). + */ + @Test + public void testIsXML() { + File f = getResourceAsFile(this, "nvdcve-modified.xml"); + assertTrue(DownloadTask.isXml(f)); + f = getResourceAsFile(this, "file.tar.gz"); + assertFalse(DownloadTask.isXml(f)); + + } }