View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2022 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
43   */
44  public class KnownExploitedVulnerabilityParser {
45  
46      /**
47       * The logger.
48       */
49      private static final Logger LOGGER = LoggerFactory.getLogger(KnownExploitedVulnerabilityParser.class);
50  
51      /**
52       * Parses the CISA Known Exploited JSON file and inserts/updates data into
53       * the database.
54       *
55       * @param in the CISA Known Exploited JSON input stream to parse
56       * @return the Known Exploited Vulnerabilities object
57       * @throws UpdateException thrown if the file could not be read
58       * @throws CorruptedDatastreamException thrown if the file was found to be a
59       * corrupted download (ZipException or premature EOF)
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          //InputStream in = new GZIPInputStream(fin);
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  }