MixAuditJsonParser.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) 2020 The OWASP Foundation. All Rights Reserved.
  17.  */
  18. package org.owasp.dependencycheck.data.elixir;

  19. import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;

  22. import javax.annotation.concurrent.NotThreadSafe;

  23. import jakarta.json.stream.JsonParsingException;
  24. import java.io.Reader;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import jakarta.json.Json;
  28. import jakarta.json.JsonArray;
  29. import jakarta.json.JsonException;
  30. import jakarta.json.JsonObject;
  31. import jakarta.json.JsonReader;
  32. import jakarta.json.JsonString;

  33. /**
  34.  * Parses json output from `mix_audit --format json`.
  35.  *
  36.  * @author Christoph Sassenberg
  37.  */
  38. @NotThreadSafe
  39. public class MixAuditJsonParser {

  40.     /**
  41.      * A key in the mix json file.
  42.      */
  43.     static final String PASS_FAIL_KEY = "pass";
  44.     /**
  45.      * A key in the mix json file.
  46.      */
  47.     static final String RESULTS_KEY = "vulnerabilities";
  48.     /**
  49.      * A key in the mix json file.
  50.      */
  51.     static final String ADVISORY_KEY = "advisory";
  52.     /**
  53.      * A key in the mix json file.
  54.      */
  55.     static final String DEPENDENCY_KEY = "dependency";

  56.     /**
  57.      * The JsonReader for parsing JSON.
  58.      */
  59.     private final JsonReader jsonReader;

  60.     /**
  61.      * The List of MixAuditResults found.
  62.      */
  63.     private final List<MixAuditResult> mixAuditResults;

  64.     /**
  65.      * Whether the mix audit passed or failed.
  66.      */
  67.     private boolean mixAuditPass;

  68.     /**
  69.      * The LOGGER
  70.      */
  71.     private static final Logger LOGGER = LoggerFactory.getLogger(MixAuditJsonParser.class);

  72.     /**
  73.      * Creates a MixAuditJsonParser from a Reader.
  74.      *
  75.      * @param reader - the java.io.Reader to read the json character stream from
  76.      */
  77.     public MixAuditJsonParser(Reader reader) {
  78.         LOGGER.debug("Creating a MixAuditJsonParser");
  79.         this.jsonReader = Json.createReader(reader);
  80.         this.mixAuditResults = new ArrayList<>();
  81.         this.mixAuditPass = false;
  82.     }

  83.     /**
  84.      * Process the input stream to create the list of dependencies.
  85.      *
  86.      * @throws AnalysisException thrown when there is an error parsing the
  87.      * results of `mix_audit --format json`
  88.      */
  89.     public void process() throws AnalysisException {
  90.         LOGGER.debug("Beginning mix_audit json output processing");
  91.         try {
  92.             final JsonObject output = jsonReader.readObject();
  93.             if (output.containsKey(PASS_FAIL_KEY)) {
  94.                 this.mixAuditPass = output.getBoolean(PASS_FAIL_KEY);
  95.             }

  96.             if (output.containsKey(RESULTS_KEY) && output.isNull(RESULTS_KEY)) {
  97.                 LOGGER.debug("Found vulnerabilities");
  98.             }
  99.             final JsonArray results = output.getJsonArray(RESULTS_KEY);
  100.             for (JsonObject result : results.getValuesAs(JsonObject.class)) {
  101.                 final JsonObject advisory = result.getJsonObject(ADVISORY_KEY);
  102.                 final JsonObject dependency = result.getJsonObject(DEPENDENCY_KEY);
  103.                 final ArrayList<String> patchedVersions = new ArrayList<>();

  104.                 for (JsonString patchedVersion : advisory.getJsonArray("patched_versions").getValuesAs(JsonString.class)) {
  105.                     patchedVersions.add(patchedVersion.getString());
  106.                 }

  107.                 final MixAuditResult r = new MixAuditResult(
  108.                         advisory.getString("id"),
  109.                         advisory.getString("cve"),
  110.                         advisory.getString("title"),
  111.                         advisory.getString("description"),
  112.                         advisory.getString("disclosure_date"),
  113.                         advisory.getString("url"),
  114.                         patchedVersions,
  115.                         dependency.getString("lockfile"),
  116.                         dependency.getString("package"),
  117.                         dependency.getString("version")
  118.                 );

  119.                 this.mixAuditResults.add(r);
  120.             }
  121.         } catch (JsonParsingException jsonpe) {
  122.             throw new AnalysisException("Error parsing stream", jsonpe);
  123.         } catch (JsonException jsone) {
  124.             throw new AnalysisException("Error reading stream", jsone);
  125.         } catch (IllegalStateException ise) {
  126.             throw new AnalysisException("Illegal state while parsing mix_audit output", ise);
  127.         } catch (ClassCastException cce) {
  128.             throw new AnalysisException("JSON not exactly matching output of `mix_audit --format json`", cce);
  129.         }
  130.     }

  131.     /**
  132.      * Gets the list of results.
  133.      *
  134.      * @return the list of results
  135.      */
  136.     public List<MixAuditResult> getResults() {
  137.         return mixAuditResults;
  138.     }
  139. }