'SAMEORIGIN', 'X-Content-Type-Options' => 'nosniff', 'Referrer-Policy' => 'strict-origin-when-cross-origin', ); } /** * Whether a query array looks like a ?author=N enumeration probe. Pure. * * @param array $query Query vars (e.g. from $_GET). * @return bool */ public static function is_author_enumeration( $query ) { return isset( $query['author'] ) && is_numeric( $query['author'] ); } public static function empty_generator() { return ''; } public static function send_security_headers() { foreach ( self::security_headers() as $name => $value ) { header( $name . ': ' . $value ); } } /** * Redirect ?author=N probes (used to harvest usernames) to the home page, * only for logged-out visitors on the front end. */ public static function block_author_enumeration() { if ( is_admin() || is_user_logged_in() ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only public query param, no form processing or state change. $author = isset( $_GET['author'] ) ? sanitize_text_field( wp_unslash( $_GET['author'] ) ) : ''; $query = ( '' !== $author ) ? array( 'author' => $author ) : array(); if ( self::is_author_enumeration( $query ) ) { wp_safe_redirect( home_url( '/' ), 301 ); exit; } } /** * Remove the REST users endpoints for logged-out requests (blocks * /wp-json/wp/v2/users enumeration) while keeping them for authed users. * * @param array $endpoints REST endpoints. * @return array */ public static function restrict_rest_users( $endpoints ) { if ( is_user_logged_in() ) { return $endpoints; } foreach ( array( '/wp/v2/users', '/wp/v2/users/(?P[\d]+)' ) as $route ) { if ( isset( $endpoints[ $route ] ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } }