Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,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):
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,25 @@ def test_destinsrc_false_positive(self):
finally:
os_helper.rmtree(TESTFN)

@os_helper.skip_unless_symlink
def test_destinsrc_symlink_bypass(self):
# gh-149835: a symlink component in dst must not allow dst to
# appear outside src in string space while being physically inside.
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :func:`shutil.move` so that the guard against moving a directory into
itself is not bypassed when the destination contains a symlink component.
:func:`!shutil._destinsrc` now resolves symlinks with :func:`os.path.realpath`
instead of using :func:`os.path.abspath`.
Loading