Skip to content

Commit 62354ab

Browse files
authored
Merge pull request #173 from fosslight/abspath
Remove unnecessary abspath
2 parents 5765fe2 + af99fba commit 62354ab

3 files changed

Lines changed: 23 additions & 68 deletions

File tree

src/fosslight_binary/_binary.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2020 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5-
import os
65
import urllib.parse
76
import logging
87
import fosslight_util.constant as constant
9-
from typing import Tuple
108
from fosslight_util.oss_item import FileItem
119

1210
EXCLUDE_TRUE_VALUE = "Exclude"
1311
TLSH_CHECKSUM_NULL = "0"
1412
MAX_EXCEL_URL_LENGTH = 255
1513
EXCEEDED_VUL_URL_LENGTH_COMMENT = f"Exceeded the maximum vulnerability URL length of {MAX_EXCEL_URL_LENGTH} characters."
16-
_PACKAGE_DIR = ["node_modules", "venv", "Pods", "Carthage"]
1714

1815
logger = logging.getLogger(constant.LOGGER_NAME)
1916

@@ -111,15 +108,3 @@ def get_print_json(self):
111108
if self.comment:
112109
json_item["comment"] = self.comment
113110
return items
114-
115-
116-
def is_package_dir(bin_with_path: str, _root_path: str) -> Tuple[bool, str]:
117-
is_pkg = False
118-
pkg_path = ""
119-
path_parts = bin_with_path.split(os.path.sep)
120-
for pkg_dir in _PACKAGE_DIR:
121-
if pkg_dir in path_parts:
122-
pkg_index = path_parts.index(pkg_dir)
123-
pkg_path = os.path.sep.join(path_parts[:pkg_index + 1]).replace(_root_path, '', 1)
124-
is_pkg = True
125-
return is_pkg, pkg_path

src/fosslight_binary/_jar_analysis.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import subprocess
1010
from fosslight_binary import get_dependency_check_script
1111
import fosslight_util.constant as constant
12-
from fosslight_binary._binary import BinaryItem, VulnerabilityItem, is_package_dir
12+
from fosslight_binary._binary import BinaryItem, VulnerabilityItem
1313
from fosslight_util.oss_item import OssItem
1414

1515
logger = logging.getLogger(constant.LOGGER_NAME)
@@ -91,10 +91,6 @@ def merge_binary_list(owasp_items, vulnerability_items, bin_list):
9191
bin_item.binary_name_without_path = os.path.basename(key)
9292
bin_item.source_name_or_path = key
9393

94-
is_pkg, _ = is_package_dir(bin_item.source_name_or_path, '')
95-
if is_pkg:
96-
continue
97-
9894
bin_item.set_oss_items(oss_list)
9995
not_found_bin.append(bin_item)
10096

@@ -246,30 +242,15 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
246242
# Even if the oss info is from pom.xml in jar file, the file name will be .jar file.
247243
# But the oss info from pom.xml could be different from .jar file.
248244
bin_with_path = val.get("filePath")
249-
250-
if any(os.path.commonpath([bin_with_path, exclude_path]) == exclude_path
251-
for exclude_path in path_to_exclude):
245+
bin_with_path_rel = os.path.relpath(bin_with_path, path_to_find_bin)
246+
# Check if bin_with_path should be excluded (compare relative paths)
247+
if bin_with_path_rel in path_to_exclude:
252248
continue
253249

254-
if not bin_with_path.endswith('.jar'):
255-
bin_with_path = bin_with_path.split('.jar')[0] + '.jar'
256-
257-
try:
258-
path_to_fild_bin_abs = os.path.abspath(path_to_find_bin)
259-
bin_with_path_abs = os.path.abspath(bin_with_path)
260-
if os.name == 'nt': # Windows
261-
drive_bin = os.path.splitdrive(bin_with_path_abs)[0].lower()
262-
drive_root = os.path.splitdrive(path_to_fild_bin_abs)[0].lower()
263-
# Different drive or UNC root -> fallback to basename
264-
if drive_bin and drive_root and drive_bin != drive_root:
265-
file_with_path = os.path.basename(bin_with_path_abs)
266-
else:
267-
file_with_path = os.path.relpath(bin_with_path_abs, path_to_fild_bin_abs)
268-
else:
269-
file_with_path = os.path.relpath(bin_with_path_abs, path_to_fild_bin_abs)
270-
except Exception as e:
271-
file_with_path = os.path.basename(bin_with_path)
272-
logger.error(f"relpath error: {e}; fallback basename: {file_with_path}")
250+
if not bin_with_path_rel.endswith('.jar'):
251+
bin_with_path_rel = bin_with_path_rel.split('.jar')[0] + '.jar'
252+
253+
file_with_path = bin_with_path_rel
273254

274255
# First, Get OSS Name and Version info from pkg_info
275256
for pkg_info in all_pkg_info:

src/fosslight_binary/binary_analysis.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import fosslight_util.constant as constant
1717
from fosslight_util.output_format import check_output_formats_v2, write_output_file
1818
from ._binary_dao import get_oss_info_from_db
19-
from ._binary import BinaryItem, TLSH_CHECKSUM_NULL, is_package_dir
19+
from ._binary import BinaryItem, TLSH_CHECKSUM_NULL
2020
from ._jar_analysis import analyze_jar_file, merge_binary_list
2121
from ._simple_mode import print_simple_mode, filter_binary, init_simple
2222
from fosslight_util.correct import correct_with_yaml
@@ -131,19 +131,16 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
131131
return _result_log, combined_paths_and_files, output_extensions, formats
132132

