From a0050c1dc55e440e8e702e088cedc2fe01bb853b Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 18 Dec 2022 19:55:49 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- views/operations/deploy.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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,