Completed
Push — update/import-sync-detection ( 0bf98c...8808a0 )
by
unknown
25:48 queued 17:49
created

Jetpack_Protect_Module::configuration_load()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 6
nop 0
dl 0
loc 23
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Protect
4
 * Module Description: Protect yourself from brute force and distributed brute force attacks, which are the most common way for hackers to get into your site.
5
 * Sort Order: 1
6
 * Recommendation Order: 4
7
 * First Introduced: 3.4
8
 * Requires Connection: Yes
9
 * Auto Activate: Yes
10
 * Module Tags: Recommended
11
 * Feature: Security
12
 * Additional Search Queries: security, jetpack protect, secure, protection, botnet, brute force, protect, login, bot, password, passwords, strong passwords, strong password, wp-login.php,  protect admin
13
 */
14
15
include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
16
17
class Jetpack_Protect_Module {
18
19
	private static $__instance = null;
20
	public $api_key;
21
	public $api_key_error;
22
	public $whitelist;
23
	public $whitelist_error;
24
	public $whitelist_saved;
25
	private $user_ip;
26
	private $local_host;
27
	private $api_endpoint;
28
	public $last_request;
29
	public $last_response_raw;
30
	public $last_response;
31
	private $block_login_with_math;
32
33
	/**
34
	 * Singleton implementation
35
	 *
36
	 * @return object
37
	 */
38
	public static function instance() {
39
		if ( ! is_a( self::$__instance, 'Jetpack_Protect_Module' ) ) {
40
			self::$__instance = new Jetpack_Protect_Module();
41
		}
42
43
		return self::$__instance;
44
	}
45
46
	/**
47
	 * Registers actions
48
	 */
49
	private function __construct() {
50
		add_action( 'jetpack_activate_module_protect', array ( $this, 'on_activation' ) );
51
		add_action( 'jetpack_deactivate_module_protect', array ( $this, 'on_deactivation' ) );
52
		add_action( 'jetpack_modules_loaded', array ( $this, 'modules_loaded' ) );
53
		add_action( 'login_form', array ( $this, 'check_use_math' ), 0 );
54
		add_filter( 'authenticate', array ( $this, 'check_preauth' ), 10, 3 );
55
		add_action( 'wp_login', array ( $this, 'log_successful_login' ), 10, 2 );
56
		add_action( 'wp_login_failed', array ( $this, 'log_failed_attempt' ) );
57
		add_action( 'admin_init', array ( $this, 'maybe_update_headers' ) );
58
		add_action( 'admin_init', array ( $this, 'maybe_display_security_warning' ) );
59
60
		// This is a backup in case $pagenow fails for some reason
61
		add_action( 'login_form', array ( $this, 'check_login_ability' ), 1 );
62
63
		// Load math fallback after math page form submission
64
		if ( isset( $_POST[ 'jetpack_protect_process_math_form' ] ) ) {
65
			include_once dirname( __FILE__ ) . '/protect/math-fallback.php';
66
			new Jetpack_Protect_Math_Authenticate;
67
		}
68
69
		// Runs a script every day to clean up expired transients so they don't
70
		// clog up our users' databases
71
		require_once( JETPACK__PLUGIN_DIR . '/modules/protect/transient-cleanup.php' );
72
	}
73
74
	/**
75
	 * On module activation, try to get an api key
76
	 */
77
	public function on_activation() {
78
		if ( is_multisite() && is_main_site() && get_site_option( 'jetpack_protect_active', 0 ) == 0 ) {
79
			update_site_option( 'jetpack_protect_active', 1 );
80
		}
81
82
		update_site_option( 'jetpack_protect_activating', 'activating' );
83
84
		// Get BruteProtect's counter number
85
		Jetpack_Protect_Module::protect_call( 'check_key' );
86
	}
87
88
	/**
89
	 * On module deactivation, unset protect_active
90
	 */
91
	public function on_deactivation() {
92
		if ( is_multisite() && is_main_site() ) {
93
			update_site_option( 'jetpack_protect_active', 0 );
94
		}
95
	}
96
97
	public function maybe_get_protect_key() {
98
		if ( get_site_option( 'jetpack_protect_activating', false ) && ! get_site_option( 'jetpack_protect_key', false ) ) {
99
			$key = $this->get_protect_key();
100
			delete_site_option( 'jetpack_protect_activating' );
101
			return $key;
102
		}
103
104
		return get_site_option( 'jetpack_protect_key' );
105
	}
106
107
	/**
108
	 * Sends a "check_key" API call once a day.  This call allows us to track IP-related
109
	 * headers for this server via the Protect API, in order to better identify the source
110
	 * IP for login attempts
111
	 */
112
	public function maybe_update_headers( $force = false ) {
113
		$updated_recently = $this->get_transient( 'jpp_headers_updated_recently' );
114
115
		if ( ! $force ) {
116
			if ( isset( $_GET['protect_update_headers'] ) ) {
117
				$force = true;
118
			}
119
		}
120
121
		// check that current user is admin so we prevent a lower level user from adding
122
		// a trusted header, allowing them to brute force an admin account
123
		if ( ( $updated_recently && ! $force ) || ! current_user_can( 'update_plugins' ) ) {
124
			return;
125
		}
126
127
		$response = Jetpack_Protect_Module::protect_call( 'check_key' );
128
		$this->set_transient( 'jpp_headers_updated_recently', 1, DAY_IN_SECONDS );
129
130
		if ( isset( $response['msg'] ) && $response['msg'] ) {
131
			update_site_option( 'trusted_ip_header', json_decode( $response['msg'] ) );
132
		}
133
134
	}
135
136
	public function maybe_display_security_warning() {
137
		if ( is_multisite() && current_user_can( 'manage_network' ) ) {
138
			if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
139
				require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
140
			}
141
142
			if ( ! ( is_plugin_active_for_network( 'jetpack/jetpack.php' ) || is_plugin_active_for_network( 'jetpack-dev/jetpack.php' ) ) ) {
143
				add_action( 'load-index.php', array ( $this, 'prepare_jetpack_protect_multisite_notice' ) );
144
			}
145
		}
146
	}
