|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import logging |
| 11 | +from typing import Iterable |
| 12 | + |
| 13 | +from vulnerabilities import utils |
| 14 | +from vulntotal.validator import DataSource |
| 15 | +from vulntotal.validator import VendorData |
| 16 | +from vulntotal.vulntotal_utils import github_constraints_satisfied |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class GithubDataSource(DataSource): |
| 22 | + spdx_license_expression = "TODO" |
| 23 | + license_url = "TODO" |
| 24 | + |
| 25 | + def fetch_github(self, graphql_query): |
| 26 | + return utils.fetch_github_graphql_query(graphql_query) |
| 27 | + |
| 28 | + def datasource_advisory(self, purl) -> Iterable[VendorData]: |
| 29 | + end_cursor = "" |
| 30 | + interesting_edges = [] |
| 31 | + while True: |
| 32 | + queryset = generate_graphql_payload(purl, end_cursor) |
| 33 | + response = self.fetch_github(queryset) |
| 34 | + self._raw_dump.append(response) |
| 35 | + security_advisories = response["data"]["securityVulnerabilities"] |
| 36 | + interesting_edges.extend(extract_interesting_edge(security_advisories["edges"], purl)) |
| 37 | + end_cursor = security_advisories["pageInfo"]["endCursor"] |
| 38 | + if not security_advisories["pageInfo"]["hasNextPage"]: |
| 39 | + break |
| 40 | + return parse_advisory(interesting_edges) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def supported_ecosystem(cls): |
| 44 | + return { |
| 45 | + "maven": "MAVEN", |
| 46 | + "nuget": "NUGET", |
| 47 | + "composer": "COMPOSER", |
| 48 | + "pypi": "PIP", |
| 49 | + "gem": "RUBYGEMS", |
| 50 | + "golang": "GO", |
| 51 | + "rust": "RUST", |
| 52 | + "npm": "NPM", |
| 53 | + "erlang": "ERLANG", |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def parse_advisory(interesting_edges) -> Iterable[VendorData]: |
| 58 | + for edge in interesting_edges: |
| 59 | + node = edge["node"] |
| 60 | + aliases = [aliase["value"] for aliase in node["advisory"]["identifiers"]] |
| 61 | + affected_versions = node["vulnerableVersionRange"].strip().replace(" ", "").split(",") |
| 62 | + fixed_versions = [node["firstPatchedVersion"]["identifier"]] |
| 63 | + yield VendorData( |
| 64 | + aliases=sorted(list(set(aliases))), |
| 65 | + affected_versions=sorted(list(set(affected_versions))), |
| 66 | + fixed_versions=sorted(list(set(fixed_versions))), |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def extract_interesting_edge(edges, purl): |
| 71 | + interesting_edges = [] |
| 72 | + for edge in edges: |
| 73 | + if github_constraints_satisfied(edge["node"]["vulnerableVersionRange"], purl.version): |
| 74 | + interesting_edges.append(edge) |
| 75 | + return interesting_edges |
| 76 | + |
| 77 | + |
| 78 | +def generate_graphql_payload(purl, end_cursor): |
| 79 | + GRAPHQL_QUERY_TEMPLATE = """ |
| 80 | + query{ |
| 81 | + securityVulnerabilities(first: 100, ecosystem: %s, package: "%s", %s){ |
| 82 | + edges { |
| 83 | + node { |
| 84 | + advisory { |
| 85 | + identifiers { |
| 86 | + type |
| 87 | + value |
| 88 | + } |
| 89 | + summary |
| 90 | + references { |
| 91 | + url |
| 92 | + } |
| 93 | + severity |
| 94 | + publishedAt |
| 95 | + } |
| 96 | + firstPatchedVersion{ |
| 97 | + identifier |
| 98 | + } |
| 99 | + package { |
| 100 | + name |
| 101 | + } |
| 102 | + vulnerableVersionRange |
| 103 | + } |
| 104 | + } |
| 105 | + pageInfo { |
| 106 | + hasNextPage |
| 107 | + endCursor |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + """ |
| 112 | + |
| 113 | + supported_ecosystem = GithubDataSource.supported_ecosystem() |
| 114 | + |
| 115 | + if purl.type not in supported_ecosystem: |
| 116 | + return |
| 117 | + |
| 118 | + end_cursor_exp = "" |
| 119 | + ecosystem = supported_ecosystem[purl.type] |
| 120 | + package_name = purl.name |
| 121 | + |
| 122 | + if end_cursor: |
| 123 | + end_cursor_exp = f'after: "{end_cursor}"' |
| 124 | + |
| 125 | + if purl.type == "maven": |
| 126 | + if not purl.namespace: |
| 127 | + logger.error(f"Invalid Maven PURL {str(purl)}") |
| 128 | + return |
| 129 | + package_name = f"{purl.namespace}:{purl.name}" |
| 130 | + |
| 131 | + elif purl.type == "composer": |
| 132 | + if not purl.namespace: |
| 133 | + logger.error(f"Invalid Composer PURL {str(purl)}") |
| 134 | + return |
| 135 | + package_name = f"{purl.namespace}/{purl.name}" |
| 136 | + |
| 137 | + elif purl.type == "golang" and purl.namespace: |
| 138 | + package_name = f"{purl.namespace}/{purl.name}" |
| 139 | + |
| 140 | + return {"query": GRAPHQL_QUERY_TEMPLATE % (ecosystem, package_name, end_cursor_exp)} |
0 commit comments