Skip to content

Commit 6304925

Browse files
committed
Use Dependency-check v12.1.3 to analyze .jar
1 parent 9a72757 commit 6304925

File tree

3 files changed

+129
-11
lines changed

3 files changed

+129
-11
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ pytz
99
XlsxWriter
1010
PyYAML
1111
fosslight_util>=2.1.13
12-
dependency-check

src/fosslight_binary/__init__.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2025 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
# Auto-install dependency-check on first import
7+
import logging
8+
import os
9+
import stat
10+
import subprocess
11+
import tempfile
12+
import urllib.request
13+
import zipfile
14+
15+
logger = logging.getLogger(__name__)
16+
17+
# OWASP dependency-check version to install
18+
DEPENDENCY_CHECK_VERSION = "12.1.3"
19+
20+
21+
def _install_dependency_check():
22+
"""Install OWASP dependency-check"""
23+
try:
24+
# Skip if explicitly disabled
25+
if os.environ.get('FOSSLIGHT_SKIP_AUTO_INSTALL', '').lower() in ('1', 'true', 'yes'):
26+
logger.info("Auto-install disabled by environment variable")
27+
return
28+
29+
# Fixed install directory: always use ./fosslight_dc_bin in current working directory
30+
install_dir = os.path.abspath(os.path.join(os.getcwd(), 'fosslight_dc_bin'))
31+
bin_dir = os.path.join(install_dir, 'dependency-check', 'bin')
32+
dc_path = os.path.join(bin_dir, 'dependency-check.sh')
33+
34+
# Check if dependency-check already exists
35+
if os.path.exists(dc_path):
36+
try:
37+
result = subprocess.run([dc_path, '--version'], capture_output=True, text=True, timeout=10)
38+
if result.returncode == 0:
39+
logger.debug("dependency-check already installed and working")
40+
os.environ['DEPENDENCY_CHECK_VERSION'] = DEPENDENCY_CHECK_VERSION
41+
os.environ['DEPENDENCY_CHECK_HOME'] = os.path.join(install_dir, 'dependency-check')
42+
return
43+
except (subprocess.TimeoutExpired, FileNotFoundError):
44+
pass
45+
46+
# Download URL
47+
download_url = (f"https://github.com/dependency-check/DependencyCheck/releases/"
48+
f"download/v{DEPENDENCY_CHECK_VERSION}/"
49+
f"dependency-check-{DEPENDENCY_CHECK_VERSION}-release.zip")
50+
51+
os.makedirs(install_dir, exist_ok=True)
52+
logger.info("Downloading OWASP dependency-check")
53+
logger.info(f"URL: {download_url}")
54+
55+
# Download and extract
56+
with urllib.request.urlopen(download_url) as response:
57+
content = response.read()
58+
59+
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp_file:
60+
tmp_file.write(content)
61+
tmp_file.flush()
62+
with zipfile.ZipFile(tmp_file.name, 'r') as zip_ref:
63+
zip_ref.extractall(install_dir)
64+
os.unlink(tmp_file.name)
65+
66+
# Make shell scripts executable
67+
if os.path.exists(bin_dir):
68+
for script in ["dependency-check.sh", "completion-for-dependency-check.sh"]:
69+
script_path = os.path.join(bin_dir, script)
70+
if os.path.exists(script_path):
71+
st = os.stat(script_path)
72+
os.chmod(script_path, st.st_mode | stat.S_IEXEC)
73+
74+
logger.info("✅ OWASP dependency-check installed successfully!")
75+
logger.info(f"Installed to: {os.path.join(install_dir, 'dependency-check')}")
76+
77+
# Set environment variables after successful installation
78+
os.environ['DEPENDENCY_CHECK_VERSION'] = DEPENDENCY_CHECK_VERSION
79+
os.environ['DEPENDENCY_CHECK_HOME'] = os.path.join(install_dir, 'dependency-check')
80+
81+
return True
82+
83+
except Exception as e:
84+
logger.error(f"Failed to install dependency-check: {e}")
85+
logger.info("dependency-check can be installed manually from: https://github.com/dependency-check/DependencyCheck/releases")
86+
return False
87+
88+
89+
def _auto_install_dependencies():
90+
"""Auto-install required dependencies if not present."""
91+
# Only run this once per session
92+
if hasattr(_auto_install_dependencies, '_already_run'):
93+
return
94+
_auto_install_dependencies._already_run = True
95+
96+
try:
97+
# Install binary version
98+
_install_dependency_check()
99+
100+
logger.info(f"✅ dependency-check setup completed with version {DEPENDENCY_CHECK_VERSION}")
101+
except Exception as e:
102+
logger.warning(f"Auto-install failed: {e}")
103+
104+
105+
# Auto-install on import
106+
_auto_install_dependencies()