147
148
	public function prepare_jetpack_protect_multisite_notice() {
149
		add_action( 'admin_print_styles', array ( $this, 'admin_banner_styles' ) );
150
		add_action( 'admin_notices', array ( $this, 'admin_jetpack_manage_notice' ) );
151
	}
152
153
	public function admin_banner_styles() {
154
		global $wp_styles;
155
156
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
157
158
		wp_enqueue_style( 'jetpack', plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION );
159
		$wp_styles->add_data( 'jetpack', 'rtl', true );
160
	}
161
162
	public function admin_jetpack_manage_notice() {
163
164
		$dismissed = get_site_option( 'jetpack_dismissed_protect_multisite_banner' );
165
166
		if ( $dismissed ) {
167
			return;
168
		}
169
170
		$referer     = '&_wp_http_referer=' . add_query_arg( '_wp_http_referer', null );
171
		$opt_out_url = wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-protect-multisite-opt-out' . $referer ), 'jetpack_protect_multisite_banner_opt_out' );
172
173
		?>
174
		<div id="message" class="updated jetpack-message jp-banner is-opt-in protect-error"
175
		     style="display:block !important;">
176
			<a class="jp-banner__dismiss" href="<?php echo esc_url( $opt_out_url ); ?>"
177
			   title="<?php esc_attr_e( 'Dismiss this notice.', 'jetpack' ); ?>"></a>
178
179
			<div class="jp-banner__content">
180
				<h2><?php esc_html_e( 'Protect cannot keep your site secure.', 'jetpack' ); ?></h2>
181
182
				<p><?php printf( __( 'Thanks for activating Protect! To start protecting your site, please network activate Jetpack on your Multisite installation and activate Protect on your primary site. Due to the way logins are handled on WordPress Multisite, Jetpack must be network-enabled in order for Protect to work properly. <a href="%s" target="_blank">Learn More</a>', 'jetpack' ), 'http://jetpack.com/support/multisite-protect' ); ?></p>
183
			</div>
184
			<div class="jp-banner__action-container is-opt-in">
185
				<a href="<?php echo esc_url( network_admin_url( 'plugins.php' ) ); ?>" class="jp-banner__button"
186
				   id="wpcom-connect"><?php _e( 'View Network Admin', 'jetpack' ); ?></a>
187
			</div>
188
		</div>
189
		<?php
190
	}
191
192
	/**
193
	 * Request an api key from wordpress.com
194
	 *
195
	 * @return bool | string
196
	 */
