'; echo '

'; echo wp_kses( sprintf( /* translators: 1: Google Privacy Policy URL, 2: Google Terms of Service URL */ __( 'This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.', 'admintrace-activity-logger' ), 'https://policies.google.com/privacy', 'https://policies.google.com/terms' ), array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true, ), ) ); echo '

'; } /** * Block interactive logins when the reCAPTCHA check fails. * * Runs late so every login POST is checked, including wrong passwords. * A failed check becomes a journal entry via wp_login_failed. * * @param \WP_User|\WP_Error|null $user Authentication result so far. * @param string $username Submitted username. * @param string $password Submitted password. * @return \WP_User|\WP_Error|null */ public static function verify_authenticate( $user, $username, $password ) { unset( $password ); if ( ! self::is_active() || ! self::is_wp_login_post() ) { return $user; } if ( $user instanceof \WP_Error ) { $codes = $user->get_error_codes(); if ( in_array( 'empty_username', $codes, true ) || in_array( 'empty_password', $codes, true ) ) { return $user; } } $result = self::verify( 'login' ); if ( 'fail' !== $result ) { return $user; } return new \WP_Error( 'srah_recaptcha', __( 'Error: reCAPTCHA verification failed. Please try again.', 'admintrace-activity-logger' ), array( 'username' => (string) $username ) ); } /** * Block lost-password requests when the challenge fails. * * @param \WP_Error $errors Errors collected so far. */ public static function verify_lostpassword( $errors ) { if ( ! self::is_active() || ! is_wp_error( $errors ) ) { return; } if ( 'fail' !== self::verify( 'lostpassword' ) ) { return; } $errors->add( 'srah_recaptcha', __( 'Error: reCAPTCHA verification failed. Please try again.', 'admintrace-activity-logger' ) ); $username = isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- auth form; captcha is the gate. self::log_block( $username, 'lostpassword' ); } /** * Block registration when the challenge fails. * * @param \WP_Error $errors Registration errors. * @param string $sanitized_user_login Login name. * @param string $user_email Email. * @return \WP_Error */ public static function verify_registration( $errors, $sanitized_user_login, $user_email ) { unset( $user_email ); if ( ! self::is_active() || ! is_wp_error( $errors ) ) { return $errors; } if ( 'fail' !== self::verify( 'register' ) ) { return $errors; } $errors->add( 'srah_recaptcha', __( 'Error: reCAPTCHA verification failed. Please try again.', 'admintrace-activity-logger' ) ); self::log_block( (string) $sanitized_user_login, 'register' ); return $errors; } /** * Record a non-login form blocked by reCAPTCHA. * * Login blocks are logged from User_Logger via wp_login_failed. * * @param string $username Attempted username. * @param string $form lostpassword|register. */ private static function log_block( $username, $form ) { if ( empty( Settings::all()['log_failed_logins'] ) ) { return; } $username = Text::plain( $username, 60 ); if ( '' === $username ) { $username = __( '(none)', 'admintrace-activity-logger' ); } if ( 'register' === $form ) { $summary = sprintf( /* translators: %s: username. */ __( 'Bot registration attempt for "%s" was blocked by reCAPTCHA.', 'admintrace-activity-logger' ), $username ); } else { $summary = sprintf( /* translators: %s: username. */ __( 'Bot password-reset attempt for "%s" was blocked by reCAPTCHA.', 'admintrace-activity-logger' ), $username ); } $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? Text::plain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 180 ) : ''; Recorder::log( array( 'summary' => $summary, 'category' => 'security', 'action' => 'security.recaptcha_blocked', 'severity' => 'warning', 'user_id' => 0, 'user_login' => $username, 'initiator' => 'anonymous', 'logger' => 'user', 'context' => array( 'reason' => 'recaptcha', 'form' => $form, 'user_agent' => $agent, ), ) ); } /** * Whether this is an interactive wp-login.php password POST. * * Skips XML-RPC, REST, CLI, and cookie/application-password flows. * * @return bool */ private static function is_wp_login_post() { if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return false; } if ( empty( $_POST['log'] ) || empty( $_POST['pwd'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- auth form; verified by core. return false; } $pagenow = isset( $GLOBALS['pagenow'] ) ? (string) $GLOBALS['pagenow'] : ''; return 'wp-login.php' === $pagenow; } /** * Verify the submitted reCAPTCHA v3 token with Google. * * @param string $expected_action Action name passed to grecaptcha.execute(). * @return string pass|fail|skip skip = network/config problem (do not lock users out). */ private static function verify( $expected_action ) { $token = isset( $_POST['g-recaptcha-response'] ) ? (string) wp_unslash( $_POST['g-recaptcha-response'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- token is opaque; verified by Google. $token = preg_replace( '/[^A-Za-z0-9_-]/', '', $token ); if ( ! is_string( $token ) || '' === $token ) { return 'fail'; } $settings = Settings::all(); $body = array( 'secret' => (string) $settings['recaptcha_secret_key'], 'response' => $token, ); $ip = self::remote_ip(); if ( '' !== $ip ) { $body['remoteip'] = $ip; } $response = wp_remote_post( self::VERIFY_URL, array( 'timeout' => 15, 'body' => $body, ) ); if ( is_wp_error( $response ) ) { return 'skip'; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { return 'skip'; } $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) ) { return 'skip'; } if ( empty( $data['success'] ) ) { $errors = isset( $data['error-codes'] ) && is_array( $data['error-codes'] ) ? $data['error-codes'] : array(); return self::is_config_error( $errors ) ? 'skip' : 'fail'; } $action = isset( $data['action'] ) ? (string) $data['action'] : ''; if ( $expected_action !== $action ) { return 'fail'; } $score = isset( $data['score'] ) ? (float) $data['score'] : 0.0; $threshold = isset( $settings['recaptcha_score'] ) ? (float) $settings['recaptcha_score'] : 0.5; return ( $score >= $threshold ) ? 'pass' : 'fail'; } /** * Whether Google returned a key/project problem rather than a bot signal. * * @param array $errors Google error-codes list. * @return bool */ private static function is_config_error( array $errors ) { foreach ( $errors as $error ) { $error = strtolower( (string) $error ); if ( 'invalid-input-secret' === $error || 'bad-request' === $error ) { return true; } if ( false !== strpos( $error, 'project' ) || false !== strpos( $error, 'not found' ) || false !== strpos( $error, 'deleted' ) ) { return true; } } return false; } /** * Visitor IP for Google verification (unmasked). * * @return string */ private static function remote_ip() { $settings = Settings::all(); $remote = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; if ( empty( $settings['trust_proxy'] ) ) { return $remote; } $forwarded = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) : ''; if ( '' === $forwarded ) { return $remote; } foreach ( explode( ',', $forwarded ) as $candidate ) { $candidate = trim( $candidate ); if ( '' !== $candidate && false !== filter_var( $candidate, FILTER_VALIDATE_IP ) ) { return $candidate; } } return $remote; } }