Skip to content

pathlib.Path.copy() can copy a directory into itself via absolute paths or symlinks #154690

Description

@dura0ok

Bug report

Bug description:

Bug report

Path.copy() guards against copying a directory into itself with ensure_distinct_paths(), but the check is lexical only. Relative vs absolute paths and symlink aliases bypass it.

from pathlib import Path
import os

Path('src').mkdir()
(Path('src') / 'f').write_text('hello')

Path('src').copy('src/nested')  # OSError - OK

Path('src').copy(Path('src').resolve() / 'nested')  # succeeds - copies into itself

os.symlink('src', 'link')
Path('src').copy('link/nested')  # also succeeds

Same class of bug as gh-149835 that was fixed with `realpath`, but `pathlib` still uses a lexical comparison.

### CPython versions tested on:

CPython main branch

### Operating systems tested on:

Linux

<!-- gh-linked-prs -->
### Linked PRs
* gh-154689
<!-- /gh-linked-prs -->

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions