Skip to content

Commit

Permalink
Merge pull request #421 from DataDog/s.obregoso/fix_tarextract_permis…
Browse files Browse the repository at this point in the history
…sions

Bugfix: Adding permissions to traverse extracted files and folders
  • Loading branch information
sobregosodd authored Jul 30, 2024
2 parents 1ab60ec + f5f5fbc commit ad60938
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion guarddog/utils/archives.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import zipfile

import stat
import tarsafe # type:ignore

log = logging.getLogger("guarddog")
Expand Down Expand Up @@ -62,7 +62,26 @@ def safe_extract(source_archive: str, target_directory: str) -> None:
"""
log.debug(f"Extracting archive {source_archive} to directory {target_directory}")
if is_tar_archive(source_archive):

def add_exec(path):
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)

def add_read(path):
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IREAD)

def recurse_add_perms(path):
add_exec(path)
for root, dirs, files in os.walk(path):
for d in dirs:
add_exec(os.path.join(root, d))
for f in files:
add_read(os.path.join(root, f))

tarsafe.open(source_archive).extractall(target_directory)
recurse_add_perms(target_directory)

elif is_zip_archive(source_archive):
with zipfile.ZipFile(source_archive, 'r') as zip:
for file in zip.namelist():
Expand Down

0 comments on commit ad60938

Please sign in to comment.