settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); add_action( 'adminease_settings_saved', [ $this, 'adminease_settings_saved' ] ); if( !empty( $this->settings['block_author_scans'] ) ) { add_filter( 'rest_endpoints', [ $this, 'rest_endpoints' ] ); add_filter( 'request', [ $this, 'request' ] ); } } /** * Removes the user routes from the REST API for visitors who are not logged in. * The route /wp/v2/users/me stays. Routes are removed by prefix, because their keys * contain regular expressions. Untyped parameter: other plugins may pass anything. * * @param mixed $endpoints The registered REST routes. * * @return mixed The routes, without the user routes for visitors. */ public function rest_endpoints( $endpoints ) { if( !is_array( $endpoints ) || is_user_logged_in() ) { return $endpoints; } foreach( array_keys( $endpoints ) as $route ) { if( '/wp/v2/users' !== $route && 0 !== strpos( $route, '/wp/v2/users/' ) ) { continue; } if( '/wp/v2/users/me' === $route ) { continue; } unset( $endpoints[ $route ] ); } return $endpoints; } /** * Turns a ?author= lookup into a 404 for visitors who are not logged in. Any value counts, * including lists such as 1,2 and negative numbers. Author links like /author/name/ use * author_name, so they keep working. Admin screens are skipped, where ?author=N is a normal filter. * Untyped parameter: other plugins may pass anything. * * @param mixed $query_vars The parsed request query variables. * * @return mixed The query variables, changed to a 404 for a visitor's author lookup. */ public function request( $query_vars ) { if( !is_array( $query_vars ) || !isset( $query_vars['author'] ) || is_admin() || is_user_logged_in() ) { return $query_vars; } $author = $query_vars['author']; if( is_array( $author ) ) { $is_lookup = !empty( $author ); } else { $is_lookup = '' !== trim( (string) $author ); } if( !$is_lookup ) { return $query_vars; } unset( $query_vars['author'], $query_vars['feed'] ); $query_vars['error'] = '404'; return $query_vars; } /** * Adds a security field to the settings array for blocking author enumeration attempts. * * @param array $fields The fields array containing existing settings configurations. * * @return array The updated fields array including the 'block_author_scans' security option. */ public function adminease_settings_fields( array $fields ): array { $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'block-author-scans', 'name' => 'adminease[security][block_author_scans]', 'value' => $this->settings['block_author_scans'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Block author scans', 'adminease' ), 'description' => __( 'Author scans are looking to find usernames of registered users (especially WordPress admin) and brute-force attack the login page of your website to gain access. This security option prevents such scans from exposing these usernames. Note: depending on the permalink configuration on your website this option might prevent visitors from accessing pages that list all articles written by a particular author.', 'adminease' ), ]; return $fields; } /** * Builds the .htaccess rule that refuses author scans on servers that read it. * * @return string The rule block. */ private function get_htaccess_rule(): string { $code = "\n"; $code .= "\tRewriteEngine On\n"; $code .= "\tRewriteBase /\n\n"; $code .= "\t# Block author enumeration attempts\n"; $code .= "\tRewriteCond %{REQUEST_URI} !^/wp-admin/ [NC]\n"; $code .= "\t# Check if unauthenticated (no Authorization header AND no login cookie)\n"; $code .= "\tRewriteCond %{HTTP:Authorization} ^$\n"; $code .= "\tRewriteCond %{HTTP_COOKIE} !wordpress_logged_in [NC]\n"; $code .= "\t# Block unauthenticated author scans and user endpoint access\n"; $code .= "\tRewriteCond %{QUERY_STRING} (^|&)author=\d+ [NC,OR]\n"; $code .= "\tRewriteCond %{REQUEST_URI} ^.*wp-json/wp/v2/(users) [NC]\n"; $code .= "\tRewriteRule .* - [F]\n"; $code .= ""; return $code; } /** * Updates .htaccess rules based on security settings to block author enumeration attempts. * * @param array $sanitized_settings The settings array containing security configurations, including the 'block_author_scans' option. * * @return void */ public function adminease_settings_saved( array $sanitized_settings ): void { if( !empty( $sanitized_settings['security']['block_author_scans'] ) ) { $code = $this->get_htaccess_rule(); } else { $code = ''; } FileHandler::get_instance()->stack_htaccess_rule( 'BLOCK_AUTHOR_SCANS', $code ); } }