| | 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 | */ |
| | 15 | class 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 | } |