197
	public function get_protect_key() {
198
199
		$protect_blog_id = Jetpack_Protect_Module::get_main_blog_jetpack_id();
200
201
		// If we can't find the the blog id, that means we are on multisite, and the main site never connected
202
		// the protect api key is linked to the main blog id - instruct the user to connect their main blog
203
		if ( ! $protect_blog_id ) {
204
			$this->api_key_error = __( 'Your main blog is not connected to WordPress.com. Please connect to get an API key.', 'jetpack' );
205
206
			return false;
207
		}
208
209
		$request = array (
210
			'jetpack_blog_id'      => $protect_blog_id,
211
			'bruteprotect_api_key' => get_site_option( 'bruteprotect_api_key' ),
212
			'multisite'            => '0',
213
		);
214
215
		// Send the number of blogs on the network if we are on multisite
216
		if ( is_multisite() ) {
217
			$request['multisite'] = get_blog_count();
218
			if ( ! $request['multisite'] ) {
219
				global $wpdb;
220
				$request['multisite'] = $wpdb->get_var( "SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0'" );
221
			}
222
		}
223
224
		// Request the key
225
		Jetpack::load_xml_rpc_client();
226
		$xml = new Jetpack_IXR_Client( array (
227
			'user_id' => get_current_user_id()
228
		) );
229
		$xml->query( 'jetpack.protect.requestKey', $request );
230
231
		// Hmm, can't talk to wordpress.com
232
		if ( $xml->isError() ) {
233
			$code                = $xml->getErrorCode();
234
			$message             = $xml->getErrorMessage();
235
			$this->api_key_error = sprintf( __( 'Error connecting to WordPress.com. Code: %1$s, %2$s', 'jetpack' ), $code, $message );
236
237
			return false;
238
		}
239
240
		$response = $xml->getResponse();
241
242
		// Hmm. Can't talk to the protect servers ( api.bruteprotect.com )
243
		if ( ! isset( $response['data'] ) ) {
244
			$this->api_key_error = __( 'No reply from Jetpack servers', 'jetpack' );
245
246
			return false;
247
		}
248
249
		// There was an issue generating the key
250
		if ( empty( $response['success'] ) ) {
251
			$this->api_key_error = $response['data'];
252
253
			return false;
254
		}
255
256
		// Key generation successful!
257
		$active_plugins = Jetpack::get_active_plugins();
258
259
		// We only want to deactivate BruteProtect if we successfully get a key
260
		if ( in_array( 'bruteprotect/bruteprotect.php', $active_plugins ) ) {
261
			Jetpack_Client_Server::deactivate_plugin( 'bruteprotect/bruteprotect.php', 'BruteProtect' );
262
		}
263
264
		$key = $response['data'];
265
		update_site_option( 'jetpack_protect_key', $key );
266
267
		return $key;
268
	}
269
270
	/**
271
	 * Called via WP action wp_login_failed to log failed attempt with the api
272
	 *
273
	 * Fires custom, plugable action jpp_log_failed_attempt with the IP
274
	 *
275
	 * @return void
276
	 */
277
	function log_failed_attempt( $login_user = null ) {
278
279
		/**
280
		 * Fires before every failed login attempt.
281
		 *
282
		 * @module protect
283
		 *
284
		 * @since 3.4.0
285
		 *
286
		 * @param array Information about failed login attempt
287
		 *   [
288
		 *     'login' => (string) Username or email used in failed login attempt
289
		 *   ]
290
		 */
291
		do_action( 'jpp_log_failed_attempt', array( 'login' => $login_user ) );
292
293
		if ( isset( $_COOKIE['jpp_math_pass'] ) ) {
294
295
			$transient = $this->get_transient( 'jpp_math_pass_' . $_COOKIE['jpp_math_pass'] );
296
			$transient--;
297
298
			if ( ! $transient || $transient < 1 ) {
299
				$this->delete_transient( 'jpp_math_pass_' . $_COOKIE['jpp_math_pass'] );
300
				setcookie( 'jpp_math_pass', 0, time() - DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false );
301
			} else {
302
				$this->set_transient( 'jpp_math_pass_' . $_COOKIE['jpp_math_pass'], $transient, DAY_IN_SECONDS );
303
			}
304
305
		}
306
		$this->protect_call( 'failed_attempt' );
307
	}
308
309
	/**
310
	 * Set up the Protect configuration page
311
	 */
312
	public function modules_loaded() {
313
		Jetpack::enable_module_configurable( __FILE__ );
314
		Jetpack::module_configuration_load( __FILE__, array ( $this, 'configuration_load' ) );
315
		Jetpack::module_configuration_head( __FILE__, array ( $this, 'configuration_head' ) );
316
		Jetpack::module_configuration_screen( __FILE__, array ( $this, 'configuration_screen' ) );
317
	}
318
319
	/**
320
	 * Logs a successful login back to our servers, this allows us to make sure we're not blocking
321
	 * a busy IP that has a lot of good logins along with some forgotten passwords. Also saves current user's ip
322
	 * to the ip address whitelist
323
	 */
324
	public function log_successful_login( $user_login, $user = null ) {
325
		if ( ! $user ) { // For do_action( 'wp_login' ) calls that lacked passing the 2nd arg.
326
			$user = get_user_by( 'login', $user_login );
327
		}
328
329
		$this->protect_call( 'successful_login', array ( 'roles' => $user->roles ) );
330
	}
331
332
333
	/**
334
	 * Checks for loginability BEFORE authentication so that bots don't get to go around the log in form.
335
	 *
336
	 * If we are using our math fallback, authenticate via math-fallback.php
337
	 *
338
	 * @param string $user
339
	 * @param string $username
340
	 * @param string $password
341
	 *
342
	 * @return string $user
343
	 */
344
	function check_preauth( $user = 'Not Used By Protect', $username = 'Not Used By Protect', $password = 'Not Used By Protect' ) {
345
		$allow_login = $this->check_login_ability( true );
346
		$use_math    = $this->get_transient( 'brute_use_math' );
347
348
		if ( ! $allow_login ) {
349
			$this->block_with_math();
350
		}
351
352
		if ( ( 1 == $use_math || 1 == $this->block_login_with_math ) && isset( $_POST['log'] ) ) {
353
			include_once dirname( __FILE__ ) . '/protect/math-fallback.php';
354
			Jetpack_Protect_Math_Authenticate::math_authenticate();
355
		}
356
357
		return $user;
358
	}
359
360
	/**
361
	 * Get all IP headers so that we can process on our server...
362
	 *
363
	 * @return string
364
	 */
365
	function get_headers() {
366
		$ip_related_headers = array (
367
			'GD_PHP_HANDLER',
368
			'HTTP_AKAMAI_ORIGIN_HOP',
369
			'HTTP_CF_CONNECTING_IP',
370
			'HTTP_CLIENT_IP',
371
			'HTTP_FASTLY_CLIENT_IP',
372
			'HTTP_FORWARDED',
373
			'HTTP_FORWARDED_FOR',
374
			'HTTP_INCAP_CLIENT_IP',
375
			'HTTP_TRUE_CLIENT_IP',
376
			'HTTP_X_CLIENTIP',
377
			'HTTP_X_CLUSTER_CLIENT_IP',
378
			'HTTP_X_FORWARDED',
379
			'HTTP_X_FORWARDED_FOR',
380
			'HTTP_X_IP_TRAIL',
381
			'HTTP_X_REAL_IP',
382
			'HTTP_X_VARNISH',
383
			'REMOTE_ADDR'
384
		);
385
386
		foreach ( $ip_related_headers as $header ) {
387
			if ( isset( $_SERVER[ $header ] ) ) {
388
				$output[ $header ] = $_SERVER[ $header ];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
389
			}
390
		}
391
392
		return $output;
0 ignored issues
show
Bug introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
393
	}
394
395
	/*
396
	 * Checks if the IP address has been whitelisted
397
	 *
398
	 * @param string $ip
399
	 *
400
	 * @return bool
401
	 */
402
	function ip_is_whitelisted( $ip ) {
403
		// If we found an exact match in wp-config
404
		if ( defined( 'JETPACK_IP_ADDRESS_OK' ) && JETPACK_IP_ADDRESS_OK == $ip ) {
405
			return true;
406
		}
407
408
		$whitelist = jetpack_protect_get_local_whitelist();
409
410
		if ( is_multisite() ) {
411
			$whitelist = array_merge( $whitelist, get_site_option( 'jetpack_protect_global_whitelist', array () ) );
412
		}
413
414
		if ( ! empty( $whitelist ) ) :
415
			foreach ( $whitelist as $item ) :
416
				// If the IPs are an exact match
417
				if ( ! $item->range && isset( $item->ip_address ) && $item->ip_address == $ip ) {
418
					return true;
419
				}
420
421
				if ( $item->range && isset( $item->range_low ) && isset( $item->range_high ) ) {
422
					if ( jetpack_protect_ip_address_is_in_range( $ip, $item->range_low, $item->range_high ) ) {
423
						return true;
424
					}
425
				}
426
			endforeach;
427
		endif;
428
429
		return false;
430
	}
