Skip to content

Commit ff6a93e

Browse files
committed
add OSS-Index DataSource
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 360077c commit ff6a93e

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

vulntotal/datasources/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
2323

2424

25-
DATASOURCE_REGISTRY = []
25+
from vulntotal.datasources import oss
26+
27+
DATASOURCE_REGISTRY = [
28+
oss.OSSDataSource,
29+
]
2630

2731
DATASOURCE_REGISTRY = {x.__module__.split(".")[-1]: x for x in DATASOURCE_REGISTRY}

vulntotal/datasources/oss.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
import os
27+
from typing import Iterable
28+
import requests
29+
30+
from vulntotal.validator import DataSource
31+
from vulntotal.validator import VendorData
32+
33+
logger = logging.getLogger(__name__)
34+
35+
36+
class OSSDataSource(DataSource):
37+
spdx_license_expression = "TODO"
38+
license_url = "TODO"
39+
api_unauthenticated = "https://ossindex.sonatype.org/api/v3/component-report"
40+
api_authenticated = "https://ossindex.sonatype.org/api/v3/authorized/component-report"
41+
42+
def fetch_json_response(self, coordinates):
43+
username = os.environ.get("OSS_USERNAME", None)
44+
token = os.environ.get("OSS_TOKEN", None)
45+
auth = None
46+
url = self.api_unauthenticated
47+
if username and token:
48+
auth = (username, token)
49+
url = self.api_authenticated
50+
response = requests.post(url, auth=auth, json={"coordinates": coordinates})
51+
52+
if response.status_code == 200:
53+
return response.json()
54+
elif response.status_code == 401:
55+
logger.error("Invalid credentials")
56+
elif response.status_code == 429:
57+
msg = (
58+
"Too many requests"
59+
if auth
60+
else "Too many requests: add OSS_USERNAME and OSS_TOKEN in .env file"
61+
)
62+
logger.error(msg)
63+
else:
64+
logger.error(f"unknown status code: {response.status_code} while fetching: {url}")
65+
66+
def datasource_advisory(self, purl) -> Iterable[VendorData]:
67+
if purl.type not in self.supported_ecosystem():
68+
logger.error("Unsupported PURL")
69+
return
70+
71+
response = self.fetch_json_response([str(purl)])
72+
if response:
73+
self._raw_dump.append(response)
74+
return parse_advisory(response)
75+
76+
@classmethod
77+
def supported_ecosystem(cls):
78+
return {
79+
"cargo": "cargo",
80+
"cocoapods": "cocoapods",
81+
"composer": "composer",
82+
"conan": "conan",
83+
"conda": "conda",
84+
"cran": "cran",
85+
"golang": "golang",
86+
"maven": "maven",
87+
"npm": "npm",
88+
"nuget": "nuget",
89+
"pypi": "pypi",
90+
"rpm": "rpm",
91+
"gem": "gem",
92+
"swift": "swift",
93+
}
94+
95+
96+
def parse_advisory(component) -> Iterable[VendorData]:
97+
response = component[0]
98+
if response["vulnerabilities"]:
99+
for vuln in response["vulnerabilities"]:
100+
aliases = [vuln["id"]]
101+
affected_versions = []
102+
fixed_versions = []
103+
if "versionRanges" in vuln:
104+
affected_versions.extend(vuln["versionRanges"])
105+
yield VendorData(
106+
aliases=aliases,
107+
affected_versions=affected_versions,
108+
fixed_versions=fixed_versions,
109+
)

0 commit comments

Comments
 (0)