Skip to content

Commit 6c7fca8

Browse files
committed
fix(security): fixed sql injection vulnerability in the zero spam admin log table query
1 parent e33ef67 commit 6c7fca8

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

core/admin/tables/class-logtable.php

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -208,79 +208,69 @@ public function prepare_items() {
208208
// @codingStandardsIgnoreLine
209209
$orderby = ! empty( $_REQUEST['orderby'] ) ? sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ) : 'date_recorded';
210210

211-
// @codingStandardsIgnoreLine
212-
$log_type = ! empty( $_REQUEST['type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['type'] ) ) : false;
213-
// @codingStandardsIgnoreLine
214-
$country = ! empty( $_REQUEST['country'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['country'] ) ) : false;
215-
// @codingStandardsIgnoreLine
216-
$user_ip = ! empty( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : false;
211+
$log_type = ! empty( $_REQUEST['type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['type'] ) ) : false;
212+
$country = ! empty( $_REQUEST['country'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['country'] ) ) : false;
213+
$user_ip = ! empty( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : false;
217214

218215
// Define the database table.
219216
$database_table = $wpdb->prefix . \ZeroSpam\Includes\DB::$tables['log'];
220217

221-
// Prepare the select statements.
222-
$select_array = array( '*' );
223-
224218
// Order & add extra select statements.
219+
$order_statement = '';
225220
switch ( $orderby ) {
226221
case 'user_ip':
227-
$order_statement = "ORDER BY user_ip $order";
222+
$order_statement = " ORDER BY user_ip $order";
228223
break;
229224
case 'country':
230-
$order_statement = "ORDER BY country $order";
225+
$order_statement = " ORDER BY country $order";
231226
break;
232227
case 'region':
233-
$order_statement = "ORDER BY country $order";
228+
$order_statement = " ORDER BY country $order";
234229
break;
235230
case 'date_recorded':
236-
$order_statement = "ORDER BY date_recorded $order";
231+
$order_statement = " ORDER BY date_recorded $order";
237232
break;
238233
case 'log_type':
239-
$order_statement = "ORDER BY log_type $order";
234+
$order_statement = " ORDER BY log_type $order";
240235
break;
241236
}
242237

243238
// Where.
244-
$where_array = array();
239+
$where_array = array();
240+
$where_statement = '';
245241

246242
if ( $log_type ) {
247-
$where_array[] = "log_type = '$log_type'";
243+
$where_array[] = "`log_type` = %s";
244+
$database_query_arguments[] = $log_type;
248245
}
249246

250247
if ( $country ) {
251-
$where_array[] = "country = '$country'";
248+
$where_array[] = "`country` = %s";
249+
$database_query_arguments[] = $country;
252250
}
253251

254252
if ( $user_ip ) {
255-
$where_array[] = "user_ip = '$user_ip'";
253+
$where_array[] = "`user_ip` = %s";
254+
$database_query_arguments[] = $user_ip;
256255
}
257256

257+
if ( $where_array ) {
258+
$where_statement .= 'WHERE ';
259+
$where_statement .= implode( ' AND ', $where_array );
260+
}
261+
262+
258263
// Limit.
259264
$limit_statement = "LIMIT $per_page";
260265
if ( $offset ) {
261266
$limit_statement .= ", $offset";
262267
}
263268

264-
// Create the query.
265-
$database_query = 'SELECT ';
266-
267-
$select_statement = implode( ', ', $select_array );
268-
$database_query .= $select_statement . ' ';
269-
270-
$database_query .= "FROM $database_table ";
271-
272-
if ( $where_array ) {
273-
$database_query .= 'WHERE ';
274-
$database_query .= implode( ' AND ', $where_array );
275-
}
276-
277-
if ( ! empty( $order_statement ) ) {
278-
$database_query .= $order_statement . ' ';
279-
}
280-
281-
$database_query .= $limit_statement;
269+
$database_query = $wpdb->prepare(
270+
"SELECT * FROM `$database_table`$where_statement$order_statement $limit_statement",
271+
$database_query_arguments
272+
);
282273

283-
// @codingStandardsIgnoreLine
284274
$data = $wpdb->get_results( $database_query, ARRAY_A );
285275

286276
if ( ! $data ) {
@@ -290,12 +280,6 @@ public function prepare_items() {
290280
// Get total number of rows.
291281
$count_query = "SELECT COUNT(*) FROM $database_table ";
292282

293-
if ( $where_array ) {
294-
$count_query .= 'WHERE ';
295-
$count_query .= implode( ' AND ', $where_array );
296-
}
297-
298-
// @codingStandardsIgnoreLine
299283
$total_items = $wpdb->get_var( $count_query );
300284

301285
$this->set_pagination_args(

readme.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Contributors: bmarshall511
33
Tags: protection, firewall, security, spam, spam blocker
44
Donate link: https://www.zerospam.org/subscribe/
55
Requires at least: 5.2
6-
Tested up to: 6.1.1
6+
Tested up to: 6.2.0
77
Requires PHP: 7.4
8-
Stable tag: 5.4.4
8+
Stable tag: 5.4.5
99
License: GNU GPLv3
1010
License URI: https://choosealicense.com/licenses/gpl-3.0/
1111

@@ -107,6 +107,10 @@ If hosting with Pantheon, see their [known issues page](https://pantheon.io/docs
107107

108108
== Changelog ==
109109

110+
= v5.4.5 =
111+
112+
* fix(security): fixed sql injection vulnerability in the zero spam admin log table query
113+
110114
= v5.4.4 =
111115

112116
* refactor(project honeypot): resolves #344, added additional check & debug info for ip type support

wordpress-zero-spam.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Plugin Name: Zero Spam for WordPress
1414
* Plugin URI: https://www.highfivery.com/projects/zero-spam/
1515
* Description: Tired of all the ineffective WordPress anti-spam & security plugins? Zero Spam for WordPress makes blocking spam &amp; malicious activity a cinch. <strong>Just activate, configure, and say goodbye to spam.</strong>
16-
* Version: 5.4.4
16+
* Version: 5.4.5
1717
* Requires at least: 5.2
1818
* Requires PHP: 7.3
1919
* Author: Highfivery LLC
@@ -31,7 +31,7 @@
3131
define( 'ZEROSPAM', __FILE__ );
3232
define( 'ZEROSPAM_PATH', plugin_dir_path( ZEROSPAM ) );
3333
define( 'ZEROSPAM_PLUGIN_BASE', plugin_basename( ZEROSPAM ) );
34-
define( 'ZEROSPAM_VERSION', '5.4.4' );
34+
define( 'ZEROSPAM_VERSION', '5.4.5' );
3535

3636
if ( defined( 'ZEROSPAM_DEVELOPMENT_URL' ) ) {
3737
define( 'ZEROSPAM_URL', ZEROSPAM_DEVELOPMENT_URL );

0 commit comments

Comments
 (0)