Simplify and add error message

This commit is contained in:
Adriane Boyd 2022-11-03 15:19:58 +01:00
parent 25d56ce0f7
commit 641f350144
2 changed files with 5 additions and 10 deletions

View File

@ -85,24 +85,17 @@ class RemoteStorage:
# This requires that the path is added correctly, relative
# to root. This is how we set things up in push()
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
):
def safe_extract(tar, path):
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)
raise ValueError(Errors.E852)
tar.extractall(path)
safe_extract(tar_file, self.root)
return url

View File

@ -542,6 +542,8 @@ class Errors(metaclass=ErrorsWithCodes):
E202 = ("Unsupported {name} mode '{mode}'. Supported modes: {modes}.")
# New errors added in v3.x
E852 = ("The tar file pulled from the remote attempted an unsafe path "
"traversal.")
E853 = ("Unsupported component factory name '{name}'. The character '.' is "
"not permitted in factory names.")
E854 = ("Unable to set doc.ents. Check that the 'ents_filter' does not "