Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CVE-2007-4559 Patch #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 frontend/mate/mate/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,26 @@ def _attach_source_lines_to_nodes(self, session: orm.Session) -> None:
# TODO(ww): Maybe support other kinds of tarballs/archives? Worth it?
local_artifact = source_artifact.persist_locally(suffix=".tar.gz")
with tarfile.open(local_artifact, "r:gz") as tar:
tar.extractall(path=temp_source_dir.name)
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)


safe_extract(tar, path=temp_source_dir.name)
source_dir = Path(temp_source_dir.name)
elif source_artifact.kind == ArtifactKind.CompileTargetBrokeredChallenge:
local_artifact = source_artifact.persist_locally(suffix=".zip")
Expand Down
42 changes: 40 additions & 2 deletions frontend/mate/mate/build/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,26 @@ def _compile_tarball_local(self, artifact: db.Artifact) -> None:
try:
# TODO(ww): Maybe support other kinds of tarballs/archives? Worth it?
with tarfile.open(local_target, "r:gz") as tar:
tar.extractall(path=local_target_dir.name)
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)


safe_extract(tar, path=local_target_dir.name)
except tarfile.TarError as e:
self._raise(
f"failed to open/extract tarball: {e}",
Expand Down Expand Up @@ -785,7 +804,26 @@ def _compile_tarball_containerized(self, artifact: db.Artifact) -> None:
local_target = artifact.persist_locally(suffix=".tar.gz")
try:
with tarfile.open(local_target, "r:gz") as tar:
tar.extractall(path=scratch_src)
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)


safe_extract(tar, path=scratch_src)
except tarfile.TarError as e:
self._raise(
"failed to open/extract tarball",
Expand Down