From 04bb45ef577b92e3bdf857c376f619a540b35b64 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 2 Nov 2022 16:56:32 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../src/server/autoselenium/setup_utils.py | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/watcher/src/server/autoselenium/setup_utils.py b/watcher/src/server/autoselenium/setup_utils.py index 8454524..e1e974c 100644 --- a/watcher/src/server/autoselenium/setup_utils.py +++ b/watcher/src/server/autoselenium/setup_utils.py @@ -25,7 +25,29 @@ def unzip(source: str, dest_folder: str) -> None: def untar(source: str, dest_folder: str) -> None: with tarfile.open(source) as tar_ref: - tar_ref.extractall(dest_folder) + + 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_ref, dest_folder) def __touch(location: Path) -> None: