diff --git a/inc/spbc-auth.php b/inc/spbc-auth.php index 65b92b888..281363579 100644 --- a/inc/spbc-auth.php +++ b/inc/spbc-auth.php @@ -3,7 +3,6 @@ use CleantalkSP\SpbctWP\Counters\SecurityCounter; use CleantalkSP\SpbctWP\Firewall\WafBlocker; use CleantalkSP\SpbctWP\G2FA\GoogleAuthenticator; -use CleantalkSP\SpbctWP\UsersPassCheckModule\UsersPassCheckModel; use CleantalkSP\SpbctWP\Variables\Cookie; use CleantalkSP\SpbctWP\Helpers\IP; use CleantalkSP\Variables\Get; @@ -18,7 +17,7 @@ die('Not allowed!'); } -add_filter('authenticate', 'spbc_authenticate', 20, 3); // Hooks for authentificate +add_filter('authenticate', 'spbc_authenticate', 20, 2); // Hooks for authentificate // Hook for token-based logins (plugins like "Temporary Login Without Password") add_action('set_logged_in_cookie', 'spbc_detect_token_login', 10, 6); @@ -30,6 +29,10 @@ add_action('profile_update', [UsersPassCheckHandler::class, 'removeUserPassOnPasswordChange'], 10, 1); add_action('login_form', 'spbc_passleak_change_password_form', 10); add_action('login_form_login', 'spbc_passleak_change_password_handler', 3); + // Clear flag on standard password reset + add_action('after_password_reset', 'spbc_passleak__clear_flag_on_reset', 10, 1); + // Global guards for forced password change + add_action('admin_init', 'spbc_passleak__force_password_change_guard', 1); } add_action('login_errors', 'spbc_fix_error_messages', 99999); // Filters error message @@ -122,6 +125,7 @@ function spbc_login_form_notification() * * @param WP_User|WP_Error $user * @param string $username + * @param string $password * * @return WP_Error|WP_User */ @@ -225,14 +229,10 @@ function spbc_authenticate($user, $username) die(); } - // Redirect if password is leaked + // Set force password change flag if password is leaked + // Guard (spbc_passleak__force_password_change_guard) will redirect to password change form if (UsersPassCheckHandler::isUserPassLeaked($user->ID)) { - wp_redirect( - wp_login_url() - . ( strpos(wp_login_url(), '?') === false ? '?' : '&' ) - . 'spbc_passleak=' . rawurlencode($user->user_login) - ); - die(); + update_user_meta($user->ID, 'spbc_force_password_change', 1); } spbc_authenticate__write_log_login($user); @@ -375,6 +375,12 @@ function spbc_detect_token_login($_logged_in_cookie, $_expire, $_expiration, $us // Mark as logged to prevent any further duplicate logging $spbc_login_logged = true; + // Check if password is leaked for token-based logins + if (UsersPassCheckHandler::isUserPassLeaked($user->ID)) { + // Set force password change flag + update_user_meta($user->ID, 'spbc_force_password_change', 1); + } + // Sends logs to get notify about superuser login. $result = spbc_send_logs(); if (empty($result['error'])) { @@ -540,29 +546,33 @@ function spbc_passleak_change_password_form() { global $spbc; - $user = null; + // Check if spbc_passleak parameter is present + $has_passleak_param = false; if (isset($_GET['spbc_passleak'])) { - $user = $_GET['spbc_passleak']; + $has_passleak_param = true; } else { // Fallback to parsing REQUEST_URI if $_GET is empty because of .htaccess settings $request_uri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL); if ($request_uri) { $params = []; parse_str(parse_url($request_uri, PHP_URL_QUERY) ?: '', $params); - $user = isset($params['spbc_passleak']) ? $params['spbc_passleak'] : null; + $has_passleak_param = isset($params['spbc_passleak']); } } - if (!$user) { + if (!$has_passleak_param) { return; } - $user_name = rawurldecode($user); - $user = spbc_get_user_by('login', $user_name); + // User must be logged in to see the password change form + $current_user = wp_get_current_user(); + if (!$current_user || !$current_user->ID) { + return; + } - if (!$user) { - wp_redirect(home_url()); - exit; + // Only show form if current user's password is actually leaked + if (!UsersPassCheckHandler::isUserPassLeaked($current_user->ID)) { + return; } // Error displaying @@ -574,6 +584,9 @@ function spbc_passleak_change_password_form() if ($_GET['spbc_passleak_error'] == '2') { $err .= __('Current password is incorrect.', 'security-malware-firewall'); } + if ($_GET['spbc_passleak_error'] == '3') { + $err .= __('Too many attempts. Please, try again later.', 'security-malware-firewall'); + } echo sprintf( '