false,
'disable_file_edit' => false,
'disable_xmlrpc' => false,
'disable_feeds' => false,
'send_nosniff' => true,
'send_frame_guard' => true,
'send_referrer' => true,
'send_hsts' => true,
'send_permissions' => true,
'header_xss' => false,
'hide_users' => true,
'keep_readme_deleted' => false,
'lock_system_folders' => false,
);
}
public static function settings() {
$stored = get_option( self::OPTION, array() );
if ( ! is_array( $stored ) ) {
$stored = array();
}
$out = self::defaults();
foreach ( $out as $key => $default ) {
$out[ $key ] = isset( $stored[ $key ] ) ? (bool) $stored[ $key ] : $default;
}
return $out;
}
public static function is_on( $key ) {
$settings = self::settings();
return ! empty( $settings[ $key ] );
}
public static function register() {
$on = self::settings();
if ( $on['hide_version'] ) {
self::hide_version();
}
if ( $on['disable_file_edit'] ) {
add_filter( 'map_meta_cap', array( __CLASS__, 'block_file_edit' ), 10, 2 );
}
if ( $on['disable_xmlrpc'] ) {
self::restrain_xmlrpc();
}
if ( $on['disable_feeds'] ) {
self::refuse_feeds();
}
if ( $on['send_nosniff'] || $on['send_frame_guard'] || $on['send_referrer'] || $on['send_hsts'] || $on['send_permissions'] || $on['header_xss'] ) {
self::send_headers();
}
if ( $on['hide_users'] ) {
self::hide_users();
}
if ( $on['keep_readme_deleted'] ) {
add_action( '_core_updated_successfully', array( __CLASS__, 'delete_readme' ) );
add_action( 'upgrader_process_complete', array( __CLASS__, 'delete_readme_after_core_update' ), 10, 2 );
}
}
private static function hide_version() {
remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_empty_string' );
foreach ( array( 'html', 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export' ) as $type ) {
add_filter( 'get_the_generator_' . $type, '__return_empty_string' );
}
}
public static function block_file_edit( $caps, $cap ) {
if ( 'edit_themes' === $cap || 'edit_plugins' === $cap || 'edit_files' === $cap ) {
return array( 'do_not_allow' );
}
return $caps;
}
private static function restrain_xmlrpc() {
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'xmlrpc_methods', array( __CLASS__, 'drop_pingback_methods' ) );
add_filter( 'wp_headers', array( __CLASS__, 'drop_pingback_header' ) );
remove_action( 'wp_head', 'rsd_link' );
}
public static function drop_pingback_methods( $methods ) {
if ( ! is_array( $methods ) ) {
return $methods;
}
unset( $methods['pingback.ping'], $methods['pingback.extensions.getPingbacks'] );
return $methods;
}
public static function drop_pingback_header( $headers ) {
if ( is_array( $headers ) ) {
unset( $headers['X-Pingback'] );
}
return $headers;
}
private static function refuse_feeds() {
$hooks = array(
'do_feed',
'do_feed_rdf',
'do_feed_rss',
'do_feed_rss2',
'do_feed_atom',
'do_feed_rss2_comments',
'do_feed_atom_comments',
);
foreach ( $hooks as $hook ) {
add_action( $hook, array( __CLASS__, 'refuse_feed' ), 1 );
}
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
}
public static function refuse_feed() {
if ( apply_filters( 'aieo_security_hardening_allow_feed', false ) ) {
return;
}
status_header( 410 );
nocache_headers();
header( 'Content-Type: text/plain; charset=utf-8' );
echo "This site does not publish feeds.\n";
exit;
}
private static function send_headers() {
add_filter( 'wp_headers', array( __CLASS__, 'add_headers' ), 10, 2 );
add_filter( 'rest_post_dispatch', array( __CLASS__, 'add_rest_headers' ), 10, 1 );
add_action( 'admin_init', array( __CLASS__, 'send_admin_headers' ) );
add_action( 'login_init', array( __CLASS__, 'send_admin_headers' ) );
}
public static function header_map( $context = 'page' ) {
$on = self::settings();
$out = array();
if ( $on['send_nosniff'] ) {
$out['X-Content-Type-Options'] = 'nosniff';
}
if ( $on['send_frame_guard'] && 'admin' !== $context ) {
$ancestors = self::frame_ancestors();
$out['Content-Security-Policy'] = 'frame-ancestors ' . implode( ' ', $ancestors );
if ( array( "'self'" ) === $ancestors ) {
$out['X-Frame-Options'] = 'SAMEORIGIN';
}
}
if ( $on['send_referrer'] ) {
$out['Referrer-Policy'] = 'strict-origin-when-cross-origin';
}
if ( $on['send_hsts'] && self::is_https() ) {
$out['Strict-Transport-Security'] = 'max-age=' . self::HSTS_MAX_AGE;
}
if ( $on['send_permissions'] && 'page' === $context ) {
$out['Permissions-Policy'] = self::PERMISSIONS_POLICY;
}
if ( $on['header_xss'] ) {
$out['X-XSS-Protection'] = '1; mode=block';
}
$out = apply_filters( 'aieo_security_hardening_headers', $out, (string) $context );
$map = array();
foreach ( is_array( $out ) ? $out : array() as $name => $value ) {
if ( is_string( $name ) && '' !== $name && is_scalar( $value ) && '' !== (string) $value ) {
$map[ $name ] = (string) $value;
}
}
return $map;
}
public static function frame_ancestors() {
$out = array( "'self'" );
$home = self::origin( home_url( '/' ) );
$admin = self::origin( admin_url( '/' ) );
if ( '' !== $admin && $admin !== $home ) {
$out[] = $admin;
}
return $out;
}
private static function origin( $url ) {
$parts = wp_parse_url( (string) $url );
if ( ! is_array( $parts ) || empty( $parts['host'] ) ) {
return '';
}
$scheme = isset( $parts['scheme'] ) ? strtolower( (string) $parts['scheme'] ) : 'https';
$port = isset( $parts['port'] ) ? ':' . (int) $parts['port'] : '';
return $scheme . '://' . strtolower( (string) $parts['host'] ) . $port;
}
public static function is_https() {
if ( is_ssl() ) {
return true;
}
if ( 0 !== strpos( (string) home_url( '/' ), 'https://' ) ) {
return false;
}
$proto = isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ? strtolower( trim( explode( ',', (string) wp_unslash( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) )[0] ) ) : '';
$cf = isset( $_SERVER['HTTP_CF_VISITOR'] ) ? (string) wp_unslash( $_SERVER['HTTP_CF_VISITOR'] ) : '';
return 'https' === $proto || false !== strpos( $cf, '"https"' );
}
private static function queued_header_names() {
$out = array();
foreach ( headers_list() as $line ) {
$name = strtolower( trim( (string) strstr( $line, ':', true ) ) );
if ( '' !== $name ) {
$out[ $name ] = true;
}
}
return $out;
}
public static function add_headers( $headers, $wp = null ) {
if ( ! is_array( $headers ) ) {
return $headers;
}
$map = self::header_map( 'page' );
if ( is_object( $wp ) && isset( $wp->query_vars ) && ! empty( $wp->query_vars['embed'] ) ) {
unset( $map['X-Frame-Options'], $map['Content-Security-Policy'] );
}
$taken = self::queued_header_names();
foreach ( array_keys( $headers ) as $name ) {
$taken[ strtolower( (string) $name ) ] = true;
}
foreach ( $map as $name => $value ) {
if ( ! isset( $taken[ strtolower( $name ) ] ) ) {
$headers[ $name ] = $value;
}
}
return $headers;
}
public static function add_rest_headers( $response ) {
if ( ! $response instanceof WP_HTTP_Response ) {
return $response;
}
$taken = array();
foreach ( array_keys( (array) $response->get_headers() ) as $name ) {
$taken[ strtolower( (string) $name ) ] = true;
}
foreach ( self::header_map( 'rest' ) as $name => $value ) {
if ( ! isset( $taken[ strtolower( $name ) ] ) ) {
$response->header( $name, $value, false );
}
}
return $response;
}
public static function send_admin_headers() {
if ( headers_sent() ) {
return;
}
$taken = self::queued_header_names();
foreach ( self::header_map( 'admin' ) as $name => $value ) {
if ( ! isset( $taken[ strtolower( $name ) ] ) ) {
header( $name . ': ' . $value );
}
}
}
private static function hide_users() {
add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'guard_user_routes' ), 10, 3 );
add_action( 'template_redirect', array( __CLASS__, 'refuse_author_by_id' ), 1 );
add_filter( 'oembed_response_data', array( __CLASS__, 'oembed_without_author' ), 20 );
add_filter( 'wp_sitemaps_add_provider', array( __CLASS__, 'drop_users_sitemap' ), 10, 2 );
add_filter( 'comment_class', array( __CLASS__, 'comment_class_without_login' ) );
}
public static function can_see_users() {
static $memo = array();
$uid = get_current_user_id();
if ( ! isset( $memo[ $uid ] ) ) {
$can = false;
if ( $uid > 0 ) {
$can = current_user_can( 'list_users' ) || current_user_can( 'edit_posts' );
if ( ! $can ) {
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $type ) {
if ( isset( $type->cap->edit_posts ) && current_user_can( $type->cap->edit_posts ) ) {
$can = true;
break;
}
}
}
}
$memo[ $uid ] = $can;
}
return (bool) apply_filters( 'aieo_security_hardening_can_see_users', $memo[ $uid ] );
}
public static function guard_user_routes( $response, $handler, $request ) {
unset( $handler );
if ( is_wp_error( $response ) || ! $request instanceof WP_REST_Request ) {
return $response;
}
if ( ! in_array( $request->get_method(), array( 'GET', 'HEAD' ), true ) ) {
return $response;
}
if ( ! preg_match( '#^/wp/v2/users(?:/(\d+|me))?/?$#', (string) $request->get_route(), $m ) ) {
return $response;
}
if ( isset( $m[1] ) && ( 'me' === $m[1] || ( is_user_logged_in() && (int) $m[1] === get_current_user_id() ) ) ) {
return $response;
}
if ( self::can_see_users() ) {
return $response;
}
return new WP_Error(
'rest_user_cannot_view',
__( 'Only signed-in staff can see the accounts of this site.', 'ai-eshop-optimizer' ),
array( 'status' => rest_authorization_required_code() )
);
}
public static function refuse_author_by_id() {
global $wp, $wp_query;
if ( ! is_author() || ! is_object( $wp ) || empty( $wp->query_vars['author'] ) || ! empty( $wp->query_vars['author_name'] ) ) {
return;
}
if ( self::can_see_users() ) {
return;
}
$wp_query->set_404();
status_header( 404 );
nocache_headers();
remove_action( 'template_redirect', 'redirect_canonical' );
}
public static function oembed_without_author( $data ) {
if ( ! is_array( $data ) ) {
return $data;
}
if ( isset( $data['author_name'] ) ) {
$data['author_name'] = get_bloginfo( 'name' );
}
if ( isset( $data['author_url'] ) ) {
$data['author_url'] = get_home_url();
}
return $data;
}
public static function drop_users_sitemap( $provider, $name ) {
return 'users' === $name ? false : $provider;
}
public static function comment_class_without_login( $classes ) {
if ( ! is_array( $classes ) || self::can_see_users() ) {
return $classes;
}
return array_values(
array_filter(
$classes,
static function ( $class ) {
return 0 !== strpos( (string) $class, 'comment-author-' );
}
)
);
}
const HTACCESS_MARKER = 'AIEO Security Tightening';
public static function server_reads_htaccess() {
$software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? strtolower( (string) $_SERVER['SERVER_SOFTWARE'] ) : '';
if ( '' === $software ) {
return function_exists( 'apache_get_modules' );
}
foreach ( array( 'apache', 'litespeed', 'lsws' ) as $needle ) {
if ( false !== strpos( $software, $needle ) ) {
return true;
}
}
return false;
}
private static function htaccess_targets() {
$deny = "\n"
. "\n"
. "Require all denied\n"
. "\n"
. "\n"
. "Order allow,deny\n"
. "Deny from all\n"
. "\n"
. "";
$allow = static function ( $file ) {
return "\n"
. "\n"
. "Require all granted\n"
. "\n"
. "\n"
. "Order allow,deny\n"
. "Allow from all\n"
. "\n"
. "";
};
$uploads = wp_get_upload_dir();
$updir = ( is_array( $uploads ) && ! empty( $uploads['basedir'] ) )
? $uploads['basedir']
: WP_CONTENT_DIR . '/uploads';
return array(
ABSPATH . 'wp-includes' => $deny . "\n" . $allow( 'wp-tinymce.php' ) . "\n" . $allow( 'ms-files.php' ),
$updir => $deny,
);
}
public static function apply_folder_lock( $enable ) {
if ( ! function_exists( 'insert_with_markers' ) ) {
require_once ABSPATH . 'wp-admin/includes/misc.php';
}
$results = array();
foreach ( self::htaccess_targets() as $dir => $rules ) {
$file = rtrim( $dir, '/\\' ) . '/.htaccess';
if ( ! is_dir( $dir ) ) {
$results[ $dir ] = 'failed';
continue;
}
$writable = file_exists( $file ) ? is_writable( $file ) : is_writable( $dir );
if ( ! $writable ) {
$results[ $dir ] = 'unwritable';
continue;
}
if ( ! $enable && ! file_exists( $file ) ) {
$results[ $dir ] = 'removed';
continue;
}
$lines = $enable ? explode( "\n", $rules ) : array();
$ok = insert_with_markers( $file, self::HTACCESS_MARKER, $lines );
$results[ $dir ] = $ok ? ( $enable ? 'written' : 'removed' ) : 'failed';
if ( $ok && ! $enable && file_exists( $file ) ) {
$body = (string) file_get_contents( $file );
$left = trim( (string) preg_replace(
'/# BEGIN ' . preg_quote( self::HTACCESS_MARKER, '/' ) . '.*?# END ' . preg_quote( self::HTACCESS_MARKER, '/' ) . '/s',
'',
$body
) );
if ( '' === $left ) {
wp_delete_file( $file );
}
}
}
return $results;
}
public static function folder_lock_state() {
$state = array();
foreach ( array_keys( self::htaccess_targets() ) as $dir ) {
$file = rtrim( $dir, '/\\' ) . '/.htaccess';
$has = false;
if ( file_exists( $file ) && is_readable( $file ) ) {
$body = (string) file_get_contents( $file );
$begin = '# BEGIN ' . self::HTACCESS_MARKER;
$end = '# END ' . self::HTACCESS_MARKER;
$from = strpos( $body, $begin );
$to = ( false === $from ) ? false : strpos( $body, $end, $from );
$has = ( false !== $from && false !== $to )
&& false !== strpos( substr( $body, $from, $to - $from ), 'Require all denied' );
}
$state[ $dir ] = $has;
}
return $state;
}
public static function delete_readme_after_core_update( $hook_extra, $options = null ) {
unset( $hook_extra );
if ( is_array( $options ) && isset( $options['type'] ) && 'core' === $options['type'] ) {
self::delete_readme();
}
}
public static function delete_readme() {
$path = ABSPATH . 'readme.html';
if ( ! file_exists( $path ) ) {
return 'absent';
}
if ( ! is_writable( dirname( $path ) ) ) {
return 'unwritable';
}
wp_delete_file( $path );
clearstatcache( true, $path );
return file_exists( $path ) ? 'failed' : 'deleted';
}
}
}