431
432
	/**
433
	 * Checks the status for a given IP. API results are cached as transients
434
	 *
435
	 * @param bool $preauth Whether or not we are checking prior to authorization
436
	 *
437
	 * @return bool Either returns true, fires $this->kill_login, or includes a math fallback and returns false
438
	 */
439
	function check_login_ability( $preauth = false ) {
440
441
		/**
442
		 * JETPACK_ALWAYS_PROTECT_LOGIN will always disable the login page, and use a page provided by Jetpack.
443
		 */
444
		if ( Jetpack_Constants::is_true( 'JETPACK_ALWAYS_PROTECT_LOGIN' ) ) {
445
			$this->kill_login();
446
		}
447
448
		if ( $this->is_current_ip_whitelisted() ) {
449
		    return true;
450
        }
451
452
		$status = $this->get_cached_status();
453
454
		if ( empty( $status ) ) {
455
			// If we've reached this point, this means that the IP isn't cached.
456
			// Now we check with the Protect API to see if we should allow login
457
			$response = $this->protect_call( $action = 'check_ip' );
458
459
			if ( isset( $response['math'] ) && ! function_exists( 'brute_math_authenticate' ) ) {
460
				include_once dirname( __FILE__ ) . '/protect/math-fallback.php';
461
				new Jetpack_Protect_Math_Authenticate;
462
463
				return false;
464
			}
465
466
			$status = $response['status'];
467
		}
468
469
		if ( 'blocked' == $status ) {
470
			$this->block_with_math();
471
		}
472
473
		if ( 'blocked-hard' == $status ) {
474
			$this->kill_login();
475
		}
476
477
		return true;
478
	}
479
480
	function is_current_ip_whitelisted() {
481
		$ip = jetpack_protect_get_ip();
482
483
		// Server is misconfigured and we can't get an IP
484
		if ( ! $ip && class_exists( 'Jetpack' ) ) {
485
			Jetpack::deactivate_module( 'protect' );
486
			ob_start();
487
			Jetpack::state( 'message', 'protect_misconfigured_ip' );
488
			ob_end_clean();
489
			return true;
490
		}
491
492
		/**
493
		 * Short-circuit check_login_ability.
494
		 *
495
		 * If there is an alternate way to validate the current IP such as
496
		 * a hard-coded list of IP addresses, we can short-circuit the rest
497
		 * of the login ability checks and return true here.
498
		 *
499
		 * @module protect
500
		 *
501
		 * @since 4.4.0
502
		 *
503
		 * @param bool false Should we allow all logins for the current ip? Default: false
504
		 */
505
		if ( apply_filters( 'jpp_allow_login', false, $ip ) ) {
506
			return true;
507
		}
508
509
		if ( jetpack_protect_ip_is_private( $ip ) ) {
510
			return true;
511
		}
512
513
		if ( $this->ip_is_whitelisted( $ip ) ) {
514
			return true;
515
		}
516
    }
517
518
    function has_login_ability() {
519
	    if ( $this->is_current_ip_whitelisted() ) {
520
		    return true;
521
	    }
522
	    $status = $this->get_cached_status();
523
	    if ( empty( $status ) || $status === 'ok' ) {
524
	        return true;
525
        }
526
        return false;
527
    }
528
529
	function get_cached_status() {
530
		$transient_name  = $this->get_transient_name();
531
		$value = $this->get_transient( $transient_name );
532
		if ( isset( $value['status'] ) ) {
533
		    return $value['status'];
534
        }
535
        return '';
536
	}
