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
21 changes: 20 additions & 1 deletion mys/cli/packages_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ def extract_dependency(name, version, archive_path):
archive_path)

with tarfile.open(archive_path) as fin:
fin.extractall(DOWNLOAD_DIRECTORY)
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(fin, DOWNLOAD_DIRECTORY)

if version == 'latest':
rename_one_matching(os.path.join(DOWNLOAD_DIRECTORY, f'{name}-*/'),
Expand Down
21 changes: 20 additions & 1 deletion mys/cli/subparsers/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ def install_extract():

with Spinner(text='Extracting package'):
with tarfile.open(archive) as fin:
fin.extractall()
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(fin)

os.remove(archive)

Expand Down
21 changes: 20 additions & 1 deletion mys/pygments/pygments/lexers/_php_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4725,7 +4725,26 @@ def get_php_functions():
def get_php_references():
download = urlretrieve(PHP_MANUAL_URL)
with tarfile.open(download[0]) as tar:
tar.extractall()
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(tar)
yield from glob.glob("%s%s" % (PHP_MANUAL_DIR, PHP_REFERENCE_GLOB))
os.remove(download[0])

Expand Down