diff --git a/infrared/core/services/workspaces.py b/infrared/core/services/workspaces.py index 828302bfc3..df13306046 100644 --- a/infrared/core/services/workspaces.py +++ b/infrared/core/services/workspaces.py @@ -473,7 +473,26 @@ def import_workspace(self, workspace_src, workspace_name=None): original_src, new_workspace.path)) with tarfile.open(workspace_src) as tar: - tar.extractall(path=new_workspace.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=numeric_owner) + + + safe_extract(tar, path=new_workspace.path) except urllib3.exceptions.HTTPError: raise exceptions.IRFailedToImportWorkspace(