537
538
	function block_with_math() {
539
		/**
540
		 * By default, Protect will allow a user who has been blocked for too
541
		 * many failed logins to start answering math questions to continue logging in
542
		 *
543
		 * For added security, you can disable this.
544
		 *
545
		 * @module protect
546
		 *
547
		 * @since 3.6.0
548
		 *
549
		 * @param bool Whether to allow math for blocked users or not.
550
		 */
551
552
		$this->block_login_with_math = 1;
553
		/**
554
		 * Allow Math fallback for blocked IPs.
555
		 *
556
		 * @module protect
557
		 *
558
		 * @since 3.6.0
559
		 *
560
		 * @param bool true Should we fallback to the Math questions when an IP is blocked. Default to true.
561
		 */
562
		$allow_math_fallback_on_fail = apply_filters( 'jpp_use_captcha_when_blocked', true );
563
		if ( ! $allow_math_fallback_on_fail  ) {
564
			$this->kill_login();
565
		}
566
		include_once dirname( __FILE__ ) . '/protect/math-fallback.php';
567
		new Jetpack_Protect_Math_Authenticate;
568
569
		return false;
570
	}
571
572
	/*
573
	 * Kill a login attempt
574
	 */
575
	function kill_login() {
576
		if (
577
			isset( $_GET['action'], $_GET['_wpnonce'] ) &&
578
			'logout' === $_GET['action'] &&
579
			wp_verify_nonce( $_GET['_wpnonce'], 'log-out' ) &&
580
			wp_get_current_user()
581
582
		) {
583
			// Allow users to logout
584
			return;
585
		}
586
587
		$ip = jetpack_protect_get_ip();
588
		/**
589
		 * Fires before every killed login.
590
		 *
591
		 * @module protect
592
		 *
593
		 * @since 3.4.0
594
		 *
595
		 * @param string $ip IP flagged by Protect.
596
		 */
597
		do_action( 'jpp_kill_login', $ip );
598
599
		if( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
600
			$die_string = sprintf( __( 'Your IP (%1$s) has been flagged for potential security violations.', 'jetpack' ), str_replace( 'http://', '', esc_url( 'http://' . $ip ) ) );
601
			wp_die(
602
				$die_string,
603
				__( 'Login Blocked by Jetpack', 'jetpack' ),
604
				array ( 'response' => 403 )
605
			);
606
		}
607
608
		require_once dirname( __FILE__ ) . '/protect/blocked-login-page.php';
609
		$blocked_login_page = Jetpack_Protect_Blocked_Login_Page::instance( $ip );
610
611
		if ( $blocked_login_page->is_blocked_user_valid() ) {
612
			return;
613
		}
614
615
		$blocked_login_page->render_and_die();
616
	}
617
618
	/*
619
	 * Checks if the protect API call has failed, and if so initiates the math captcha fallback.
620
	 */
621
	public function check_use_math() {
622
		$use_math = $this->get_transient( 'brute_use_math' );
623
		if ( $use_math ) {
624
			include_once dirname( __FILE__ ) . '/protect/math-fallback.php';
625
			new Jetpack_Protect_Math_Authenticate;
626
		}
627
	}
628
629
	/**
630
	 * Get or delete API key
631
	 */
632
	public function configuration_load() {
633
634
		if ( isset( $_POST['action'] ) && $_POST['action'] == 'jetpack_protect_save_whitelist' && wp_verify_nonce( $_POST['_wpnonce'], 'jetpack-protect' ) ) {
635
			$whitelist             = str_replace( ' ', '', $_POST['whitelist'] );
636
			$whitelist             = explode( PHP_EOL, $whitelist );
637
			$result                = jetpack_protect_save_whitelist( $whitelist );
638
			$this->whitelist_saved = ! is_wp_error( $result );
639
			$this->whitelist_error = is_wp_error( $result );
640
		}
641
642
		if ( isset( $_POST['action'] ) && 'get_protect_key' == $_POST['action'] && wp_verify_nonce( $_POST['_wpnonce'], 'jetpack-protect' ) ) {
643
			$result = $this->get_protect_key();
644
			// Only redirect on success
645
			// If it fails we need access to $this->api_key_error
646
			if ( $result ) {
647
				wp_safe_redirect( Jetpack::module_configuration_url( 'protect' ) );
648
				exit;
649
			}
650
		}
651
652
		$this->api_key = get_site_option( 'jetpack_protect_key', false );
653
		$this->user_ip = jetpack_protect_get_ip();
654
	}
655
656
	public function configuration_head() {
657
		wp_enqueue_style( 'jetpack-protect' );
658
	}
659
660
	/**
661
	 * Prints the configuration screen
662
	 */
663
	public function configuration_screen() {
664
		require_once dirname( __FILE__ ) . '/protect/config-ui.php';
665
	}
