pathlib.Path.copy() can copy a directory into itself via absolute paths or symlinks
未關閉
還沒有人認領這個 Issue。
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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 -->
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 pathlib.Path.copy() 及其 ensure_distinct_paths() 檢查開始,然後在 Linux 上重現 issue 中的絕對路徑和 symlink 範例。完成的標準是:兩個別名都會因試圖將目錄複製到其自身中而被拒絕,同時現有的相對路徑行為仍受到涵蓋;查看連結的 PR gh-154689,了解已在進行的工作。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100