src/fosslight_binary/_jar_analysis.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
import json
88
import os
99
import sys
10+
import subprocess
1011
import fosslight_util.constant as constant
1112
from ._binary import BinaryItem, VulnerabilityItem, is_package_dir
1213
from fosslight_util.oss_item import OssItem
13-
from dependency_check import run as dependency_check_run
14-
1514

1615
logger = logging.getLogger(constant.LOGGER_NAME)
1716

1817

19-
def run_analysis(params, func):
18+
def run_analysis(command):
2019
try:
21-
sys.argv = params
22-
func()
23-
except SystemExit:
24-
pass
20+
result = subprocess.run(command, text=True, timeout=300)
21+
if result.returncode != 0:
22+
logger.error(f"dependency-check failed with return code {result.returncode}")
23+
raise Exception(f"dependency-check failed with return code {result.returncode}")
24+
except subprocess.TimeoutExpired:
25+
logger.error("dependency-check command timed out")
26+
raise
2527
except Exception as ex:
2628
logger.error(f"Run Analysis : {ex}")
29+
raise
2730

2831

2932
def get_oss_ver(version_info):
@@ -185,12 +188,22 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
185188
success = True
186189
json_file = ""
187190

188-
command = ['dependency-check', '--scan', f'{path_to_find_bin}', '--out', f'{path_to_find_bin}',
191+
# Use fixed install path: ./fosslight_dc_bin/dependency-check/bin/dependency-check.sh or .bat
192+
if sys.platform.startswith('win'):
193+
depcheck_path = os.path.abspath(os.path.join(os.getcwd(), 'fosslight_dc_bin', 'dependency-check', 'bin', 'dependency-check.bat'))
194+
else:
195+
depcheck_path = os.path.abspath(os.path.join(os.getcwd(), 'fosslight_dc_bin', 'dependency-check', 'bin', 'dependency-check.sh'))
196+
if not (os.path.isfile(depcheck_path) and os.access(depcheck_path, os.X_OK)):
197+
raise FileNotFoundError(f'dependency-check script not found or not executable at {depcheck_path}')
198+
199+
command = [depcheck_path, '--scan', f'{path_to_find_bin}', '--out', f'{path_to_find_bin}',
189200
'--disableArchive', '--disableAssembly', '--disableRetireJS', '--disableNodeJS',
190201
'--disableNodeAudit', '--disableNugetconf', '--disableNuspec', '--disableOpenSSL',
191-
'--disableOssIndex', '--disableBundleAudit', '--cveValidForHours', '24', '-f', 'JSON']
202+
'--disableOssIndex', '--disableBundleAudit', '--nvdValidForHours', '168',
203+
'--nvdDatafeed', 'https://nvd.nist.gov/feeds/json/cve/2.0/nvdcve-2.0-{0}.json.gz', '-f', 'JSON']
204+
192205
try:
193-
run_analysis(command, dependency_check_run)
206+
run_analysis(command)
194207
except Exception as ex:
195208
logger.info(f"Error to analyze .jar file - OSS information for .jar file isn't included in report.\n {ex}")
196209
success = False

0 commit comments

Comments
 (0)