666
667
	/**
668
	 * If we're in a multisite network, return the blog ID of the primary blog
669
	 *
670
	 * @return int
671
	 */
672
	public function get_main_blog_id() {
673
		if ( ! is_multisite() ) {
674
			return false;
675
		}
676
677
		global $current_site;
678
		$primary_blog_id = $current_site->blog_id;
679
680
		return $primary_blog_id;
681
	}
682
683
	/**
684
	 * Get jetpack blog id, or the jetpack blog id of the main blog in the main network
685
	 *
686
	 * @return int
687
	 */
688
	public function get_main_blog_jetpack_id() {
689
		if ( ! is_main_site() ) {
690
			switch_to_blog( $this->get_main_blog_id() );
691
			$id = Jetpack::get_option( 'id', false );
692
			restore_current_blog();
693
		} else {
694
			$id = Jetpack::get_option( 'id' );
695
		}
696
697
		return $id;
698
	}
699
700
	public function check_api_key() {
701
		$response = $this->protect_call( 'check_key' );
702
703
		if ( isset( $response['ckval'] ) ) {
704
			return true;
705
		}
706
707
		if ( isset( $response['error'] ) ) {
708
709
			if ( $response['error'] == 'Invalid API Key' ) {
710
				$this->api_key_error = __( 'Your API key is invalid', 'jetpack' );
711
			}
712
713
			if ( $response['error'] == 'API Key Required' ) {
714
				$this->api_key_error = __( 'No API key', 'jetpack' );
715
			}
716
		}
717
718
		$this->api_key_error = __( 'There was an error contacting Jetpack servers.', 'jetpack' );
719
720
		return false;
721
	}
722
723
	/**
724
	 * Calls over to the api using wp_remote_post
725
	 *
726
	 * @param string $action 'check_ip', 'check_key', or 'failed_attempt'
727
	 * @param array $request Any custom data to post to the api
728
	 *
729
	 * @return array
730
	 */
731
	function protect_call( $action = 'check_ip', $request = array () ) {
732
		global $wp_version;
733
734
		$api_key = $this->maybe_get_protect_key();
735
736
		$user_agent = "WordPress/{$wp_version} | Jetpack/" . constant( 'JETPACK__VERSION' );
737
738
		$request['action']            = $action;
739
		$request['ip']                = jetpack_protect_get_ip();
740
		$request['host']              = $this->get_local_host();
741
		$request['headers']           = json_encode( $this->get_headers() );
742
		$request['jetpack_version']   = constant( 'JETPACK__VERSION' );
743
		$request['wordpress_version'] = strval( $wp_version );
744
		$request['api_key']           = $api_key;
745
		$request['multisite']         = "0";
746
747
		if ( is_multisite() ) {
748
			$request['multisite'] = get_blog_count();
749
		}
750
751
752
		/**
753
		 * Filter controls maximum timeout in waiting for reponse from Protect servers.
754
		 *
755
		 * @module protect
756
		 *
757
		 * @since 4.0.4
758
		 *
759
		 * @param int $timeout Max time (in seconds) to wait for a response.
760
		 */
761
		$timeout = apply_filters( 'jetpack_protect_connect_timeout', 30 );
762
763
		$args = array (
764
			'body'        => $request,
765
			'user-agent'  => $user_agent,
766
			'httpversion' => '1.0',
767
			'timeout'     => absint( $timeout )
768
		);
769
770
		$response_json           = wp_remote_post( $this->get_api_host(), $args );
771
		$this->last_response_raw = $response_json;
772
773
		$transient_name = $this->get_transient_name();
774
		$this->delete_transient( $transient_name );
775
776
		if ( is_array( $response_json ) ) {
777
			$response = json_decode( $response_json['body'], true );
778
		}
779
780
		if ( isset( $response['blocked_attempts'] ) && $response['blocked_attempts'] ) {
781
			update_site_option( 'jetpack_protect_blocked_attempts', $response['blocked_attempts'] );
782
		}
783
784
		if ( isset( $response['status'] ) && ! isset( $response['error'] ) ) {
785
			$response['expire'] = time() + $response['seconds_remaining'];
786
			$this->set_transient( $transient_name, $response, $response['seconds_remaining'] );
787
			$this->delete_transient( 'brute_use_math' );
788
		} else { // Fallback to Math Captcha if no response from API host
789
			$this->set_transient( 'brute_use_math', 1, 600 );
790
			$response['status'] = 'ok';
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
791
			$response['math']   = true;
792
		}
793
794
		if ( isset( $response['error'] ) ) {
795
			update_site_option( 'jetpack_protect_error', $response['error'] );
796
		} else {
797
			delete_site_option( 'jetpack_protect_error' );
798
		}
799
800
		return $response;
801
	}
