From 734c067787753564d14d9579a3451b4cd74586de Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 21 Dec 2022 02:03:48 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- AU_rcnn/utils/download.py | 21 ++++++++++++++++++++- CNN/utils/download.py | 21 ++++++++++++++++++++- ROI_nets/utils/download.py | 21 ++++++++++++++++++++- lstm_end_to_end/utils/download.py | 21 ++++++++++++++++++++- two_stream_rgb_flow/utils/download.py | 21 ++++++++++++++++++++- 5 files changed, 100 insertions(+), 5 deletions(-) diff --git a/AU_rcnn/utils/download.py b/AU_rcnn/utils/download.py index 314b329..0b7886a 100644 --- a/AU_rcnn/utils/download.py +++ b/AU_rcnn/utils/download.py @@ -129,4 +129,23 @@ def extractall(file_path, destination, ext): t.extractall(destination) elif ext == '.gz' or ext == '.tgz': with tarfile.open(file_path, 'r:gz') as t: - t.extractall(destination) + 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, destination) diff --git a/CNN/utils/download.py b/CNN/utils/download.py index 314b329..0b7886a 100644 --- a/CNN/utils/download.py +++ b/CNN/utils/download.py @@ -129,4 +129,23 @@ def extractall(file_path, destination, ext): t.extractall(destination) elif ext == '.gz' or ext == '.tgz': with tarfile.open(file_path, 'r:gz') as t: - t.extractall(destination) + 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, destination) diff --git a/ROI_nets/utils/download.py b/ROI_nets/utils/download.py index 314b329..0b7886a 100644 --- a/ROI_nets/utils/download.py +++ b/ROI_nets/utils/download.py @@ -129,4 +129,23 @@ def extractall(file_path, destination, ext): t.extractall(destination) elif ext == '.gz' or ext == '.tgz': with tarfile.open(file_path, 'r:gz') as t: - t.extractall(destination) + 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, destination) diff --git a/lstm_end_to_end/utils/download.py b/lstm_end_to_end/utils/download.py index 314b329..0b7886a 100644 --- a/lstm_end_to_end/utils/download.py +++ b/lstm_end_to_end/utils/download.py @@ -129,4 +129,23 @@ def extractall(file_path, destination, ext): t.extractall(destination) elif ext == '.gz' or ext == '.tgz': with tarfile.open(file_path, 'r:gz') as t: - t.extractall(destination) + 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, destination) diff --git a/two_stream_rgb_flow/utils/download.py b/two_stream_rgb_flow/utils/download.py index 314b329..0b7886a 100644 --- a/two_stream_rgb_flow/utils/download.py +++ b/two_stream_rgb_flow/utils/download.py @@ -129,4 +129,23 @@ def extractall(file_path, destination, ext): t.extractall(destination) elif ext == '.gz' or ext == '.tgz': with tarfile.open(file_path, 'r:gz') as t: - t.extractall(destination) + 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, destination)