UrlEcosystemMapper.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.nvd.ecosystem;

  19. import java.util.Objects;
  20. import java.util.Optional;
  21. import java.util.TreeMap;

  22. import javax.annotation.concurrent.NotThreadSafe;

  23. import io.github.jeremylong.openvulnerability.client.nvd.DefCveItem;
  24. import io.github.jeremylong.openvulnerability.client.nvd.Reference;

  25. import com.hankcs.algorithm.AhoCorasickDoubleArrayTrie;
  26. import com.hankcs.algorithm.AhoCorasickDoubleArrayTrie.Hit;
  27. import io.github.jeremylong.openvulnerability.client.nvd.CveItem;
  28. import java.util.List;

  29. @NotThreadSafe
  30. public class UrlEcosystemMapper {

  31.     /**
  32.      * The ecosystem map.
  33.      */
  34.     private static final TreeMap<String, String> ECOSYSTEM_MAP;

  35.     /**
  36.      * TThe search array.
  37.      */
  38.     private final AhoCorasickDoubleArrayTrie<String> search;

  39.     static {
  40.         ECOSYSTEM_MAP = new TreeMap<>();
  41.         for (UrlHostHint urlHostHint : UrlHostHint.values()) {
  42.             ECOSYSTEM_MAP.put(urlHostHint.getValue(), urlHostHint.getEcosystem());
  43.         }
  44.         for (UrlPathHint urlPathHint : UrlPathHint.values()) {
  45.             ECOSYSTEM_MAP.put(urlPathHint.getValue(), urlPathHint.getEcosystem());
  46.         }
  47.     }

  48.     /**
  49.      * Constructs a new URL ecosystem mapper.
  50.      */
  51.     public UrlEcosystemMapper() {
  52.         search = new AhoCorasickDoubleArrayTrie<>();
  53.         search.build(ECOSYSTEM_MAP);
  54.     }

  55.     /**
  56.      * Determines the ecosystem for the given CVE.
  57.      *
  58.      * @param cve the CVE data
  59.      * @return the ecosystem
  60.      */
  61.     public String getEcosystem(DefCveItem cve) {
  62.         final List<Reference> references = Optional.ofNullable(cve)
  63.                 .map(DefCveItem::getCve)
  64.                 .map(CveItem::getReferences)
  65.                 .orElse(null);

  66.         if (Objects.nonNull(references)) {
  67.             for (Reference r : references) {
  68.                 final Hit<String> ecosystem = search.findFirst(r.getUrl());
  69.                 if (ecosystem != null) {
  70.                     return ecosystem.value;
  71.                 }
  72.             }
  73.         }
  74.         return null;
  75.     }
  76. }