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

(security) Fix tar slip in micropackaging #3559

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion kedro/framework/cli/micropkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ def _is_within_directory(directory: Path, target: Path) -> bool:


def safe_extract(tar: tarfile.TarFile, path: Path) -> None:
safe_members = []
for member in tar.getmembers():
member_path = path / member.name
if not _is_within_directory(path, member_path):
# noqa: broad-exception-raised
raise Exception("Failed to safely extract tar file.")
tar.extractall(path) # nosec B202
safe_members.append(member)
tar.extractall(path, members=safe_members) # nosec B202
ankatiyar marked this conversation as resolved.
Show resolved Hide resolved


def _unpack_sdist(location: str, destination: Path, fs_args: str | None) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/cli/micropkg/test_micropkg_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def test_micropkg_pull_invalid_sdist(
assert sdist_file.is_file()

with tarfile.open(sdist_file, "r:gz") as tar:
tar.extractall(tmp_path)
safe_extract(tar, tmp_path)

# Create extra project
extra_project = tmp_path / f"{PIPELINE_NAME}-0.1_extra"
Expand Down Expand Up @@ -801,7 +801,7 @@ def test_micropkg_pull_invalid_package_contents(
assert sdist_file.is_file()

with tarfile.open(sdist_file, "r:gz") as tar:
tar.extractall(tmp_path)
safe_extract(tar, tmp_path)

# Create extra package
extra_package = tmp_path / f"{PIPELINE_NAME}-0.1" / f"{PIPELINE_NAME}_extra"
Expand Down