Skip to content

Commit f691199

Browse files
authored
Add edge case handling checksum verification of Hello Dolly (#119)
* Add edge case handling for Hello Dolly (Core vs Plugin) * Compare hello.php against core checksum instead of plugin repo * Fixed PHPCS Linting errors
1 parent 7ae0201 commit f691199

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

features/checksum-plugin.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,12 @@ Feature: Validate checksums for WordPress plugins
215215
"""
216216
Verified 1 of 1 plugins.
217217
"""
218+
219+
Scenario: Verifies Hello Dolly
220+
Given a WP install
221+
222+
When I run `wp plugin verify-checksums hello`
223+
Then STDOUT should contain:
224+
"""
225+
Verified 1 of 1 plugins.
226+
"""

src/Checksum_Plugin_Command.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function __invoke( $args, $assoc_args ) {
105105
continue;
106106
}
107107

108+
if ( 'hello' === $plugin->name ) {
109+
$this->verify_hello_dolly_from_core( $assoc_args );
110+
continue;
111+
}
112+
108113
if ( false === $version ) {
109114
WP_CLI::warning( "Could not retrieve the version for plugin {$plugin->name}, skipping." );
110115
++$skips;
@@ -143,7 +148,6 @@ public function __invoke( $args, $assoc_args ) {
143148
if ( ! $strict && $this->is_soft_change_file( $file ) ) {
144149
continue;
145150
}
146-
147151
$result = $this->check_file_checksum( dirname( $plugin->file ) . '/' . $file, $checksums[ $file ] );
148152
if ( true !== $result ) {
149153
$this->add_error( $plugin->name, $file, is_string( $result ) ? $result : 'Checksum does not match' );
@@ -173,6 +177,29 @@ public function __invoke( $args, $assoc_args ) {
173177
);
174178
}
175179

180+
private function verify_hello_dolly_from_core( $assoc_args ) {
181+
$file = 'hello.php';
182+
$wp_version = get_bloginfo( 'version', 'display' );
183+
$insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false );
184+
$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );
185+
$locale = '';
186+
187+
try {
188+
$checksums = $wp_org_api->get_core_checksums( $wp_version, empty( $locale ) ? 'en_US' : $locale );
189+
} catch ( Exception $exception ) {
190+
WP_CLI::error( $exception );
191+
}
192+
193+
if ( ! is_array( $checksums ) || ! isset( $checksums['wp-content/plugins/hello.php'] ) ) {
194+
WP_CLI::error( "Couldn't get hello.php checksum from WordPress.org." );
195+
}
196+
197+
$md5_file = md5_file( $this->get_absolute_path( '/' ) . $file );
198+
if ( $md5_file !== $checksums['wp-content/plugins/hello.php'] ) {
199+
$this->add_error( 'hello', $file, 'Checksum does not match' );
200+
}
201+
}
202+
176203
/**
177204
* Adds a new error to the array of detected errors.
178205
*
@@ -255,7 +282,6 @@ private function check_file_checksum( $path, $checksums ) {
255282
&& array_key_exists( 'sha256', $checksums )
256283
) {
257284
$sha256 = $this->get_sha256( $this->get_absolute_path( $path ) );
258-
259285
return in_array( $sha256, (array) $checksums['sha256'], true );
260286
}
261287

0 commit comments

Comments
 (0)