From 3267a12900a7ee30c70529a22166b1fea3c28bea Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 13 Dec 2022 10:48:32 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tools/js/npm_pack.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/js/npm_pack.py b/tools/js/npm_pack.py index 9eb6e34365c6..15adadb57e14 100755 --- a/tools/js/npm_pack.py +++ b/tools/js/npm_pack.py @@ -59,7 +59,26 @@ def main(args): if is_bundled(tar): print('%s already has bundled node_modules' % filename) return 1 - tar.extractall(path=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, path=tmpdir) oldpwd = os.getcwd() os.chdir(os.path.join(tmpdir, 'package'))