0; } /** * {@inheritDoc} */ public function register() { if ( Settings::get( 'harden_file_edit' ) ) { $this->disable_file_editor(); } if ( Settings::get( 'harden_user_enum' ) ) { add_action( 'init', array( $this, 'block_author_scan' ), 1 ); add_filter( 'rest_pre_dispatch', array( $this, 'block_rest_users' ), 10, 3 ); add_filter( 'wp_sitemaps_add_provider', array( $this, 'remove_user_sitemap' ), 10, 2 ); } if ( Settings::get( 'harden_generator' ) ) { remove_action( 'wp_head', 'wp_generator' ); add_filter( 'the_generator', '__return_empty_string' ); } if ( Settings::get( 'harden_app_passwords' ) ) { add_filter( 'wp_is_application_passwords_available', '__return_false' ); } } /** * Turn off the theme and plugin file editors. Respects a value already set in wp-config.php. */ protected function disable_file_editor() { if ( ! defined( 'DISALLOW_FILE_EDIT' ) ) { define( 'DISALLOW_FILE_EDIT', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- WordPress core constant, only set when not already defined. } } /** * Visitors who are not logged in cannot use ?author=N to find usernames: they are sent to the * home page instead of being redirected to the author's archive URL. */ public function block_author_scan() { if ( is_admin() || is_user_logged_in() ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check of a public query var. if ( isset( $_GET['author'] ) ) { Log::add( 'user_enum', __( '?author= request redirected to the home page', '007aj-security-scanner-firewall' ) ); wp_safe_redirect( home_url( '/' ), 301 ); exit; } } /** * The REST users endpoints answer 401 to visitors who are not logged in. * * @param mixed $result Response so far. * @param \WP_REST_Server $server Server. * @param \WP_REST_Request $request Request. * @return mixed */ public function block_rest_users( $result, $server, $request ) { unset( $server ); if ( null !== $result || is_user_logged_in() ) { return $result; } if ( 0 === strpos( (string) $request->get_route(), '/wp/v2/users' ) ) { Log::add( 'user_enum', __( 'REST user listing refused', '007aj-security-scanner-firewall' ) ); return new \WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to list users.', '007aj-security-scanner-firewall' ), array( 'status' => 401 ) ); } return $result; } /** * Remove the users sitemap (it lists author archive URLs, which contain usernames). * * @param \WP_Sitemaps_Provider|false $provider Provider. * @param string $name Provider name. * @return \WP_Sitemaps_Provider|false */ public function remove_user_sitemap( $provider, $name ) { return 'users' === $name ? false : $provider; } }