From 11c38c063e47c64803927c4102dc23795a05cb3d Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 6 Jan 2023 01:54:50 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- flame/manage.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/flame/manage.py b/flame/manage.py index 6519b81..84bd392 100644 --- a/flame/manage.py +++ b/flame/manage.py @@ -274,7 +274,26 @@ def action_import(model): # unpack tar.gz. This is done for any kind of export file with tarfile.open(importfile, 'r:gz') as tar: - tar.extractall(base_path) + 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) + + + safe_extract(tar, base_path) # when importing a single version we need to clone the last folder to 'dev' inner_dirs = os.listdir(base_path)