-
Notifications
You must be signed in to change notification settings - Fork 84
Validate and remove corrupted cache files during plugin/theme installation #472
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58cd3da
Initial plan
Copilot e6c6ab6
Add cache validation for plugin and theme downloads
Copilot f80b016
Simplify cache validation retry logic
Copilot c3bd788
Improve PackageValidator for cross-platform compatibility
Copilot 61d08c9
Update phpcs configuration for new validator classes
Copilot e8a6f23
Address code review feedback - improve security and code quality
Copilot 6552b3a
Add security comments clarifying safe shell command usage
Copilot 98e5868
Fix alignment
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| <?php | ||
|
|
||
| namespace WP_CLI; | ||
|
|
||
| use WP_CLI; | ||
|
|
||
| /** | ||
| * Validates downloaded package files (zip archives) before installation. | ||
| * | ||
| * This class provides validation for cached and freshly downloaded package files | ||
| * to ensure they are not corrupted. Corrupted files can occur when: | ||
| * - A download was interrupted | ||
| * - Filesystem issues caused incomplete writes | ||
| * - A license expired and the download returned an error message instead of a zip | ||
| * - Network issues caused partial downloads | ||
| */ | ||
| class PackageValidator { | ||
|
|
||
| /** | ||
| * Minimum acceptable file size in bytes. | ||
| * Files smaller than this are considered corrupted. | ||
| */ | ||
| const MIN_FILE_SIZE = 20; | ||
|
|
||
| /** | ||
| * Validates a package file to ensure it's a valid zip archive. | ||
| * | ||
| * Performs the following checks: | ||
| * 1. File exists | ||
| * 2. File size is at least MIN_FILE_SIZE bytes | ||
| * 3. If 'unzip' command is available, validates zip integrity | ||
| * | ||
| * @param string $file_path Path to the file to validate. | ||
| * @return true|\WP_Error True if valid, WP_Error if validation fails. | ||
| */ | ||
| public static function validate( $file_path ) { | ||
| // Check if file exists. | ||
| if ( ! file_exists( $file_path ) ) { | ||
| return new \WP_Error( | ||
| 'package_not_found', | ||
| sprintf( 'Package file not found: %s', $file_path ) | ||
| ); | ||
| } | ||
|
|
||
| // Check minimum file size. | ||
| $file_size = filesize( $file_path ); | ||
| if ( false === $file_size || $file_size < self::MIN_FILE_SIZE ) { | ||
| return new \WP_Error( | ||
| 'package_too_small', | ||
| sprintf( | ||
| 'Package file is too small (%d bytes). This usually indicates a corrupted download.', | ||
| $file_size ?: 0 | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| // If unzip is available, test the zip file integrity. | ||
| if ( self::is_unzip_available() ) { | ||
| $validation_result = self::validate_with_unzip( $file_path ); | ||
| if ( is_wp_error( $validation_result ) ) { | ||
| return $validation_result; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the 'unzip' command is available in the system PATH. | ||
| * | ||
| * @return bool True if unzip is available, false otherwise. | ||
| */ | ||
| private static function is_unzip_available() { | ||
| static $is_available = null; | ||
|
|
||
| if ( null === $is_available ) { | ||
| // Check if unzip is in PATH by trying to get its version. | ||
| // Suppress output to avoid cluttering the console. | ||
| // Note: Redirection to null device is safe as the device path is a hardcoded constant. | ||
| $null_device = '\\' === DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'; | ||
| $result = WP_CLI::launch( | ||
| 'unzip -v > ' . escapeshellarg( $null_device ) . ' 2>&1', | ||
| false, | ||
| true | ||
| ); | ||
| $is_available = ( 0 === $result->return_code ); | ||
| } | ||
|
|
||
| return $is_available; | ||
| } | ||
|
|
||
| /** | ||
| * Validates zip file integrity using the 'unzip -t' command. | ||
| * | ||
| * @param string $file_path Path to the zip file. | ||
| * @return true|\WP_Error True if valid, WP_Error if validation fails. | ||
| */ | ||
| private static function validate_with_unzip( $file_path ) { | ||
| // Suppress output - use platform-appropriate null device. | ||
| // Note: Null device path is a hardcoded constant, safe to use in shell commands. | ||
| $null_device = '\\' === DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'; | ||
| $command = 'unzip -t ' . escapeshellarg( $file_path ) . ' > ' . escapeshellarg( $null_device ) . ' 2>&1'; | ||
|
|
||
| $result = WP_CLI::launch( | ||
| $command, | ||
| false, | ||
| true | ||
| ); | ||
|
|
||
| if ( 0 !== $result->return_code ) { | ||
| return new \WP_Error( | ||
| 'package_corrupted', | ||
| 'Package file failed zip integrity check. This usually indicates a corrupted or incomplete download.' | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a corrupted package file. | ||
| * | ||
| * @param string $file_path Path to the file to delete. | ||
| * @return bool True if file was deleted or didn't exist, false on failure. | ||
| */ | ||
| public static function delete_corrupted_file( $file_path ) { | ||
| if ( ! file_exists( $file_path ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| $result = unlink( $file_path ); | ||
|
|
||
| // Log if deletion failed, but don't throw an error. | ||
| if ( ! $result ) { | ||
| WP_CLI::debug( | ||
| sprintf( 'Failed to delete corrupted file: %s', $file_path ), | ||
| 'extension-command' | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| namespace WP_CLI; | ||
|
|
||
| use WP_CLI; | ||
|
|
||
| /** | ||
| * Trait for upgraders that validates downloaded packages before installation. | ||
| * | ||
| * This trait adds package validation to WP_Upgrader subclasses to detect and | ||
| * handle corrupted cache files and failed downloads. | ||
| */ | ||
| trait UpgraderWithValidation { | ||
|
|
||
| /** | ||
| * Downloads a package with validation. | ||
| * | ||
| * This method overrides WP_Upgrader::download_package() to add validation | ||
| * of the downloaded file before it's used for installation. If validation | ||
| * fails, the corrupted file is deleted and an error is returned. | ||
| * | ||
| * @param string $package The URI of the package. | ||
| * @param bool $check_signatures Whether to validate file signatures. Default false. | ||
| * @param array $hook_extra Extra arguments to pass to hooked filters. Default empty array. | ||
| * @return string|\WP_Error The full path to the downloaded package file, or a WP_Error object. | ||
| */ | ||
| public function download_package( $package, $check_signatures = false, $hook_extra = array() ) { | ||
| // Call parent download_package to get the file (from cache or fresh download). | ||
| $download = parent::download_package( $package, $check_signatures, $hook_extra ); | ||
|
|
||
| // If download failed, return the error. | ||
| if ( is_wp_error( $download ) ) { | ||
| return $download; | ||
| } | ||
|
|
||
| // Validate the downloaded file. | ||
| $validation = PackageValidator::validate( $download ); | ||
|
|
||
| // If validation passed, return the file path. | ||
| if ( true === $validation ) { | ||
| return $download; | ||
| } | ||
|
|
||
| // Validation failed - log the issue and clean up. | ||
| WP_CLI::debug( | ||
| sprintf( | ||
| 'Package validation failed: %s', | ||
| $validation->get_error_message() | ||
| ), | ||
| 'extension-command' | ||
| ); | ||
|
|
||
| // Delete the corrupted file to prevent it from being reused. | ||
| if ( PackageValidator::delete_corrupted_file( $download ) ) { | ||
| WP_CLI::debug( | ||
| 'Deleted corrupted package file from cache.', | ||
| 'extension-command' | ||
| ); | ||
| } | ||
|
|
||
| // Return a detailed error message. | ||
| return new \WP_Error( | ||
| 'package_validation_failed', | ||
| sprintf( | ||
| 'Downloaded package failed validation (%s). The corrupted file has been removed from cache. Please try the command again.', | ||
| $validation->get_error_message() | ||
| ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace WP_CLI; | ||
|
|
||
| /** | ||
| * Plugin upgrader with package validation. | ||
| * | ||
| * This extends WordPress core's Plugin_Upgrader to add validation | ||
| * of downloaded packages before installation. | ||
| */ | ||
| class ValidatingPluginUpgrader extends \Plugin_Upgrader { | ||
| use UpgraderWithValidation; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace WP_CLI; | ||
|
|
||
| /** | ||
| * Theme upgrader with package validation. | ||
| * | ||
| * This extends WordPress core's Theme_Upgrader to add validation | ||
| * of downloaded packages before installation. | ||
| */ | ||
| class ValidatingThemeUpgrader extends \Theme_Upgrader { | ||
| use UpgraderWithValidation; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.