|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 4 | +# The VulnTotal software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with VulnTotal require an acknowledgment. |
| 6 | +# |
| 7 | +# You may not use this software except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | +# specific language governing permissions and limitations under the License. |
| 13 | +# |
| 14 | +# When you publish or redistribute any data created with VulnTotal or any VulnTotal |
| 15 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 16 | +# |
| 17 | +# Generated with VulnTotal and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 18 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 19 | +# VulnTotal should be considered or used as legal advice. Consult an Attorney |
| 20 | +# for any legal advice. |
| 21 | +# VulnTotal is a free software tool from nexB Inc. and others. |
| 22 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 23 | + |
| 24 | +import json |
| 25 | +import logging |
| 26 | +import os |
| 27 | +from typing import Iterable |
| 28 | + |
| 29 | +import requests |
| 30 | + |
| 31 | +from vulntotal.validator import DataSource |
| 32 | +from vulntotal.validator import VendorData |
| 33 | + |
| 34 | +logger = logging.getLogger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +class OSSDataSource(DataSource): |
| 38 | + spdx_license_expression = "TODO" |
| 39 | + license_url = "TODO" |
| 40 | + api_unauthenticated = "https://ossindex.sonatype.org/api/v3/component-report" |
| 41 | + api_authenticated = "https://ossindex.sonatype.org/api/v3/authorized/component-report" |
| 42 | + |
| 43 | + def fetch_json_response(self, coordinates): |
| 44 | + username = os.environ.get("OSS_USERNAME", None) |
| 45 | + token = os.environ.get("OSS_TOKEN", None) |
| 46 | + auth = None |
| 47 | + url = self.api_unauthenticated |
| 48 | + if username and token: |
| 49 | + auth = (username, token) |
| 50 | + url = self.api_authenticated |
| 51 | + response = requests.post(url, auth=auth, json={"coordinates": coordinates}) |
| 52 | + |
| 53 | + if response.status_code == 200: |
| 54 | + return response.json() |
| 55 | + elif response.status_code == 401: |
| 56 | + logger.error("Invalid credentials") |
| 57 | + elif response.status_code == 429: |
| 58 | + msg = ( |
| 59 | + "Too many requests" |
| 60 | + if auth |
| 61 | + else "Too many requests: add OSS_USERNAME and OSS_TOKEN in .env file" |
| 62 | + ) |
| 63 | + logger.error(msg) |
| 64 | + else: |
| 65 | + logger.error(f"unknown status code: {response.status_code} while fetching: {url}") |
| 66 | + |
| 67 | + def datasource_advisory(self, purl) -> Iterable[VendorData]: |
| 68 | + if purl.type not in self.supported_ecosystem(): |
| 69 | + logger.error("Unsupported PURL") |
| 70 | + return |
| 71 | + |
| 72 | + response = self.fetch_json_response([str(purl)]) |
| 73 | + if response: |
| 74 | + self._raw_dump.append(response) |
| 75 | + return parse_advisory(response) |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def supported_ecosystem(cls): |
| 79 | + return { |
| 80 | + "cargo": "cargo", |
| 81 | + "cocoapods": "cocoapods", |
| 82 | + "composer": "composer", |
| 83 | + "conan": "conan", |
| 84 | + "conda": "conda", |
| 85 | + "cran": "cran", |
| 86 | + "golang": "golang", |
| 87 | + "maven": "maven", |
| 88 | + "npm": "npm", |
| 89 | + "nuget": "nuget", |
| 90 | + "pypi": "pypi", |
| 91 | + "rpm": "rpm", |
| 92 | + "gem": "gem", |
| 93 | + "swift": "swift", |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def parse_advisory(component) -> Iterable[VendorData]: |
| 98 | + response = component[0] |
| 99 | + if response["vulnerabilities"]: |
| 100 | + for vuln in response["vulnerabilities"]: |
| 101 | + aliases = [vuln["id"]] |
| 102 | + affected_versions = [] |
| 103 | + fixed_versions = [] |
| 104 | + if "versionRanges" in vuln: |
| 105 | + affected_versions.extend(vuln["versionRanges"]) |
| 106 | + yield VendorData( |
| 107 | + aliases=aliases, |
| 108 | + affected_versions=affected_versions, |
| 109 | + fixed_versions=fixed_versions, |
| 110 | + ) |
0 commit comments