|
| 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 | +from typing import Iterable |
| 26 | + |
| 27 | +import requests |
| 28 | +from packageurl import PackageURL |
| 29 | + |
| 30 | +from vulntotal.validator import Validator |
| 31 | +from vulntotal.validator import VendorData |
| 32 | + |
| 33 | + |
| 34 | +class DepsValidator(Validator): |
| 35 | + spdx_license_expression = "TODO" |
| 36 | + license_url = "TODO" |
| 37 | + |
| 38 | + def validator_advisory(self, purl) -> Iterable[VendorData]: |
| 39 | + supported_ecosystem = { |
| 40 | + "npm": "npm", |
| 41 | + "maven": "maven", |
| 42 | + "go": "go", |
| 43 | + "pypi": "pypi", |
| 44 | + "cargo": "cargo", |
| 45 | + # Coming soon |
| 46 | + # "nuget": "nuget", |
| 47 | + } |
| 48 | + |
| 49 | + url_advisories_meta = ( |
| 50 | + "https://deps.dev/_/s/{ecosystem}/p/{package}/v/{version}/dependencies" |
| 51 | + ) |
| 52 | + url_advisory = "https://deps.dev/_/advisory/{source}/{sourceID}" |
| 53 | + |
| 54 | + if purl.type in supported_ecosystem: |
| 55 | + request_advisories_meta = requests.get( |
| 56 | + url_advisories_meta.format( |
| 57 | + ecosystem=supported_ecosystem[purl.type], |
| 58 | + package=purl.name, |
| 59 | + version=purl.version, |
| 60 | + ) |
| 61 | + ) |
| 62 | + if request_advisories_meta.status_code == 200 and request_advisories_meta != "Not Found": |
| 63 | + response = json.loads(request_advisories_meta.text) |
| 64 | + advisories = response["dependencies"][0]["advisories"] |
| 65 | + |
| 66 | + for advisory in advisories: |
| 67 | + request_advisory = requests.get( |
| 68 | + url_advisory.format(source=advisory["source"], sourceID=advisory["sourceID"]) |
| 69 | + ) |
| 70 | + |
| 71 | + if request_advisory.status_code == 200: |
| 72 | + parsed_advisory = json.loads(request_advisory.text) |
| 73 | + affected_versions = [ |
| 74 | + event["version"] |
| 75 | + for event in parsed_advisory["packages"][0]["versionsAffected"] |
| 76 | + ] |
| 77 | + fixed_versions = [ |
| 78 | + event["version"] |
| 79 | + for event in parsed_advisory["packages"][0]["versionsUnaffected"] |
| 80 | + ] |
| 81 | + yield VendorData( |
| 82 | + aliases=parsed_advisory["aliases"], |
| 83 | + affected_versions=sorted(affected_versions), |
| 84 | + fixed_versions=sorted(fixed_versions), |
| 85 | + raw_dump=request_advisories_meta.text, |
| 86 | + ) |
0 commit comments