-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143968: Prevent pathlib.Path.copy() from processing special files (FIFO, devices) #143966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Previously, `pathlib.Path.copy()` would indiscriminately attempt to open and read from the source path if it wasn't a directory or symlink. This caused severe issues with special files: 1. Blocking indefinitely when reading from a FIFO (named pipe). 2. Entering an infinite loop when reading from zero-generators like `/dev/zero`. This commit modifies the copy logic to explicitly verify that the source is a regular file using `is_file()` before attempting to copy its content. If the source exists but is a special file, `io.UnsupportedOperation` is now raised. This change aligns `pathlib`'s behavior with safety expectations and prevents resource exhaustion or hanging processes. Existing behavior for dangling symlinks (raising FileNotFoundError) is preserved. Tests added: - `test_copy_fifo`: Ensures copying a FIFO raises UnsupportedOperation. - `test_copy_char_device`: Ensures copying a character device (e.g. /dev/null) raises UnsupportedOperation.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Closes gh-143968
[3.14] gh-143968: Prevent pathlib.Path.copy() from processing special files (FIFO, devices)
Description
This PR addresses an issue where
pathlib.Path.copy()(introduced for 3.14) could indiscriminately attempt to open and read from special files like FIFOs (named pipes) or character devices (e.g.,/dev/zero).The Problem
/dev/zeroresults in an infinite loop that continues until disk space is exhausted or the process is killed.The Fix
The copy logic in
_copy_frominsideLib/pathlib/__init__.pyhas been modified to explicitly checkis_file()before attempting to copy content.io.UnsupportedOperation.shutil.copyfile's philosophy of safely handling special files by refusing to copy them as streams.FileNotFoundError) is strictly preserved.Tests
Added new test cases in
Lib/test/test_pathlib/test_copy.pyto cover these scenarios:test_copy_fifo: Verifies that copying a FIFO (created viaos.mkfifo) raisesUnsupportedOperationimmediately instead of blocking.test_copy_char_device: Verifies that copying a character device (e.g.,/dev/null) raisesUnsupportedOperation.Validation
python -m test test_pathliblocally and all 1375 tests passed.