1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.update;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.List;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.parsers.SAXParser;
27 import static org.owasp.dependencycheck.data.nvdcve.DatabaseProperties.LAST_CPE_UPDATE;
28 import org.owasp.dependencycheck.data.update.cpe.CPEHandler;
29 import org.owasp.dependencycheck.data.update.cpe.Cpe;
30 import org.owasp.dependencycheck.data.update.exception.UpdateException;
31 import org.owasp.dependencycheck.utils.DateUtil;
32 import org.owasp.dependencycheck.utils.DownloadFailedException;
33 import org.owasp.dependencycheck.utils.Downloader;
34 import org.owasp.dependencycheck.utils.ExtractionUtil;
35 import org.owasp.dependencycheck.utils.Settings;
36 import org.owasp.dependencycheck.utils.XmlUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.xml.sax.SAXException;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @Deprecated
56 public class CpeUpdater extends BaseUpdater implements CachedWebDataSource {
57
58
59
60
61 private static final Logger LOGGER = LoggerFactory.getLogger(CpeUpdater.class);
62
63 @Override
64 public void update() throws UpdateException {
65
66
67
68
69
70
71
72
73
74
75
76 try {
77 openDataStores();
78 if (updateNeeded()) {
79 LOGGER.info("Updating the Common Platform Enumeration (CPE)");
80 final File xml = downloadCpe();
81 final List<Cpe> cpes = processXML(xml);
82 getCveDB().deleteUnusedCpe();
83 for (Cpe cpe : cpes) {
84 getCveDB().addCpe(cpe.getValue(), cpe.getVendor(), cpe.getProduct());
85 }
86 final long now = System.currentTimeMillis();
87 getProperties().save(LAST_CPE_UPDATE, Long.toString(now));
88 LOGGER.info("CPE update complete");
89 }
90 } finally {
91 closeDataStores();
92 }
93 }
94
95
96
97
98
99
100
101
102 private File downloadCpe() throws UpdateException {
103 File xml;
104 final URL url;
105 try {
106 url = new URL(Settings.getString(Settings.KEYS.CPE_URL));
107 xml = File.createTempFile("cpe", ".xml", Settings.getTempDirectory());
108 Downloader.fetchFile(url, xml);
109 if (url.toExternalForm().endsWith(".xml.gz")) {
110 ExtractionUtil.extractGzip(xml);
111 }
112
113 } catch (MalformedURLException ex) {
114 throw new UpdateException("Invalid CPE URL", ex);
115 } catch (DownloadFailedException ex) {
116 throw new UpdateException("Unable to download CPE XML file", ex);
117 } catch (IOException ex) {
118 throw new UpdateException("Unable to create temporary file to download CPE", ex);
119 }
120 return xml;
121 }
122
123
124
125
126
127
128
129
130
131 private List<Cpe> processXML(final File xml) throws UpdateException {
132 try {
133 final SAXParser saxParser = XmlUtils.buildSecureSaxParser();
134 final CPEHandler handler = new CPEHandler();
135 saxParser.parse(xml, handler);
136 return handler.getData();
137 } catch (ParserConfigurationException ex) {
138 throw new UpdateException("Unable to parse CPE XML file due to SAX Parser Issue", ex);
139 } catch (SAXException ex) {
140 throw new UpdateException("Unable to parse CPE XML file due to SAX Parser Exception", ex);
141 } catch (IOException ex) {
142 throw new UpdateException("Unable to parse CPE XML file due to IO Failure", ex);
143 }
144 }
145
146
147
148
149
150
151
152 private boolean updateNeeded() {
153 final long now = System.currentTimeMillis();
154 final int days = Settings.getInt(Settings.KEYS.CPE_MODIFIED_VALID_FOR_DAYS, 30);
155 long timestamp = 0;
156 final String ts = getProperties().getProperty(LAST_CPE_UPDATE);
157 if (ts != null && ts.matches("^[0-9]+$")) {
158 timestamp = Long.parseLong(ts);
159 }
160 return !DateUtil.withinDateRange(timestamp, now, days);
161 }
162 }