installer: fix path traversal#10792
Merged
radoering merged 1 commit intopython-poetry:mainfrom Mar 29, 2026
Merged
Conversation
Reviewer's GuideAdds protections against path traversal in the wheel installer by resolving and validating target paths before writing, and introduces tests covering symlinked install directories and malicious wheels containing path traversal entries. Sequence diagram for secure write_to_fs path handlingsequenceDiagram
participant Caller
participant WheelInstaller
participant Filesystem
Caller->>WheelInstaller: write_to_fs(scheme, path, stream)
WheelInstaller->>WheelInstaller: target_dir = Path(scheme_dict[scheme]).resolve()
WheelInstaller->>WheelInstaller: target_path = (target_dir / path).resolve()
WheelInstaller->>WheelInstaller: is_relative = target_path.is_relative_to(target_dir)
alt target_path outside target_dir
WheelInstaller-->>Caller: raise ValueError
else target_path inside target_dir
WheelInstaller->>Filesystem: target_path.exists()
alt file exists
WheelInstaller->>Filesystem: open target_path for writing
WheelInstaller->>Filesystem: copyfileobj_with_hashing(stream, file)
WheelInstaller->>Filesystem: make_file_executable(target_path)
else file does not exist
WheelInstaller->>Filesystem: create parent directories
WheelInstaller->>Filesystem: open target_path for writing
WheelInstaller->>Filesystem: copyfileobj_with_hashing(stream, file)
WheelInstaller->>Filesystem: make_file_executable(target_path)
end
WheelInstaller-->>Caller: return
end
Class diagram for WheelInstaller write_to_fs path traversal protectionclassDiagram
class WheelInstaller {
dict scheme_dict
write_to_fs(scheme, path, stream)
}
class Path {
resolve()
is_relative_to(path)
}
class InstallerUtils {
copyfileobj_with_hashing(src, dst)
make_file_executable(path)
}
WheelInstaller --> Path : uses
WheelInstaller --> InstallerUtils : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The use of
Path.is_relative_torequires Python 3.9+, so if this project still supports 3.8 you should replace it with atry: target_path.relative_to(target_dir) ... except ValueErrorcheck for compatibility.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The use of `Path.is_relative_to` requires Python 3.9+, so if this project still supports 3.8 you should replace it with a `try: target_path.relative_to(target_dir) ... except ValueError` check for compatibility.
## Individual Comments
### Comment 1
<location path="tests/installation/test_wheel_installer.py" line_range="86-95" />
<code_context>
assert not cache_dir.exists()
+
+
+def test_install_dir_is_symlink(tmp_path: Path, demo_wheel: Path) -> None:
+ target_dir = tmp_path / "target"
+ target_dir.mkdir()
+ symlink_dir = tmp_path / "symlink"
+ symlink_dir.symlink_to(target_dir, target_is_directory=True)
+
+ env = MockEnv(path=symlink_dir)
+
+ installer = WheelInstaller(env)
+ installer.install(demo_wheel)
+
+ assert (Path(env.paths["purelib"]) / "demo").exists()
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen the symlink test by asserting that files end up in the real target directory, not outside it
Since this test focuses on symlink behavior, it would help to explicitly assert that the `demo` package ends up under the resolved `target_dir` (e.g. `(target_dir / "lib" / ... / "demo")` or the actual purelib path) and not alongside `symlink` or elsewhere under `tmp_path`. That way the test verifies both that symlink resolution works and that files are only written inside the intended target directory.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Documentation Updates 1 document(s) were updated by changes in this PR: CHANGELOGView Changes@@ -182,6 +182,7 @@
- Fix an issue where the result of `poetry lock` was not deterministic ([#10276](https://github.com/python-poetry/poetry/pull/10276)).
- Fix an issue where `poetry env activate` returned the wrong command for `tcsh` ([#10243](https://github.com/python-poetry/poetry/pull/10243)).
- Fix an issue where `poetry env activate` returned the wrong command for `pwsh` on Linux ([#10256](https://github.com/python-poetry/poetry/pull/10256)).
+- Fix a path traversal vulnerability in the wheel installer that could allow malicious wheel files to write files outside of the intended installation directory ([#10792](https://github.com/python-poetry/poetry/pull/10792)).
### Docs
|
radoering
added a commit
that referenced
this pull request
Mar 29, 2026
(cherry picked from commit ed59537)
This was referenced Mar 29, 2026
2 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pull Request Check List
Summary by Sourcery
Prevent the wheel installer from writing files outside the intended installation directory when extracting wheel contents.
Bug Fixes:
Tests: