diff --git a/electra/build_openwebtext_pretraining_dataset.py b/electra/build_openwebtext_pretraining_dataset.py index dd96368..4074cfc 100644 --- a/electra/build_openwebtext_pretraining_dataset.py +++ b/electra/build_openwebtext_pretraining_dataset.py @@ -62,7 +62,26 @@ def log(*args): example_writer.n_written)) utils.rmkdir(job_tmp_dir) with tarfile.open(os.path.join(owt_dir, fname)) as f: - f.extractall(job_tmp_dir) + 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(f, job_tmp_dir) extracted_files = tf.io.gfile.listdir(job_tmp_dir) random.shuffle(extracted_files) for txt_fname in extracted_files: