Softvolt_Ai_Bodyguard_Helpers::request_path() ) ); wp_safe_redirect( home_url( '/' ), 301 ); exit; } } public static function no_author_canonical( $redirect, $requested ) { if ( ! is_user_logged_in() && preg_match( '/[?&]author=\d+/', (string) $requested ) ) { return false; } return $redirect; } public static function remove_user_endpoints( $endpoints ) { if ( is_user_logged_in() ) { return $endpoints; } foreach ( array_keys( $endpoints ) as $route ) { if ( 0 === strpos( $route, '/wp/v2/users' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } public static function remove_user_sitemap( $provider, $name ) { return 'users' === $name ? false : $provider; } public static function strip_oembed_author( $data ) { unset( $data['author_name'], $data['author_url'] ); return $data; } public static function generic_login_error( $error ) { if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'hide_login_errors' ) ) { return $error; // Login security already replaces it. } return $error; } public static function kill_author_archive() { if ( is_author() && ! is_user_logged_in() ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } public static function rest_require_auth( $result ) { if ( true === $result || is_wp_error( $result ) ) { return $result; } if ( is_user_logged_in() ) { return $result; } $route = self::current_rest_route(); if ( '' === $route ) { return $result; } $allow = Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'rest_allowlist', '' ) ); foreach ( $allow as $prefix ) { $prefix = trim( $prefix, '/' ); if ( '' !== $prefix && 0 === strpos( $route, $prefix ) ) { return $result; } } return new WP_Error( 'rest_not_logged_in', __( 'You must be logged in to use the REST API on this site.', 'ai-guardwall' ), array( 'status' => 401 ) ); } private static function current_rest_route() { $route = ''; if ( isset( $_GET['rest_route'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $route = sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } else { $prefix = rest_get_url_prefix(); $path = Softvolt_Ai_Bodyguard_Helpers::request_path(); $pos = strpos( $path, '/' . $prefix . '/' ); if ( false !== $pos ) { $route = substr( $path, $pos + strlen( $prefix ) + 2 ); } elseif ( '/' . $prefix === rtrim( $path, '/' ) ) { $route = '/'; } } return trim( $route, '/' ); } public static function send_security_headers() { if ( headers_sent() ) { return; } if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'security_headers' ) ) { header( 'X-Content-Type-Options: nosniff' ); header( 'X-Frame-Options: SAMEORIGIN' ); header( 'Referrer-Policy: strict-origin-when-cross-origin' ); header( 'Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()' ); header( 'X-Permitted-Cross-Domain-Policies: none' ); } if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'hsts' ) && is_ssl() ) { $age = max( 300, (int) Softvolt_Ai_Bodyguard_Settings::get( 'headers_hsts_max_age', 31536000 ) ); header( 'Strict-Transport-Security: max-age=' . $age . '; includeSubDomains' ); } if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'csp' ) ) { $csp = trim( (string) Softvolt_Ai_Bodyguard_Settings::get( 'headers_csp', '' ) ); if ( '' !== $csp ) { header( 'Content-Security-Policy: ' . str_replace( array( "\r", "\n" ), ' ', $csp ) ); } } } public static function check_password_strength( $errors, $update, $user ) { // The new password on WordPress's own profile or reset form (WordPress checks that form's nonce). It is // only hashed for the breach check; sanitizing would change it. $pass = isset( $_POST['pass1'] ) ? (string) wp_unslash( $_POST['pass1'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( '' === $pass ) { return; } $problem = self::password_problem( $pass ); if ( $problem ) { $errors->add( 'svabg_weak_password', '' . __( 'Error:', 'ai-guardwall' ) . ' ' . $problem ); } } public static function check_password_reset( $errors, $user ) { // The new password on WordPress's own profile or reset form (WordPress checks that form's nonce). It is // only hashed for the breach check; sanitizing would change it. $pass = isset( $_POST['pass1'] ) ? (string) wp_unslash( $_POST['pass1'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( '' === $pass ) { return; } $problem = self::password_problem( $pass ); if ( $problem ) { $errors->add( 'svabg_weak_password', '' . __( 'Error:', 'ai-guardwall' ) . ' ' . $problem ); } } public static function password_problem( $pass ) { $min = max( 8, (int) Softvolt_Ai_Bodyguard_Settings::get( 'login_min_password_length', 12 ) ); if ( strlen( $pass ) < $min ) { return sprintf( /* translators: %d: minutes */ __( 'Passwords must be at least %d characters long.', 'ai-guardwall' ), $min ); } $classes = (int) preg_match( '/[a-z]/', $pass ) + (int) preg_match( '/[A-Z]/', $pass ) + (int) preg_match( '/\d/', $pass ) + (int) preg_match( '/[^a-zA-Z\d]/', $pass ); if ( $classes < 3 ) { return __( 'Passwords must contain at least three of: lower-case letters, upper-case letters, numbers, symbols.', 'ai-guardwall' ); } $lower = strtolower( $pass ); foreach ( array( 'password', 'qwerty', '123456', 'letmein', 'welcome', 'admin', 'wordpress' ) as $weak ) { if ( false !== strpos( $lower, $weak ) ) { return __( 'That password contains a very common word. Choose something harder to guess.', 'ai-guardwall' ); } } return ''; } public static function idle_logout() { if ( ! is_user_logged_in() || wp_doing_ajax() || wp_doing_cron() ) { return; } $minutes = max( 5, (int) Softvolt_Ai_Bodyguard_Settings::get( 'idle_logout_minutes', 60 ) ); $user_id = get_current_user_id(); $last = (int) get_user_meta( $user_id, 'svabg_last_activity', true ); $now = time(); if ( $last && ( $now - $last ) > $minutes * MINUTE_IN_SECONDS ) { wp_destroy_current_session(); wp_clear_auth_cookie(); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'logout', __( 'Session ended after inactivity.', 'ai-guardwall' ), array(), $user_id ); wp_safe_redirect( add_query_arg( 'svabg_idle', '1', wp_login_url() ) ); exit; } if ( ! $last || ( $now - $last ) > 60 ) { update_user_meta( $user_id, 'svabg_last_activity', $now ); } } public static function block_subscribers_admin() { if ( wp_doing_ajax() || ! is_user_logged_in() || current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) { return; } global $pagenow; if ( in_array( $pagenow, array( 'admin-post.php', 'admin-ajax.php', 'profile.php' ), true ) ) { return; } wp_safe_redirect( home_url( '/' ) ); exit; } public static function hide_bar_for_subscribers( $show ) { if ( is_user_logged_in() && ! current_user_can( 'edit_posts' ) ) { return false; } return $show; } public static function admin_ip_allowlist() { if ( wp_doing_ajax() || wp_doing_cron() ) { return; } global $pagenow; if ( in_array( $pagenow, array( 'admin-post.php', 'admin-ajax.php' ), true ) ) { return; } $list = Softvolt_Ai_Bodyguard_Settings::get( 'admin_ip_allowlist', '' ); if ( '' === trim( (string) $list ) ) { return; // Never lock everyone out because of an empty list. } if ( Softvolt_Ai_Bodyguard_Helpers::ip_in_list( Softvolt_Ai_Bodyguard_Helpers::client_ip(), $list ) ) { return; } Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'admin_ip_allowlist' ), __( 'The administration area is restricted to approved IP addresses.', 'ai-guardwall' ), 403, true ); } public static function upload_prefilter( $file ) { $name = isset( $file['name'] ) ? (string) $file['name'] : ''; $tmp = isset( $file['tmp_name'] ) ? (string) $file['tmp_name'] : ''; if ( preg_match( '/\.(php\d?|phtml|phar|pht|phps|shtml|cgi|pl|py|sh|bash|exe|dll|com|bat|cmd|jsp|asp|aspx|htaccess|ini|user\.ini)(\.|$)/i', $name ) ) { $file['error'] = __( 'AI GuardWall blocked this upload: executable or configuration file types are not allowed.', 'ai-guardwall' ); Softvolt_Ai_Bodyguard_Logger::log( 'warning', 'upload_blocked', sprintf( /* translators: %s: name */ __( 'Blocked upload of "%s" (dangerous extension).', 'ai-guardwall' ), $name ) ); return $file; } if ( Softvolt_Ai_Bodyguard_Settings::get( 'upload_block_svg', false ) && preg_match( '/\.svgz?$/i', $name ) ) { $file['error'] = __( 'AI GuardWall blocked this upload: SVG uploads are disabled.', 'ai-guardwall' ); return $file; } if ( Softvolt_Ai_Bodyguard_Settings::get( 'upload_block_html', true ) && preg_match( '/\.(html?|xhtml|xml|svg)$/i', $name ) && ! current_user_can( 'unfiltered_html' ) ) { $file['error'] = __( 'AI GuardWall blocked this upload: HTML/SVG uploads require the unfiltered_html capability.', 'ai-guardwall' ); return $file; } if ( $tmp && is_readable( $tmp ) ) { $head = @file_get_contents( $tmp, false, null, 0, 262144 ); if ( false !== $head ) { if ( preg_match( '/<\?php|<\?=|