KnownExploitedVulnerabilityParser.java

  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. import com.fasterxml.jackson.core.JsonParser;
  20. import com.fasterxml.jackson.databind.DeserializationFeature;
  21. import com.fasterxml.jackson.databind.ObjectMapper;
  22. import com.fasterxml.jackson.databind.ObjectReader;
  23. import com.fasterxml.jackson.module.blackbird.BlackbirdModule;
  24. import java.io.EOFException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import static java.nio.charset.StandardCharsets.UTF_8;
  29. import java.util.zip.ZipException;
  30. import org.owasp.dependencycheck.data.knownexploited.json.KnownExploitedVulnerabilitiesSchema;
  31. import org.owasp.dependencycheck.data.update.exception.CorruptedDatastreamException;
  32. import org.owasp.dependencycheck.data.update.exception.UpdateException;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;

  35. /**
  36.  *
  37.  * @author Jeremy Long
  38.  */
  39. public class KnownExploitedVulnerabilityParser {

  40.     /**
  41.      * The logger.
  42.      */
  43.     private static final Logger LOGGER = LoggerFactory.getLogger(KnownExploitedVulnerabilityParser.class);

  44.     /**
  45.      * Parses the CISA Known Exploited JSON file and inserts/updates data into
  46.      * the database.
  47.      *
  48.      * @param in the CISA Known Exploited JSON input stream to parse
  49.      * @return the Known Exploited Vulnerabilities object
  50.      * @throws UpdateException thrown if the file could not be read
  51.      * @throws CorruptedDatastreamException thrown if the file was found to be a
  52.      * corrupted download (ZipException or premature EOF)
  53.      */
  54.     public KnownExploitedVulnerabilitiesSchema parse(InputStream in) throws UpdateException, CorruptedDatastreamException {

  55.         final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  56.         objectMapper.registerModule(new BlackbirdModule());

  57.         final ObjectReader objectReader = objectMapper.readerFor(KnownExploitedVulnerabilitiesSchema.class);

  58.         //InputStream in = new GZIPInputStream(fin);
  59.         try (InputStreamReader isr = new InputStreamReader(in, UTF_8);
  60.                 JsonParser parser = objectReader.getFactory().createParser(isr)) {
  61.             final KnownExploitedVulnerabilitiesSchema data = objectReader.readValue(parser);
  62.             return data;
  63.         } catch (ZipException | EOFException ex) {
  64.             throw new CorruptedDatastreamException("Error parsing CISA Known Exploited Vulnerabilities file", ex);
  65.         } catch (IOException ex) {
  66.             LOGGER.error("Error reading CISA Known Exploited Vulnerabilities JSON data");
  67.             LOGGER.debug("Error extracting the CISA Known Exploited Vulnerabilities JSON data", ex);
  68.             throw new UpdateException("Unable to find the CISA Known Exploited Vulnerabilities file to parse", ex);
  69.         }
  70.     }
  71. }