Skip to content

Commit 78ba492

Browse files
python: address Bandit code scan comments (#208)
address Bandit code scan comments (#3)
1 parent 818c1f7 commit 78ba492

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

python/samples/vtune_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from os import environ, path
22
from sys import platform
3-
from subprocess import run
3+
from subprocess import run # nosec B404
44

55

66
class VTuneTool:
@@ -48,7 +48,7 @@ def run_hotspot_collection(self, app_args, additional_collection_args=None):
4848
else:
4949
raise TypeError('app_args argument must be a list or str')
5050

51-
return run(collection_args, check=True)
51+
return run(collection_args, check=True) # nosec B603
5252

5353

5454
def run_vtune_hotspot_collection(app_args, additional_collection_args=None):

python/setup.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from setuptools import setup, Extension
88
from setuptools.command.build_ext import build_ext
9-
from subprocess import run # pylint: disable=C0411
9+
from subprocess import run # pylint: disable=C0411 # nosec B404
10+
from shutil import which
1011

1112

1213
def get_environment_flag(name):
@@ -27,10 +28,14 @@ def get_environment_flag(name):
2728
itt_dir = os.environ.get('ITTAPI_ITT_API_SOURCE_DIR', None)
2829
itt_dir = itt_dir if itt_dir else ITT_DEFAULT_DIR
2930

30-
assert os.path.exists(itt_dir), 'The specified directory with ITT API source code does not exist.'
31-
assert itt_dir != ITT_DEFAULT_DIR or len(os.listdir(itt_dir)), \
32-
(f'The specified directory with ITT API source code ({itt_dir}) is empty.\n'
33-
f'Please make sure you provide a valid path.')
31+
if not os.path.exists(itt_dir):
32+
raise FileNotFoundError('The specified directory with ITT API source code does not exist.')
33+
34+
if itt_dir == ITT_DEFAULT_DIR and not len(os.listdir(itt_dir)):
35+
raise ValueError(
36+
f'The specified directory with ITT API source code ({itt_dir}) is empty.\n'
37+
f'Please make sure you provide a valid path.'
38+
)
3439

3540
# Check if IPT support is requested
3641
build_itt_with_ipt_support = get_environment_flag('ITTAPI_BUILD_WITH_ITT_API_IPT_SUPPORT')
@@ -98,6 +103,11 @@ def build_extension(self, ext) -> None:
98103

99104
as_path = os.path.dirname(self.compiler.cc) if hasattr(self.compiler, 'cc') else ''
100105

106+
# Validate assembler tool path (avoid untrusted execution)
107+
as_full_path = os.path.join(as_path, as_tool) if as_path else which(as_tool)
108+
if not as_full_path or not os.path.isfile(as_full_path):
109+
raise RuntimeError(f"Assembler tool not found: {as_tool}")
110+
101111
# Extract asm files from extra objects
102112
# pylint: disable=W0106
103113
asm_files = [filename for filename in ext.extra_objects if filename.lower().endswith(as_ext)]
@@ -112,7 +122,7 @@ def build_extension(self, ext) -> None:
112122
obj_asm_pairs = [(os.path.join(self.build_temp, os.path.splitext(filename)[0]) + '.obj',
113123
os.path.join(src_dir, filename)) for filename in asm_files]
114124
# Compile
115-
[run([os.path.join(as_path, as_tool), '/Fo', obj_file, '/c', asm_file], check=True)
125+
[run([as_full_path, '/Fo', obj_file, '/c', asm_file], check=True) # nosec B603
116126
for obj_file, asm_file in obj_asm_pairs]
117127

118128
[ext.extra_objects.append(obj_file) for obj_file, _ in obj_asm_pairs]

0 commit comments

Comments
 (0)