|
| 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 | +import json |
| 23 | +import logging |
| 24 | +from datetime import datetime |
| 25 | +from datetime import timezone |
| 26 | +from io import BytesIO |
| 27 | +from typing import Iterable |
| 28 | +from zipfile import ZipFile |
| 29 | + |
| 30 | +import requests |
| 31 | +from packageurl import PackageURL |
| 32 | +from univers.version_range import GitHubVersionRange |
| 33 | +from univers.version_range import PypiVersionRange |
| 34 | +from univers.versions import SemverVersion |
| 35 | + |
| 36 | +from vulnerabilities.importer import AdvisoryData |
| 37 | +from vulnerabilities.importer import AffectedPackage |
| 38 | +from vulnerabilities.importer import Importer |
| 39 | +from vulnerabilities.importer import Reference |
| 40 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 41 | +from vulnerabilities.severity_systems import SCORING_SYSTEMS |
| 42 | + |
| 43 | +logger = logging.getLogger(__name__) |
| 44 | + |
| 45 | + |
| 46 | +class PyPlImporter(Importer): |
| 47 | + spdx_license_expression = "Apache-2.0" |
| 48 | + |
| 49 | + def advisory_data(self) -> Iterable[AdvisoryData]: |
| 50 | + url = "https://osv-vulnerabilities.storage.googleapis.com/PyPI/all.zip" |
| 51 | + response = requests.get(url).content |
| 52 | + try: |
| 53 | + with ZipFile(BytesIO(response)) as zip_file: |
| 54 | + for file_name in zip_file.namelist(): |
| 55 | + with zip_file.open(file_name) as f: |
| 56 | + vul_info = json.loads(f.read()) |
| 57 | + yield parse_advisory_data(vul_info) |
| 58 | + except requests.exceptions.RequestException: |
| 59 | + logger.error(f"Failed to fetch osv-vulnerabilities PyPI: HTTP ") |
| 60 | + |
| 61 | + |
| 62 | +def parse_advisory_data(raw_data: dict) -> AdvisoryData: |
| 63 | + summary = raw_data["summary"] if "summary" in raw_data else raw_data["details"][slice(50)] |
| 64 | + aliases = raw_data["aliases"] if "aliases" in raw_data else raw_data["id"] |
| 65 | + |
| 66 | + date_published = datetime.strptime(raw_data["published"][0:19], "%Y-%m-%dT%H:%M:%S").replace( |
| 67 | + tzinfo=timezone.utc |
| 68 | + ) |
| 69 | + |
| 70 | + severity = [] |
| 71 | + if "ecosystem_specific" in raw_data and "severity" in raw_data["ecosystem_specific"]: |
| 72 | + severity = VulnerabilitySeverity( |
| 73 | + system=SCORING_SYSTEMS["generic_textual"], |
| 74 | + value=raw_data["ecosystem_specific"]["severity"], |
| 75 | + ) |
| 76 | + |
| 77 | + references = [] |
| 78 | + for ref in raw_data["references"]: |
| 79 | + references.append(Reference(url=ref["url"], severities=severity)) |
| 80 | + |
| 81 | + affected_package = [] |
| 82 | + # HINT: Values larger than 1/3 of a buffer page cannot be indexed. |
| 83 | + for affected_pkg in raw_data["affected"]: |
| 84 | + purl = PackageURL( |
| 85 | + type=affected_pkg["package"]["ecosystem"], name=affected_pkg["package"]["name"] |
| 86 | + ) |
| 87 | + affected_version_range = ( |
| 88 | + PypiVersionRange(affected_pkg["versions"][slice(50)]) |
| 89 | + if "versions" in affected_pkg |
| 90 | + else None |
| 91 | + ) |
| 92 | + |
| 93 | + for fix_range in affected_pkg["ranges"]: |
| 94 | + fixed_version = set() |
| 95 | + if fix_range["type"] == "ECOSYSTEM": |
| 96 | + filter_fixed = list(filter(lambda x: x.keys() == {"fixed"}, fix_range["events"])) |
| 97 | + list_fixed = [i["fixed"] for i in filter_fixed] |
| 98 | + fixed_version.add(PypiVersionRange(list_fixed)) |
| 99 | + |
| 100 | + if fix_range["type"] == "SEMVER": |
| 101 | + filter_fixed = list(filter(lambda x: x.keys() == {"fixed"}, fix_range["events"])) |
| 102 | + list_fixed = [i["fixed"] for i in filter_fixed] |
| 103 | + for i in list_fixed: |
| 104 | + fixed_version.add(SemverVersion(i)) |
| 105 | + |
| 106 | + if fix_range["type"] == "GIT": |
| 107 | + filter_fixed = list(filter(lambda x: x.keys() == {"fixed"}, fix_range["events"])) |
| 108 | + list_fixed = [i["fixed"] for i in filter_fixed] |
| 109 | + fixed_version.add(GitHubVersionRange(list_fixed)) |
| 110 | + |
| 111 | + affected_package.append( |
| 112 | + AffectedPackage( |
| 113 | + package=purl, |
| 114 | + affected_version_range=affected_version_range, |
| 115 | + fixed_version=fixed_version, |
| 116 | + ) |
| 117 | + ) |
| 118 | + |
| 119 | + return AdvisoryData( |
| 120 | + aliases=aliases, |
| 121 | + summary=summary, |
| 122 | + affected_packages=affected_package, |
| 123 | + references=references, |
| 124 | + date_published=date_published, |
| 125 | + ) |
0 commit comments