802
803
	function get_transient_name() {
804
		$headers     = $this->get_headers();
805
		$header_hash = md5( json_encode( $headers ) );
806
807
		return 'jpp_li_' . $header_hash;
808
	}
809
810
	/**
811
	 * Wrapper for WordPress set_transient function, our version sets
812
	 * the transient on the main site in the network if this is a multisite network
813
	 *
814
	 * We do it this way (instead of set_site_transient) because of an issue where
815
	 * sitewide transients are always autoloaded
816
	 * https://core.trac.wordpress.org/ticket/22846
817
	 *
818
	 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
819
	 *                           45 characters or fewer in length.
820
	 * @param mixed $value Transient value. Must be serializable if non-scalar.
821
	 *                           Expected to not be SQL-escaped.
822
	 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
823
	 *
824
	 * @return bool False if value was not set and true if value was set.
825
	 */
826
	function set_transient( $transient, $value, $expiration ) {
827
		if ( is_multisite() && ! is_main_site() ) {
828
			switch_to_blog( $this->get_main_blog_id() );
829
			$return = set_transient( $transient, $value, $expiration );
830
			restore_current_blog();
831
832
			return $return;
833
		}
834
835
		return set_transient( $transient, $value, $expiration );
836
	}
837
838
	/**
839
	 * Wrapper for WordPress delete_transient function, our version deletes
840
	 * the transient on the main site in the network if this is a multisite network
841
	 *
842
	 * @param string $transient Transient name. Expected to not be SQL-escaped.
843
	 *
844
	 * @return bool true if successful, false otherwise
845
	 */
846 View Code Duplication
	function delete_transient( $transient ) {
847
		if ( is_multisite() && ! is_main_site() ) {
848
			switch_to_blog( $this->get_main_blog_id() );
849
			$return = delete_transient( $transient );
850
			restore_current_blog();
851
852
			return $return;
853
		}
854
855
		return delete_transient( $transient );
856
	}
857
858
	/**
859
	 * Wrapper for WordPress get_transient function, our version gets
860
	 * the transient on the main site in the network if this is a multisite network
861
	 *
862
	 * @param string $transient Transient name. Expected to not be SQL-escaped.
863
	 *
864
	 * @return mixed Value of transient.
865
	 */
866 View Code Duplication
	function get_transient( $transient ) {
867
		if ( is_multisite() && ! is_main_site() ) {
868
			switch_to_blog( $this->get_main_blog_id() );
869
			$return = get_transient( $transient );
870
			restore_current_blog();
871
872
			return $return;
873
		}
874
875
		return get_transient( $transient );
876
	}
877
878
	function get_api_host() {
879
		if ( isset( $this->api_endpoint ) ) {
880
			return $this->api_endpoint;
881
		}
882
883
		//Check to see if we can use SSL
884
		$this->api_endpoint = Jetpack::fix_url_for_bad_hosts( JETPACK_PROTECT__API_HOST );
885
886
		return $this->api_endpoint;
887
	}
888
889
	function get_local_host() {
890
		if ( isset( $this->local_host ) ) {
891
			return $this->local_host;
892
		}
893
894
		$uri = 'http://' . strtolower( $_SERVER['HTTP_HOST'] );
895
896
		if ( is_multisite() ) {
897
			$uri = network_home_url();
898
		}
899
900
		$uridata = parse_url( $uri );
901
902
		$domain = $uridata['host'];
903
904
		// If we still don't have the site_url, get it
905
		if ( ! $domain ) {
906
			$uri     = get_site_url( 1 );
907
			$uridata = parse_url( $uri );
908
			$domain  = $uridata['host'];
909
		}
910
911
		$this->local_host = $domain;
912
913
		return $this->local_host;
914
	}
915
916
}
917
918
$jetpack_protect = Jetpack_Protect_Module::instance();
919
920
global $pagenow;
921
if ( isset( $pagenow ) && 'wp-login.php' == $pagenow ) {
922
	$jetpack_protect->check_login_ability();
923
}
924