Adding tarfile member sanitization to extractall()
This commit is contained in:
parent
c9b63cc3e5
commit
543df3797a
|
@ -79,7 +79,26 @@ class UpgradeGoldenTest(BitcoinTestFramework):
|
||||||
regtest_path = self.options.tmpdir+"/node"+ str(i)+"/regtest"
|
regtest_path = self.options.tmpdir+"/node"+ str(i)+"/regtest"
|
||||||
shutil.rmtree(regtest_path)
|
shutil.rmtree(regtest_path)
|
||||||
with tarfile.open(upgrade.tgz_path, "r:gz") as tgz:
|
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
|
# Upgrade each node to the latest network version. If any fails to
|
||||||
# start, this will fail the test.
|
# start, this will fail the test.
|
||||||
|
|
Loading…
Reference in New Issue