kollapsminoriteten/wp-content/plugins/jetpack/modules/protect/math-fallback.php

179 lines
6.7 KiB
PHP
Raw Normal View History

2022-06-16 14:01:47 +02:00
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2019-11-15 23:26:29 +01:00
if ( ! class_exists( 'Jetpack_Protect_Math_Authenticate' ) ) {
2022-06-16 14:01:47 +02:00
/**
2019-11-15 23:26:29 +01:00
* The math captcha fallback if we can't talk to the Protect API
*/
class Jetpack_Protect_Math_Authenticate {
2022-06-16 14:01:47 +02:00
/**
* If the class is loaded.
*
* @var bool
*/
public static $loaded;
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
/**
* Class constructor.
*/
public function __construct() {
2019-11-15 23:26:29 +01:00
if ( self::$loaded ) {
return;
}
self::$loaded = 1;
add_action( 'login_form', array( $this, 'math_form' ) );
2022-06-16 14:01:47 +02:00
if ( isset( $_POST['jetpack_protect_process_math_form'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No changes made, just queues the math authenticator hook.
2019-11-15 23:26:29 +01:00
add_action( 'init', array( $this, 'process_generate_math_page' ) );
}
}
2022-06-16 14:01:47 +02:00
/**
* The timeout window.
*/
2019-11-15 23:26:29 +01:00
private static function time_window() {
return ceil( time() / ( MINUTE_IN_SECONDS * 2 ) );
}
/**
* Verifies that a user answered the math problem correctly while logging in.
*
* @return bool Returns true if the math is correct
2022-06-16 14:01:47 +02:00
* @throws Error If insuffient $_POST variables are present.
* @throws Error Message if the math is wrong.
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public static function math_authenticate() {
if ( isset( $_COOKIE['jpp_math_pass'] ) ) {
2019-11-15 23:26:29 +01:00
$jetpack_protect = Jetpack_Protect_Module::instance();
2022-06-16 14:01:47 +02:00
$transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . sanitize_key( $_COOKIE['jpp_math_pass'] ) );
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
if ( ! $transient || $transient < 1 ) {
self::generate_math_page();
2019-11-15 23:26:29 +01:00
}
return true;
}
2022-06-16 14:01:47 +02:00
$ans = isset( $_POST['jetpack_protect_num'] ) ? (int) $_POST['jetpack_protect_num'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- answers are salted.
$correct_ans = isset( $_POST['jetpack_protect_answer'] ) ? sanitize_key( $_POST['jetpack_protect_answer'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
$time_window = self::time_window();
2019-11-15 23:26:29 +01:00
$salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
$salted_ans_1 = hash_hmac( 'sha1', $ans, $salt . $time_window );
$salted_ans_2 = hash_hmac( 'sha1', $ans, $salt . ( $time_window - 1 ) );
if ( ! $correct_ans || ! $ans ) {
2022-06-16 14:01:47 +02:00
self::generate_math_page();
2019-11-15 23:26:29 +01:00
} elseif ( ! hash_equals( $salted_ans_1, $correct_ans ) && ! hash_equals( $salted_ans_2, $correct_ans ) ) {
wp_die(
2021-09-07 21:40:48 +02:00
wp_kses(
__(
'<strong>You failed to correctly answer the math problem.</strong> This is used to combat spam when the Protect API is unavailable. Please use your browsers back button to return to the login form, press the "refresh" button to generate a new math problem, and try to log in again.',
'jetpack'
),
array( 'strong' => array() )
),
'',
array( 'response' => 401 )
2019-11-15 23:26:29 +01:00
);
} else {
return true;
}
}
/**
* Creates an interim page to collect answers to a math captcha
*
2022-06-16 14:01:47 +02:00
* @param string $error - the error message.
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public static function generate_math_page( $error = false ) {
2019-11-15 23:26:29 +01:00
ob_start();
?>
2021-09-07 21:40:48 +02:00
<h2><?php esc_html_e( 'Please solve this math problem to prove that you are not a bot. Once you solve it, you will need to log in again.', 'jetpack' ); ?></h2>
2022-06-16 14:01:47 +02:00
<?php if ( $error ) : ?>
2019-11-15 23:26:29 +01:00
<h3><?php esc_html_e( 'Your answer was incorrect, please try again.', 'jetpack' ); ?></h3>
<?php endif ?>
2022-06-16 14:01:47 +02:00
<form action="<?php echo esc_url( wp_login_url() ); ?>" method="post" accept-charset="utf-8">
<?php self::math_form(); ?>
2019-11-15 23:26:29 +01:00
<input type="hidden" name="jetpack_protect_process_math_form" value="1" id="jetpack_protect_process_math_form" />
<p><input type="submit" value="<?php esc_attr_e( 'Continue &rarr;', 'jetpack' ); ?>"></p>
</form>
2022-06-16 14:01:47 +02:00
<?php
2019-11-15 23:26:29 +01:00
$mathpage = ob_get_contents();
ob_end_clean();
wp_die(
2022-06-16 14:01:47 +02:00
$mathpage, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- content is escaped.
2019-11-15 23:26:29 +01:00
'',
2022-06-16 14:01:47 +02:00
array( 'response' => 401 )
2019-11-15 23:26:29 +01:00
);
}
2022-06-16 14:01:47 +02:00
/**
* Generates the math page.
*/
2019-11-15 23:26:29 +01:00
public function process_generate_math_page() {
2022-06-16 14:01:47 +02:00
$ans = isset( $_POST['jetpack_protect_num'] ) ? (int) $_POST['jetpack_protect_num'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- answers are salted.
$correct_ans = isset( $_POST['jetpack_protect_answer'] ) ? sanitize_key( $_POST['jetpack_protect_answer'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
$time_window = self::time_window();
2019-11-15 23:26:29 +01:00
$salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
$salted_ans_1 = hash_hmac( 'sha1', $ans, $salt . $time_window );
$salted_ans_2 = hash_hmac( 'sha1', $ans, $salt . ( $time_window - 1 ) );
if ( ! hash_equals( $salted_ans_1, $correct_ans ) && ! hash_equals( $salted_ans_2, $correct_ans ) ) {
2022-06-16 14:01:47 +02:00
self::generate_math_page( true );
2019-11-15 23:26:29 +01:00
} else {
2022-06-16 14:01:47 +02:00
$temp_pass = substr( hash_hmac( 'sha1', wp_rand( 1, 100000000 ), get_site_option( 'jetpack_protect_key' ) ), 5, 25 );
2019-11-15 23:26:29 +01:00
$jetpack_protect = Jetpack_Protect_Module::instance();
$jetpack_protect->set_transient( 'jpp_math_pass_' . $temp_pass, 3, DAY_IN_SECONDS );
2022-06-16 14:01:47 +02:00
setcookie( 'jpp_math_pass', $temp_pass, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, true );
2019-11-15 23:26:29 +01:00
remove_action( 'login_form', array( $this, 'math_form' ) );
return true;
}
}
/**
* Requires a user to solve a simple equation. Added to any WordPress login form.
*
* @return VOID outputs html
*/
2022-06-16 14:01:47 +02:00
public static function math_form() {
// Check if jpp_math_pass cookie is set and it matches valid transient.
if ( isset( $_COOKIE['jpp_math_pass'] ) ) {
2019-11-15 23:26:29 +01:00
$jetpack_protect = Jetpack_Protect_Module::instance();
2022-06-16 14:01:47 +02:00
$transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . sanitize_key( $_COOKIE['jpp_math_pass'] ) );
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
if ( $transient && $transient > 0 ) {
2019-11-15 23:26:29 +01:00
return '';
}
}
2022-06-16 14:01:47 +02:00
$num1 = wp_rand( 0, 10 );
$num2 = wp_rand( 1, 10 );
2019-11-15 23:26:29 +01:00
$ans = $num1 + $num2;
2022-06-16 14:01:47 +02:00
$time_window = self::time_window();
2019-11-15 23:26:29 +01:00
$salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|';
$salted_ans = hash_hmac( 'sha1', $ans, $salt . $time_window );
?>
<div style="margin: 5px 0 20px;">
2022-04-02 10:26:41 +02:00
<p style="font-size: 14px;">
2019-11-15 23:26:29 +01:00
<?php esc_html_e( 'Prove your humanity', 'jetpack' ); ?>
2022-04-02 10:26:41 +02:00
</p>
2019-11-15 23:26:29 +01:00
<br/>
2022-04-02 10:26:41 +02:00
<label for="jetpack_protect_answer" style="vertical-align:super;">
2019-11-15 23:26:29 +01:00
<?php echo esc_html( "$num1 &nbsp; + &nbsp; $num2 &nbsp; = &nbsp;" ); ?>
2022-04-02 10:26:41 +02:00
</label>
<input type="number" id="jetpack_protect_answer" name="jetpack_protect_num" value="" size="2" style="width:50px;height:25px;vertical-align:middle;font-size:13px;" class="input" />
2019-11-15 23:26:29 +01:00
<input type="hidden" name="jetpack_protect_answer" value="<?php echo esc_attr( $salted_ans ); ?>" />
</div>
2022-06-16 14:01:47 +02:00
<?php
2019-11-15 23:26:29 +01:00
}
}
}