PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
← All changes | src/component/class-blacklist-lockout.php +65 -27 5.8.1 → 6.3.0 View file →
@@ -13,9 +13,9 @@
13 13 use Calotes\Helper\Array_Cache;
14 14 use WP_Defender\Traits\Country;
15 15 use WP_Defender\Traits\Continent;
16 16 use WP_Defender\Model\Lockout_Log;
17 -use MaxMind\Db\Reader\InvalidDatabaseException;
17 +use WP_DEFENDER_VENDOR\MaxMind\Db\Reader\InvalidDatabaseException;
18 18 use WP_Defender\Integrations\MaxMind_Geolocation;
19 19 use WP_Defender\Model\Setting\Blacklist_Lockout as Model_Blacklist_Lockout;
20 20 use WP_Defender\Integrations\Main_Wp;
21 21 use WP_Filesystem_Base;
@@ -75,9 +75,9 @@
75 75 return false;
76 76 }
77 77 $model = new Model_Blacklist_Lockout();
78 78 $whitelist = $model->get_country_whitelist();
79 - if ( ! is_array( $whitelist ) || array() === $whitelist ) {
79 + if ( array() === $whitelist ) {
80 80 return false;
81 81 }
82 82
83 83 if ( isset( $country['iso'] ) && '' !== $country['iso'] ) {
@@ -82,19 +82,11 @@
82 82
83 83 if ( isset( $country['iso'] ) && '' !== $country['iso'] ) {
84 84 $country_iso = strtoupper( $country['iso'] );
85 85
86 - // Check if the specific country is in the whitelist.
87 - if ( in_array( $country_iso, $whitelist, true ) ) {
86 + if ( $this->is_country_iso_in_list( $country_iso, $whitelist ) ) {
88 87 return true;
89 88 }
90 -
91 - // Check if any continent containing this country is in the whitelist.
92 - foreach ( $whitelist as $allowed_code ) {
93 - if ( $this->is_country_in_continent( $country_iso, $allowed_code ) ) {
94 - return true;
95 - }
96 - }
97 89 }
98 90
99 91 return false;
100 92 }
@@ -107,14 +99,15 @@
107 99 private function get_default_ip_whitelisted(): array {
108 100 $server_addr = defender_get_data_from_request( 'SERVER_ADDR', 's' );
109 101 $remote_addr = defender_get_data_from_request( 'REMOTE_ADDR', 's' );
110 102 $ips = array(
111 - ...$this->fetch_latest_ips(),
103 + ...self::fetch_latest_ips(),
112 104 '127.0.0.1',
113 - isset( $server_addr ) ? $server_addr : $remote_addr,
105 + $server_addr ?? $remote_addr,
114 106 );
115 107
116 - return (array) apply_filters( 'ip_lockout_default_whitelist_ip', $ips );
108 + $whitelist_ips = apply_filters( 'ip_lockout_default_whitelist_ip', $ips );
109 + return is_array( $whitelist_ips ) ? $whitelist_ips : (array) $whitelist_ips;
117 110 }
118 111
119 112 /**
120 113 * Fetch the latest IP list from the API.
@@ -234,19 +227,11 @@
234 227
235 228 if ( isset( $country['iso'] ) && '' !== $country['iso'] ) {
236 229 $country_iso = strtoupper( $country['iso'] );
237 230
238 - // Check if the specific country is in the blacklist.
239 - if ( in_array( $country_iso, $blacklisted, true ) ) {
231 + if ( $this->is_country_iso_in_list( $country_iso, $blacklisted ) ) {
240 232 return true;
241 233 }
242 -
243 - // Check if any continent containing this country is in the blacklist.
244 - foreach ( $blacklisted as $blocked_code ) {
245 - if ( $this->is_country_in_continent( $country_iso, $blocked_code ) ) {
246 - return true;
247 - }
248 - }
249 234 }
250 235
251 236 return false;
252 237 }
@@ -311,9 +296,9 @@
311 296 public function is_geodb_downloaded(): bool {
312 297 $model = new Model_Blacklist_Lockout();
313 298 $license_key = $model->maxmind_license_key;
314 299 // Using Geo DB without a license key is not advisable.
315 - if ( '' === $license_key && is_file( $model->geodb_path ) ) {
300 + if ( '' === $license_key && is_string( $model->geodb_path ) && is_file( $model->geodb_path ) ) {
316 301 $service_geo = wd_di()->get( MaxMind_Geolocation::class );
317 302 $service_geo->delete_database();
318 303 $model->geodb_path = '';
319 304 $model->save();
@@ -323,9 +308,9 @@
323 308
324 309 // Likely the case after the config import with the existed MaxMind license key.
325 310 if (
326 311 '' !== $license_key
327 - && ( is_null( $model->geodb_path ) || ! is_file( $model->geodb_path ) )
312 + && ( ! is_string( $model->geodb_path ) || ! is_file( $model->geodb_path ) )
328 313 ) {
329 314 $service_geo = wd_di()->get( MaxMind_Geolocation::class );
330 315 $tmp = $service_geo->get_downloaded_url( $license_key );
331 316 if ( ! is_wp_error( $tmp ) ) {
@@ -362,9 +347,9 @@
362 347 }
363 348 }
364 349
365 350 // Check again.
366 - if ( is_null( $model->geodb_path ) || ! is_file( $model->geodb_path ) ) {
351 + if ( ! is_string( $model->geodb_path ) || ! is_file( $model->geodb_path ) ) {
367 352 return false;
368 353 }
369 354
370 355 // Check if the file exists on the site. The file can exist on the same server but for different sites.
@@ -380,9 +365,9 @@
380 365
381 366 $rel_path = $abs_path . DIRECTORY_SEPARATOR . $path_parts['basename'];
382 367 if ( file_exists( $rel_path ) ) {
383 368 return true;
384 - } elseif ( ! is_null( $model->geodb_path ) && '' !== $model->geodb_path && file_exists( $model->geodb_path ) ) {
369 + } elseif ( '' !== $model->geodb_path && file_exists( $model->geodb_path ) ) {
385 370 // The case if ABSPATH was changed e.g. in wp-config.php.
386 371 return true;
387 372 }
388 373
@@ -469,8 +454,9 @@
469 454 "SELECT country_iso_code, COUNT(ip) AS ip_count FROM {$wpdb->base_prefix}defender_lockout_log WHERE (type = %s OR type = %s OR type = %s) AND date >= %d AND country_iso_code IS NOT NULL GROUP BY country_iso_code ORDER BY ip_count DESC LIMIT %d",
470 455 Lockout_Log::LOCKOUT_404,
471 456 Lockout_Log::AUTH_LOCK,
472 457 Lockout_Log::LOCKOUT_UA,
458 + // todo: add other 404- and UA-lockouts.
473 459 strtotime( '-' . $max_age_days . ' days', time() ),
474 460 $limit
475 461 ),
476 462 ARRAY_A
@@ -515,6 +501,58 @@
515 501 }
516 502 }
517 503
518 504 return true;
505 + }
506 +
507 + /**
508 + * Check whether a code identifies a continent without also identifying a country.
509 + *
510 + * @param string $code The code to check.
511 + * @return bool True when the code identifies only a continent.
512 + */
513 + private function is_continent_code( $code ): bool {
514 + $countries_with_continents = $this->get_countries_with_continents();
515 +
516 + if ( ! isset( $countries_with_continents[ $code ] ) ) {
517 + return false;
518 + }
519 +
520 + foreach ( $countries_with_continents as $continent ) {
521 + if ( ! isset( $continent['area'] ) ) {
522 + continue;
523 + }
524 +
525 + foreach ( $continent['area'] as $area ) {
526 + if ( isset( $area['countries'][ $code ] ) ) {
527 + return false;
528 + }
529 + }
530 + }
531 +
532 + return true;
533 + }
534 +
535 + /**
536 + * Checks whether a country ISO code (or any continent it belongs to) is present in the given list.
537 + *
538 + * @param string $country_iso The upcased two-letter ISO country code.
539 + * @param array $iso_list The list of country/continent codes to check against.
540 + *
541 + * @return bool True if the country or one of its continents is found in the list.
542 + */
543 + private function is_country_iso_in_list( string $country_iso, array $iso_list ): bool {
544 + // Check if the specific country is in the list.
545 + if ( in_array( $country_iso, $iso_list, true ) ) {
546 + return true;
547 + }
548 +
549 + // Check if any continent containing this country is in the list.
550 + foreach ( $iso_list as $code ) {
551 + if ( $this->is_continent_code( $code ) && $this->is_country_in_continent( $country_iso, $code ) ) {
552 + return true;
553 + }
554 + }
555 +
556 + return false;
519 557 }
520 558 }