Skip to content

Commit f458de1

Browse files
committed
Keep web-readable permissions on in-place replacements
tempnam() creates the temporary file with mode 0600 and rename() keeps it, while FS_CHMOD_FILE is usually not defined in a WP-CLI run. The replaced file could then be unreadable by the web server. Keep the mode of the file being replaced, or derive it from the directory like wp_handle_sideload() does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 2f90273 commit f458de1

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎features/media-replace.feature‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ Feature: Replace WordPress attachment files
5050
And the wp-content/uploads/{OLD_FILE} file should exist
5151
And the wp-content/uploads/replacement.jpg file should not exist
5252

53+
Scenario: Keep the file permissions when replacing an attachment file in place
54+
Given download:
55+
| path | url |
56+
| {CACHE_DIR}/canola.jpg | http://wp-cli.github.io/behat-data/canola.jpg |
57+
| {CACHE_DIR}/replacement.jpg | http://wp-cli.github.io/behat-data/canola.jpg |
58+
And I run `wp option update uploads_use_yearmonth_folders 0`
59+
60+
When I run `wp media import {CACHE_DIR}/canola.jpg --porcelain`
61+
Then save STDOUT as {ATTACHMENT_ID}
62+
63+
When I run `wp eval 'chmod( get_attached_file( {ATTACHMENT_ID} ), 0644 );'`
64+
And I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/replacement.jpg`
65+
Then STDOUT should contain:
66+
"""
67+
Success: Replaced 1 of 1 attachments.
68+
"""
69+
70+
When I run `wp eval 'clearstatcache(); echo decoct( fileperms( get_attached_file( {ATTACHMENT_ID} ) ) & 0777 );'`
71+
Then STDOUT should be:
72+
"""
73+
644
74+
"""
75+
5376
Scenario: Replace an attachment file and keep its existing upload folder
5477
Given download:
5578
| path | url |

‎src/Media_Command.php‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,11 +1134,21 @@ private function atomic_replace( $source, $destination ) {
11341134
return false;
11351135
}
11361136

1137-
if ( defined( 'FS_CHMOD_FILE' ) ) {
1138-
// chmod() can fail on read-only or non-POSIX mounts; failure is non-fatal.
1139-
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1140-
@chmod( $temp, FS_CHMOD_FILE );
1137+
/*
1138+
* tempnam() creates the file with mode 0600. Keep the mode of the file being
1139+
* replaced, or derive it from the directory like wp_handle_sideload() does,
1140+
* so the web server can still read the file.
1141+
*/
1142+
$perms = file_exists( $destination ) ? fileperms( $destination ) : false;
1143+
if ( false !== $perms ) {
1144+
$perms = $perms & 0777;
1145+
} else {
1146+
$stat = stat( dirname( $destination ) );
1147+
$perms = $stat ? $stat['mode'] & 0000666 : 0644;
11411148
}
1149+
// chmod() can fail on read-only or non-POSIX mounts; failure is non-fatal.
1150+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1151+
@chmod( $temp, $perms );
11421152

11431153
// On most filesystems rename() replaces the destination atomically.
11441154
if ( @rename( $temp, $destination ) ) {

0 commit comments

Comments
 (0)