Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions features/checksum-plugin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,41 @@ Feature: Validate checksums for WordPress plugins
Error: No plugins verified (1 failed).
"""

Scenario: Downloads the checksums of several plugins at once
Given a WP install
And a wp-content/plugins/custom-plugin/custom-plugin.php file:
"""
<?php
/**
* Plugin Name: Custom Plugin
* Version: 1.0
*/
"""

When I run `wp plugin install duplicate-post --version=3.2.1`
And I run `wp plugin install voice-search --version=1.2.0`
Then STDERR should be empty

Given "Duplicate Post" replaced with "Different Name" in the wp-content/plugins/duplicate-post/duplicate-post.php file

When I try `wp plugin verify-checksums duplicate-post voice-search custom-plugin --format=json --debug=checksum`
Then STDOUT should contain:
"""
"plugin_name":"duplicate-post","file":"duplicate-post.php","message":"Checksum does not match"
"""
And STDERR should contain:
"""
Downloaded the checksums of 2 of 3 plugins in parallel.
"""
And STDERR should contain:
"""
Warning: Could not retrieve the checksums for version 1.0 of plugin custom-plugin, skipping.
"""
And STDERR should contain:
"""
Error: Only verified 1 of 3 plugins (1 failed, 1 skipped).
"""

Scenario: Soft changes are only reported in strict mode
Given a WP install

Expand Down
114 changes: 103 additions & 11 deletions src/Checksum_Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use WP_CLI\Fetchers;
use WP_CLI\Formatter;
use WP_CLI\RequestsLibrary;
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;

Expand Down Expand Up @@ -107,8 +108,19 @@ public function __invoke( $args, $assoc_args ) {

$skips = 0;

$versions = [];
foreach ( $plugins as $plugin ) {
$version = empty( $version_arg ) ? $this->get_plugin_version( $plugin->file ) : $version_arg;
if ( 'hello' === $plugin->name || in_array( $plugin->name, $exclude_list, true ) ) {
continue;
}

$versions[ $plugin->file ] = empty( $version_arg ) ? $this->get_plugin_version( $plugin->file ) : $version_arg;
}

$prefetched = $this->prefetch_plugin_checksums( $plugins, $versions );

foreach ( $plugins as $plugin ) {
$version = isset( $versions[ $plugin->file ] ) ? $versions[ $plugin->file ] : false;

if ( in_array( $plugin->name, $exclude_list, true ) ) {
++$skips;
Expand All @@ -132,16 +144,20 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );

try {
/**
* @var array|false $checksums
*/
$checksums = $wp_org_api->get_plugin_checksums( $plugin->name, $version );
} catch ( Exception $exception ) {
WP_CLI::warning( $exception->getMessage() );
$checksums = false;
if ( isset( $prefetched[ $plugin->file ] ) ) {
$checksums = $prefetched[ $plugin->file ];
} else {
$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );

try {
/**
* @var array|false $checksums
*/
$checksums = $wp_org_api->get_plugin_checksums( $plugin->name, $version );
} catch ( Exception $exception ) {
WP_CLI::warning( $exception->getMessage() );
$checksums = false;
}
}

if ( false === $checksums ) {
Expand Down Expand Up @@ -251,6 +267,82 @@ private function add_error( $plugin_name, $file, $message ) {
$this->errors[] = $error;
}

/**
* Downloads the checksums of several plugins at once.
*
* This only saves time. Checksums that aren't downloaded here, because a
* request failed or returned something unexpected, are downloaded again
* one at a time, with the usual retries and error messages.
*
* @param array<object{name: string, file: string}> $plugins Plugins to verify.
* @param array<string, string|false> $versions Plugin versions, keyed by plugin file.
* @return array<string, array<mixed>> Checksums, keyed by plugin file.
*/
private function prefetch_plugin_checksums( $plugins, $versions ) {
$requests = [];
foreach ( $plugins as $plugin ) {
if ( empty( $versions[ $plugin->file ] ) ) {
continue;
}

$requests[ $plugin->file ] = [
'url' => sprintf( '%s%s/%s.json', WpOrgApi::PLUGIN_CHECKSUMS_ENDPOINT, $plugin->name, $versions[ $plugin->file ] ),
'headers' => [ 'Accept' => 'application/json' ],
];
}

if ( count( $requests ) < 2 ) {
return [];
}

$options = [ 'verify' => ! empty( ini_get( 'curl.cainfo' ) ) ? ini_get( 'curl.cainfo' ) : true ];

foreach ( $requests as $file => $request ) {
// Let the same filter apply as to every other request WP-CLI makes.
$request_options = WP_CLI::do_hook( 'http_request_options', $options, 'GET', $request['url'], null, $request['headers'] );

$requests[ $file ]['options'] = is_array( $request_options ) ? $request_options : $options;
}

RequestsLibrary::register_autoloader();

/** @var callable $request_multiple */
$request_multiple = [ RequestsLibrary::get_class_name(), 'request_multiple' ];

$checksums = [];

// Limit the number of simultaneous connections to WordPress.org.
foreach ( array_chunk( $requests, 10, true ) as $chunk ) {
try {
/** @var array<string, mixed> $responses */
$responses = $request_multiple( $chunk, $options );
} catch ( Exception $exception ) {
continue;
}

foreach ( $responses as $file => $response ) {
if (
! is_object( $response )
|| ! isset( $response->success, $response->status_code, $response->body )
|| ! $response->success
|| 200 !== (int) $response->status_code
) {
continue;
}

$data = json_decode( (string) $response->body, true );

if ( is_array( $data ) && isset( $data['files'] ) && is_array( $data['files'] ) ) {
$checksums[ $file ] = $data['files'];
}
}
}

WP_CLI::debug( sprintf( 'Downloaded the checksums of %d of %d plugins in parallel.', count( $checksums ), count( $requests ) ), 'checksum' );

return $checksums;
}

/**
* Gets the currently installed version for a given plugin.
*
Expand Down
Loading