1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.update.cisa;
19
20 import com.fasterxml.jackson.core.JsonParser;
21 import com.fasterxml.jackson.databind.DeserializationFeature;
22 import com.fasterxml.jackson.databind.Module;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.ObjectReader;
25 import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
26 import com.fasterxml.jackson.module.blackbird.BlackbirdModule;
27 import java.io.EOFException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import static java.nio.charset.StandardCharsets.UTF_8;
32 import java.util.zip.ZipException;
33 import org.owasp.dependencycheck.data.knownexploited.json.KnownExploitedVulnerabilitiesSchema;
34 import org.owasp.dependencycheck.data.update.exception.CorruptedDatastreamException;
35 import org.owasp.dependencycheck.data.update.exception.UpdateException;
36 import org.owasp.dependencycheck.utils.Utils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44 public class KnownExploitedVulnerabilityParser {
45
46
47
48
49 private static final Logger LOGGER = LoggerFactory.getLogger(KnownExploitedVulnerabilityParser.class);
50
51
52
53
54
55
56
57
58
59
60
61 public KnownExploitedVulnerabilitiesSchema parse(InputStream in) throws UpdateException, CorruptedDatastreamException {
62
63 final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
64 final Module module;
65 if (Utils.getJavaVersion() <= 8) {
66 module = new AfterburnerModule();
67 } else {
68 module = new BlackbirdModule();
69 }
70 objectMapper.registerModule(module);
71
72 final ObjectReader objectReader = objectMapper.readerFor(KnownExploitedVulnerabilitiesSchema.class);
73
74
75 try (InputStreamReader isr = new InputStreamReader(in, UTF_8);
76 JsonParser parser = objectReader.getFactory().createParser(isr)) {
77 final KnownExploitedVulnerabilitiesSchema data = objectReader.readValue(parser);
78 return data;
79 } catch (ZipException | EOFException ex) {
80 throw new CorruptedDatastreamException("Error parsing CISA Known Exploited Vulnerabilities file", ex);
81 } catch (IOException ex) {
82 LOGGER.error("Error reading CISA Known Exploited Vulnerabilities JSON data");
83 LOGGER.debug("Error extracting the CISA Known Exploited Vulnerabilities JSON data", ex);
84 throw new UpdateException("Unable to find the CISA Known Exploited Vulnerabilities file to parse", ex);
85 }
86 }
87 }