diff --git a/src/python/WMCore/WMRuntime/Unpacker.py b/src/python/WMCore/WMRuntime/Unpacker.py index 731dd71d16..d3ab5e916d 100644 --- a/src/python/WMCore/WMRuntime/Unpacker.py +++ b/src/python/WMCore/WMRuntime/Unpacker.py @@ -78,7 +78,26 @@ def createWorkArea(sandbox): os.makedirs(os.path.join(jobDir, 'StartupScript')) with tarfile.open(sandbox, "r") as tfile: - tfile.extractall(jobDir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tfile, jobDir) # need to pull out the startup file from the zipball with zipfile.ZipFile(os.path.join(jobDir, 'WMCore.zip'), 'r') as zfile: