*/ public static function defaults(): array { return array( 'enabled' => false, 'hide_generator' => true, // Remove the WP version meta tag + feeds. 'strip_ver' => true, // Strip ?ver= from enqueued assets. 'remove_headers' => true, // RSD, wlwmanifest, shortlink head links. 'disable_xmlrpc' => true, // Kill xmlrpc.php (pingback DDoS vector). 'hide_login_error' => true, // "Wrong password" reveals valid usernames. 'block_enum' => true, // ?author=N redirects + REST /users lockdown. 'security_headers' => true, // X-Content-Type-Options, X-Frame-Options, referrer policy. 'hide_admin_bar' => false, // Hide this plugin's own admin-bar menu. ); } public function register( Container $container ): void {} public function boot( Container $container ): void { add_action( 'rest_api_init', array( $this, 'routes' ) ); $s = $container->get( Options::class )->get( 'hardening', self::defaults() ); if ( empty( $s['enabled'] ) ) { return; } if ( ! empty( $s['hide_generator'] ) ) { remove_action( 'wp_head', 'wp_generator' ); add_filter( 'the_generator', '__return_empty_string' ); } if ( ! empty( $s['strip_ver'] ) ) { $strip = static function ( string $src ): string { return $src ? (string) remove_query_arg( 'ver', $src ) : $src; }; add_filter( 'style_loader_src', $strip, 999 ); add_filter( 'script_loader_src', $strip, 999 ); // Script modules (WP 6.5+) version with opaque content hashes, which // leak nothing — strip anyway where core provides the filter. add_filter( 'script_module_loader_src', $strip, 999 ); } if ( ! empty( $s['remove_headers'] ) ) { remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' ); remove_action( 'wp_head', 'wp_shortlink_wp_head' ); remove_action( 'template_redirect', 'wp_shortlink_header', 11 ); } if ( ! empty( $s['disable_xmlrpc'] ) ) { add_filter( 'xmlrpc_enabled', '__return_false' ); add_filter( 'wp_headers', static function ( array $headers ): array { unset( $headers['X-Pingback'] ); return $headers; } ); add_filter( 'pings_open', '__return_false' ); } if ( ! empty( $s['hide_login_error'] ) ) { add_filter( 'login_errors', static fn() => __( 'The credentials do not match. Please try again.', 'ai-rank-suite' ) ); } if ( ! empty( $s['block_enum'] ) ) { // /?author=1 must not redirect to /author/username/. Guard::action( 'template_redirect', static function (): void { if ( is_admin() ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['author'] ) && ! is_user_logged_in() ) { wp_safe_redirect( home_url( '/' ), 301 ); exit; } }, 0 ); // REST /wp/v2/users lists usernames to anyone — require login. add_filter( 'rest_endpoints', static function ( array $endpoints ): array { if ( is_user_logged_in() ) { return $endpoints; } unset( $endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P[\d]+)'] ); return $endpoints; } ); // Author sitemaps expose the same list. add_filter( 'wp_sitemaps_add_provider', static fn( $provider, $name ) => 'users' === $name ? false : $provider, 10, 2 ); } if ( ! empty( $s['security_headers'] ) ) { add_action( 'send_headers', static function (): void { if ( headers_sent() ) { return; } 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=()' ); } ); } } public function routes(): void { // .htaccess. Every route here sits behind manage_settings and, on the // write paths, a rate limit — a control that can 500 the entire site // should not be reachable in a loop. register_rest_route( self::NAMESPACE, '/htaccess', array( array( 'methods' => 'GET', 'callback' => static fn() => new WP_REST_Response( array_merge( \AiRankSuite\Modules\Advanced\Services\Htaccess::state(), array( 'recipes' => \AiRankSuite\Modules\Advanced\Services\Htaccess::recipes() ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => static function ( WP_REST_Request $request ) { $result = \AiRankSuite\Modules\Advanced\Services\Htaccess::save( (string) $request->get_param( 'block' ) ); return $result instanceof WP_Error ? $result : new WP_REST_Response( $result ); }, 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'htaccess', 30 ); }, 'args' => array( 'block' => array( 'type' => 'string', 'required' => true, ), ), ), ) ); register_rest_route( self::NAMESPACE, '/htaccess/check', array( 'methods' => 'POST', 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( array( 'problems' => \AiRankSuite\Modules\Advanced\Services\Htaccess::problems( (string) $request->get_param( 'block' ) ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'block' => array( 'type' => 'string', 'required' => true, ), ), ) ); register_rest_route( self::NAMESPACE, '/htaccess/restore', array( 'methods' => 'POST', 'callback' => static function ( WP_REST_Request $request ) { $result = \AiRankSuite\Modules\Advanced\Services\Htaccess::restore( (string) $request->get_param( 'id' ) ); return $result instanceof WP_Error ? $result : new WP_REST_Response( $result ); }, 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'htaccess', 30 ); }, 'args' => array( 'id' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key', ), ), ) ); register_rest_route( self::NAMESPACE, '/settings/hardening', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array_map( static fn() => array( 'type' => 'boolean' ), self::defaults() ), ), ) ); } public function get_settings(): WP_REST_Response { return new WP_REST_Response( array( 'settings' => \AiRankSuite\Kernel::service( Options::class )->get( 'hardening', self::defaults() ), ) ); } public function update_settings( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = (bool) $request->get_param( $key ); } } \AiRankSuite\Kernel::service( Options::class )->set( 'hardening', $values, $defaults ); return $this->get_settings(); } }