-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
pathlib.PurePath.relative_to(walk_up=True) mishandles '..' components #105002
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
Labels
3.12
only security fixes
3.13
bugs and security fixes
topic-pathlib
type-bug
An unexpected behavior, bug, or error
Comments
@domragusa FYI! A patch like this should do the trick: diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 8cb5279d73..e6bf388f1f 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -602,6 +602,8 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
for step, path in enumerate([other] + list(other.parents)):
if self.is_relative_to(path):
break
+ elif path.name == '..':
+ raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
else:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors") |
kukovecz
added a commit
to kukovecz/cpython
that referenced
this issue
Jul 22, 2023
kukovecz
added a commit
to kukovecz/cpython
that referenced
this issue
Jul 22, 2023
For absolute paths, this was working as intended, only new test cases were added. For relative paths it makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
kukovecz
added a commit
to kukovecz/cpython
that referenced
this issue
Jul 23, 2023
For absolute paths, this was working as intended, only new test cases were added. For relative paths it makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
kukovecz
added a commit
to kukovecz/cpython
that referenced
this issue
Jul 24, 2023
It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
kukovecz
added a commit
to kukovecz/cpython
that referenced
this issue
Jul 25, 2023
It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
barneygale
pushed a commit
that referenced
this issue
Jul 26, 2023
…07014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Jul 26, 2023
…." (pythonGH-107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown. (cherry picked from commit e7e6e4b) Co-authored-by: János Kukovecz <kukoveczjanos@gmail.com>
barneygale
pushed a commit
that referenced
this issue
Jul 26, 2023
….." (GH-107014) (#107315) gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (GH-107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown. (cherry picked from commit e7e6e4b) Co-authored-by: János Kukovecz <kukoveczjanos@gmail.com>
Resolved - thanks @kukovecz! |
jtcave
pushed a commit
to jtcave/cpython
that referenced
this issue
Jul 27, 2023
…." (python#107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
3.12
only security fixes
3.13
bugs and security fixes
topic-pathlib
type-bug
An unexpected behavior, bug, or error
pathlib.PurePath.relative_to(other, walk_up=True)
doesn't handle '..' segments in its other argument correctly:PurePath
objects do not know the current working directory, nor can they safely eliminate..
segments without resolving symlinks, so I think raisingValueError
is the only reasonable thing to do.Linked PRs
The text was updated successfully, but these errors were encountered: