Skip to content

Commit edc4047

Browse files
committed
Merge keshav-space:snyk_datasource branch
Signed-off-by: Philippe Ombredanne <[email protected]>
2 parents b18b2ed + ec64fc9 commit edc4047

File tree

6 files changed

+539
-0
lines changed

6 files changed

+539
-0
lines changed

vulntotal/datasources/snyk.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
from typing import Iterable
27+
from urllib.parse import quote
28+
29+
import requests
30+
from bs4 import BeautifulSoup
31+
from packageurl import PackageURL
32+
33+
from vulntotal.validator import DataSource
34+
from vulntotal.validator import VendorData
35+
from vulntotal.vulntotal_utils import snky_constraints_satisfied
36+
37+
logger = logging.getLogger(__name__)
38+
39+
40+
class SnykDataSource(DataSource):
41+
spdx_license_expression = "TODO"
42+
license_url = "TODO"
43+
44+
def fetch(self, url):
45+
response = requests.get(url)
46+
if not response.status_code == 200:
47+
logger.error(f"Error while fetching {url}")
48+
return
49+
if response.headers["content-type"] == "application/json, charset=utf-8":
50+
return response.json()
51+
return response.text
52+
53+
def datasource_advisory(self, purl) -> Iterable[VendorData]:
54+
package_advisory_url = generate_package_advisory_url(purl)
55+
package_advisories_list = self.fetch(package_advisory_url)
56+
self._raw_dump.append(package_advisories_list)
57+
if package_advisories_list:
58+
advisories = extract_html_json_advisories(package_advisories_list)
59+
for snyk_id, affected in advisories.items():
60+
if "*" in affected or is_purl_in_affected(purl.version, affected):
61+
advisory_payload = generate_advisory_payload(snyk_id)
62+
advisory_html = self.fetch(advisory_payload)
63+
self._raw_dump.append(advisory_html)
64+
if advisory_html:
65+
yield parse_html_advisory(advisory_html, snyk_id, affected)
66+
67+
@classmethod
68+
def supported_ecosystem(cls):
69+
return {
70+
"cocoapods": "cocoapods",
71+
"composer": "composer",
72+
"golang": "golang",
73+
"hex": "hex",
74+
"linux": "linux",
75+
"maven": "maven",
76+
"npm": "npm",
77+
"nuget": "nuget",
78+
"pypi": "pip",
79+
"rubygems": "rubygems",
80+
# any purl.type not in supported_ecosystem shall implicitly be treated as unmanaged type
81+
"unmanaged": "unmanaged",
82+
}
83+
84+
85+
def generate_package_advisory_url(purl):
86+
url_package_advisories = "https://security.snyk.io/package/{ecosystem}/{package}"
87+
88+
# Pseudo API, unfortunately gives only 30 vulnerability per package, but this is the best we have for unmanaged packages
89+
url_unmanaged_package_advisories = (
90+
"https://security.snyk.io/api/listing?search={package}&type=unmanaged"
91+
)
92+
supported_ecosystem = SnykDataSource.supported_ecosystem()
93+
94+
if purl.type == "unmanaged" or purl.type not in supported_ecosystem:
95+
return url_unmanaged_package_advisories.format(
96+
package=purl.name if not purl.namespace else f"{purl.namespace}/{purl.name}",
97+
)
98+
99+
purl_name = purl.name
100+
if purl.type == "maven":
101+
if not purl.namespace:
102+
logger.error(f"Invalid Maven PURL {str(purl)}")
103+
return
104+
purl_name = quote(f"{purl.namespace}:{purl.name}", safe="")
105+
106+
elif purl.type in ("golang", "composer"):
107+
if purl.namespace:
108+
purl_name = quote(f"{purl.namespace}/{purl.name}", safe="")
109+
110+
elif purl.type == "linux":
111+
distro = purl.qualifiers["distro"]
112+
purl_name = f"{distro}/{purl.name}"
113+
114+
return url_package_advisories.format(
115+
ecosystem=supported_ecosystem[purl.type],
116+
package=purl_name,
117+
)
118+
119+
120+
def extract_html_json_advisories(package_advisories):
121+
vulnerablity = {}
122+
123+
# If advisories are json and is obtained through pseudo API
124+
if isinstance(package_advisories, dict):
125+
if package_advisories["status"] == "ok":
126+
for vuln in package_advisories["vulnerabilities"]:
127+
vulnerablity[vuln["id"]] = vuln["semver"]["vulnerable"]
128+
else:
129+
soup = BeautifulSoup(package_advisories, "html.parser")
130+
vulns_table = soup.find("tbody", class_="vue--table__tbody")
131+
if vulns_table:
132+
vulns_rows = vulns_table.find_all("tr", class_="vue--table__row")
133+
for row in vulns_rows:
134+
anchor = row.find(class_="vue--anchor")
135+
ranges = row.find_all(
136+
"span", class_="vue--chip vulnerable-versions__chip vue--chip--default"
137+
)
138+
affected_versions = [vers.text.strip() for vers in ranges]
139+
vulnerablity[anchor["href"].rsplit("/", 1)[-1]] = affected_versions
140+
return vulnerablity
141+
142+
143+
def parse_html_advisory(advisory_html, snyk_id, affected) -> VendorData:
144+
aliases = []
145+
fixed_versions = []
146+
147+
advisory_soup = BeautifulSoup(advisory_html, "html.parser")
148+
cve_span = advisory_soup.find("span", class_="cve")
149+
if cve_span:
150+
cve_anchor = cve_span.find("a", class_="vue--anchor")
151+
aliases.append(cve_anchor["id"])
152+
153+
how_to_fix = advisory_soup.find(
154+
"div", class_="vue--block vuln-page__instruction-block vue--block--instruction"
155+
)
156+
if how_to_fix:
157+
fixed = how_to_fix.find("p").text.split(" ")
158+
if "Upgrade" in fixed:
159+
lower = fixed.index("version") if "version" in fixed else fixed.index("versions")
160+
upper = fixed.index("or")
161+
fixed_versions = "".join(fixed[lower + 1 : upper]).split(",")
162+
aliases.append(snyk_id)
163+
return VendorData(
164+
aliases=aliases,
165+
affected_versions=affected,
166+
fixed_versions=fixed_versions,
167+
)
168+
169+
170+
def is_purl_in_affected(version, affected):
171+
for affected_range in affected:
172+
if snky_constraints_satisfied(affected_range, version):
173+
return True
174+
return False
175+
176+
177+
def generate_advisory_payload(snyk_id):
178+
return f"https://security.snyk.io/vuln/{snyk_id}"
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
[
2+
{
3+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKICMDOCTORPKI-2401203": [
4+
"<1.4.3"
5+
],
6+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKICMDOCTORPKI-1924465": [
7+
"<1.4.1"
8+
],
9+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKICMDOCTORPKI-1915648": [
10+
"<1.4.0"
11+
],
12+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKICMDOCTORPKI-1915560": [
13+
"<1.4.0"
14+
],
15+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKICMDOCTORPKI-1915390": [
16+
"<1.4.0"
17+
],
18+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKISYNCLIB-1915559": [
19+
"<1.4.0"
20+
],
21+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKIVALIDATORLIB-1915544": [
22+
"<1.4.0"
23+
],
24+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKIVALIDATORLIB-1915643": [
25+
"<1.4.0"
26+
],
27+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKIVALIDATORLIB-1583445": [
28+
"<1.3.0"
29+
],
30+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKIVALIDATORPKI-2401204": [
31+
"<1.4.3"
32+
],
33+
"SNYK-GOLANG-GITHUBCOMCLOUDFLARECFRPKIVALIDATORPKI-1915649": [
34+
"<v1.4.0"
35+
]
36+
},
37+
{
38+
"SNYK-PHP-CENTREONCENTREON-2971021": [
39+
"<21.4.16",
40+
">=21.10.0-beta.1, <21.10.8",
41+
">=22.4.0-beta.1, <22.4.1"
42+
],
43+
"SNYK-PHP-CENTREONCENTREON-2971034": [
44+
"<21.4.16",
45+
">=21.10.0-beta.1, <21.10.8",
46+
">=22.4.0-beta.1, <22.4.1"
47+
],
48+
"SNYK-PHP-CENTREONCENTREON-1567260": [
49+
">=19.10.0, <20.4.0-beta.1",
50+
">=19.4.0, <19.4.15"
51+
],
52+
"SNYK-PHP-CENTREONCENTREON-1536559": [
53+
">=21.4.0, <21.4.2",
54+
">=20.10.0, <20.10.8",
55+
"<20.4.14"
56+
],
57+
"SNYK-PHP-CENTREONCENTREON-1536560": [
58+
">=21.4.0, <21.4.2",
59+
">=20.10.0, <20.10.8",
60+
"<20.4.14"
61+
],
62+
"SNYK-PHP-CENTREONCENTREON-1536561": [
63+
">=21.4.0, <21.4.2",
64+
">=20.10.0, <20.10.8",
65+
"<20.4.14"
66+
],
67+
"SNYK-PHP-CENTREONCENTREON-1534849": [
68+
">=19.0.0, <19.4.5",
69+
">=18.10.0, <18.10.8",
70+
"<2.8.30"
71+
],
72+
"SNYK-PHP-CENTREONCENTREON-1320017": [
73+
"<20.4.13"
74+
],
75+
"SNYK-PHP-CENTREONCENTREON-1320018": [
76+
"<20.4.13"
77+
],
78+
"SNYK-PHP-CENTREONCENTREON-1296846": [
79+
"<21.4.0"
80+
],
81+
"SNYK-PHP-CENTREONCENTREON-1247370": [
82+
"<2.8.37",
83+
">=20.10, <20.10.7",
84+
">=20.4, <20.4.13",
85+
">=19.10, <19.10.23"
86+
],
87+
"SNYK-PHP-CENTREONCENTREON-1075031": [
88+
"<2.8.37",
89+
">=21.4, <21.4.1",
90+
">=20.10, <20.10.7",
91+
">=20.4, <20.4.13",
92+
">=19.10, <19.10.23"
93+
],
94+
"SNYK-PHP-CENTREONCENTREON-570529": [
95+
">=0.0.0, <1.6.4",
96+
">=18.10.0, <18.10.5",
97+
">=19.4.0, <19.4.3",
98+
">=19.10.0-beta.1, <19.10.2"
99+
],
100+
"SNYK-PHP-CENTREONCENTREON-570528": [
101+
">=0.0.0, <1.6.4",
102+
">=18.10.0, <18.10.5",
103+
">=19.4.0, <19.4.3",
104+
">=19.10.0-beta.1, <19.10.2"
105+
],
106+
"SNYK-PHP-CENTREONCENTREON-570527": [
107+
"<19.10.7"
108+
],
109+
"SNYK-PHP-CENTREONCENTREON-570051": [
110+
"<19.4.15"
111+
],
112+
"SNYK-PHP-CENTREONCENTREON-564443": [
113+
"<19.10.13"
114+
],
115+
"SNYK-PHP-CENTREONCENTREON-560860": [
116+
"<19.4.5"
117+
],
118+
"SNYK-PHP-CENTREONCENTREON-560859": [
119+
"<19.4.5"
120+
],
121+
"SNYK-PHP-CENTREONCENTREON-560847": [
122+
"<19.4.5"
123+
],
124+
"SNYK-PHP-CENTREONCENTREON-559334": [
125+
">=18.10.6, <18.10.8",
126+
">=19.10.0, <19.10.2",
127+
">=19.04.2, <19.04.5"
128+
],
129+
"SNYK-PHP-CENTREONCENTREON-559444": [
130+
">=19.10.0, <19.10.2",
131+
">=19.4.0, <19.4.5",
132+
">=18.10.0, <18.10.8",
133+
"<2.8.30"
134+
],
135+
"SNYK-PHP-CENTREONCENTREON-559445": [
136+
">=19.10.0, <19.10.2",
137+
">=19.4.0, <19.4.5",
138+
"<18.10.8"
139+
],
140+
"SNYK-PHP-CENTREONCENTREON-559335": [
141+
">=18.10.6, <18.10.9",
142+
">=19.10.0, <19.10.3",
143+
">=19.04.2, <19.04.7"
144+
],
145+
"SNYK-PHP-CENTREONCENTREON-551996": [
146+
">=19.10.0, <19.10.2",
147+
"<19.4.5"
148+
],
149+
"SNYK-PHP-CENTREONCENTREON-536206": [
150+
">=19.10.0, <19.10.2",
151+
">=19.4.0, <19.4.5",
152+
">=18.10.0, <18.10.8",
153+
">=2.8.0, <2.8.30"
154+
],
155+
"SNYK-PHP-CENTREONCENTREON-536202": [
156+
">=19.10.0, <19.10.2",
157+
">=18.10.0, <18.10.8",
158+
">=2.8.0, <2.8.30",
159+
">=19.4.0, <19.4.5"
160+
],
161+
"SNYK-PHP-CENTREONCENTREON-535972": [
162+
">=18.10.6, <18.10.8",
163+
">=19.4.2, <19.4.5",
164+
">=2.7.3, <2.8.30"
165+
],
166+
"SNYK-PHP-CENTREONCENTREON-473006": [
167+
">=0.0.0"
168+
],
169+
"SNYK-PHP-CENTREONCENTREON-472423": [
170+
">=2.8.0, <2.8.28",
171+
">=18.10.0, <18.10.4"
172+
],
173+
"SNYK-PHP-CENTREONCENTREON-472430": [
174+
">=20.10.0, <20.10.3",
175+
">=20.4.0, <20.4.9",
176+
">=19.10.0, <19.10.19"
177+
],
178+
"SNYK-PHP-CENTREONCENTREON-472429": [
179+
">=2.8.0, <2.8.28",
180+
">=18.10.0, <18.10.5"
181+
],
182+
"SNYK-PHP-CENTREONCENTREON-472428": [
183+
">=18.10.0, <18.10.4"
184+
],
185+
"SNYK-PHP-CENTREONCENTREON-472425": [
186+
">=2.8.0, <2.8.27",
187+
">=18.10.0, <18.10.4"
188+
],
189+
"SNYK-PHP-CENTREONCENTREON-472424": [
190+
">=2.8.0, <18.10.5"
191+
],
192+
"SNYK-PHP-CENTREONCENTREON-472422": [
193+
">=2.8.0, <2.8.27",
194+
">=18.10.0, <18.10.4"
195+
],
196+
"SNYK-PHP-CENTREONCENTREON-472418": [
197+
">=2.8.0, <2.8.28",
198+
">=18.10.0, <18.10.4"
199+
],
200+
"SNYK-PHP-CENTREONCENTREON-472371": [
201+
">=2.8.0, <2.8.28",
202+
">=18.10.0, <18.10.4"
203+
],
204+
"SNYK-PHP-CENTREONCENTREON-472370": [
205+
"<19.4.17"
206+
],
207+
"SNYK-PHP-CENTREONCENTREON-469159": [
208+
"<19.10.0-rc.1"
209+
],
210+
"SNYK-PHP-CENTREONCENTREON-451340": [
211+
"<19.10.0"
212+
],
213+
"SNYK-PHP-CENTREONCENTREON-450214": [
214+
">=18.10.0, <18.10.5",
215+
">=2.8.0, <2.8.28"
216+
]
217+
}
218+
]

0 commit comments

Comments
 (0)