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'))