From d9885aecfe60991900e25895398ee099b187f1b4 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 13 Oct 2022 18:42:05 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- worker/worker.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/worker/worker.py b/worker/worker.py index 01a36c8..6d21365 100755 --- a/worker/worker.py +++ b/worker/worker.py @@ -103,7 +103,26 @@ def ejecutar(corrector, timeout): # Asimismo, el modo ‘r|’ (en lugar de ‘r’) indica que fileobj no es # seekable. with tarfile.open(fileobj=sys.stdin.buffer, mode="r|") as tar: - tar.extractall(tmpdir) + 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(tar, tmpdir) signal.alarm(timeout + 5) try: