Skip to content

Commit e7b33f5

Browse files
committed
Upgrade/Install: Improve the handling of the upgrade working directory.
Merges [64877] to the 7.1 branch. Props umut3806, marcs0h, rfaile313, jorbin. git-svn-id: https://develop.svn.wordpress.org/branches/7.1@64889 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5e13ffd commit e7b33f5

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎src/wp-admin/includes/class-wp-upgrader.php‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,21 @@ public function download_package( $package, $check_signatures = false, $hook_ext
343343
return $download_file;
344344
}
345345

346+
/**
347+
* Builds an unpredictable name for the directory a package is unpacked into.
348+
*
349+
* @since 7.1.4
350+
*
351+
* @param string $package The full local path or URI of the package.
352+
* @return string The directory name, without a path.
353+
*/
354+
private function get_working_dir_name( $package ) {
355+
// Strip off any .tmp or .zip suffixes.
356+
$name = basename( basename( $package, '.tmp' ), '.zip' );
357+
358+
return $name . '-' . wp_generate_password( 16, false );
359+
}
360+
346361
/**
347362
* Unpacks a compressed package file.
348363
*
@@ -366,16 +381,32 @@ public function unpack_package( $package, $delete_package = true ) {
366381

367382
$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
368383

369-
// Clean up contents of upgrade directory beforehand.
384+
// Clean up contents of upgrade directory beforehand, keeping its index.php.
370385
$upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
371386
if ( ! empty( $upgrade_files ) ) {
372387
foreach ( $upgrade_files as $file ) {
388+
if ( 'index.php' === $file['name'] ) {
389+
continue;
390+
}
391+
373392
$wp_filesystem->delete( $upgrade_folder . $file['name'], true );
374393
}
375394
}
376395

377-
// We need a working directory - strip off any .tmp or .zip suffixes.
378-
$working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
396+
/*
397+
* The upgrade directory is web-served. Keep it from being listed, as core does
398+
* for wp-content and its plugins and themes directories, so the name of the
399+
* working directory created below cannot be discovered from a directory index.
400+
*/
401+
if ( ! $wp_filesystem->is_dir( $upgrade_folder ) ) {
402+
$wp_filesystem->mkdir( $upgrade_folder, FS_CHMOD_DIR );
403+
}
404+
if ( ! $wp_filesystem->exists( $upgrade_folder . 'index.php' ) ) {
405+
$wp_filesystem->put_contents( $upgrade_folder . 'index.php', "<?php\n// Silence is golden.\n", FS_CHMOD_FILE );
406+
}
407+
408+
// We need a working directory.
409+
$working_dir = $upgrade_folder . $this->get_working_dir_name( $package );
379410

380411
// Clean up working directory.
381412
if ( $wp_filesystem->is_dir( $working_dir ) ) {

‎src/wp-admin/includes/update-core.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,8 +1545,9 @@ function update_core( $from, $to ) {
15451545
/*
15461546
* Don't copy wp-content, we'll deal with that below.
15471547
* We also copy version.php last so failed updates report their old version.
1548+
* Never copy a wp-config.php, which could have been written to the working directory.
15481549
*/
1549-
$skip = array( 'wp-content', 'wp-includes/version.php' );
1550+
$skip = array( 'wp-content', 'wp-includes/version.php', 'wp-config.php' );
15501551
$check_is_writable = array();
15511552

15521553
// Check to see which files don't really need updating - only available for 3.7 and higher.

0 commit comments

Comments
 (0)