From d9a1adf9b19bba308b1ad117145cf7151c89d50a Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 13 Oct 2022 18:26:48 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../src/DataDownload.py" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/\345\237\272\347\241\200\346\225\231\347\250\213/A2-\347\245\236\347\273\217\347\275\221\347\273\234\345\237\272\346\234\254\345\216\237\347\220\206/src/DataDownload.py" "b/\345\237\272\347\241\200\346\225\231\347\250\213/A2-\347\245\236\347\273\217\347\275\221\347\273\234\345\237\272\346\234\254\345\216\237\347\220\206/src/DataDownload.py" index 030de4682..c8c0cc311 100644 --- "a/\345\237\272\347\241\200\346\225\231\347\250\213/A2-\347\245\236\347\273\217\347\275\221\347\273\234\345\237\272\346\234\254\345\216\237\347\220\206/src/DataDownload.py" +++ "b/\345\237\272\347\241\200\346\225\231\347\250\213/A2-\347\245\236\347\273\217\347\275\221\347\273\234\345\237\272\346\234\254\345\216\237\347\220\206/src/DataDownload.py" @@ -17,7 +17,29 @@ print("Extracting...") try: with tarfile.open(to_path) as file: - file.extractall(path = local) + + 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(file, path=local) print("Done.") print("All Work Finished!") except: