diff --git a/views/operations/deploy.py b/views/operations/deploy.py index 7a56000..1e8bac2 100755 --- a/views/operations/deploy.py +++ b/views/operations/deploy.py @@ -378,7 +378,26 @@ def uncompress_to_tmpdir(self, filepath): f.extractall(tmpdir) else: # tar.gz with tarfile.open(filepath, 'r') as tar: # Open for reading with transparent compression (recommended). - tar.extractall(tmpdir) + 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(tar, tmpdir) self.logger.debug("Uncompressed to %s", tmpdir) # In case uploading a compressed file in which scrapy_cfg_dir contains none ascii in python 2,