From e78d988aa2ee98338715520686a3bf04a1a193e7 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 23 Dec 2020 19:11:20 +0000 Subject: [PATCH] Security, Site Health: Detect HTTPS support and encourage switching. This changeset modifies the Site Health panel for HTTPS to provide more accurate recommendations based on whether the environment is already set up for HTTPS. * Introduces `wp_is_using_https()` to check whether the site is configured to use HTTPS (via its Site Address and WordPress Address). * Introduces `wp_is_https_supported()` to check whether the environment supports HTTPS. This relies on a cron job which periodically checks support using a loopback request. Props Clorith, flixos90, miinasikk, westonruter. Fixes #47577. git-svn-id: https://develop.svn.wordpress.org/trunk@49904 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-site-health.php | 42 ++- src/wp-includes/default-filters.php | 5 + src/wp-includes/https-detection.php | 181 ++++++++++ src/wp-settings.php | 1 + tests/phpunit/tests/https-detection.php | 313 ++++++++++++++++++ 5 files changed, 528 insertions(+), 14 deletions(-) create mode 100644 src/wp-includes/https-detection.php create mode 100644 tests/phpunit/tests/https-detection.php diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index fcc1ed7247..711fe19dc1 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -1493,12 +1493,13 @@ class WP_Site_Health { * enabled, but only if you visit the right site address. * * @since 5.2.0 + * @since 5.7.0 Updated to rely on {@see wp_is_using_https()} and {@see wp_is_https_supported()}. * * @return array The test results. */ public function get_test_https_status() { $result = array( - 'label' => __( 'Your website is using an active HTTPS connection.' ), + 'label' => __( 'Your website is using an active HTTPS connection' ), 'status' => 'good', 'badge' => array( 'label' => __( 'Security' ), @@ -1519,15 +1520,11 @@ class WP_Site_Health { 'test' => 'https_status', ); - if ( is_ssl() ) { - $wp_url = get_bloginfo( 'wpurl' ); - $site_url = get_bloginfo( 'url' ); - - if ( 'https' !== substr( $wp_url, 0, 5 ) || 'https' !== substr( $site_url, 0, 5 ) ) { - $result['status'] = 'recommended'; - - $result['label'] = __( 'Only parts of your site are using HTTPS' ); + if ( ! wp_is_using_https() ) { + $result['status'] = 'critical'; + $result['label'] = __( 'Your website does not use HTTPS' ); + if ( is_ssl() ) { $result['description'] = sprintf( '

%s

', sprintf( @@ -1536,17 +1533,34 @@ class WP_Site_Health { esc_url( admin_url( 'options-general.php' ) ) ) ); + } else { + $result['description'] = sprintf( + '

%s

', + sprintf( + /* translators: %s: URL to General Settings screen. */ + __( 'Your WordPress Address is not set up to use HTTPS.' ), + esc_url( admin_url( 'options-general.php' ) ) + ) + ); + } - $result['actions'] .= sprintf( + if ( wp_is_https_supported() ) { + $result['description'] .= sprintf( + '

%s

', + __( 'HTTPS is already supported for your website.' ) + ); + + $result['actions'] = sprintf( '

%s

', esc_url( admin_url( 'options-general.php' ) ), __( 'Update your site addresses' ) ); + } else { + $result['description'] .= sprintf( + '

%s

