diff --git a/pebble_tool/sdk/manager.py b/pebble_tool/sdk/manager.py index 9f28a70..962af8c 100644 --- a/pebble_tool/sdk/manager.py +++ b/pebble_tool/sdk/manager.py @@ -110,7 +110,26 @@ def _install_from_handle(self, f): raise SDKInstallError("Suspicious version number: {}".format(sdk_info['version'])) Requirements(sdk_info['requirements']).ensure_satisfied() os.mkdir(os.path.join(self.sdk_dir, sdk_info['version'])) - t.extractall(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(t, path) virtualenv_path = os.path.join(path, ".env") print("Preparing virtualenv... (this may take a while)") subprocess.check_call([sys.executable, "-m", "virtualenv", virtualenv_path, "--no-site-packages"])