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

Fix #181: Arbitrary file write during tarfile extraction #182

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Security-20240808-154439.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Security
body: Fix arbitrary file write during tarfile extraction
time: 2024-08-08T15:44:39.601346-05:00
custom:
Author: aranke
PR: "182"
26 changes: 25 additions & 1 deletion dbt_common/clients/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,35 @@ def rename(from_path: str, to_path: str, force: bool = False) -> None:
shutil.move(from_path, to_path)


def safe_extract(tarball, path=".", members=None, *, numeric_owner=False):
aranke marked this conversation as resolved.
Show resolved Hide resolved
"""
Fix for CWE-22: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Solution copied from https://github.com/mindsdb/mindsdb/blob/main/mindsdb/utilities/fs.py
"""

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

# for py >= 3.12
if hasattr(tarball, "data_filter"):
tarball.extractall(path, members=members, numeric_owner=numeric_owner, filter="data")
else:
for member in tarball.getmembers():
member_path = os.path.join(path, member.name)
if not _is_within_directory(path, member_path):
raise tarfile.OutsideDestinationError(member, path)

tarball.extractall(path, members=members, numeric_owner=numeric_owner)


def untar_package(tar_path: str, dest_dir: str, rename_to: Optional[str] = None) -> None:
tar_path = convert_path(tar_path)
tar_dir_name = None
with tarfile.open(tar_path, "r:gz") as tarball:
tarball.extractall(dest_dir)
safe_extract(tarball, dest_dir)
tar_dir_name = os.path.commonprefix(tarball.getnames())
if rename_to:
downloaded_path = os.path.join(dest_dir, tar_dir_name)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_system_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,18 @@ def test_untar_package_empty(self) -> None:
with self.assertRaises(tarfile.ReadError) as exc:
dbt_common.clients.system.untar_package(named_file.name, self.tempdest)
self.assertEqual("empty file", str(exc.exception))

def test_untar_package_outside_directory(self) -> None:
with NamedTemporaryFile(
prefix="my-package.2", suffix=".tar.gz", dir=self.tempdir, delete=False
) as named_tar_file:
tar_file_full_path = named_tar_file.name
with NamedTemporaryFile(prefix="a", suffix=".txt", dir=self.tempdir) as file_a:
file_a.write(b"some text in the text file")
relative_file_a = "/../" + os.path.basename(file_a.name)
with tarfile.open(fileobj=named_tar_file, mode="w:gz") as tar:
tar.addfile(tarfile.TarInfo(relative_file_a), open(file_a.name))

assert tarfile.is_tarfile(tar.name)
with self.assertRaises(tarfile.OutsideDestinationError):
dbt_common.clients.system.untar_package(tar_file_full_path, self.tempdest)
Loading