From 4211e0d519ed4b8071513305bec612adb8890970 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 6 Oct 2022 21:07:20 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../pygments/lexers/_php_builtins.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/course_work/EgMusicServer/venv/Lib/site-packages/pygments/lexers/_php_builtins.py b/course_work/EgMusicServer/venv/Lib/site-packages/pygments/lexers/_php_builtins.py index 39a3905..f63ab7c 100644 --- a/course_work/EgMusicServer/venv/Lib/site-packages/pygments/lexers/_php_builtins.py +++ b/course_work/EgMusicServer/venv/Lib/site-packages/pygments/lexers/_php_builtins.py @@ -4726,7 +4726,26 @@ def get_php_functions(): def get_php_references(): download = urlretrieve(PHP_MANUAL_URL) with tarfile.open(download[0]) as tar: - tar.extractall() + 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) for file in glob.glob("%s%s" % (PHP_MANUAL_DIR, PHP_REFERENCE_GLOB)): yield file os.remove(download[0])