|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# http://nexb.com and https://github.com/nexB/scancode.io |
| 4 | +# The ScanCode.io software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode.io is provided as-is without warranties. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 16 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 17 | +# ScanCode.io should be considered or used as legal advice. Consult an Attorney |
| 18 | +# for any legal advice. |
| 19 | +# |
| 20 | +# ScanCode.io is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/scancode.io for support and download. |
| 22 | + |
| 23 | +import logging |
| 24 | + |
| 25 | +from django.conf import settings |
| 26 | + |
| 27 | +import requests |
| 28 | + |
| 29 | +label = "VulnerableCode" |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | +session = requests.Session() |
| 32 | + |
| 33 | + |
| 34 | +VULNERABLECODE_API_URL = None |
| 35 | +VULNERABLECODE_URL = settings.VULNERABLECODE_URL |
| 36 | +if VULNERABLECODE_URL: |
| 37 | + VULNERABLECODE_API_URL = f'{VULNERABLECODE_URL.rstrip("/")}/api/' |
| 38 | + |
| 39 | +# Basic Authentication |
| 40 | +VULNERABLECODE_USER = settings.VULNERABLECODE_USER |
| 41 | +VULNERABLECODE_PASSWORD = settings.VULNERABLECODE_PASSWORD |
| 42 | +basic_auth_enabled = VULNERABLECODE_USER and VULNERABLECODE_PASSWORD |
| 43 | +if basic_auth_enabled: |
| 44 | + session.auth = (VULNERABLECODE_USER, VULNERABLECODE_PASSWORD) |
| 45 | + |
| 46 | +# Authentication with single API key |
| 47 | +VULNERABLECODE_API_KEY = settings.VULNERABLECODE_API_KEY |
| 48 | +if VULNERABLECODE_API_KEY: |
| 49 | + session.headers.update({"Authorization": f"Token {VULNERABLECODE_API_KEY}"}) |
| 50 | + |
| 51 | + |
| 52 | +def is_configured(): |
| 53 | + """ |
| 54 | + Returns True if the required VulnerableCode settings have been set. |
| 55 | + """ |
| 56 | + if VULNERABLECODE_API_URL: |
| 57 | + return True |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | +def is_available(): |
| 62 | + """ |
| 63 | + Returns True if the configured VulnerableCode server is available. |
| 64 | + """ |
| 65 | + if not is_configured(): |
| 66 | + return False |
| 67 | + |
| 68 | + try: |
| 69 | + response = session.head(VULNERABLECODE_API_URL) |
| 70 | + response.raise_for_status() |
| 71 | + except requests.exceptions.RequestException as request_exception: |
| 72 | + logger.debug(f"{label} is_available() error: {request_exception}") |
| 73 | + return False |
| 74 | + |
| 75 | + return response.status_code == requests.codes.ok |
| 76 | + |
| 77 | + |
| 78 | +def get_base_purl(purl): |
| 79 | + """ |
| 80 | + Returns the `purl` without qualifiers and subpath. |
| 81 | + """ |
| 82 | + return purl.split("?")[0] |
| 83 | + |
| 84 | + |
| 85 | +def get_purls(packages, base=False): |
| 86 | + """ |
| 87 | + Returns the PURLs for the given list of `packages`. |
| 88 | + Do not include qualifiers nor subpath when `base` is provided. |
| 89 | + """ |
| 90 | + return [ |
| 91 | + get_base_purl(package_url) if base else package_url |
| 92 | + for package in packages |
| 93 | + if (package_url := package.package_url) |
| 94 | + ] |
| 95 | + |
| 96 | + |
| 97 | +def request_get( |
| 98 | + url, |
| 99 | + payload=None, |
| 100 | + timeout=None, |
| 101 | +): |
| 102 | + """ |
| 103 | + Wraps the HTTP request calls on the API. |
| 104 | + """ |
| 105 | + if not url: |
| 106 | + return |
| 107 | + |
| 108 | + params = {"format": "json"} |
| 109 | + if payload: |
| 110 | + params.update(payload) |
| 111 | + |
| 112 | + logger.debug(f"VulnerableCode: url={url} params={params}") |
| 113 | + try: |
| 114 | + response = session.get(url, params=params, timeout=timeout) |
| 115 | + response.raise_for_status() |
| 116 | + return response.json() |
| 117 | + except (requests.RequestException, ValueError, TypeError) as exception: |
| 118 | + logger.debug(f"{label} [Exception] {exception}") |
| 119 | + |
| 120 | + |
| 121 | +def request_post( |
| 122 | + url, |
| 123 | + data, |
| 124 | + timeout=None, |
| 125 | +): |
| 126 | + try: |
| 127 | + response = session.post(url, json=data, timeout=timeout) |
| 128 | + response.raise_for_status() |
| 129 | + return response.json() |
| 130 | + except (requests.RequestException, ValueError, TypeError) as exception: |
| 131 | + logger.debug(f"{label} [Exception] {exception}") |
| 132 | + |
| 133 | + |
| 134 | +def _get_vulnerabilities( |
| 135 | + url, |
| 136 | + field_name, |
| 137 | + field_value, |
| 138 | + timeout=None, |
| 139 | +): |
| 140 | + """ |
| 141 | + Get the list of vulnerabilities. |
| 142 | + """ |
| 143 | + payload = {field_name: field_value} |
| 144 | + |
| 145 | + response = request_get(url=url, payload=payload, timeout=timeout) |
| 146 | + if response and response.get("count"): |
| 147 | + results = response["results"] |
| 148 | + return results |
| 149 | + |
| 150 | + |
| 151 | +def get_vulnerabilities_by_purl( |
| 152 | + purl, |
| 153 | + timeout=None, |
| 154 | + api_url=VULNERABLECODE_API_URL, |
| 155 | +): |
| 156 | + """ |
| 157 | + Get the list of vulnerabilities providing a package `purl`. |
| 158 | + """ |
| 159 | + return _get_vulnerabilities( |
| 160 | + url=f"{api_url}packages/", |
| 161 | + field_name="purl", |
| 162 | + field_value=get_base_purl(purl), |
| 163 | + timeout=timeout, |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +def get_vulnerabilities_by_cpe( |
| 168 | + cpe, |
| 169 | + timeout=None, |
| 170 | + api_url=VULNERABLECODE_API_URL, |
| 171 | +): |
| 172 | + """ |
| 173 | + Get the list of vulnerabilities providing a package or component `cpe`. |
| 174 | + """ |
| 175 | + return _get_vulnerabilities( |
| 176 | + url=f"{api_url}cpes/", |
| 177 | + field_name="cpe", |
| 178 | + field_value=cpe, |
| 179 | + timeout=timeout, |
| 180 | + ) |
| 181 | + |
| 182 | + |
| 183 | +def bulk_search_by_purl( |
| 184 | + purls, |
| 185 | + timeout=None, |
| 186 | + api_url=VULNERABLECODE_API_URL, |
| 187 | +): |
| 188 | + """ |
| 189 | + Bulk search of vulnerabilities using the provided list of `purls`. |
| 190 | + """ |
| 191 | + url = f"{api_url}packages/bulk_search" |
| 192 | + |
| 193 | + data = { |
| 194 | + "purls": purls, |
| 195 | + } |
| 196 | + |
| 197 | + logger.debug(f"VulnerableCode: url={url} purls_count={len(purls)}") |
| 198 | + return request_post(url, data, timeout) |
| 199 | + |
| 200 | + |
| 201 | +def bulk_search_by_cpes( |
| 202 | + cpes, |
| 203 | + timeout=None, |
| 204 | + api_url=VULNERABLECODE_API_URL, |
| 205 | +): |
| 206 | + """ |
| 207 | + Bulk search of vulnerabilities using the provided list of `cpes`. |
| 208 | + """ |
| 209 | + url = f"{api_url}cpes/bulk_search" |
| 210 | + |
| 211 | + data = { |
| 212 | + "cpes": cpes, |
| 213 | + } |
| 214 | + |
| 215 | + logger.debug(f"VulnerableCode: url={url} cpes_count={len(cpes)}") |
| 216 | + return request_post(url, data, timeout) |
0 commit comments