-
Notifications
You must be signed in to change notification settings - Fork 6
/
wpvulnerability-api.php
513 lines (447 loc) · 18.7 KB
/
wpvulnerability-api.php
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* WPVulnerability REST API Endpoints
*
* @package WPVulnerability
*
* @since 3.3.0
*/
/**
* Handle the core vulnerabilities REST API request.
*
* This function handles the request for retrieving core vulnerabilities.
* It includes the necessary files and fetches the vulnerabilities data.
*
* @since 3.3.0
*
* @return WP_REST_Response Core vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_core_vulnerabilities() {
// Include the files containing the functions to get core vulnerabilities.
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-general.php';
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-core.php';
// Get the core vulnerabilities.
$core_vulnerabilities = wpvulnerability_core_get_vulnerabilities();
$core_complete = array();
$vulnerabilities = array();
// Check if vulnerabilities are found and is an array.
if ( $core_vulnerabilities && is_array( $core_vulnerabilities ) ) {
// Loop through each core vulnerability.
foreach ( $core_vulnerabilities as $vulnerability ) {
$core_complete_temp = array();
// Process vulnerability version.
$core_complete_temp['version'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['name'], 'strip' ) ) );
// Process vulnerability severity.
$core_complete_temp['severity'] = null;
if ( isset( $vulnerability['impact']['cvss']['severity'] ) ) {
$core_complete_temp['severity'] = wpvulnerability_severity( $vulnerability['impact']['cvss']['severity'] );
}
// Process CWE details.
$core_complete_temp['cwe'] = array();
if ( isset( $vulnerability['impact']['cwe'] ) && count( $vulnerability['impact']['cwe'] ) ) {
foreach ( $vulnerability['impact']['cwe'] as $vulnerability_cwe ) {
$core_complete_temp['cwe'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['name'], 'strip' ) ) ),
'description' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['description'], 'strip' ) ) ),
);
}
}
// Process CVSS score.
$core_complete_temp['score'] = null;
if ( isset( $vulnerability['impact']['cvss']['score'] ) ) {
$core_complete_temp['score'] = number_format( (float) $vulnerability['impact']['cvss']['score'], 1, '.', '' );
}
// Process vulnerability sources.
$core_complete_temp['source'] = array();
if ( isset( $vulnerability['source'] ) && count( $vulnerability['source'] ) ) {
foreach ( $vulnerability['source'] as $vulnerability_source ) {
$core_complete_temp['source'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_source['name'], 'strip' ) ) ),
'link' => esc_url_raw( (string) $vulnerability_source['link'], 'strip' ),
'description' => trim( html_entity_decode( wp_kses( (string) $vulnerability_source['description'], 'strip' ) ) ),
);
}
}
$core_complete[] = $core_complete_temp;
unset( $core_complete_temp );
}
}
// Return the vulnerabilities in the response.
return new WP_REST_Response( $core_complete, 200 );
}
/**
* Handle the plugins vulnerabilities REST API request.
*
* This function handles the request for retrieving plugins vulnerabilities.
* It includes the necessary files and fetches the vulnerabilities data.
*
* @since 3.3.0
*
* @return WP_REST_Response Plugins vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_plugins_vulnerabilities() {
// Include the files containing the functions to get plugins vulnerabilities.
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-general.php';
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-plugins.php';
// Get the plugins vulnerabilities.
$plugins_vulnerabilities = wpvulnerability_plugin_get_vulnerabilities();
$plugins_complete = array();
// Loop through each plugin vulnerability.
foreach ( $plugins_vulnerabilities as $plugin ) {
// Check if the plugin is vulnerable.
if ( 1 === $plugin['vulnerable'] ) {
$plugins_complete_temp = array();
// Process plugin name and slug.
$plugins_complete_temp['name'] = trim( html_entity_decode( wp_kses( (string) $plugin['Name'], 'strip' ) ) );
$plugins_complete_temp['slug'] = trim( html_entity_decode( wp_kses( (string) $plugin['slug'], 'strip' ) ) );
// Prepare the vulnerabilities array for output.
foreach ( $plugin['vulnerabilities'] as $vulnerability ) {
$plugins_complete_temp_vulnerabilities = array();
// Process vulnerability severity.
$plugins_complete_temp_vulnerabilities['severity'] = null;
if ( isset( $vulnerability['impact']['cvss']['severity'] ) ) {
$plugins_complete_temp_vulnerabilities['severity'] = wpvulnerability_severity( $vulnerability['impact']['cvss']['severity'] );
}
// Process vulnerability details.
$plugins_complete_temp_vulnerabilities['version'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['version'], 'strip' ) ) );
$plugins_complete_temp_vulnerabilities['affected'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['versions'], 'strip' ) ) );
$plugins_complete_temp_vulnerabilities['name'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['name'], 'strip' ) ) );
$plugins_complete_temp_vulnerabilities['closed'] = (int) $vulnerability['closed'];
$plugins_complete_temp_vulnerabilities['unfixed'] = (int) $vulnerability['unfixed'];
// Process CWE details.
$plugins_complete_temp_vulnerabilities['cwe'] = array();
if ( isset( $vulnerability['impact']['cwe'] ) && count( $vulnerability['impact']['cwe'] ) ) {
foreach ( $vulnerability['impact']['cwe'] as $vulnerability_cwe ) {
$plugins_complete_temp_vulnerabilities['cwe'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['name'], 'strip' ) ) ),
'description' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['description'], 'strip' ) ) ),
);
}
}
// Process CVSS score.
$plugins_complete_temp_vulnerabilities['score'] = null;
if ( isset( $vulnerability['impact']['cvss']['score'] ) ) {
$plugins_complete_temp_vulnerabilities['score'] = number_format( (float) $vulnerability['impact']['cvss']['score'], 1, '.', '' );
}
// Process vulnerability sources.
$plugins_complete_temp_vulnerabilities['source'] = array();
if ( isset( $vulnerability['source'] ) && count( $vulnerability['source'] ) ) {
foreach ( $vulnerability['source'] as $vulnerability_source ) {
$plugins_complete_temp_vulnerabilities['source'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_source['name'], 'strip' ) ) ),
'link' => esc_url_raw( (string) $vulnerability_source['link'], 'strip' ),
);
}
}
// Add processed vulnerability to the temporary array.
$plugins_complete_temp['vulnerabilities'][] = $plugins_complete_temp_vulnerabilities;
}
// Add processed plugin data to the complete array.
$plugins_complete[] = $plugins_complete_temp;
}
}
// Return the vulnerabilities in the response.
return new WP_REST_Response( $plugins_complete, 200 );
}
/**
* Handle the themes vulnerabilities REST API request.
*
* This function handles the request for retrieving themes vulnerabilities.
* It includes the necessary files and fetches the vulnerabilities data.
*
* @since 3.3.0
*
* @return WP_REST_Response Themes vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_themes_vulnerabilities() {
// Include the file containing the function to get themes vulnerabilities.
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-general.php';
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-themes.php';
// Get the themes vulnerabilities.
$themes_vulnerabilities = wpvulnerability_theme_get_vulnerabilities();
$themes_complete = array();
// Loop through each theme vulnerability.
foreach ( $themes_vulnerabilities as $theme ) {
// Check if the theme is vulnerable.
if ( 1 === $theme['wpvulnerability']['vulnerable'] ) {
$themes_complete_temp = array();
// Process theme name and slug.
$themes_complete_temp['name'] = trim( html_entity_decode( wp_kses( (string) $theme['wpvulnerability']['name'], 'strip' ) ) );
$themes_complete_temp['slug'] = trim( html_entity_decode( wp_kses( (string) $theme['wpvulnerability']['slug'], 'strip' ) ) );
// Prepare the vulnerabilities array for output.
foreach ( $theme['wpvulnerability']['vulnerabilities'] as $vulnerability ) {
$themes_complete_temp_vulnerabilities = array();
// Process vulnerability severity.
$themes_complete_temp_vulnerabilities['severity'] = null;
if ( isset( $vulnerability['impact']['cvss']['severity'] ) ) {
$themes_complete_temp_vulnerabilities['severity'] = wpvulnerability_severity( $vulnerability['impact']['cvss']['severity'] );
}
// Process vulnerability details.
$themes_complete_temp_vulnerabilities['version'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['version'], 'strip' ) ) );
$themes_complete_temp_vulnerabilities['affected'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['versions'], 'strip' ) ) );
$themes_complete_temp_vulnerabilities['name'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['name'], 'strip' ) ) );
$themes_complete_temp_vulnerabilities['closed'] = (int) $vulnerability['closed'];
$themes_complete_temp_vulnerabilities['unfixed'] = (int) $vulnerability['unfixed'];
// Process CWE details.
$themes_complete_temp_vulnerabilities['cwe'] = array();
if ( isset( $vulnerability['impact']['cwe'] ) && count( $vulnerability['impact']['cwe'] ) ) {
foreach ( $vulnerability['impact']['cwe'] as $vulnerability_cwe ) {
$themes_complete_temp_vulnerabilities['cwe'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['name'], 'strip' ) ) ),
'description' => trim( html_entity_decode( wp_kses( (string) $vulnerability_cwe['description'], 'strip' ) ) ),
);
}
}
// Process CVSS score.
$themes_complete_temp_vulnerabilities['score'] = null;
if ( isset( $vulnerability['impact']['cvss']['score'] ) ) {
$themes_complete_temp_vulnerabilities['score'] = number_format( (float) $vulnerability['impact']['cvss']['score'], 1, '.', '' );
}
// Process vulnerability sources.
$themes_complete_temp_vulnerabilities['source'] = array();
if ( isset( $vulnerability['source'] ) && count( $vulnerability['source'] ) ) {
foreach ( $vulnerability['source'] as $vulnerability_source ) {
$themes_complete_temp_vulnerabilities['source'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $vulnerability_source['name'], 'strip' ) ) ),
'link' => esc_url_raw( (string) $vulnerability_source['link'], 'strip' ),
);
}
}
// Add processed vulnerability to the temporary array.
$themes_complete_temp['vulnerabilities'][] = $themes_complete_temp_vulnerabilities;
}
// Add processed theme data to the complete array.
$themes_complete[] = $themes_complete_temp;
}
}
// Return the vulnerabilities in the response.
return new WP_REST_Response( $themes_complete, 200 );
}
/**
* Handle vulnerabilities REST API request for different software types.
*
* This function processes the request to retrieve vulnerabilities for the specified software type.
* It loads the necessary files and fetches the vulnerability data, then returns the data in a structured format.
*
* @since 3.5.0
*
* @param string $software_type The type of software to retrieve vulnerabilities for.
* @return WP_REST_Response The vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_software_vulnerabilities( $software_type ) {
// Include the general file for retrieving vulnerabilities.
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-general.php';
require_once WPVULNERABILITY_PLUGIN_PATH . '/wpvulnerability-software.php';
// Get vulnerabilities based on the software type.
$vulnerabilities = array();
switch ( $software_type ) {
case 'php':
case 'apache':
case 'nginx':
case 'mariadb':
case 'mysql':
case 'imagemagick':
case 'curl':
case 'memcached':
case 'redis':
case 'sqlite':
$vulnerabilities = wpvulnerability_get_vulnerabilities( $software_type, wpvulnerability_get_software_version( $software_type ) );
break;
default:
WP_REST_Response( array(), 400 ); // Invalid software type.
}
$complete_vulnerabilities = array();
if ( isset( $vulnerabilities ) && is_array( $vulnerabilities ) ) {
// Process each vulnerability.
foreach ( $vulnerabilities as $vulnerability ) {
$temp = array();
$temp['version'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['version'], 'strip' ) ) );
$temp['affected'] = trim( html_entity_decode( wp_kses( (string) $vulnerability['versions'], 'strip' ) ) );
$temp['unfixed'] = (int) $vulnerability['unfixed'];
// Process vulnerability sources.
$temp['source'] = array();
if ( isset( $vulnerability['source'] ) && count( $vulnerability['source'] ) ) {
foreach ( $vulnerability['source'] as $source ) {
$temp['source'][] = array(
'name' => trim( html_entity_decode( wp_kses( (string) $source['id'], 'strip' ) ) ),
'description' => trim( html_entity_decode( wp_kses( (string) $source['description'], 'strip' ) ) ),
'link' => esc_url_raw( (string) $source['link'], 'strip' ),
);
}
}
// Add processed vulnerability to the complete array.
$complete_vulnerabilities[] = $temp;
}
}
// Return the vulnerabilities in the response.
return new WP_REST_Response( $complete_vulnerabilities, 200 );
}
/**
* Handle the PHP vulnerabilities REST API request.
*
* @since 3.3.0
*
* @return WP_REST_Response PHP vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_php_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'php' );
}
/**
* Handle the Apache vulnerabilities REST API request.
*
* @since 3.3.0
*
* @return WP_REST_Response Apache vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_apache_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'apache' );
}
/**
* Handle the Nginx vulnerabilities REST API request.
*
* @since 3.3.0
*
* @return WP_REST_Response Nginx vulnerabilities data or a message if none found.
*/
function wpvulnerability_rest_nginx_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'nginx' );
}
/**
* Handle the MariaDB vulnerabilities REST API request.
*
* @since 3.4.0
*
* @return WP_REST_Response MariaDB vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_mariadb_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'mariadb' );
}
/**
* Handle the MySQL vulnerabilities REST API request.
*
* @since 3.4.0
*
* @return WP_REST_Response MySQL vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_mysql_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'mysql' );
}
/**
* Handle the ImageMagick vulnerabilities REST API request.
*
* @since 3.5.0
*
* @return WP_REST_Response ImageMagick vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_imagemagick_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'imagemagick' );
}
/**
* Handle the curl vulnerabilities REST API request.
*
* @since 3.5.0
*
* @return WP_REST_Response curl vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_curl_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'curl' );
}
/**
* Handle the memcached vulnerabilities REST API request.
*
* @since 3.5.0
*
* @return WP_REST_Response memcached vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_memcached_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'memcached' );
}
/**
* Handle the Redis vulnerabilities REST API request.
*
* @since 3.5.0
*
* @return WP_REST_Response Redis vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_redis_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'redis' );
}
/**
* Handle the SQLite vulnerabilities REST API request.
*
* @since 3.5.0
*
* @return WP_REST_Response SQLite vulnerabilities data or an empty array if none found.
*/
function wpvulnerability_rest_sqlite_vulnerabilities() {
return wpvulnerability_rest_software_vulnerabilities( 'sqlite' );
}
/**
* Custom permission check for the WPVulnerability REST API.
*
* This function checks if the request is authenticated using an Application Password.
*
* @since 3.3.0
*
* @param WP_REST_Request $request The REST API request.
*
* @return bool True if the user has permission, false otherwise.
*/
function wpvulnerability_permission_check( WP_REST_Request $request ) {
// Check if application passwords are available.
if ( wp_is_application_passwords_available() ) {
$authorization_header = $request->get_header( 'authorization' );
// Check if the authorization header is present and properly formatted.
if ( $authorization_header && preg_match( '/^Basic\s(.+)$/i', $authorization_header, $matches ) ) {
$auth_string = base64_decode( (string) $matches[1] ); // phpcs:ignore
list( $user, $password ) = explode( ':', $auth_string );
// Authenticate the user using the application password.
if ( wp_authenticate_application_password( null, $user, $password ) instanceof WP_User ) {
return true;
}
}
}
return false;
}
/**
* Registers REST API routes for WPVulnerability.
*
* This function sets up the REST API routes for WPVulnerability to handle requests
* related to vulnerabilities in various components like core, plugins, themes, PHP, and more.
*
* @since 3.3.0
*
* @return void
*/
function wpvulnerability_register_rest_routes() {
// Define the endpoints to be registered.
$endpoints = array(
'core',
'plugins',
'themes',
'php',
'apache',
'nginx',
'mariadb',
'mysql',
'imagemagick',
'curl',
'memcached',
'redis',
'sqlite',
);
// Loop through each endpoint and register it.
foreach ( $endpoints as $endpoint ) {
register_rest_route(
'wpvulnerability/v1', // Namespace and version.
'/' . $endpoint, // Endpoint URL.
array(
'methods' => 'GET', // HTTP method.
'callback' => 'wpvulnerability_rest_' . $endpoint . '_vulnerabilities', // Callback function.
'permission_callback' => 'wpvulnerability_permission_check', // Permission check callback.
)
);
}
}
// Hook to initialize REST API endpoints.
add_action( 'rest_api_init', 'wpvulnerability_register_rest_routes' );