Adding tarfile member sanitization to extractall()

This commit is contained in:
TrellixVulnTeam 2022-11-02 13:28:31 +00:00 committed by Kris Nuttycombe
parent c9b63cc3e5
commit 543df3797a
1 changed files with 20 additions and 1 deletions

View File

@ -79,7 +79,26 @@ class UpgradeGoldenTest(BitcoinTestFramework):
regtest_path = self.options.tmpdir+"/node"+ str(i)+"/regtest"
shutil.rmtree(regtest_path)
with tarfile.open(upgrade.tgz_path, "r:gz") as tgz:
tgz.extractall(path = regtest_path)
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(tgz, path=regtest_path)
# Upgrade each node to the latest network version. If any fails to
# start, this will fail the test.