(string) get_option( 'cfsp_worker_key' ) ); } /** Is this request the exporter (right secret key)? */ public static function is_exporter() { $sent = isset( $_SERVER['HTTP_X_CFSP_KEY'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_CFSP_KEY'] ) ) : ''; $key = (string) get_option( 'cfsp_worker_key' ); return '' !== $sent && '' !== $key && hash_equals( $key, $sent ); } /** * WordPress.com calling Jetpack's own REST API (jetpack/… and wpcom/… routes). Those endpoints * verify WordPress.com's signature themselves, so letting them through keeps the Jetpack * connection working ("WordPress.com requests to your site are being blocked") while every * other page and API stays private. * * @param WP $wp */ private static function is_jetpack_request( $wp ) { if ( ! defined( 'JETPACK__VERSION' ) && ! class_exists( 'Automattic\Jetpack\Connection\Manager' ) ) { return false; } $route = isset( $wp->query_vars['rest_route'] ) ? (string) $wp->query_vars['rest_route'] : ''; $allow = '' !== $route && (bool) preg_match( '#^/(?:jetpack|wpcom)/#', $route ); return (bool) apply_filters( 'cfsp_private_allow_jetpack', $allow, $route ); } public static function noindex_header() { if ( ! headers_sent() && ! self::is_exporter() ) { header( 'X-Robots-Tag: noindex, nofollow', true ); } } /** @param WP $wp */ public static function gate( $wp ) { if ( self::is_exporter() || is_user_logged_in() || self::is_jetpack_request( $wp ) ) { return; } self::noindex_header(); nocache_headers(); // Bots asking for robots.txt get a "keep out" file. (The exporter gets the real one.) if ( ! empty( $wp->query_vars['robots'] ) ) { status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); echo "User-agent: *\nDisallow: /\n"; exit; } if ( 'notfound' === CFSP_Settings::get( 'private_action' ) ) { status_header( 404 ); header( 'Content-Type: text/html; charset=utf-8' ); echo 'Not found

Not found.

'; exit; } $here = set_url_scheme( 'http' . ( is_ssl() ? 's' : '' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/' ) ); wp_safe_redirect( wp_login_url( $here ), 302 ); exit; } /** * Health Check: request the homepage the way a visitor would and the way the exporter does. * * @return array[] Diagnostics rows. */ public static function diagnostics() { if ( ! self::enabled() ) { return array( CFSP_Diagnostics::check( 'Private WordPress', 'info', 'Off: anyone can view this WordPress site. Turn it on in Stealth if WordPress runs on its own hostname.' ) ); } // Ask WordPress itself (straight to this server when direct fetch is on): when the static site // shares the hostname, the normal route reaches the static host, not WordPress. $args = CFSP_Exporter::loopback_args( array( 'timeout' => 15, 'redirection' => 0, 'sslverify' => (bool) CFSP_Settings::get( 'sslverify' ), ) ); $route = empty( $args['cfsp_direct'] ) ? '' : ' (asked directly on this server)'; $visitor = wp_remote_get( home_url( '/' ), $args ); $code = is_wp_error( $visitor ) ? 0 : (int) wp_remote_retrieve_response_code( $visitor ); $blocked = in_array( $code, array( 302, 404 ), true ); if ( 403 === $code && CFSP_Diagnostics::is_proxy_block( $visitor ) ) { return array( CFSP_Diagnostics::check( 'Private WordPress: visitors', 'info', 'Can\'t be tested from this server: Cloudflare (or another firewall in front of WordPress) answers its requests with HTTP 403 before WordPress runs. Turn on direct fetch under Advanced to test WordPress itself.' ) ); } if ( $blocked && CFSP_Exporter::is_login_wall( (string) wp_remote_retrieve_header( $visitor, 'location' ) ) ) { return array( CFSP_Diagnostics::check( 'Private WordPress: visitors', 'info', 'Cloudflare Access answers before WordPress, so this mode can\'t be tested yet. Test again after removing the Access application.' ) ); } $out = array( CFSP_Diagnostics::check( 'Private WordPress: visitors', $blocked ? 'ok' : 'fail', $blocked ? 'Blocked (HTTP ' . $code . '), with an X-Robots-Tag: noindex header' . $route . '.' : 'Not blocked: a logged-out request got ' . ( $code ? 'HTTP ' . $code : $visitor->get_error_message() ) . $route . '. A page cache may be serving pages before WordPress runs; exclude this hostname from it.' ), ); if ( is_file( CFSP_Paths::home_dir() . 'robots.txt' ) ) { $out[] = CFSP_Diagnostics::check( 'Private WordPress: robots.txt', 'warn', 'A robots.txt file exists on disk, so the web server shows it to everyone instead of the "keep out" version. The static site still gets it. Harmless while the noindex header is sent, but you can block it in the web server config.' ); } return $out; } }