From 3c8cdfdb87bd9a7d15c12d56e0fef45e6bbaa46c Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 8 Oct 2022 20:14:40 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- .../programacao_distribuida_com_DASK/prep.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/98-dados/programacao_distribuida_com_DASK/prep.py b/98-dados/programacao_distribuida_com_DASK/prep.py index 573137b..487730e 100644 --- a/98-dados/programacao_distribuida_com_DASK/prep.py +++ b/98-dados/programacao_distribuida_com_DASK/prep.py @@ -30,7 +30,26 @@ def flights(): print("- Extracting flight data... ", end='', flush=True) tar_path = os.path.join(data_dir, 'nycflights.tar.gz') with tarfile.open(tar_path, mode='r:gz') as flights: - flights.extractall('data/') + 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(flights, "data/") print("done", flush=True) if not os.path.exists(jsondir): From d2605a616f97e730bf16ce621e65d2a69f287c84 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 8 Oct 2022 20:15:05 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- 98-dados/programacao_distribuida_com_DASK/prep.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/98-dados/programacao_distribuida_com_DASK/prep.py b/98-dados/programacao_distribuida_com_DASK/prep.py index 487730e..a4b5b25 100644 --- a/98-dados/programacao_distribuida_com_DASK/prep.py +++ b/98-dados/programacao_distribuida_com_DASK/prep.py @@ -119,7 +119,7 @@ def create_weather(growth=32): if not filenames: ws_dir = os.path.join(data_dir, 'weather-small') - raise ValueError('Did not find any hdf5 files in {}'.format(ws_dir)) + raise ValueError(f'Did not find any hdf5 files in {ws_dir}') if not os.path.exists(os.path.join(data_dir, 'weather-big')): os.mkdir(os.path.join(data_dir, 'weather-big'))