Skip to content
Closed
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
17 changes: 17 additions & 0 deletions features/checksum-core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,20 @@ Feature: Validate checksums for WordPress install
Success: WordPress installation verifies against checksums.
"""
And STDERR should be empty

Scenario: Verify core checksums with excluded file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An additional test with a file in a subfolder would be beneficial, to verify that works too.

Given a WP install
And I run `rm wp-config-sample.php`

When I try `wp core verify-checksums`
Then STDERR should contain:
"""
Warning: File doesn't exist: wp-config-sample.php
"""

When I run `wp core verify-checksums --exclude=wp-config-sample.php`
Then STDOUT should be:
"""
Success: WordPress installation verifies against checksums.
"""
And STDERR should be empty
39 changes: 39 additions & 0 deletions src/Checksum_Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Checksum_Core_Command extends Checksum_Base_Command {
*/
private $include_root = false;

/**
* Files or directories to exclude from verification.
*
* @var array
*/
private $exclude_paths = [];

/**
* Verifies WordPress files against WordPress.org's checksums.
*
Expand Down Expand Up @@ -44,6 +51,9 @@ class Checksum_Core_Command extends Checksum_Base_Command {
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* [--exclude=<paths>]
* : Comma-separated list of files or directories to exclude from checksum verification.
*
* ## EXAMPLES
*
* # Verify checksums
Expand Down Expand Up @@ -83,6 +93,10 @@ public function __invoke( $args, $assoc_args ) {
$this->include_root = true;
}

if ( ! empty( $assoc_args['exclude'] ) ) {
$this->exclude_paths = explode( ',', $assoc_args['exclude'] );
}

if ( empty( $wp_version ) ) {
$details = self::get_wp_details();
$wp_version = $details['wp_version'];
Expand Down Expand Up @@ -112,6 +126,11 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

// Skip excluded paths
if ( $this->is_excluded( $file ) ) {
continue;
}

if ( ! file_exists( ABSPATH . $file ) ) {
WP_CLI::warning( "File doesn't exist: {$file}" );
$has_errors = true;
Expand All @@ -131,6 +150,10 @@ public function __invoke( $args, $assoc_args ) {

if ( ! empty( $additional_files ) ) {
foreach ( $additional_files as $additional_file ) {
// Skip excluded paths
if ( $this->is_excluded( $additional_file ) ) {
continue;
}
WP_CLI::warning( "File should not exist: {$additional_file}" );
}
}
Expand All @@ -142,6 +165,22 @@ public function __invoke( $args, $assoc_args ) {
}
}

/**
* Checks if a file path is excluded.
*
* @param string $file Path to a file.
*
* @return bool
*/
private function is_excluded( $file ) {
foreach ( $this->exclude_paths as $exclude_path ) {
if ( strpos( $file, $exclude_path ) !== false ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too broad IMHO. It means I can do --exclude=a and every file with the letter a in it will be excluded. I don't think that's desired.

We should make this a strict comparison, where the path has to match exactly. Could be a simple in_array() check.

return true;
}
}
return false;
}

/**
* Whether to include the file in the verification or not.
*
Expand Down