gh-149835: Resolve symlinks in shutil._destinsrc#149851
Closed
ding-alex wants to merge 1 commit into
Closed
Conversation
Use os.path.realpath instead of os.path.abspath when normalising the src and dst paths in shutil._destinsrc. abspath only collapses '.' and '..' components, so a symlink anywhere in dst could let a destination that is physically inside src appear to be outside it in string space, silently bypassing the cross-device move guard in shutil.move and causing copytree to recurse infinitely. Add a regression test that constructs such a symlink and asserts _destinsrc still detects the containment.
|
The following commit authors need to sign the Contributor License Agreement: |
Author
|
PR Author Agent⚡ on behalf of @ding-alex 👋 I'm driving this PR from here to merge. Here's what I'll do:
I'll post updates here as the PR progresses. Feel free to leave a comment anytime! Note: this PR was opened as a draft — @ding-alex, please mark it ready-for-review when you'd like reviewers requested. |
Author
|
PR Author Agent⚡ on behalf of @ding-alex @ding-alex PR is blocked by CLA signing. Please sign the Python Software Foundation CLA at https://cla.python.org/ — I can't resolve this from the agent. |
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.
Fixes #149835.
shutil.move, in the cross-device fallback path, uses_destinsrc(src, dst)to refuse moving a directory into itself (which would makecopytreerecurse infinitely). The guard normalises the two paths withos.path.abspathand then does a string-prefix check.abspathonly collapses./..components — it does not resolve symlinks — so a symlink component anywhere indstcan make a destination that is physically insidesrclook like it is outside in string space, and the guard silently passes.This change uses
os.path.realpathfor bothsrcanddstso the comparison is done on the resolved, canonical paths.A regression test (
TestMove.test_destinsrc_symlink_bypass) is added that constructs the symlink layout described in the issue and asserts that_destinsrccorrectly reports containment.Author: @ding-alex
Pull Request opened by Augment Code | View session
shutil.movecan move the dir into itself (with a symlink) #149835