Repository navigation
Expand file tree
/
Copy pathvulnerablecode.py
More file actions
194 lines (164 loc) · 5.46 KB
/
Copy pathvulnerablecode.py
File metadata and controls
194 lines (164 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# DejaCode is a trademark of nexB Inc.
# SPDX-License-Identifier: AGPL-3.0-only
# See https://github.com/aboutcode-org/dejacode for support or download.
# See https://aboutcode.org for more information about AboutCode FOSS projects.
#
from django.core.cache import caches
from dejacode_toolkit import BaseService
from dejacode_toolkit import get_settings
from dejacode_toolkit import logger
cache = caches["vulnerabilities"]
class VulnerableCode(BaseService):
label = "VulnerableCode"
settings_prefix = "VULNERABLECODE"
url_field_name = "vulnerablecode_url"
api_key_field_name = "vulnerablecode_api_key"
user_agent = get_settings("VULNERABLECODE_USER_AGENT", default="VCIO_API_AGENT")
def get_session(self):
session = super().get_session()
session.headers.update({"User-Agent": self.user_agent})
return session
def get_vulnerabilities(
self,
url,
field_name,
field_value,
timeout=None,
):
"""Get list of vulnerabilities."""
cached_results = cache.get(field_value)
if cached_results:
return cached_results
payload = {field_name: field_value}
response = self.request_get(url=url, params=payload, timeout=timeout)
if response and response.get("count"):
results = response["results"]
cache.set(field_value, results)
return results
def get_vulnerabilities_by_purl(
self,
purl,
timeout=None,
):
"""Get list of vulnerabilities providing a package `purl`."""
return self.get_vulnerabilities(
url=f"{self.api_url}packages/",
field_name="purl",
field_value=get_plain_purl(purl),
timeout=timeout,
)
def get_vulnerabilities_by_cpe(
self,
cpe,
timeout=None,
):
"""Get list of vulnerabilities providing a package or component `cpe`."""
return self.get_vulnerabilities(
url=f"{self.api_url}cpes/",
field_name="cpe",
field_value=cpe,
timeout=timeout,
)
def bulk_search_by_purl(
self,
purls,
purl_only,
timeout=None,
):
"""Bulk search of vulnerabilities using the provided list of `purls`."""
url = f"{self.api_url}packages/bulk_search"
data = {
"purls": purls,
"purl_only": purl_only,
"plain_purl": True,
}
logger.debug(f"VulnerableCode: url={url} purls_count={len(purls)}")
return self.request_post(url=url, json=data, timeout=timeout)
def bulk_search_by_cpes(
self,
cpes,
timeout=None,
):
"""Bulk search of vulnerabilities using the provided list of `cpes`."""
url = f"{self.api_url}cpes/bulk_search"
data = {
"cpes": cpes,
}
logger.debug(f"VulnerableCode: url={url} cpes_count={len(cpes)}")
return self.request_post(url, json=data, timeout=timeout)
def get_vulnerable_purls(self, packages, purl_only=True, timeout=10):
"""
Return a list of PURLs for which at least one `affected_by_vulnerabilities`
was found in the VulnerableCodeDB for the given list of `packages`.
"""
plain_purls = get_plain_purls(packages)
if not plain_purls:
return []
vulnerable_purls = self.bulk_search_by_purl(
plain_purls,
purl_only=purl_only,
timeout=timeout,
)
return vulnerable_purls or []
def get_vulnerable_cpes(self, components):
"""
Return a list of vulnerable CPEs found in the VulnerableCodeDB for the given
list of `components`.
"""
cpes = [component.cpe for component in components if component.cpe]
if not cpes:
return []
search_results = self.bulk_search_by_cpes(cpes)
if not search_results:
return []
vulnerable_cpes = [
reference.get("reference_id")
for entry in search_results
for reference in entry.get("references")
if reference.get("reference_id").startswith("cpe")
]
return list(set(vulnerable_cpes))
def get_package_url_available_types(self):
# Replace by fetching the endpoint once available.
# https://github.com/aboutcode-org/vulnerablecode/issues/1561#issuecomment-2298764730
return [
"alpine",
"alpm",
"apache",
"cargo",
"composer",
"conan",
"deb",
"gem",
"generic",
"github",
"golang",
"hex",
"mattermost",
"maven",
"mozilla",
"nginx",
"npm",
"nuget",
"openssl",
"pypi",
"rpm",
"ruby",
]
def get_plain_purl(purl_str):
"""Remove the PURL qualifiers and subpath from the search lookups."""
return purl_str.split("?")[0]
def get_plain_purls(packages):
"""
Return the PURLs for the given list of `packages`.
List comprehension is not used on purpose to avoid crafting each
PURL twice.
"""
unique_plain_purls = set(
plain_package_url
for package in packages
if (plain_package_url := package.plain_package_url)
)
return list(unique_plain_purls)