From 1ff56d33fe279b47e24541b86447f80852c62795 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 24 Oct 2022 01:44:03 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- lineflow/datasets/cnn_dailymail.py | 21 ++++++++++++++++++++- lineflow/datasets/imdb.py | 21 ++++++++++++++++++++- lineflow/datasets/text_classification.py | 21 ++++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lineflow/datasets/cnn_dailymail.py b/lineflow/datasets/cnn_dailymail.py index fb5a2be..20f0299 100644 --- a/lineflow/datasets/cnn_dailymail.py +++ b/lineflow/datasets/cnn_dailymail.py @@ -22,7 +22,26 @@ def creator(path): target_path = os.path.join(root, 'raw') with tarfile.open(archive_path, 'r') as archive: print(f'Extracting to {target_path}') - archive.extractall(target_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(archive, target_path) dataset = {} for split in ('train', 'dev', 'test'): diff --git a/lineflow/datasets/imdb.py b/lineflow/datasets/imdb.py index 7f1a5f9..78f9f18 100644 --- a/lineflow/datasets/imdb.py +++ b/lineflow/datasets/imdb.py @@ -20,7 +20,26 @@ def creator(path): archive_path = gdown.cached_download(url) with tarfile.open(archive_path, 'r') as archive: print(f'Extracting to {root}...') - archive.extractall(root) + 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(archive, root) extracted_path = os.path.join(root, 'aclImdb') diff --git a/lineflow/datasets/text_classification.py b/lineflow/datasets/text_classification.py index 68ee8b4..4182b8c 100644 --- a/lineflow/datasets/text_classification.py +++ b/lineflow/datasets/text_classification.py @@ -60,7 +60,26 @@ def easyfile_creator(path): with tarfile.open(archive_path, 'r') as archive: print(f'Extracting to {root}...') - archive.extractall(root) + 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(archive, root) dataset = {} for split in ('train', 'test'):