diff --git a/volttron/services/control/control_service.py b/volttron/services/control/control_service.py index e710dcc..a1be1b3 100644 --- a/volttron/services/control/control_service.py +++ b/volttron/services/control/control_service.py @@ -832,7 +832,26 @@ def backup_agent_data(output_filename, source_dir): def restore_agent_data_from_tgz(source_file, output_dir): # Open tarfile with tarfile.open(source_file, mode="r:gz") as tar: - tar.extractall(output_dir) + 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, output_dir) def get_agent_data_dir_by_uuid(opts, agent_uuid):