Reject file scripts outside project root#948
Open
xujiantop-crypto wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Using
Path.is_relative_toties this code to Python 3.9+; if Poetry still supports older Python versions, consider replacing it with atry/exceptaroundrelative_toto preserve compatibility. - To avoid redundant resolution and make the intent clearer, resolve the project root once (e.g.
root = self._path.resolve()) and then computeabs_path = root.joinpath(source).resolve()before performing the containment check.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Using `Path.is_relative_to` ties this code to Python 3.9+; if Poetry still supports older Python versions, consider replacing it with a `try/except` around `relative_to` to preserve compatibility.
- To avoid redundant resolution and make the intent clearer, resolve the project root once (e.g. `root = self._path.resolve()`) and then compute `abs_path = root.joinpath(source).resolve()` before performing the containment check.
## Individual Comments
### Comment 1
<location path="src/poetry/core/masonry/builders/builder.py" line_range="323" />
<code_context>
)
abs_path = Path.joinpath(self._path, source)
+ if not abs_path.resolve().is_relative_to(self._path.resolve()):
+ raise RuntimeError(
+ f"{source} in {name} is outside the project root. Expected"
</code_context>
<issue_to_address>
**issue (bug_risk):** Usage of Path.is_relative_to may break on Python < 3.9.
`Path.is_relative_to` was added in Python 3.9. On Python 3.8 this will raise `AttributeError` at runtime if `poetry-core` still supports that version. You can get the same behavior in a version‑compatible way:
```python
resolved_root = self._path.resolve()
resolved_abs = abs_path.resolve()
try:
resolved_abs.relative_to(resolved_root)
except ValueError:
raise RuntimeError(
f"{source} in {name} is outside the project root. Expected"
" relative path inside the project root."
)
```
This avoids `is_relative_to` while preserving the current semantics.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| ) | ||
|
|
||
| abs_path = Path.joinpath(self._path, source) | ||
| if not abs_path.resolve().is_relative_to(self._path.resolve()): |
There was a problem hiding this comment.
issue (bug_risk): Usage of Path.is_relative_to may break on Python < 3.9.
Path.is_relative_to was added in Python 3.9. On Python 3.8 this will raise AttributeError at runtime if poetry-core still supports that version. You can get the same behavior in a version‑compatible way:
resolved_root = self._path.resolve()
resolved_abs = abs_path.resolve()
try:
resolved_abs.relative_to(resolved_root)
except ValueError:
raise RuntimeError(
f"{source} in {name} is outside the project root. Expected"
" relative path inside the project root."
)This avoids is_relative_to while preserving the current semantics.
452369e to
fa3562e
Compare
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.
Supersedes #947 after renaming the contribution branch.
Resolves: python-poetry#
Summary
Tests
PYTHONPATH=src python -m pytest tests\masonry\builders\test_builder.py -q
PYTHONPATH=src python -m pytest tests\masonry\builders\test_wheel.py::test_wheel_package tests\masonry\builders\test_wheel.py::test_wheel_file_is_closed tests\masonry\builders\test_wheel.py::test_wheel_include_formats -q
python -m ruff check src\poetry\core\masonry\builders\builder.py tests\masonry\builders\test_builder.py
Added tests for changed code.
Updated documentation for changed code.