-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathChecksum_Core_Command.php
More file actions
301 lines (260 loc) · 8.31 KB
/
Checksum_Core_Command.php
File metadata and controls
301 lines (260 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
use WP_CLI\Formatter;
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;
/**
* Verifies core file integrity by comparing to published checksums.
*
* @package wp-cli
*/
class Checksum_Core_Command extends Checksum_Base_Command {
/**
* Whether or not to verify contents of the root directory.
*
* @var boolean
*/
private $include_root = false;
/**
* Files to exclude from the verification.
*
* @var array
*/
private $exclude_files = [];
/**
* Array of detected errors.
*
* @var array
*/
private $errors = [];
/**
* Verifies WordPress files against WordPress.org's checksums.
*
* Downloads md5 checksums for the current version from WordPress.org, and
* compares those checksums against the currently installed files.
*
* For security, avoids loading WordPress when verifying checksums.
*
* If you experience issues verifying from this command, ensure you are
* passing the relevant `--locale` and `--version` arguments according to
* the values from the `Dashboard->Updates` menu in the admin area of the
* site.
*
* ## OPTIONS
*
* [--include-root]
* : Verify all files and folders in the root directory, and warn if any non-WordPress items are found.
*
* [--version=<version>]
* : Verify checksums against a specific version of WordPress.
*
* [--locale=<locale>]
* : Verify checksums against a specific locale of WordPress.
*
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* [--exclude=<files>]
* : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths.
*
* [--format=<format>]
* : Render output in a specific format. When provided, messages are displayed in the chosen format.
* ---
* default: plain
* options:
* - plain
* - table
* - json
* - csv
* - yaml
* - count
* ---
*
* ## EXAMPLES
*
* # Verify checksums
* $ wp core verify-checksums
* Success: WordPress installation verifies against checksums.
*
* # Verify checksums for given WordPress version
* $ wp core verify-checksums --version=4.0
* Success: WordPress installation verifies against checksums.
*
* # Verify checksums for given locale
* $ wp core verify-checksums --locale=en_US
* Success: WordPress installation verifies against checksums.
*
* # Verify checksums for given locale
* $ wp core verify-checksums --locale=ja
* Warning: File doesn't verify against checksum: wp-includes/version.php
* Warning: File doesn't verify against checksum: readme.html
* Warning: File doesn't verify against checksum: wp-config-sample.php
* Error: WordPress installation doesn't verify against checksums.
*
* # Verify checksums and exclude files
* $ wp core verify-checksums --exclude="readme.html"
* Success: WordPress installation verifies against checksums.
*
* # Verify checksums with formatted output
* $ wp core verify-checksums --format=json
* [{"file":"readme.html","message":"File doesn't verify against checksum"}]
* Error: WordPress installation doesn't verify against checksums.
*
* @when before_wp_load
*/
public function __invoke( $args, $assoc_args ) {
$wp_version = '';
$locale = '';
if ( ! empty( $assoc_args['version'] ) ) {
$wp_version = $assoc_args['version'];
}
if ( ! empty( $assoc_args['locale'] ) ) {
$locale = $assoc_args['locale'];
}
if ( ! empty( $assoc_args['include-root'] ) ) {
$this->include_root = true;
}
if ( ! empty( $assoc_args['exclude'] ) ) {
$exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
$this->exclude_files = explode( ',', $exclude );
}
if ( empty( $wp_version ) ) {
$details = self::get_wp_details();
$wp_version = $details['wp_version'];
if ( empty( $locale ) ) {
$locale = $details['wp_local_package'];
}
}
$insecure = Utils\get_flag_value( $assoc_args, 'insecure', false );
$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );
try {
$checksums = $wp_org_api->get_core_checksums( $wp_version, empty( $locale ) ? 'en_US' : $locale );
} catch ( Exception $exception ) {
WP_CLI::error( $exception );
}
if ( ! is_array( $checksums ) ) {
WP_CLI::error( "Couldn't get checksums from WordPress.org." );
}
$has_errors = false;
foreach ( $checksums as $file => $checksum ) {
// Skip files which get updated
if ( 'wp-content' === substr( $file, 0, 10 ) ) {
continue;
}
if ( in_array( $file, $this->exclude_files, true ) ) {
continue;
}
if ( ! file_exists( ABSPATH . $file ) ) {
$this->errors[] = [
'file' => $file,
'message' => "File doesn't exist",
];
$has_errors = true;
continue;
}
$md5_file = md5_file( ABSPATH . $file );
if ( $checksum !== $md5_file ) {
$this->errors[] = [
'file' => $file,
'message' => "File doesn't verify against checksum",
];
$has_errors = true;
}
}
$core_checksums_files = array_filter( array_keys( $checksums ), [ $this, 'filter_file' ] );
$core_files = $this->get_files( ABSPATH );
$additional_files = array_diff( $core_files, $core_checksums_files );
if ( ! empty( $additional_files ) ) {
foreach ( $additional_files as $additional_file ) {
if ( in_array( $additional_file, $this->exclude_files, true ) ) {
continue;
}
$this->errors[] = [
'file' => $additional_file,
'message' => 'File should not exist',
];
}
}
if ( ! empty( $this->errors ) ) {
if ( ! isset( $assoc_args['format'] ) || 'plain' === $assoc_args['format'] ) {
foreach ( $this->errors as $error ) {
WP_CLI::warning( sprintf( '%s: %s', $error['message'], $error['file'] ) );
}
} else {
$formatter = new Formatter(
$assoc_args,
array( 'file', 'message' )
);
$formatter->display_items( $this->errors );
}
}
if ( ! $has_errors ) {
WP_CLI::success( 'WordPress installation verifies against checksums.' );
} else {
WP_CLI::error( "WordPress installation doesn't verify against checksums." );
}
}
/**
* Whether to include the file in the verification or not.
*
* @param string $filepath Path to a file.
*
* @return bool
*/
protected function filter_file( $filepath ) {
if ( true === $this->include_root ) {
return ( 1 !== preg_match( '/^(\.htaccess$|\.maintenance$|wp-config\.php$|wp-content\/)/', $filepath ) );
}
return ( 0 === strpos( $filepath, 'wp-admin/' )
|| 0 === strpos( $filepath, 'wp-includes/' )
|| 1 === preg_match( '/^wp-(?!config\.php)([^\/]*)$/', $filepath )
);
}
/**
* Gets version information from `wp-includes/version.php`.
*
* @return array {
* @type string $wp_version The WordPress version.
* @type int $wp_db_version The WordPress DB revision.
* @type string $tinymce_version The TinyMCE version.
* @type string $wp_local_package The TinyMCE version.
* }
*/
private static function get_wp_details() {
$versions_path = ABSPATH . 'wp-includes/version.php';
if ( ! is_readable( $versions_path ) ) {
WP_CLI::error(
"This does not seem to be a WordPress install.\n" .
'Pass --path=`path/to/wordpress` or run `wp core download`.'
);
}
$version_content = (string) file_get_contents( $versions_path, false, null, 6, 2048 );
$vars = [ 'wp_version', 'wp_db_version', 'tinymce_version', 'wp_local_package' ];
$result = [];
foreach ( $vars as $var_name ) {
$result[ $var_name ] = self::find_var( $var_name, $version_content );
}
return $result;
}
/**
* Searches for the value assigned to variable `$var_name` in PHP code `$code`.
*
* This is equivalent to matching the `\$VAR_NAME = ([^;]+)` regular expression and returning
* the first match either as a `string` or as an `integer` (depending if it's surrounded by
* quotes or not).
*
* @param string $var_name Variable name to search for.
* @param string $code PHP code to search in.
*
* @return string|null
*/
private static function find_var( $var_name, $code ) {
$start = strpos( $code, '$' . $var_name . ' = ' );
if ( false === $start ) {
return null;
}
$start = $start + strlen( $var_name ) + 3;
$end = strpos( $code, ';', $start );
$value = substr( $code, $start, $end - $start );
return trim( $value, " '" );
}
}