| 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.FileNotFoundException; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.sql.SQLException; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.Map; |
| 26 | |
import java.util.concurrent.Callable; |
| 27 | |
import javax.xml.parsers.ParserConfigurationException; |
| 28 | |
import javax.xml.parsers.SAXParser; |
| 29 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 30 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 31 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties; |
| 32 | |
import org.owasp.dependencycheck.data.update.exception.UpdateException; |
| 33 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 34 | |
import org.owasp.dependencycheck.utils.Settings; |
| 35 | |
import org.owasp.dependencycheck.utils.XmlUtils; |
| 36 | |
import org.slf4j.Logger; |
| 37 | |
import org.slf4j.LoggerFactory; |
| 38 | |
import org.xml.sax.SAXException; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public class ProcessTask implements Callable<ProcessTask> { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(ProcessTask.class); |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | private UpdateException exception = null; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public UpdateException getException() { |
| 63 | 0 | return exception; |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public void setException(UpdateException exception) { |
| 72 | 0 | this.exception = exception; |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final CveDB cveDB; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
private final DownloadTask filePair; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private final DatabaseProperties properties; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
private final Settings settings; |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 0 | public ProcessTask(final CveDB cveDB, final DownloadTask filePair, Settings settings) { |
| 102 | 0 | this.cveDB = cveDB; |
| 103 | 0 | this.filePair = filePair; |
| 104 | 0 | this.properties = cveDB.getDatabaseProperties(); |
| 105 | 0 | this.settings = settings; |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
@Override |
| 116 | |
public ProcessTask call() throws Exception { |
| 117 | |
try { |
| 118 | 0 | Settings.setInstance(settings); |
| 119 | 0 | processFiles(); |
| 120 | 0 | } catch (UpdateException ex) { |
| 121 | 0 | this.exception = ex; |
| 122 | |
} finally { |
| 123 | 0 | Settings.cleanup(false); |
| 124 | 0 | } |
| 125 | 0 | return this; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
protected void importXML(File file, File oldVersion) throws ParserConfigurationException, |
| 143 | |
SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException { |
| 144 | |
|
| 145 | 0 | final SAXParser saxParser = XmlUtils.buildSecureSaxParser(); |
| 146 | |
|
| 147 | 0 | final NvdCve12Handler cve12Handler = new NvdCve12Handler(); |
| 148 | 0 | saxParser.parse(oldVersion, cve12Handler); |
| 149 | 0 | final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities(); |
| 150 | |
|
| 151 | 0 | final NvdCve20Handler cve20Handler = new NvdCve20Handler(); |
| 152 | 0 | cve20Handler.setCveDB(cveDB); |
| 153 | 0 | cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap); |
| 154 | 0 | saxParser.parse(file, cve20Handler); |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private void processFiles() throws UpdateException { |
| 164 | 0 | LOGGER.info("Processing Started for NVD CVE - {}", filePair.getNvdCveInfo().getId()); |
| 165 | 0 | final long startProcessing = System.currentTimeMillis(); |
| 166 | |
try { |
| 167 | 0 | importXML(filePair.getFirst(), filePair.getSecond()); |
| 168 | 0 | cveDB.commit(); |
| 169 | 0 | properties.save(filePair.getNvdCveInfo()); |
| 170 | 0 | } catch (FileNotFoundException ex) { |
| 171 | 0 | throw new UpdateException(ex); |
| 172 | 0 | } catch (ParserConfigurationException ex) { |
| 173 | 0 | throw new UpdateException(ex); |
| 174 | 0 | } catch (SAXException ex) { |
| 175 | 0 | throw new UpdateException(ex); |
| 176 | 0 | } catch (IOException ex) { |
| 177 | 0 | throw new UpdateException(ex); |
| 178 | 0 | } catch (SQLException ex) { |
| 179 | 0 | throw new UpdateException(ex); |
| 180 | 0 | } catch (DatabaseException ex) { |
| 181 | 0 | throw new UpdateException(ex); |
| 182 | 0 | } catch (ClassNotFoundException ex) { |
| 183 | 0 | throw new UpdateException(ex); |
| 184 | |
} finally { |
| 185 | 0 | filePair.cleanup(); |
| 186 | 0 | } |
| 187 | 0 | LOGGER.info("Processing Complete for NVD CVE - {} ({} ms)", filePair.getNvdCveInfo().getId(), |
| 188 | 0 | System.currentTimeMillis() - startProcessing); |
| 189 | 0 | } |
| 190 | |
} |