|
| 1 | +# Copyright (c) 2017 nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +import yaml |
| 24 | +import re |
| 25 | +import json |
| 26 | +import requests |
| 27 | +from typing import Set |
| 28 | +from typing import List |
| 29 | + |
| 30 | +from packageurl import PackageURL |
| 31 | + |
| 32 | +from vulnerabilities.data_source import GitDataSource |
| 33 | +from vulnerabilities.data_source import GitDataSourceConfiguration |
| 34 | +from vulnerabilities.data_source import Advisory |
| 35 | +from vulnerabilities.data_source import Reference |
| 36 | + |
| 37 | + |
| 38 | +class ElixirSecurityDataSource(GitDataSource): |
| 39 | + def __enter__(self): |
| 40 | + super(ElixirSecurityDataSource, self).__enter__() |
| 41 | + |
| 42 | + if not getattr(self, "_added_files", None): |
| 43 | + self._added_files, self._updated_files = self.file_changes( |
| 44 | + recursive=True, file_ext="yml", subdir="./packages" |
| 45 | + ) |
| 46 | + |
| 47 | + def updated_advisories(self) -> Set[Advisory]: |
| 48 | + files = self._updated_files |
| 49 | + advisories = [] |
| 50 | + for f in files: |
| 51 | + processed_data = self.process_file(f) |
| 52 | + if processed_data: |
| 53 | + advisories.append(processed_data) |
| 54 | + return self.batch_advisories(advisories) |
| 55 | + |
| 56 | + def added_advisories(self) -> Set[Advisory]: |
| 57 | + files = self._added_files |
| 58 | + advisories = [] |
| 59 | + for f in files: |
| 60 | + processed_data = self.process_file(f) |
| 61 | + if processed_data: |
| 62 | + advisories.append(processed_data) |
| 63 | + return self.batch_advisories(advisories) |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def generate_all_versions_list(pkg_name): |
| 67 | + resp = requests.get(f"https://hex.pm/api/packages/{pkg_name}") |
| 68 | + resp = resp.content |
| 69 | + json_resp = json.loads(resp) |
| 70 | + versions_list = [] |
| 71 | + for release in json_resp["releases"]: |
| 72 | + versions_list.append(release["version"]) |
| 73 | + return versions_list |
| 74 | + |
| 75 | + def get_pkg_from_range(self, versions_list, pkg_name): |
| 76 | + pkg_versions = [] |
| 77 | + all_versions_list = self.generate_all_versions_list(pkg_name) |
| 78 | + if versions_list is None: |
| 79 | + return |
| 80 | + for version in versions_list: |
| 81 | + if re.match("^>=", version): |
| 82 | + index = all_versions_list.index(version[3:]) |
| 83 | + pkg_versions = pkg_versions + all_versions_list[0: index + 1] |
| 84 | + elif re.match("^>", version): |
| 85 | + index = all_versions_list.index(version[2:]) |
| 86 | + pkg_versions = pkg_versions + all_versions_list[0:index] |
| 87 | + elif re.match("^<", version): |
| 88 | + index = all_versions_list.index(version[2:]) |
| 89 | + pkg_versions = pkg_versions + all_versions_list[index + 1: -1] |
| 90 | + else: |
| 91 | + pkg_versions.append(version[3:]) |
| 92 | + return pkg_versions |
| 93 | + |
| 94 | + def process_file(self, path): |
| 95 | + with open(path) as f: |
| 96 | + yaml_file = yaml.safe_load(f) |
| 97 | + pkg_name = yaml_file["package"] |
| 98 | + safe_pkg_versions = [] |
| 99 | + if yaml_file.get("unaffected_versions"): |
| 100 | + safe_pkg_versions = self.get_pkg_from_range( |
| 101 | + yaml_file["patched_versions"] + yaml_file["unaffected_versions"], |
| 102 | + pkg_name, |
| 103 | + ) |
| 104 | + else: |
| 105 | + safe_pkg_versions = self.get_pkg_from_range( |
| 106 | + yaml_file["patched_versions"], pkg_name |
| 107 | + ) |
| 108 | + cve_id = yaml_file["cve"] |
| 109 | + safe_purls = [] |
| 110 | + if safe_pkg_versions is not None: |
| 111 | + safe_purls = { |
| 112 | + PackageURL(name=pkg_name, type="hex", version=version) |
| 113 | + for version in safe_pkg_versions |
| 114 | + } |
| 115 | + |
| 116 | + vuln_reference = [ |
| 117 | + Reference( |
| 118 | + url=yaml_file["link"], |
| 119 | + ) |
| 120 | + ] |
| 121 | + |
| 122 | + return Advisory( |
| 123 | + summary=yaml_file["description"], |
| 124 | + impacted_package_urls=[], |
| 125 | + resolved_package_urls=safe_purls, |
| 126 | + cve_id=cve_id, |
| 127 | + vuln_references=vuln_reference, |
| 128 | + ) |
0 commit comments