-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(framework): Improve zip extraction robustness in flwr install #6627
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d4036e
fix(framework): Improve zip extraction robustness in flwr install
danieljanes f0ce1ec
Align exception messages
danieljanes 94515fc
Merge branch 'main' into fix-install-zip-extraction
danieljanes 4ec92de
Update tests
danieljanes ccad97e
Merge branch 'main' into fix-install-zip-extraction
panh99 764aebc
fix
panh99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright 2026 Flower Labs GmbH. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
| """Archive helpers for CLI commands.""" | ||
|
|
||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
|
|
||
|
|
||
| def safe_extract_zip( | ||
| zf: zipfile.ZipFile, | ||
| dest_dir: Path, | ||
| ) -> None: | ||
| """Extract ZIP contents safely into the destination directory. | ||
|
|
||
| This prevents path traversal (zip-slip) by validating that each member path resolves | ||
| within ``dest_dir`` before extraction. | ||
| """ | ||
| base_dir = dest_dir.resolve() | ||
|
|
||
| for member in zf.infolist(): | ||
| target = (base_dir / member.filename).resolve() | ||
| try: | ||
| target.relative_to(base_dir) | ||
| except ValueError: | ||
| raise click.ClickException( | ||
| f"Unsafe path in FAB: {member.filename}" | ||
danieljanes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) from None | ||
|
|
||
| zf.extractall(base_dir) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2026 Flower Labs GmbH. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
| """Tests for Flower command line interface `install` command.""" | ||
|
|
||
|
|
||
| import io | ||
| import zipfile | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
| import pytest | ||
|
|
||
| from .archive_utils import safe_extract_zip | ||
| from .install import install_from_fab | ||
|
|
||
|
|
||
| def _zip_bytes(entries: list[tuple[str, bytes]]) -> bytes: | ||
| """Create ZIP bytes from (path, content) entries.""" | ||
| buf = io.BytesIO() | ||
| with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: | ||
| for name, content in entries: | ||
| zf.writestr(name, content) | ||
| return buf.getvalue() | ||
|
|
||
|
|
||
| def test_safe_extract_zip_extracts_regular_files(tmp_path: Path) -> None: | ||
| """Safe extraction should succeed for regular archive entries.""" | ||
| zip_bytes = _zip_bytes([("dir/file.txt", b"hello")]) | ||
|
|
||
| with zipfile.ZipFile(io.BytesIO(zip_bytes), "r") as zf: | ||
| safe_extract_zip(zf, tmp_path, archive_name="FAB archive") | ||
danieljanes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert (tmp_path / "dir" / "file.txt").read_bytes() == b"hello" | ||
|
|
||
|
|
||
| def test_safe_extract_zip_rejects_parent_traversal(tmp_path: Path) -> None: | ||
| """Safe extraction should reject path traversal via '..' entries.""" | ||
| zip_bytes = _zip_bytes([("../evil.txt", b"x")]) | ||
|
|
||
| with zipfile.ZipFile(io.BytesIO(zip_bytes), "r") as zf: | ||
| with pytest.raises(click.ClickException, match="Unsafe path in FAB archive"): | ||
| safe_extract_zip(zf, tmp_path, archive_name="FAB archive") | ||
danieljanes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert not (tmp_path.parent / "evil.txt").exists() | ||
|
|
||
|
|
||
| def test_safe_extract_zip_rejects_absolute_paths(tmp_path: Path) -> None: | ||
| """Safe extraction should reject absolute archive paths.""" | ||
| zip_bytes = _zip_bytes([("/tmp/evil.txt", b"x")]) | ||
|
|
||
| with zipfile.ZipFile(io.BytesIO(zip_bytes), "r") as zf: | ||
| with pytest.raises(click.ClickException, match="Unsafe path in FAB archive"): | ||
| safe_extract_zip(zf, tmp_path, archive_name="FAB archive") | ||
danieljanes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_install_from_fab_rejects_zip_slip(tmp_path: Path) -> None: | ||
| """install_from_fab should fail fast on zip-slip entries.""" | ||
| fab_bytes = _zip_bytes( | ||
| [ | ||
| ("../evil.txt", b"x"), | ||
| (".info/CONTENT", b""), | ||
| ] | ||
| ) | ||
|
|
||
| with pytest.raises(click.ClickException, match="Unsafe path in FAB archive"): | ||
| _ = install_from_fab(fab_bytes, flwr_dir=tmp_path, skip_prompt=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.