Skip to content

Commit 1a44dfb

Browse files
Add --include-root parameter to also verify root directory (#102)
* Add --include-root parameter Allows warning for unexpected files in ABSPATH. * Add test for --include-root parameter * when --allow-root is set, do not warn about wp-config.php and wp-contents/plugins/* * PHPCS: align equals signs * Update src/Checksum_Core_Command.php Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co> * when --include-root is enabled, skip entire wp-content directory from extra file checks * add private $include_root variable * update help text to indicate it looks for files and folders * Add test for wp-cli.yml * Update features/checksum-core.feature Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co> * Fix Scenario indentation * Add more tests to clarify expected behavior * Remove extraneous helper * Clean up `wp-cli.yml` scenario * Rebuild README with new flag * Avoid random test failures when the order changes * Remove extraneous argument * Fix these assertions --------- Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co> Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
1 parent e9dcb2b commit 1a44dfb

File tree

3 files changed

+123
-18
lines changed

3 files changed

+123
-18
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This package implements the following commands:
1616
Verifies WordPress files against WordPress.org's checksums.
1717

1818
~~~
19-
wp core verify-checksums [--version=<version>] [--locale=<locale>] [--insecure]
19+
wp core verify-checksums [--include-root] [--version=<version>] [--locale=<locale>] [--insecure]
2020
~~~
2121

2222
Downloads md5 checksums for the current version from WordPress.org, and
@@ -31,6 +31,9 @@ site.
3131

3232
**OPTIONS**
3333

34+
[--include-root]
35+
Verify all files and folders in the root directory, and warn if any non-WordPress items are found.
36+
3437
[--version=<version>]
3538
Verify checksums against a specific version of WordPress.
3639

features/checksum-core.feature

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@ Feature: Validate checksums for WordPress install
3333
Warning: File doesn't exist: readme.html
3434
Error: WordPress installation doesn't verify against checksums.
3535
"""
36+
And the return code should be 1
37+
38+
Scenario: Core checksums don't verify because wp-cli.yml is present
39+
Given a WP install
40+
And a wp-cli.yml file:
41+
"""
42+
plugin install:
43+
- user-switching
44+
"""
45+
46+
When I try `wp core verify-checksums`
47+
Then STDERR should be:
48+
"""
49+
Warning: File should not exist: wp-cli.yml
50+
"""
51+
And STDOUT should be:
52+
"""
53+
Success: WordPress installation verifies against checksums.
54+
"""
55+
And the return code should be 0
56+
57+
When I run `rm wp-cli.yml`
58+
Then STDERR should be empty
59+
60+
When I run `wp core verify-checksums`
61+
Then STDERR should be empty
62+
And STDOUT should be:
63+
"""
64+
Success: WordPress installation verifies against checksums.
65+
"""
66+
And the return code should be 0
3667

3768
Scenario: Verify core checksums without loading WordPress
3869
Given an empty directory
@@ -96,23 +127,76 @@ Feature: Validate checksums for WordPress install
96127
"""
97128
And the return code should be 0
98129

99-
Scenario: Verify core checksums when extra files prefixed with 'wp-' are included in WordPress root
100-
Given a WP install
101-
And a wp-extra-file.php file:
102-
"""
103-
hello world
104-
"""
105-
106-
When I try `wp core verify-checksums`
107-
Then STDERR should be:
108-
"""
109-
Warning: File should not exist: wp-extra-file.php
110-
"""
111-
And STDOUT should be:
112-
"""
113-
Success: WordPress installation verifies against checksums.
114-
"""
115-
And the return code should be 0
130+
Scenario: Verify core checksums when extra files prefixed with 'wp-' are included in WordPress root
131+
Given a WP install
132+
And a wp-extra-file.php file:
133+
"""
134+
hello world
135+
"""
136+
137+
When I try `wp core verify-checksums`
138+
Then STDERR should be:
139+
"""
140+
Warning: File should not exist: wp-extra-file.php
141+
"""
142+
And STDOUT should be:
143+
"""
144+
Success: WordPress installation verifies against checksums.
145+
"""
146+
And the return code should be 0
147+
148+
Scenario: Verify core checksums when extra files are included in WordPress root and --include-root is passed
149+
Given a WP install
150+
And a extra-file.php file:
151+
"""
152+
hello world
153+
"""
154+
And a unknown-folder/unknown-file.php file:
155+
"""
156+
taco burrito
157+
"""
158+
And a wp-content/unknown-file.php file:
159+
"""
160+
foobar
161+
"""
162+
163+
When I try `wp core verify-checksums --include-root`
164+
Then STDERR should contain:
165+
"""
166+
Warning: File should not exist: unknown-folder/unknown-file.php
167+
"""
168+
And STDERR should contain:
169+
"""
170+
Warning: File should not exist: extra-file.php
171+
"""
172+
And STDERR should not contain:
173+
"""
174+
Warning: File should not exist: wp-content/unknown-file.php
175+
"""
176+
And STDOUT should be:
177+
"""
178+
Success: WordPress installation verifies against checksums.
179+
"""
180+
And the return code should be 0
181+
182+
When I run `wp core verify-checksums`
183+
Then STDERR should not contain:
184+
"""
185+
Warning: File should not exist: unknown-folder/unknown-file.php
186+
"""
187+
And STDERR should not contain:
188+
"""
189+
Warning: File should not exist: extra-file.php
190+
"""
191+
And STDERR should not contain:
192+
"""
193+
Warning: File should not exist: wp-content/unknown-file.php
194+
"""
195+
And STDOUT should be:
196+
"""
197+
Success: WordPress installation verifies against checksums.
198+
"""
199+
And the return code should be 0
116200

117201
Scenario: Verify core checksums with a plugin that has wp-admin
118202
Given a WP install

src/Checksum_Core_Command.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*/
1111
class Checksum_Core_Command extends Checksum_Base_Command {
1212

13+
/**
14+
* Whether or not to verify contents of the root directory.
15+
*
16+
* @var boolean
17+
*/
18+
private $include_root = false;
19+
1320
/**
1421
* Verifies WordPress files against WordPress.org's checksums.
1522
*
@@ -25,6 +32,9 @@ class Checksum_Core_Command extends Checksum_Base_Command {
2532
*
2633
* ## OPTIONS
2734
*
35+
* [--include-root]
36+
* : Verify all files and folders in the root directory, and warn if any non-WordPress items are found.
37+
*
2838
* [--version=<version>]
2939
* : Verify checksums against a specific version of WordPress.
3040
*
@@ -69,6 +79,10 @@ public function __invoke( $args, $assoc_args ) {
6979
$locale = $assoc_args['locale'];
7080
}
7181

82+
if ( ! empty( $assoc_args['include-root'] ) ) {
83+
$this->include_root = true;
84+
}
85+
7286
if ( empty( $wp_version ) ) {
7387
$details = self::get_wp_details();
7488
$wp_version = $details['wp_version'];
@@ -136,6 +150,10 @@ public function __invoke( $args, $assoc_args ) {
136150
* @return bool
137151
*/
138152
protected function filter_file( $filepath ) {
153+
if ( true === $this->include_root ) {
154+
return ( 1 !== preg_match( '/^(wp-config\.php$|wp-content\/)/', $filepath ) );
155+
}
156+
139157
return ( 0 === strpos( $filepath, 'wp-admin/' )
140158
|| 0 === strpos( $filepath, 'wp-includes/' )
141159
|| 1 === preg_match( '/^wp-(?!config\.php)([^\/]*)$/', $filepath )

0 commit comments

Comments
 (0)