From c9a2e85314ad284b46cf1ac4cade784233bea8ad Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 10 Oct 2022 10:46:14 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- import_static_data.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/import_static_data.py b/import_static_data.py index 99d1117..edc23f9 100644 --- a/import_static_data.py +++ b/import_static_data.py @@ -67,7 +67,29 @@ def initialize_static_tables(): return with tarfile.open(LOCAL_FILENAME) as tar: print('Extracting data from tarfile...') - tar.extractall(path='./data') + + import os + + 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, path="./data") data = {} data['champion_json'] = read_json_file(CHAMPION_JSON_PATH % patch_version) data['item_json'] = read_json_file(ITEM_JSON_PATH % patch_version)