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.ObjectMapper;
23 import com.fasterxml.jackson.databind.ObjectReader;
24 import com.fasterxml.jackson.module.blackbird.BlackbirdModule;
25 import java.io.EOFException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import static java.nio.charset.StandardCharsets.UTF_8;
30 import java.util.zip.ZipException;
31 import org.owasp.dependencycheck.data.knownexploited.json.KnownExploitedVulnerabilitiesSchema;
32 import org.owasp.dependencycheck.data.update.exception.CorruptedDatastreamException;
33 import org.owasp.dependencycheck.data.update.exception.UpdateException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41 public class KnownExploitedVulnerabilityParser {
42
43
44
45
46 private static final Logger LOGGER = LoggerFactory.getLogger(KnownExploitedVulnerabilityParser.class);
47
48
49
50
51
52
53
54
55
56
57
58 public KnownExploitedVulnerabilitiesSchema parse(InputStream in) throws UpdateException, CorruptedDatastreamException {
59
60 final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
61 objectMapper.registerModule(new BlackbirdModule());
62
63 final ObjectReader objectReader = objectMapper.readerFor(KnownExploitedVulnerabilitiesSchema.class);
64
65
66 try (InputStreamReader isr = new InputStreamReader(in, UTF_8);
67 JsonParser parser = objectReader.getFactory().createParser(isr)) {
68 final KnownExploitedVulnerabilitiesSchema data = objectReader.readValue(parser);
69 return data;
70 } catch (ZipException | EOFException ex) {
71 throw new CorruptedDatastreamException("Error parsing CISA Known Exploited Vulnerabilities file", ex);
72 } catch (IOException ex) {
73 LOGGER.error("Error reading CISA Known Exploited Vulnerabilities JSON data");
74 LOGGER.debug("Error extracting the CISA Known Exploited Vulnerabilities JSON data", ex);
75 throw new UpdateException("Unable to find the CISA Known Exploited Vulnerabilities file to parse", ex);
76 }
77 }
78 }