Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion enot/pac_cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,29 @@ def unpackage(self, package: Package): # TODO move me to package? use current d
ensure_empty(unpack_dir)
info('Extract ' + enotpack)
with tarfile.open(enotpack) as pack:
pack.extractall(unpack_dir)

import os

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(pack, unpack_dir)
package.path = unpack_dir # update path pointer
copy_file(enotpack, join(unpack_dir, package.name + '.ep'))

Expand Down
21 changes: 20 additions & 1 deletion enot/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@ def tar(path: str, dirs: list, dst: str):

def untar(path: str, dst: str):
with tarfile.open(path, 'r') as archive:
archive.extractall(dst)
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(archive, dst)


# TODO catch read errors
Expand Down