-
Notifications
You must be signed in to change notification settings - Fork 6
/
wpvulnerability-core.php
226 lines (191 loc) · 8.01 KB
/
wpvulnerability-core.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
<?php
/**
* Core functions
*
* @package WPVulnerability
*
* @version 2.0.0
*/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
* Adds a vulnerability notice under vulnerable core.
*
* @since 2.0.0
*
* @return void
*/
function wpvulnerability_core_info_after() {
// Retrieve the vulnerabilities for core from the options table and decode the JSON.
if ( is_multisite() ) {
$core_vulnerabilities = json_decode( get_site_option( 'wpvulnerability-core' ), true );
} else {
$core_vulnerabilities = json_decode( get_option( 'wpvulnerability-core' ), true );
}
// Generate the vulnerability notice message.
$message = sprintf(
/* translators: 1: core version */
__( 'WordPress %1$s has a known vulnerability that may be affecting this version.', 'wpvulnerability' ),
get_bloginfo( 'version' )
);
$information = '<p class="text-red"><img src="' . esc_url( WPVULNERABILITY_PLUGIN_URL ) . 'assets/logo16.png" style="height: 16px; vertical-align: text-top; width: 16px;" alt="" title="WPVulnerability"> <strong>' . $message . '</strong></p>';
$information .= '<table class="wp-list-table widefat wpvulnerability">';
// Loop through all vulnerabilities for the current version and add their details to the table row HTML markup.
if ( is_array( $core_vulnerabilities ) ) {
foreach ( $core_vulnerabilities as $vulnerability ) {
$what = array();
if ( isset( $vulnerability['impact']['cwe'] ) ) {
foreach ( $vulnerability['impact']['cwe'] as $vulnerability_cwe ) {
$what[] = '<div><b>' . wp_kses( (string) $vulnerability_cwe['name'], 'strip' ) . '</b></div><div><i>' . wp_kses_post( (string) $vulnerability_cwe['description'] ) . '</i></div>';
}
}
$sources = array();
if ( isset( $vulnerability['source'] ) ) {
foreach ( $vulnerability['source'] as $vulnerability_source ) {
$sources[] = '<a href="' . esc_url_raw( (string) $vulnerability_source['link'] ) . '" target="_blank" rel="external nofollow noopener noreferrer">[+]</a> ' . wp_kses( (string) $vulnerability_source['name'], 'strip' );
}
}
$source = count( $sources ) ? '<div style="padding-bottom: 5px;">' . implode( '<br>', $sources ) . '</div>' : '';
$score = isset( $vulnerability['impact']['cvss']['score'] ) ? number_format( (float) $vulnerability['impact']['cvss']['score'], 1, '.', '' ) : null;
$severity = isset( $vulnerability['impact']['cvss']['severity'] ) ? wpvulnerability_severity( $vulnerability['impact']['cvss']['severity'] ) : null;
$information .= '<tr>';
$information .= '<td style="max-width: 256px; min-width: 96px;">WordPress <b>' . wp_kses( (string) $vulnerability['name'], 'strip' ) . '</b></td>';
$information .= '<td>';
if ( count( $what ) ) {
$information .= '<div style="padding-bottom: 5px;">' . implode( '', $what ) . '</div>';
}
if ( ! is_null( $score ) || ! is_null( $severity ) ) {
$information .= '<div style="padding-bottom: 5px;">';
if ( ! is_null( $score ) ) {
$information .= '<div>' . __( 'Global score: ', 'wpvulnerability' ) . $score . ' / 10</div>';
}
if ( ! is_null( $severity ) ) {
$information .= '<div>' . __( 'Severity: ', 'wpvulnerability' ) . $severity . '</div>';
}
$information .= '</div>';
}
$information .= wp_kses( (string) $source, 'post' );
$information .= '</td>';
$information .= '</tr>';
}
}
$information .= '</table>';
echo $information; // phpcs:ignore
}
/**
* Retrieves vulnerabilities for a given WordPress core version and updates its data.
*
* @since 2.0.0
*
* @return array|false The updated core data array or false if no vulnerabilities are found.
*/
function wpvulnerability_get_fresh_core_vulnerabilities() {
// Get the core version and sanitize it.
$version = wpvulnerability_sanitize_version( get_bloginfo( 'version' ) );
// Retrieve vulnerabilities for the core version.
$response = wpvulnerability_get_core( $version, 0 );
$core_data = array();
// If no vulnerabilities are found, return false.
if ( empty( $response ) ) {
return false;
}
// If vulnerabilities are found, update the core data.
foreach ( $response as $v ) {
if ( isset( $v['name'], $v['source'], $v['impact'] ) ) { // Ensure expected keys exist.
$core_data[] = array(
'name' => wp_kses( (string) $v['name'], 'strip' ),
'source' => $v['source'],
'impact' => $v['impact'],
);
}
}
return ! empty( $core_data ) ? $core_data : false; // Return false if core_data is empty.
}
/**
* Get Vulnerabilities
*
* Retrieves and caches the vulnerabilities for the installed WordPress core version.
*
* @since 2.0.0
*
* @return string JSON-encoded array of core data with vulnerabilities and vulnerable status.
*/
function wpvulnerability_core_get_installed() {
$wpvulnerability_core_vulnerable = 0;
// Get fresh core vulnerabilities.
$core = wpvulnerability_get_fresh_core_vulnerabilities();
// Check if vulnerabilities were found and count them.
if ( is_array( $core ) && count( $core ) > 0 ) {
$wpvulnerability_core_vulnerable = count( $core );
}
// Cache the vulnerability data and the timestamp for cache expiration.
if ( is_multisite() ) {
update_site_option( 'wpvulnerability-core', wp_json_encode( $core ) );
update_site_option( 'wpvulnerability-core-vulnerable', wp_json_encode( number_format( $wpvulnerability_core_vulnerable, 0, '.', '' ) ) );
update_site_option( 'wpvulnerability-core-cache', wp_json_encode( number_format( time() + ( 3600 * WPVULNERABILITY_CACHE_HOURS ), 0, '.', '' ) ) );
} else {
update_option( 'wpvulnerability-core', wp_json_encode( $core ) );
update_option( 'wpvulnerability-core-vulnerable', wp_json_encode( number_format( $wpvulnerability_core_vulnerable, 0, '.', '' ) ) );
update_option( 'wpvulnerability-core-cache', wp_json_encode( number_format( time() + ( 3600 * WPVULNERABILITY_CACHE_HOURS ), 0, '.', '' ) ) );
}
// Return the JSON-encoded array of core vulnerabilities.
return wp_json_encode( $core );
}
/**
* Get the cached vulnerabilities or update the cache if it's stale or missing.
*
* @since 2.0.0
*
* @return array Array of core with their vulnerabilities.
*/
function wpvulnerability_core_get_vulnerabilities() {
// Initialize variables.
$core_data_cache = null;
$core_data = null;
if ( is_multisite() ) {
// Get the cached core data and decode it for multisite.
$core_data_cache = json_decode( get_site_option( 'wpvulnerability-core-cache' ), true );
$core_data = json_decode( get_site_option( 'wpvulnerability-core' ), true );
} else {
// Get the cached core data and decode it for single site.
$core_data_cache = json_decode( get_option( 'wpvulnerability-core-cache' ), true );
$core_data = json_decode( get_option( 'wpvulnerability-core' ), true );
}
// If the cache is stale or the core data is empty, update the cache.
if ( ( null !== $core_data_cache && $core_data_cache < time() ) || empty( $core_data ) ) {
$core_data = json_decode( wpvulnerability_core_get_installed(), true );
}
// Return the core data with vulnerabilities.
return $core_data;
}
/**
* Update the core cache and remove any old cache data.
*
* @since 2.0.0
*
* @return void
*/
function wpvulnerability_core_get_vulnerabilities_clean() {
wpvulnerability_clear_cache( 'core' );
wpvulnerability_core_get_installed();
}
/**
* Adds vulnerability information after the core version and notices on the update-core.php page.
*
* @since 2.0.0
*
* @return void
*/
function wpvulnerability_core_page() {
// Check if the current page is the update-core.php page.
global $pagenow;
if ( wpvulnerability_analyze_filter( 'core' ) && 'update-core.php' === $pagenow && wpvulnerability_capabilities() ) {
// Get the vulnerabilities for the core.
$core = wpvulnerability_core_get_vulnerabilities();
// If there are vulnerabilities, add an action to display them after the core auto updates settings.
if ( is_array( $core ) && ! empty( $core ) ) {
add_action( 'after_core_auto_updates_settings', 'wpvulnerability_core_info_after' );
}
}
}
// Add notices for vulnerable core on the core page.
add_action( 'admin_head', 'wpvulnerability_core_page' );