66
77from setuptools import setup , Extension
88from 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
1213def get_environment_flag (name ):
@@ -27,10 +28,14 @@ def get_environment_flag(name):
2728itt_dir = os .environ .get ('ITTAPI_ITT_API_SOURCE_DIR' , None )
2829itt_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
3641build_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