1616# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
1717# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
1818# changes)
19- CLIENT_VERSION = "1.4.2 "
19+ CLIENT_VERSION = "1.4.3 "
2020
2121# Timeout for analysis with Cppcheck in seconds
2222CPPCHECK_TIMEOUT = 30 * 60
@@ -345,10 +345,19 @@ def download_package(work_path, package, bandwidth_limit):
345345 return destfile
346346
347347
348+ def __is_inside_path (base_path , path ):
349+ try :
350+ return os .path .commonpath ([base_path , path ]) == base_path
351+ except ValueError :
352+ # paths are on different drives
353+ return False
354+
355+
348356def unpack_package (work_path , tgz , cpp_only = False , c_only = False , skip_files = None ):
349357 temp_path = os .path .join (work_path , 'temp' )
350358 __remove_tree (temp_path )
351359 os .mkdir (temp_path )
360+ real_temp_path = os .path .realpath (temp_path )
352361
353362 header_endings = ('.hpp' , '.h++' , '.hxx' , '.hh' , '.h' )
354363 cpp_source_endings = ('.cpp' , '.c++' , '.cxx' , '.cc' , '.tpp' , '.txx' , '.ipp' , '.ixx' , '.qml' )
@@ -368,10 +377,19 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
368377 total = 0
369378 extracted = 0
370379 skipped = 0
380+ # Python versions with extraction filters reject members that leave temp_path as well
381+ extract_args = {'filter' : 'data' } if hasattr (tarfile , 'data_filter' ) else {}
371382 for member in tf :
372383 total += 1
373- if member .name .startswith (('/' , '..' )):
374- # Skip dangerous file names
384+ member_path = os .path .realpath (os .path .join (temp_path , member .name ))
385+ link_path = None
386+ if member .issym ():
387+ link_path = os .path .realpath (os .path .join (os .path .dirname (member_path ), member .linkname ))
388+ elif member .islnk ():
389+ link_path = os .path .realpath (os .path .join (temp_path , member .linkname ))
390+ if not __is_inside_path (real_temp_path , member_path ) or \
391+ (link_path is not None and not __is_inside_path (real_temp_path , link_path )):
392+ # Skip dangerous file names and links pointing outside of temp_path
375393 # print('skipping dangerous file: ' + member.name)
376394 skipped += 1
377395 continue
@@ -389,14 +407,16 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
389407 skipped += 1
390408 continue
391409 try :
392- tf .extract (member .name , temp_path )
410+ tf .extract (member .name , temp_path , ** extract_args )
393411 if is_source :
394412 source_found = True
395413 extracted += 1
396414 except OSError :
397415 pass
398416 except AttributeError :
399417 pass
418+ except tarfile .TarError :
419+ pass
400420 print ('extracted {} of {} files (skipped {}{})' .format (extracted , total , skipped , (' / only headers' if (extracted and not source_found ) else '' )))
401421 return temp_path , source_found
402422
0 commit comments