133133

134-
def get_file_list(path_to_find, abs_path_to_exclude):
134+
def get_file_list(path_to_find, excluded_files):
135135
bin_list = []
136136
file_cnt = 0
137137
found_jar = False
138138

139139
for root, dirs, files in os.walk(path_to_find):
140-
if os.path.abspath(root) in abs_path_to_exclude:
141-
continue
142140
for file in files:
143-
file_path = os.path.join(root, file)
144-
file_abs_path = os.path.abspath(file_path)
145-
if any(os.path.commonpath([file_abs_path, exclude_path]) == exclude_path
146-
for exclude_path in abs_path_to_exclude):
141+
bin_with_path = os.path.join(root, file)
142+
rel_path_file = os.path.relpath(bin_with_path, path_to_find).replace('\\', '/')
143+
if rel_path_file in excluded_files:
147144
continue
148145
file_lower_case = file.lower()
149146
extension = os.path.splitext(file_lower_case)[1][1:].strip()
@@ -155,26 +152,18 @@ def get_file_list(path_to_find, abs_path_to_exclude):
155152
dir_path = directory.replace(_root_path, '', 1).lower()
156153
dir_path = os.path.sep + dir_path + os.path.sep
157154

158-
bin_with_path = os.path.join(root, file)
159155
bin_item = BinaryItem(bin_with_path)
160156
bin_item.binary_name_without_path = file
161157
bin_item.source_name_or_path = bin_with_path.replace(_root_path, '', 1)
162158

163-
is_pkg, pkg_path = is_package_dir(bin_with_path, _root_path)
164-
if is_pkg:
165-
bin_item.source_name_or_path = pkg_path
166-
if not any(x.source_name_or_path == bin_item.source_name_or_path for x in bin_list):
167-
bin_item.exclude = True
168-
bin_list.append(bin_item)
169-
continue
170-
171159
bin_list.append(bin_item)
172160
file_cnt += 1
173161
return file_cnt, bin_list, found_jar
174162

175163

176164
def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=False,
177-
correct_mode=True, correct_filepath="", path_to_exclude=[]):
165+
correct_mode=True, correct_filepath="", path_to_exclude=[],
166+
all_exclude_mode=()):
178167
global start_time, _root_path, _result_log
179168

180169
mode = "Normal Mode"
@@ -200,10 +189,12 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
200189
bin_list = []
201190
scan_item = ScannerItem(PKG_NAME, "")
202191

203-
excluded_path_with_default_exclusion, excluded_path_without_dot, excluded_files, cnt_file_except_skipped \
204-
= get_excluded_paths(path_to_find_bin, path_to_exclude)
205-
206-
abs_path_to_exclude = [os.path.abspath(os.path.join(path_to_find_bin, path)) for path in excluded_files]
192+
if all_exclude_mode and len(all_exclude_mode) == 4:
193+
excluded_path_with_default_exclusion, excluded_path_without_dot, excluded_files, cnt_file_except_skipped = all_exclude_mode
194+
else:
195+
excluded_path_with_default_exclusion, excluded_path_without_dot, excluded_files, cnt_file_except_skipped \
196+
= get_excluded_paths(path_to_find_bin, path_to_exclude)
197+
logger.debug(f"Skipped paths: {excluded_path_with_default_exclusion}")
207198

208199
if not os.path.isdir(path_to_find_bin):
209200
error_occured(error_msg=f"(-p option) Can't find the directory: {path_to_find_bin}",
@@ -213,7 +204,7 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
213204
if not correct_filepath:
214205
correct_filepath = path_to_find_bin
215206
try:
216-
total_file_cnt, file_list, found_jar = get_file_list(path_to_find_bin, abs_path_to_exclude)
207+
total_file_cnt, file_list, found_jar = get_file_list(path_to_find_bin, excluded_files)
217208
return_list = list(return_bin_only(file_list))
218209
except Exception as ex:
219210
error_occured(error_msg=f"Failed to check whether it is binary or not : {ex}",
@@ -245,7 +236,7 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
245236
logger.warning(f"Java version {java_ver} detected (<11). FOSSLight Binary Scanner requires Java 11+ to analyze .jar files.")
246237
else:
247238
logger.info("Run OWASP Dependency-check to analyze .jar file")
248-
owasp_items, vulnerability_items, success = analyze_jar_file(path_to_find_bin, abs_path_to_exclude)
239+
owasp_items, vulnerability_items, success = analyze_jar_file(path_to_find_bin, excluded_files)
249240
if success:
250241
return_list = merge_binary_list(owasp_items, vulnerability_items, return_list)
251242
else:
@@ -263,8 +254,6 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
263254
logger.info("Success to correct with yaml.")
264255

265256
scan_item.set_cover_comment(f"Detected binaries: {len(return_list)} (Scanned Files : {cnt_file_except_skipped})")
266-
if total_bin_cnt == 0:
267-
scan_item.set_cover_comment("(No binary detected.) ")
268257

269258
for combined_path_and_file, output_extension, output_format in zip(result_reports, output_extensions, formats):
270259
results.append(write_output_file(combined_path_and_file, output_extension, scan_item,

0 commit comments

Comments
 (0)