Skip to content

Commit 5fd1f9b

Browse files
schlesseraclaude
andauthored
Save the archive of core download --no-extract safely (#357)
* Save the archive of core download --no-extract safely An archive downloaded from a URL was named after the URL since #355, which let whoever provides the URL choose the name and type of a file written into the target directory. Keep the random name of the temporary file for these again, with the extension detected from the archive. Save the archive by writing a new file with a random name next to its destination and moving it into place, so that a symlink at the destination is replaced instead of written through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the permissions of an archive that is replaced Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Cover keeping the permissions of a replaced archive with a scenario Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 07b1815 commit 5fd1f9b

2 files changed

Lines changed: 129 additions & 10 deletions

File tree

‎features/core-download.feature‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,82 @@ Feature: Download WordPress
552552
"""
553553
And the return code should be 1
554554

555+
Scenario: Core download with no-extract does not name an archive after its URL
556+
Given an empty directory
557+
And that HTTP requests to http://example.com/payload.php.md5 will respond with:
558+
"""
559+
HTTP/1.1 200 OK
560+
Content-Type: text/plain
561+
562+
3c58c7ed6e95bcf8fadbadc3cc2ca52b
563+
"""
564+
And that HTTP requests to http://example.com/payload.php will respond with:
565+
"""
566+
HTTP/1.1 200 OK
567+
Content-Type: application/octet-stream
568+
569+
PK This only needs to look like a zip archive.
570+
"""
571+
572+
# The mocked responses make the HTTP library emit warnings, so STDERR is not checked.
573+
When I try `wp core download http://example.com/payload.php --no-extract`
574+
Then STDOUT should contain:
575+
"""
576+
Success: WordPress downloaded.
577+
"""
578+
And the return code should be 0
579+
And the payload.php file should not exist
580+
And the {RUN_DIR} directory should contain:
581+
"""
582+
.zip
583+
"""
584+
585+
@skip-windows
586+
Scenario: Core download with no-extract replaces a symlink at the archive path instead of writing through it
587+
Given an empty directory
588+
And an empty cache
589+
And a victim.txt file:
590+
"""
591+
Not an archive.
592+
"""
593+
594+
When I run `ln -s victim.txt wordpress-4.5-de_DE.zip`
595+
And I run `wp core download --version=4.5 --locale=de_DE --no-extract`
596+
Then the victim.txt file should be:
597+
"""
598+
Not an archive.
599+
"""
600+
And the wordpress-4.5-de_DE.zip file should exist
601+
602+
When I run `rm wordpress-4.5-de_DE.zip`
603+
And I run `ln -s victim.txt wordpress-4.5-de_DE.zip`
604+
And I run `wp core download --version=4.5 --locale=de_DE --no-extract`
605+
Then STDOUT should contain:
606+
"""
607+
Using cached file
608+
"""
609+
And the victim.txt file should be:
610+
"""
611+
Not an archive.
612+
"""
613+
614+
@skip-windows
615+
Scenario: Core download with no-extract keeps the permissions of an archive it replaces
616+
Given an empty directory
617+
And an empty cache
618+
And a wordpress-4.5-de_DE.zip file:
619+
"""
620+
An older copy of the archive.
621+
"""
622+
623+
When I run `chmod 600 wordpress-4.5-de_DE.zip`
624+
And I run `wp core download --version=4.5 --locale=de_DE --no-extract`
625+
And I run `php -r 'printf( "%o", fileperms( "wordpress-4.5-de_DE.zip" ) & 0777 );'`
626+
Then STDOUT should be:
627+
"""
628+
600
629+
"""
630+
555631
Scenario: Core download with no-extract should not unzip the download file (already cached)
556632
Given an empty directory
557633

‎src/Core_Command.php‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ public function download( $args, $assoc_args ) {
292292
WP_CLI::warning( 'Extraction failed, downloading a new copy...' );
293293
$bad_cache = true;
294294
}
295-
} elseif ( ! copy( $cache_file, $download_dir . basename( $cache_file ) ) ) {
296-
WP_CLI::error( "Couldn't copy WordPress archive to '{$download_dir}" . basename( $cache_file ) . "'." );
295+
} else {
296+
self::save_archive( $cache_file, $download_dir . basename( $cache_file ) );
297297
}
298298
}
299299

@@ -414,14 +414,11 @@ function () use ( $old_temp ) {
414414
WP_CLI::error( "Couldn't extract WordPress archive. {$exception->getMessage()}" );
415415
}
416416
} else {
417-
// Name the archive like the cached one, rather than after the temporary file.
418-
$archive_name = $from_url ? basename( (string) Utils\parse_url( $download_url, PHP_URL_PATH ) ) : "wordpress-{$version}-{$locale}.{$extension}";
419-
if ( '' === $archive_name || '.' === $archive_name ) {
420-
$archive_name = basename( $temp );
421-
}
422-
if ( ! copy( $temp, $download_dir . $archive_name ) ) {
423-
WP_CLI::error( "Couldn't copy WordPress archive to '{$download_dir}{$archive_name}'." );
424-
}
417+
// Name a release like the cached copy of it. An archive downloaded from a URL keeps
418+
// the random name of the temporary file, so that the URL cannot choose the name or
419+
// type of a file written into the target directory.
420+
$archive_name = $from_url ? basename( $temp ) : "wordpress-{$version}-{$locale}.{$extension}";
421+
self::save_archive( $temp, $download_dir . $archive_name );
425422
}
426423

427424
// Do not use the cache for nightly builds or for downloaded URLs
@@ -2251,6 +2248,52 @@ private function remove_directory( $dir, $abspath_realpath_trailing ) {
22512248
return true;
22522249
}
22532250

2251+
/**
2252+
* Save a downloaded archive at its destination in the download directory.
2253+
*
2254+
* The archive is written to a new file with a random name next to the destination first, and
2255+
* then moved into place, so that a symlink at the destination is replaced instead of followed.
2256+
*
2257+
* @param string $source Path of the archive to save.
2258+
* @param string $destination Path to save the archive at.
2259+
*/
2260+
private static function save_archive( $source, $destination ): void {
2261+
$partial = dirname( $destination ) . '/.wp-cli-archive-' . bin2hex( random_bytes( 8 ) ) . '.part';
2262+
2263+
// Creating the file exclusively fails if anything, including a symlink, is already at its path.
2264+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Failures are reported below.
2265+
$input = @fopen( $source, 'rb' );
2266+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Failures are reported below.
2267+
$output = @fopen( $partial, 'xb' );
2268+
2269+
$copied = false !== $input && false !== $output && false !== stream_copy_to_stream( $input, $output );
2270+
2271+
if ( false !== $input ) {
2272+
fclose( $input );
2273+
}
2274+
2275+
if ( false !== $output ) {
2276+
fclose( $output );
2277+
}
2278+
2279+
// Keep the permissions of an archive that is replaced, as copying over it would have.
2280+
if ( $copied && ! is_link( $destination ) && is_file( $destination ) ) {
2281+
$mode = fileperms( $destination );
2282+
if ( false !== $mode ) {
2283+
chmod( $partial, $mode & 0777 );
2284+
}
2285+
}
2286+
2287+
// Moving the file into place replaces whatever is at the destination, rather than writing through a symlink there.
2288+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Failures are reported below.
2289+
if ( ! $copied || ! @rename( $partial, $destination ) ) {
2290+
if ( file_exists( $partial ) ) {
2291+
unlink( $partial );
2292+
}
2293+
WP_CLI::error( "Couldn't copy WordPress archive to '{$destination}'." );
2294+
}
2295+
}
2296+
22542297
private static function strip_content_dir( $zip_file ) {
22552298
$new_zip_file = Utils\make_temp_file( 'wp_', '.zip' );
22562299
register_shutdown_function(

0 commit comments

Comments
 (0)