From c8bbd79d2dfe46b3f1ef12d6f127317c4546249d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 19 Feb 2021 21:11:02 +0000 Subject: [PATCH] Security: Fix bug in `wp_is_local_html_output()`. Prior to this changeset, the check for the correct RSD link output was relying on a specific protocol, although it needs to accept both the HTTP and HTTPS version of the URL. Props TimothyBlynJacobs. Fixes #52542. See #47577. git-svn-id: https://develop.svn.wordpress.org/trunk@50391 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/https-detection.php | 4 ++-- tests/phpunit/tests/https-detection.php | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/https-detection.php b/src/wp-includes/https-detection.php index 2ced175047..7642fda3da 100644 --- a/src/wp-includes/https-detection.php +++ b/src/wp-includes/https-detection.php @@ -204,7 +204,7 @@ function wp_cron_conditionally_prevent_sslverify( $request ) { function wp_is_local_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(). + $pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) ); // See rsd_link(). return false !== strpos( $html, $pattern ); } @@ -218,7 +218,7 @@ function wp_is_local_html_output( $html ) { // 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(). + $pattern = preg_replace( '#^https?:(?=//)#', '', esc_url( get_rest_url() ) ); // See rest_output_link_wp_head(). return false !== strpos( $html, $pattern ); } diff --git a/tests/phpunit/tests/https-detection.php b/tests/phpunit/tests/https-detection.php index 70eeef51fd..e8c9d9057f 100644 --- a/tests/phpunit/tests/https-detection.php +++ b/tests/phpunit/tests/https-detection.php @@ -171,6 +171,7 @@ class Tests_HTTPS_Detection extends WP_UnitTestCase { /** * @ticket 47577 + * @ticket 52542 */ public function test_wp_is_local_html_output_via_rsd_link() { // HTML includes RSD link. @@ -183,6 +184,12 @@ class Tests_HTTPS_Detection extends WP_UnitTestCase { $html = $this->get_sample_html_string( $head_tag ); $this->assertTrue( wp_is_local_html_output( $html ) ); + // HTML includes RSD link with alternative URL scheme. + $head_tag = get_echo( 'rsd_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_local_html_output( $html ) ); + // HTML does not include RSD link. $html = $this->get_sample_html_string(); $this->assertFalse( wp_is_local_html_output( $html ) );