Fix potential path or symlink traversal
Use `realpath` instead of `abspath` and remove unused `safe_extract` arguments & defaults. Reviewed with @daira @str4d @sellout
This commit is contained in:
parent
543df3797a
commit
95e3447b96
|
@ -80,25 +80,22 @@ class UpgradeGoldenTest(BitcoinTestFramework):
|
||||||
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:
|
||||||
def is_within_directory(directory, target):
|
def is_within_directory(directory, target):
|
||||||
|
abs_directory = os.path.realpath(directory, strict=True)
|
||||||
abs_directory = os.path.abspath(directory)
|
abs_target = os.path.realpath(target, strict=True)
|
||||||
abs_target = os.path.abspath(target)
|
|
||||||
|
|
||||||
prefix = os.path.commonprefix([abs_directory, abs_target])
|
prefix = os.path.commonprefix([abs_directory, abs_target])
|
||||||
|
|
||||||
return prefix == abs_directory
|
return prefix == abs_directory
|
||||||
|
|
||||||
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
|
def safe_extract(tar, path):
|
||||||
|
|
||||||
for member in tar.getmembers():
|
for member in tar.getmembers():
|
||||||
member_path = os.path.join(path, member.name)
|
member_path = os.path.join(path, member.name)
|
||||||
if not is_within_directory(path, member_path):
|
if not is_within_directory(path, member_path):
|
||||||
raise Exception("Attempted Path Traversal in Tar File")
|
raise Exception("Attempted Path or Symlink Traversal in Tar File")
|
||||||
|
|
||||||
tar.extractall(path, members, numeric_owner=numeric_owner)
|
tar.extractall(path)
|
||||||
|
|
||||||
|
safe_extract(tgz, regtest_path)
|
||||||
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