mirror of
https://github.com/python/cpython.git
synced 2026-06-10 20:01:28 +00:00
gh-149835: Use realpath() instead of abspath() in shutil.move() (GH-149986)
This commit is contained in:
parent
b18b6a49c6
commit
fab449bddb
3 changed files with 22 additions and 2 deletions
|
|
@ -944,8 +944,8 @@ def move(src, dst, copy_function=copy2):
|
|||
return real_dst
|
||||
|
||||
def _destinsrc(src, dst):
|
||||
src = os.path.abspath(src)
|
||||
dst = os.path.abspath(dst)
|
||||
src = os.path.realpath(src)
|
||||
dst = os.path.realpath(dst)
|
||||
if not src.endswith(os.path.sep):
|
||||
src += os.path.sep
|
||||
if not dst.endswith(os.path.sep):
|
||||
|
|
|
|||
|
|
@ -2914,6 +2914,23 @@ def test_destinsrc_false_positive(self):
|
|||
finally:
|
||||
os_helper.rmtree(TESTFN)
|
||||
|
||||
@os_helper.skip_unless_symlink
|
||||
def test_destinsrc_symlink_bypass(self):
|
||||
tmp = self.mkdtemp()
|
||||
src = os.path.join(tmp, 'src')
|
||||
os.makedirs(src)
|
||||
# tmp/link -> tmp (one level up)
|
||||
link = os.path.join(tmp, 'link')
|
||||
os.symlink(tmp, link)
|
||||
# raw path: tmp/link/src/sub - no src prefix in string space
|
||||
# real path: tmp/src/sub - physically inside src
|
||||
dst = os.path.join(link, 'src', 'sub')
|
||||
self.assertTrue(
|
||||
shutil._destinsrc(src, dst),
|
||||
msg='_destinsrc failed to detect dst inside src via symlink '
|
||||
'(dst=%s, src=%s)' % (dst, src),
|
||||
)
|
||||
|
||||
@os_helper.skip_unless_symlink
|
||||
@mock_rename
|
||||
def test_move_file_symlink(self):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
:func:`shutil.move` now resolves symlinks via :func:`os.path.realpath`
|
||||
when checking whether the destination is inside the source directory,
|
||||
preventing a symlink-based bypass of that guard.
|
||||
Loading…
Add table
Add a link
Reference in a new issue