Skip to content

Commit bb2c990

Browse files
committed
Mapping vul. url to each OSS
1 parent 1281150 commit bb2c990

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ XlsxWriter
1010
PyYAML
1111
fosslight_util>=2.1.6
1212
dependency-check
13+
Levenshtein

src/fosslight_binary/_binary.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,35 @@
33
# Copyright (c) 2020 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
55
from fosslight_util.oss_item import FileItem
6+
import Levenshtein
67

78
EXCLUDE_TRUE_VALUE = "Exclude"
89
TLSH_CHECKSUM_NULL = "0"
910

1011

12+
def find_most_similar_word(input_string, oss_name_list):
13+
most_similar_word = None
14+
min_distance = float('inf')
15+
16+
for oss in oss_name_list:
17+
distance = Levenshtein.distance(input_string, oss.name)
18+
if distance < min_distance:
19+
min_distance = distance
20+
most_similar_word = oss.name
21+
return most_similar_word
22+
23+
1124
class VulnerabilityItem:
1225
file_path = ""
1326
vul_id = ""
1427
nvd_url = ""
28+
oss_items = []
1529

16-
def __init__(self, file_path, id, url):
30+
def __init__(self, file_path, id, url, oss_items):
1731
self.file_path = file_path
1832
self.vul_id = id
1933
self.nvd_url = url
34+
self.oss_items = oss_items
2035

2136

2237
class BinaryItem(FileItem):
@@ -42,9 +57,29 @@ def set_oss_items(self, new_oss_list, exclude=False, exclude_msg=""):
4257
# Append New input OSS
4358
self.oss_items.extend(new_oss_list)
4459

45-
def get_vulnerability_items(self):
46-
nvd_url = [vul_item.nvd_url for vul_item in self.vulnerability_items]
47-
return ", ".join(nvd_url)
60+
def get_vulnerability_items(self, oss_name):
61+
nvd_url = []
62+
nvd_urls = ""
63+
nvd_url_dict = {}
64+
65+
for vul_item in self.vulnerability_items:
66+
found_oss_name = ""
67+
68+
if vul_item.file_path == self.source_name_or_path:
69+
if len(self.oss_items) > 1:
70+
if vul_item.nvd_url:
71+
found_oss_name = find_most_similar_word(vul_item.nvd_url, vul_item.oss_items)
72+
if oss_name == found_oss_name:
73+
nvd_urls = f"{nvd_urls}\n{vul_item.nvd_url}"
74+
else:
75+
nvd_url = nvd_url_dict.get(vul_item.file_path)
76+
if nvd_url:
77+
nvd_url.append(vul_item.nvd_url)
78+
nvd_urls = "\n".join(nvd_url)
79+
else:
80+
nvd_url_dict[vul_item.file_path] = [vul_item.nvd_url]
81+
nvd_urls = "\n".join(nvd_url_dict[vul_item.file_path])
82+
return nvd_urls.strip()
4883

4984
def get_print_binary_only(self):
5085
return (self.source_name_or_path + "\t" + self.checksum + "\t" + self.tlsh)
@@ -55,7 +90,7 @@ def get_print_array(self):
5590
for oss in self.oss_items:
5691
lic = ",".join(oss.license)
5792
exclude = EXCLUDE_TRUE_VALUE if (self.exclude or oss.exclude) else ""
58-
nvd_url = self.get_vulnerability_items()
93+
nvd_url = self.get_vulnerability_items(oss.name)
5994
items.append([self.source_name_or_path, oss.name, oss.version,
6095
lic, oss.download_location, oss.homepage,
6196
oss.copyright, exclude, oss.comment,

src/fosslight_binary/_jar_analysis.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def merge_binary_list(owasp_items, vulnerability_items, bin_list):
8686
return bin_list
8787

8888

89-
def get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, remove_vulnerability_items):
89+
def get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, remove_vulnerability_items, owasp_items):
9090
if vulnerability:
9191
try:
9292
for vul_info in vulnerability:
@@ -97,8 +97,8 @@ def get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, r
9797
vul_id = val
9898
elif key == 'url':
9999
nvd_url = val
100-
101-
vul_item = VulnerabilityItem(file_with_path, vul_id, nvd_url)
100+
101+
vul_item = VulnerabilityItem(file_with_path, vul_id, nvd_url, owasp_items[file_with_path])
102102

103103
remove_vulnerability_items = vulnerability_items.get(file_with_path)
104104
if remove_vulnerability_items:
@@ -257,9 +257,6 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
257257
if oss_dl_url == "":
258258
oss_dl_url = get_oss_dl_url(vendor_info)
259259

260-
# Get Vulnerability Info.
261-
vulnerability_items = get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, remove_vulnerability_items)
262-
263260
if oss_name != "" or oss_ver != "" or oss_license != "" or oss_dl_url != "":
264261
oss = OssItem(oss_name, oss_ver, oss_license, oss_dl_url)
265262
oss.comment = "OWASP result"
@@ -269,6 +266,9 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
269266
remove_owasp_item.append(oss)
270267
else:
271268
owasp_items[file_with_path] = [oss]
269+
270+
# Get Vulnerability Info.
271+
vulnerability_items = get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, remove_vulnerability_items, owasp_items)
272272
except Exception as ex:
273273
logger.debug(f"Error to get depency Info in jar_contets: {ex}")
274274
success = False

0 commit comments

Comments
 (0)