get_transient( 'jpp_math_pass_' . sanitize_key( $_COOKIE['jpp_math_pass'] ) ); if ( ! $transient || $transient < 1 ) { self::generate_math_page(); } return true; } $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 $time_window = self::time_window(); $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 ) { self::generate_math_page(); } elseif ( ! hash_equals( $salted_ans_1, $correct_ans ) && ! hash_equals( $salted_ans_2, $correct_ans ) ) { wp_die( wp_kses( __( 'You failed to correctly answer the math problem. This is used to combat spam when the Protect API is unavailable. Please use your browser’s 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 ) ); } else { return true; } } /** * Creates an interim page to collect answers to a math captcha * * @param string $error - the error message. */ public static function generate_math_page( $error = false ) { ob_start(); ?>
401 ) ); } /** * Generates the math page. */ public function process_generate_math_page() { $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 $time_window = self::time_window(); $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 ) ) { self::generate_math_page( true ); } else { $temp_pass = substr( hash_hmac( 'sha1', wp_rand( 1, 100000000 ), get_site_option( 'jetpack_protect_key' ) ), 5, 25 ); $jetpack_protect = Jetpack_Protect_Module::instance(); $jetpack_protect->set_transient( 'jpp_math_pass_' . $temp_pass, 3, DAY_IN_SECONDS ); setcookie( 'jpp_math_pass', $temp_pass, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, true ); 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 */ public static function math_form() { // Check if jpp_math_pass cookie is set and it matches valid transient. if ( isset( $_COOKIE['jpp_math_pass'] ) ) { $jetpack_protect = Jetpack_Protect_Module::instance(); $transient = $jetpack_protect->get_transient( 'jpp_math_pass_' . sanitize_key( $_COOKIE['jpp_math_pass'] ) ); if ( $transient && $transient > 0 ) { return ''; } } $num1 = wp_rand( 0, 10 ); $num2 = wp_rand( 1, 10 ); $ans = $num1 + $num2; $time_window = self::time_window(); $salt = get_site_option( 'jetpack_protect_key' ) . '|' . get_site_option( 'admin_email' ) . '|'; $salted_ans = hash_hmac( 'sha1', $ans, $salt . $time_window ); ?>