diff options
17 files changed, 147 insertions, 18 deletions
diff --git a/src/wp-admin/includes/class-file-upload-upgrader.php b/src/wp-admin/includes/class-file-upload-upgrader.php index e62561518d..1201c6d188 100644 --- a/src/wp-admin/includes/class-file-upload-upgrader.php +++ b/src/wp-admin/includes/class-file-upload-upgrader.php @@ -70,24 +70,7 @@ class File_Upload_Upgrader { } if ( 'pluginzip' === $form || 'themezip' === $form ) { - $archive_is_valid = false; - - /** This filter is documented in wp-admin/includes/file.php */ - if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { - $archive = new ZipArchive(); - $archive_is_valid = $archive->open( $file['file'], ZIPARCHIVE::CHECKCONS ); - - if ( true === $archive_is_valid ) { - $archive->close(); - } - } else { - require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; - - $archive = new PclZip( $file['file'] ); - $archive_is_valid = is_array( $archive->properties() ); - } - - if ( true !== $archive_is_valid ) { + if ( ! wp_zip_file_is_valid( $file['file'] ) ) { wp_delete_file( $file['file'] ); wp_die( __( 'Incompatible Archive.' ) ); } diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index c3863ba2ea..583256955e 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -1564,6 +1564,37 @@ function wp_trusted_keys() { } /** + * Determines whether the given file is a valid ZIP file. + * + * This function does not test to ensure that a file exists. Non-existent files + * are not valid ZIPs, so those will also return false. + * + * @since 6.4.4 + * + * @param string $file Full path to the ZIP file. + * @return bool Whether the file is a valid ZIP file. + */ +function wp_zip_file_is_valid( $file ) { + /** This filter is documented in wp-admin/includes/file.php */ + if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { + $archive = new ZipArchive(); + $archive_is_valid = $archive->open( $file, ZipArchive::CHECKCONS ); + if ( true === $archive_is_valid ) { + $archive->close(); + return true; + } + } + + // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. + require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; + + $archive = new PclZip( $file ); + $archive_is_valid = is_array( $archive->properties() ); + + return $archive_is_valid; +} + +/** * Unzips a specified ZIP file to a location on the filesystem via the WordPress * Filesystem Abstraction. * diff --git a/tests/phpunit/data/filesystem/_source-license b/tests/phpunit/data/filesystem/_source-license new file mode 100644 index 0000000000..580a1492a4 --- /dev/null +++ b/tests/phpunit/data/filesystem/_source-license @@ -0,0 +1,33 @@ +The following files were sourced from https://github.com/ZJONSSON/node-unzipper a fork of https://github.com/EvanOxfeld/node-unzip + +* archive-comment.zip +* archive-cp866.zip +* archive-directory-entry.zip +* archive-encrypted.zip +* archive-flags-set.zip +* archive-invalid.zip +* archive-large.zip +* archive-uncompressed.zip +* archive.crx + +Copyright (c) 2012 - 2013 Near Infinity Corporation +Copyright (c) 2016 - 2024 Ziggy Jonsson (ziggy.jonsson.nyc@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/tests/phpunit/data/filesystem/archive-comment.zip b/tests/phpunit/data/filesystem/archive-comment.zip Binary files differnew file mode 100644 index 0000000000..14a862f1f1 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-comment.zip diff --git a/tests/phpunit/data/filesystem/archive-cp866.zip b/tests/phpunit/data/filesystem/archive-cp866.zip Binary files differnew file mode 100644 index 0000000000..04bd3c9372 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-cp866.zip diff --git a/tests/phpunit/data/filesystem/archive-directory-entry.zip b/tests/phpunit/data/filesystem/archive-directory-entry.zip Binary files differnew file mode 100644 index 0000000000..e81a6aa7e0 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-directory-entry.zip diff --git a/tests/phpunit/data/filesystem/archive-encrypted.zip b/tests/phpunit/data/filesystem/archive-encrypted.zip Binary files differnew file mode 100644 index 0000000000..fd58fb4f1d --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-encrypted.zip diff --git a/tests/phpunit/data/filesystem/archive-flags-set.zip b/tests/phpunit/data/filesystem/archive-flags-set.zip Binary files differnew file mode 100644 index 0000000000..015ce233c4 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-flags-set.zip diff --git a/tests/phpunit/data/filesystem/archive-gnome.zip b/tests/phpunit/data/filesystem/archive-gnome.zip Binary files differnew file mode 100644 index 0000000000..0f1fc181ea --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-gnome.zip diff --git a/tests/phpunit/data/filesystem/archive-invalid-ext.md b/tests/phpunit/data/filesystem/archive-invalid-ext.md new file mode 100644 index 0000000000..1531b03435 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-invalid-ext.md @@ -0,0 +1,7 @@ +# Shucked + +Shucked is a 2022 musical with music and lyrics by Brandy Clark and Shane McAnally, and a book by Robert Horn. The Broadway production began previews at the Nederlander Theatre on March 8, 2023, before opening on April 4. The show received positive reviews and went on to receive nine nominations at the 76th Tony Awards, including Best Musical. Cast member Alex Newell became one of the first two openly non-binary performers to be nominated for and win a Tony Award, with their win for Best Featured Actor in a Musical. + +A U.S. tour, West End production, and feature film adaptation are currently planned. + +From Wikipedia (https://en.wikipedia.org/wiki/Shucked) under Creative Commons Attribution CC-BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/legalcode) diff --git a/tests/phpunit/data/filesystem/archive-invalid.zip b/tests/phpunit/data/filesystem/archive-invalid.zip Binary files differnew file mode 100644 index 0000000000..d13ba6b7ff --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-invalid.zip diff --git a/tests/phpunit/data/filesystem/archive-large.zip b/tests/phpunit/data/filesystem/archive-large.zip Binary files differnew file mode 100644 index 0000000000..57c1635fdf --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-large.zip diff --git a/tests/phpunit/data/filesystem/archive-macos.zip b/tests/phpunit/data/filesystem/archive-macos.zip Binary files differnew file mode 100644 index 0000000000..a5493f9fa0 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-macos.zip diff --git a/tests/phpunit/data/filesystem/archive-ubuntu-nautilus.zip b/tests/phpunit/data/filesystem/archive-ubuntu-nautilus.zip Binary files differnew file mode 100644 index 0000000000..a1d65d1771 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-ubuntu-nautilus.zip diff --git a/tests/phpunit/data/filesystem/archive-uncompressed.zip b/tests/phpunit/data/filesystem/archive-uncompressed.zip Binary files differnew file mode 100644 index 0000000000..2d3626d6b6 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive-uncompressed.zip diff --git a/tests/phpunit/data/filesystem/archive.crx b/tests/phpunit/data/filesystem/archive.crx Binary files differnew file mode 100644 index 0000000000..c07b21c6c1 --- /dev/null +++ b/tests/phpunit/data/filesystem/archive.crx diff --git a/tests/phpunit/tests/filesystem/wpZipFileIsValid.php b/tests/phpunit/tests/filesystem/wpZipFileIsValid.php new file mode 100644 index 0000000000..94a5f4ebbd --- /dev/null +++ b/tests/phpunit/tests/filesystem/wpZipFileIsValid.php @@ -0,0 +1,75 @@ +<?php + +/** + * Tests wp_zip_file_is_valid(). + * + * @group file + * @group filesystem + * + * @covers ::wp_zip_file_is_valid + */ +class Tests_Filesystem_WpZipFileIsValid extends WP_UnitTestCase { + + /** + * The test data directory. + * + * @var string $test_data_dir + */ + private static $test_data_dir; + + /** + * Sets up the filesystem and test data directory property + * before any tests run. + */ + public static function set_up_before_class() { + parent::set_up_before_class(); + + require_once ABSPATH . 'wp-admin/includes/file.php'; + WP_Filesystem(); + + self::$test_data_dir = DIR_TESTDATA . '/filesystem/'; + } + + /** + * Tests ZIP file validity is correctly determined. + * + * @ticket 60398 + * + * @dataProvider data_zip_file_validity + * + * @param string $file The ZIP file to test. + * @param bool $expected Whether the ZIP file is expected to be valid. + */ + public function test_zip_file_validity( $file, $expected ) { + $zip_file = self::$test_data_dir . $file; + + $expected_message = $expected ? 'valid' : 'invalid'; + $this->assertSame( $expected, wp_zip_file_is_valid( $zip_file ), "Expected archive to be {$expected_message}." ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_zip_file_validity() { + return array( + 'standard zip' => array( 'archive.zip', true ), + 'large zip' => array( 'archive-large.zip', true ), + 'commented zip' => array( 'archive-comment.zip', true ), + 'cp866 zip' => array( 'archive-cp866.zip', true ), + 'directory entry zip' => array( 'archive-directory-entry.zip', true ), + 'encrypted zip' => array( 'archive-encrypted.zip', true ), + 'flags-set zip' => array( 'archive-flags-set.zip', true ), + 'uncompressed zip' => array( 'archive-uncompressed.zip', true ), + 'crx zip' => array( 'archive.crx', true ), + 'macos generated zip' => array( 'archive-macos.zip', true ), + 'gnome generated zip' => array( 'archive-gnome.zip', true ), + 'ubuntu nautilus zip' => array( 'archive-ubuntu-nautilus.zip', true ), + + 'invalid zip file' => array( 'archive-invalid.zip', false ), + 'invalid file extension' => array( 'archive-invalid-ext.md', false ), + 'non-existent file' => array( 'archive-non-existent.zip', false ), + ); + } +} |