', + __( 'Talk to your web host about supporting HTTPS for your website.' ) + ); } - } else { - $result['status'] = 'recommended'; - - $result['label'] = __( 'Your site does not use HTTPS' ); } return $result; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 96ef7d82eb..334a12a929 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -337,6 +337,11 @@ if ( ! defined( 'DOING_CRON' ) ) { add_action( 'init', 'wp_cron' ); } +// HTTPS detection. +add_action( 'init', 'wp_schedule_https_detection' ); +add_action( 'wp_https_detection', 'wp_update_https_detection_errors' ); +add_filter( 'cron_request', 'wp_cron_conditionally_prevent_sslverify', 9999 ); + // 2 Actions 2 Furious. add_action( 'do_feed_rdf', 'do_feed_rdf', 10, 0 ); add_action( 'do_feed_rss', 'do_feed_rss', 10, 0 ); diff --git a/src/wp-includes/https-detection.php b/src/wp-includes/https-detection.php new file mode 100644 index 0000000000..895b3a8516 --- /dev/null +++ b/src/wp-includes/https-detection.php @@ -0,0 +1,181 @@ + array( + 'Cache-Control' => 'no-cache', + ), + 'sslverify' => true, + ) + ); + + if ( is_wp_error( $response ) ) { + $unverified_response = wp_remote_request( + home_url( '/', 'https' ), + array( + 'headers' => array( + 'Cache-Control' => 'no-cache', + ), + 'sslverify' => false, + ) + ); + + if ( is_wp_error( $unverified_response ) ) { + $support_errors->add( + $unverified_response->get_error_code(), + $unverified_response->get_error_message() + ); + } else { + $support_errors->add( + 'ssl_verification_failed', + $response->get_error_message() + ); + } + + $response = $unverified_response; + } + + if ( ! is_wp_error( $response ) ) { + if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { + $support_errors->add( 'bad_response_code', wp_remote_retrieve_response_message( $response ) ); + } elseif ( false === wp_is_owned_html_output( wp_remote_retrieve_body( $response ) ) ) { + $support_errors->add( 'bad_response_source', __( 'It looks like the response did not come from this site.' ) ); + } + } + + update_option( 'https_detection_errors', $support_errors->errors ); +} + +/** + * Schedules the Cron hook for detecting HTTPS support. + * + * @since 5.7.0 + * @access private + */ +function wp_schedule_https_detection() { + if ( ! wp_next_scheduled( 'wp_https_detection' ) ) { + wp_schedule_event( time(), 'twicedaily', 'wp_https_detection' ); + } +} + +/** + * Disables SSL verification if the 'cron_request' arguments include an HTTPS URL. + * + * This prevents an issue if HTTPS breaks, where there would be a failed attempt to verify HTTPS. + * + * @since 5.7.0 + * @access private + * + * @param array $request The Cron request arguments. + * @return array $request The filtered Cron request arguments. + */ +function wp_cron_conditionally_prevent_sslverify( $request ) { + if ( 'https' === wp_parse_url( $request['url'], PHP_URL_SCHEME ) ) { + $request['args']['sslverify'] = false; + } + return $request; +} + +/** + * Checks whether a given HTML string is likely an output from this WordPress site. + * + * This function attempts to check for various common WordPress patterns whether they are included in the HTML string. + * Since any of these actions may be disabled through third-party code, this function may also return null to indicate + * that it was not possible to determine ownership. + * + * @since 5.7.0 + * @access private + * + * @param string $html Full HTML output string, e.g. from a HTTP response. + * @return bool|null True/false for whether HTML was generated by this site, null if unable to determine. + */ +function wp_is_owned_html_output( $html ) { + // 1. Check if HTML includes the site's Really Simple Discovery link. + if ( has_action( 'wp_head', 'rsd_link' ) ) { + $pattern = esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ); // See rsd_link(). + return false !== strpos( $html, $pattern ); + } + + // 2. Check if HTML includes the site's Windows Live Writer manifest link. + if ( has_action( 'wp_head', 'wlwmanifest_link' ) ) { + // Try both HTTPS and HTTP since the URL depends on context. + $pattern = preg_replace( '#^https?:(?=//)#', '', includes_url( 'wlwmanifest.xml' ) ); // See wlwmanifest_link(). + return false !== strpos( $html, $pattern ); + } + + // 3. Check if HTML includes the site's REST API link. + if ( has_action( 'wp_head', 'rest_output_link_wp_head' ) ) { + // Try both HTTPS and HTTP since the URL depends on context. + $pattern = esc_url( preg_replace( '#^https?:(?=//)#', '', get_rest_url() ) ); // See rest_output_link_wp_head(). + return false !== strpos( $html, $pattern ); + } + + // Otherwise the result cannot be determined. + return null; +} diff --git a/src/wp-settings.php b/src/wp-settings.php index 71868ecc89..63c59c6cef 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -171,6 +171,7 @@ require ABSPATH . WPINC . '/class-wp-date-query.php'; require ABSPATH . WPINC . '/theme.php'; require ABSPATH . WPINC . '/class-wp-theme.php'; require ABSPATH . WPINC . '/template.php'; +require ABSPATH . WPINC . '/https-detection.php'; require ABSPATH . WPINC . '/class-wp-user-request.php'; require ABSPATH . WPINC . '/user.php'; require ABSPATH . WPINC . '/class-wp-user-query.php'; diff --git a/tests/phpunit/tests/https-detection.php b/tests/phpunit/tests/https-detection.php new file mode 100644 index 0000000000..b02cbb5fb5 --- /dev/null +++ b/tests/phpunit/tests/https-detection.php @@ -0,0 +1,313 @@ +assertFalse( wp_is_using_https() ); + + // Expect false if only one of the two relevant URLs is HTTPS. + update_option( 'siteurl', 'https://example.com/' ); + $this->assertFalse( wp_is_using_https() ); + + update_option( 'home', 'https://example.com/' ); + $this->assertTrue( wp_is_using_https() ); + + // Test that the manually included 'site_url' filter works as expected + // by using it to set the URL to use HTTP. + add_filter( 'site_url', $this->filter_set_url_scheme( 'http' ) ); + $this->assertFalse( wp_is_using_https() ); + } + + /** + * @ticket 47577 + */ + public function test_wp_is_https_supported() { + // The function works with cached errors, so only test that here. + $wp_error = new WP_Error(); + + // No errors, so HTTPS is supported. + update_option( 'https_detection_errors', $wp_error->errors ); + $this->assertTrue( wp_is_https_supported() ); + + // Errors, so HTTPS is not supported. + $wp_error->add( 'ssl_verification_failed', 'SSL verification failed.' ); + update_option( 'https_detection_errors', $wp_error->errors ); + $this->assertFalse( wp_is_https_supported() ); + } + + /** + * @ticket 47577 + */ + public function test_wp_update_https_detection_errors() { + // Set HTTP URL, the request below should use its HTTPS version. + update_option( 'home', 'http://example.com/' ); + add_filter( 'pre_http_request', array( $this, 'record_request_url' ), 10, 3 ); + + // If initial request succeeds, all good. + add_filter( 'pre_http_request', array( $this, 'mock_success_with_sslverify' ), 10, 2 ); + wp_update_https_detection_errors(); + $this->assertEquals( array(), get_option( 'https_detection_errors' ) ); + + // If initial request fails and request without SSL verification succeeds, + // return error with 'ssl_verification_failed' error code. + add_filter( 'pre_http_request', array( $this, 'mock_error_with_sslverify' ), 10, 2 ); + add_filter( 'pre_http_request', array( $this, 'mock_success_without_sslverify' ), 10, 2 ); + wp_update_https_detection_errors(); + $this->assertEquals( + array( 'ssl_verification_failed' => array( 'Bad SSL certificate.' ) ), + get_option( 'https_detection_errors' ) + ); + + // If both initial request and request without SSL verification fail, + // return actual error from request. + add_filter( 'pre_http_request', array( $this, 'mock_error_with_sslverify' ), 10, 2 ); + add_filter( 'pre_http_request', array( $this, 'mock_error_without_sslverify' ), 10, 2 ); + wp_update_https_detection_errors(); + $this->assertEquals( + array( 'bad_ssl_certificate' => array( 'Bad SSL certificate.' ) ), + get_option( 'https_detection_errors' ) + ); + + // If request succeeds, but response is not 200, return error with + // 'bad_response_code' error code. + add_filter( 'pre_http_request', array( $this, 'mock_not_found' ), 10, 2 ); + wp_update_https_detection_errors(); + $this->assertEquals( + array( 'bad_response_code' => array( 'Not Found' ) ), + get_option( 'https_detection_errors' ) + ); + + // If request succeeds, but response was not generated by this + // WordPress site, return error with 'bad_response_source' error code. + add_filter( 'pre_http_request', array( $this, 'mock_bad_source' ), 10, 2 ); + wp_update_https_detection_errors(); + $this->assertEquals( + array( 'bad_response_source' => array( 'It looks like the response did not come from this site.' ) ), + get_option( 'https_detection_errors' ) + ); + + // Check that the requests are made to the correct URL. + $this->assertEquals( 'https://example.com/', $this->last_request_url ); + } + + /** + * @ticket 47577 + */ + public function test_wp_schedule_https_detection() { + wp_schedule_https_detection(); + $this->assertEquals( 'twicedaily', wp_get_schedule( 'wp_https_detection' ) ); + } + + /** + * @ticket 47577 + */ + public function test_wp_cron_conditionally_prevent_sslverify() { + // If URL is not using HTTPS, don't set 'sslverify' to false. + $request = array( + 'url' => 'http://example.com/', + 'args' => array( 'sslverify' => true ), + ); + $this->assertEquals( $request, wp_cron_conditionally_prevent_sslverify( $request ) ); + + // If URL is using HTTPS, set 'sslverify' to false. + $request = array( + 'url' => 'https://example.com/', + 'args' => array( 'sslverify' => true ), + ); + $expected = $request; + $expected['args']['sslverify'] = false; + $this->assertEquals( $expected, wp_cron_conditionally_prevent_sslverify( $request ) ); + } + + /** + * @ticket 47577 + */ + public function test_wp_is_owned_html_output_via_rsd_link() { + // HTML includes RSD link. + $head_tag = get_echo( 'rsd_link' ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML includes modified RSD link but same URL. + $head_tag = str_replace( ' />', '>', get_echo( 'rsd_link' ) ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML does not include RSD link. + $html = $this->get_sample_html_string(); + $this->assertFalse( wp_is_owned_html_output( $html ) ); + } + + /** + * @ticket 47577 + */ + public function test_wp_is_owned_html_output_via_wlwmanifest_link() { + remove_action( 'wp_head', 'rsd_link' ); + + // HTML includes WLW manifest link. + $head_tag = get_echo( 'wlwmanifest_link' ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML includes modified WLW manifest link but same URL. + $head_tag = str_replace( ' />', '>', get_echo( 'wlwmanifest_link' ) ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML includes WLW manifest link with alternative URL scheme. + $head_tag = get_echo( 'wlwmanifest_link' ); + $head_tag = false !== strpos( $head_tag, 'https://' ) ? str_replace( 'https://', 'http://', $head_tag ) : str_replace( 'http://', 'https://', $head_tag ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML does not include WLW manifest link. + $html = $this->get_sample_html_string(); + $this->assertFalse( wp_is_owned_html_output( $html ) ); + } + + /** + * @ticket 47577 + */ + public function test_wp_is_owned_html_output_via_rest_link() { + remove_action( 'wp_head', 'rsd_link' ); + remove_action( 'wp_head', 'wlwmanifest_link' ); + + // HTML includes REST API link. + $head_tag = get_echo( 'rest_output_link_wp_head' ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML includes modified REST API link but same URL. + $head_tag = str_replace( ' />', '>', get_echo( 'rest_output_link_wp_head' ) ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML includes REST API link with alternative URL scheme. + $head_tag = get_echo( 'rest_output_link_wp_head' ); + $head_tag = false !== strpos( $head_tag, 'https://' ) ? str_replace( 'https://', 'http://', $head_tag ) : str_replace( 'http://', 'https://', $head_tag ); + $html = $this->get_sample_html_string( $head_tag ); + $this->assertTrue( wp_is_owned_html_output( $html ) ); + + // HTML does not include REST API link. + $html = $this->get_sample_html_string(); + $this->assertFalse( wp_is_owned_html_output( $html ) ); + } + + /** + * @ticket 47577 + */ + public function test_wp_is_owned_html_output_cannot_determine() { + remove_action( 'wp_head', 'rsd_link' ); + remove_action( 'wp_head', 'wlwmanifest_link' ); + remove_action( 'wp_head', 'rest_output_link_wp_head' ); + + // The HTML here doesn't matter because all hooks are removed. + $html = $this->get_sample_html_string(); + $this->assertNull( wp_is_owned_html_output( $html ) ); + } + + public function record_request_url( $preempt, $parsed_args, $url ) { + $this->last_request_url = $url; + return $preempt; + } + + public function mock_success_with_sslverify( $preempt, $parsed_args ) { + if ( ! empty( $parsed_args['sslverify'] ) ) { + return $this->mock_success(); + } + return $preempt; + } + + public function mock_error_with_sslverify( $preempt, $parsed_args ) { + if ( ! empty( $parsed_args['sslverify'] ) ) { + return $this->mock_error(); + } + return $preempt; + } + + public function mock_success_without_sslverify( $preempt, $parsed_args ) { + if ( empty( $parsed_args['sslverify'] ) ) { + return $this->mock_success(); + } + return $preempt; + } + + public function mock_error_without_sslverify( $preempt, $parsed_args ) { + if ( empty( $parsed_args['sslverify'] ) ) { + return $this->mock_error(); + } + return $preempt; + } + + public function mock_not_found() { + return array( + 'body' => '404Not Found', + 'response' => array( + 'code' => 404, + 'message' => 'Not Found', + ), + ); + } + + public function mock_bad_source() { + // Looks like a success response, but is not generated by WordPress (e.g. missing RSD link). + return array( + 'body' => $this->get_sample_html_string(), + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + ); + } + + private function mock_success() { + // Success response containing RSD link. + return array( + 'body' => $this->get_sample_html_string( get_echo( 'rsd_link' ) ), + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + ); + } + + private function mock_error() { + return new WP_Error( 'bad_ssl_certificate', 'Bad SSL certificate.' ); + } + + private function get_sample_html_string( $head_tag = '' ) { + return 'Page Title' . $head_tag . 'Page Content.'; + } + + /** + * Returns a filter callback that expects a URL and will set the URL scheme + * to the provided $scheme. + * + * @param string $scheme URL scheme to set. + * @return callable Filter callback. + */ + private function filter_set_url_scheme( $scheme ) { + return function( $url ) use ( $scheme ) { + return set_url_scheme( $url, $scheme ); + }; + } +}