Make WordPress Core

Ticket #66000: 66000.diff

File 66000.diff, 10.2 KB (added by aayushk777, 2 months ago)
  • src/wp-includes/load.php

    diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php
    index 061754e8b4e..dd13a282249 100644
    a b function wp_is_site_protected_by_basic_auth( $context = '' ) {  
    20692069
    20702070        $is_protected = ! empty( $_SERVER['PHP_AUTH_USER'] ) || ! empty( $_SERVER['PHP_AUTH_PW'] );
    20712071
     2072        /*
     2073         * Credentials in the request are not proof that the server enforces Basic Auth.
     2074         * Under the CGI/FastCGI SAPI, PHP populates these keys from any `Authorization: Basic`
     2075         * header the client chooses to send, and browsers keep replaying cached credentials
     2076         * for the rest of the session after Basic Auth has been switched off.
     2077         *
     2078         * Confirm with the server before trusting the request.
     2079         */
     2080        if ( $is_protected ) {
     2081                $is_protected = wp_is_basic_auth_enforced_by_server();
     2082        }
     2083
    20722084        /**
    20732085         * Filters whether a site is protected by HTTP Basic Auth.
    20742086         *
    … … function wp_is_site_protected_by_basic_auth( $context = '' ) {  
    20792091         */
    20802092        return apply_filters( 'wp_is_site_protected_by_basic_auth', $is_protected, $context );
    20812093}
     2094
     2095/**
     2096 * Checks whether the server itself enforces HTTP Basic Auth.
     2097 *
     2098 * Makes an unauthenticated loopback request to the home URL and looks for the
     2099 * `WWW-Authenticate: Basic` challenge a server protected by Basic Auth must send.
     2100 *
     2101 * The result is cached briefly. The check only runs when Basic Auth credentials
     2102 * are present in the current request, so sites that are not affected never make
     2103 * the request.
     2104 *
     2105 * If the loopback request cannot be completed, the server is assumed to enforce
     2106 * Basic Auth, preserving the behavior from before this check existed.
     2107 *
     2108 * @since 7.2.0
     2109 * @access private
     2110 *
     2111 * @return bool Whether the server enforces Basic Auth.
     2112 */
     2113function wp_is_basic_auth_enforced_by_server() {
     2114        $cached = get_transient( 'wp_basic_auth_enforced' );
     2115
     2116        if ( false !== $cached ) {
     2117                return '1' === $cached;
     2118        }
     2119
     2120        $response = wp_remote_head(
     2121                home_url( '/' ),
     2122                array(
     2123                        'timeout'     => 3,
     2124                        'redirection' => 0,
     2125                        'sslverify'   => false,
     2126                )
     2127        );
     2128
     2129        if ( is_wp_error( $response ) ) {
     2130                // Do not cache a failure to reach the site; the next request can try again.
     2131                return true;
     2132        }
     2133
     2134        $challenge = wp_remote_retrieve_header( $response, 'www-authenticate' );
     2135
     2136        // A server may send more than one challenge.
     2137        if ( is_array( $challenge ) ) {
     2138                $challenge = implode( ', ', $challenge );
     2139        }
     2140
     2141        $is_enforced = 401 === wp_remote_retrieve_response_code( $response )
     2142                && str_starts_with( strtolower( $challenge ), 'basic' );
     2143
     2144        /*
     2145         * Kept short: the server's configuration can change at any time, and a stale
     2146         * result is wrong in both directions until it expires.
     2147         */
     2148        set_transient( 'wp_basic_auth_enforced', $is_enforced ? '1' : '0', 15 * MINUTE_IN_SECONDS );
     2149
     2150        return $is_enforced;
     2151}
  • new file tests/phpunit/tests/load/wpIsSiteProtectedByBasicAuth.php

    diff --git a/tests/phpunit/tests/load/wpIsSiteProtectedByBasicAuth.php b/tests/phpunit/tests/load/wpIsSiteProtectedByBasicAuth.php
    new file mode 100644
    index 00000000000..ac8bfe67b18
    - +  
     1<?php
     2
     3/**
     4 * Tests for wp_is_site_protected_by_basic_auth().
     5 *
     6 * @package WordPress
     7 * @subpackage UnitTests
     8 * @since 7.2.0
     9 *
     10 * @group load
     11 *
     12 * @covers ::wp_is_site_protected_by_basic_auth
     13 * @covers ::wp_is_basic_auth_enforced_by_server
     14 */
     15class Tests_Load_wpIsSiteProtectedByBasicAuth extends WP_UnitTestCase {
     16
     17        /**
     18         * Backup of the Basic Auth related $_SERVER keys.
     19         *
     20         * @var array
     21         */
     22        private $server_backup = array();
     23
     24        public function set_up() {
     25                parent::set_up();
     26
     27                foreach ( array( 'PHP_AUTH_USER', 'PHP_AUTH_PW' ) as $key ) {
     28                        $this->server_backup[ $key ] = isset( $_SERVER[ $key ] ) ? $_SERVER[ $key ] : null;
     29                        unset( $_SERVER[ $key ] );
     30                }
     31
     32                delete_transient( 'wp_basic_auth_enforced' );
     33        }
     34
     35        public function tear_down() {
     36                foreach ( $this->server_backup as $key => $value ) {
     37                        if ( null === $value ) {
     38                                unset( $_SERVER[ $key ] );
     39                        } else {
     40                                $_SERVER[ $key ] = $value;
     41                        }
     42                }
     43
     44                delete_transient( 'wp_basic_auth_enforced' );
     45
     46                parent::tear_down();
     47        }
     48
     49        /**
     50         * Short-circuits the loopback request with a given response.
     51         *
     52         * @param int   $status  Response status code.
     53         * @param array $headers Response headers. Default empty array.
     54         */
     55        private function mock_loopback_response( $status, $headers = array() ) {
     56                add_filter(
     57                        'pre_http_request',
     58                        static function () use ( $status, $headers ) {
     59                                return array(
     60                                        'headers'  => $headers,
     61                                        'body'     => '',
     62                                        'response' => array(
     63                                                'code'    => $status,
     64                                                'message' => get_status_header_desc( $status ),
     65                                        ),
     66                                );
     67                        }
     68                );
     69        }
     70
     71        /**
     72         * @ticket 66000
     73         */
     74        public function test_should_return_false_when_no_credentials_are_present() {
     75                $this->assertFalse( wp_is_site_protected_by_basic_auth( 'front' ) );
     76        }
     77
     78        /**
     79         * @ticket 66000
     80         */
     81        public function test_should_not_make_a_loopback_request_when_no_credentials_are_present() {
     82                $requests = 0;
     83
     84                add_filter(
     85                        'pre_http_request',
     86                        static function ( $preempt ) use ( &$requests ) {
     87                                ++$requests;
     88                                return $preempt;
     89                        }
     90                );
     91
     92                wp_is_site_protected_by_basic_auth( 'front' );
     93
     94                $this->assertSame( 0, $requests );
     95        }
     96
     97        /**
     98         * @ticket 66000
     99         *
     100         * @dataProvider data_credentials
     101         *
     102         * @param array $server   Values to set on $_SERVER.
     103         * @param bool  $expected Expected result.
     104         */
     105        public function test_should_detect_credentials( $server, $expected ) {
     106                foreach ( $server as $key => $value ) {
     107                        $_SERVER[ $key ] = $value;
     108                }
     109
     110                $this->mock_loopback_response( 401, array( 'www-authenticate' => 'Basic realm="Restricted"' ) );
     111
     112                $this->assertSame( $expected, wp_is_site_protected_by_basic_auth( 'front' ) );
     113        }
     114
     115        /**
     116         * Data provider.
     117         *
     118         * @return array[]
     119         */
     120        public function data_credentials() {
     121                return array(
     122                        'a user only'         => array( array( 'PHP_AUTH_USER' => 'user' ), true ),
     123                        'a password only'     => array( array( 'PHP_AUTH_PW' => 'pass' ), true ),
     124                        'a user and password' => array(
     125                                array(
     126                                        'PHP_AUTH_USER' => 'user',
     127                                        'PHP_AUTH_PW'   => 'pass',
     128                                ),
     129                                true,
     130                        ),
     131                        'an empty user'       => array( array( 'PHP_AUTH_USER' => '' ), false ),
     132                        'an empty password'   => array( array( 'PHP_AUTH_PW' => '' ), false ),
     133                );
     134        }
     135
     136        /**
     137         * Stale credentials replayed by the browser must not count as protection.
     138         *
     139         * @ticket 66000
     140         */
     141        public function test_should_return_false_for_stale_credentials() {
     142                $_SERVER['PHP_AUTH_USER'] = 'staleuser';
     143                $_SERVER['PHP_AUTH_PW']   = 'stalepass';
     144
     145                $this->mock_loopback_response( 200 );
     146
     147                $this->assertFalse( wp_is_site_protected_by_basic_auth( 'front' ) );
     148        }
     149
     150        /**
     151         * A 401 without a Basic challenge is some other authentication scheme.
     152         *
     153         * @ticket 66000
     154         */
     155        public function test_should_return_false_for_a_non_basic_challenge() {
     156                $_SERVER['PHP_AUTH_USER'] = 'user';
     157
     158                $this->mock_loopback_response( 401, array( 'www-authenticate' => 'Bearer realm="api"' ) );
     159
     160                $this->assertFalse( wp_is_site_protected_by_basic_auth( 'front' ) );
     161        }
     162
     163        /**
     164         * @ticket 66000
     165         */
     166        public function test_should_return_true_when_the_server_sends_a_basic_challenge() {
     167                $_SERVER['PHP_AUTH_USER'] = 'user';
     168
     169                $this->mock_loopback_response( 401, array( 'www-authenticate' => 'Basic realm="Restricted"' ) );
     170
     171                $this->assertTrue( wp_is_site_protected_by_basic_auth( 'front' ) );
     172        }
     173
     174        /**
     175         * A blocked loopback request must not unlock Application Passwords.
     176         *
     177         * @ticket 66000
     178         */
     179        public function test_should_return_true_when_the_loopback_request_fails() {
     180                $_SERVER['PHP_AUTH_USER'] = 'user';
     181
     182                add_filter(
     183                        'pre_http_request',
     184                        static function () {
     185                                return new WP_Error( 'http_request_failed', 'Connection refused.' );
     186                        }
     187                );
     188
     189                $this->assertTrue( wp_is_site_protected_by_basic_auth( 'front' ) );
     190                $this->assertFalse( get_transient( 'wp_basic_auth_enforced' ), 'A failed request should not be cached.' );
     191        }
     192
     193        /**
     194         * @ticket 66000
     195         */
     196        public function test_should_only_make_one_loopback_request() {
     197                $_SERVER['PHP_AUTH_USER'] = 'user';
     198                $requests                 = 0;
     199
     200                add_filter(
     201                        'pre_http_request',
     202                        static function () use ( &$requests ) {
     203                                ++$requests;
     204                                return array(
     205                                        'headers'  => array(),
     206                                        'body'     => '',
     207                                        'response' => array(
     208                                                'code'    => 200,
     209                                                'message' => 'OK',
     210                                        ),
     211                                );
     212                        }
     213                );
     214
     215                wp_is_site_protected_by_basic_auth( 'front' );
     216                wp_is_site_protected_by_basic_auth( 'front' );
     217
     218                $this->assertSame( 1, $requests );
     219        }
     220
     221        /**
     222         * The documented escape hatch must keep working.
     223         *
     224         * @ticket 66000
     225         */
     226        public function test_filter_should_override_a_positive_detection() {
     227                $_SERVER['PHP_AUTH_USER'] = 'user';
     228
     229                $this->mock_loopback_response( 401, array( 'www-authenticate' => 'Basic realm="Restricted"' ) );
     230
     231                add_filter( 'wp_is_site_protected_by_basic_auth', '__return_false' );
     232
     233                $this->assertFalse( wp_is_site_protected_by_basic_auth( 'front' ) );
     234        }
     235
     236        /**
     237         * @ticket 66000
     238         */
     239        public function test_filter_should_override_a_negative_detection() {
     240                add_filter( 'wp_is_site_protected_by_basic_auth', '__return_true' );
     241
     242                $this->assertTrue( wp_is_site_protected_by_basic_auth( 'front' ) );
     243        }
     244
     245        /**
     246         * @ticket 66000
     247         */
     248        public function test_filter_should_receive_the_explicit_context() {
     249                $context = null;
     250
     251                add_filter(
     252                        'wp_is_site_protected_by_basic_auth',
     253                        static function ( $is_protected, $passed_context ) use ( &$context ) {
     254                                $context = $passed_context;
     255                                return $is_protected;
     256                        },
     257                        10,
     258                        2
     259                );
     260
     261                wp_is_site_protected_by_basic_auth( 'login' );
     262
     263                $this->assertSame( 'login', $context );
     264        }
     265
     266        /**
     267         * @ticket 66000
     268         */
     269        public function test_context_should_default_to_login_on_the_login_screen() {
     270                global $pagenow;
     271
     272                $original = $pagenow;
     273                $pagenow  = 'wp-login.php';
     274                $context  = null;
     275
     276                add_filter(
     277                        'wp_is_site_protected_by_basic_auth',
     278                        static function ( $is_protected, $passed_context ) use ( &$context ) {
     279                                $context = $passed_context;
     280                                return $is_protected;
     281                        },
     282                        10,
     283                        2
     284                );
     285
     286                wp_is_site_protected_by_basic_auth();
     287
     288                $pagenow = $original;
     289
     290                $this->assertSame( 'login', $context );
     291        }
     292}