forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-detection.php
More file actions
352 lines (302 loc) · 11.5 KB
/
https-detection.php
File metadata and controls
352 lines (302 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* @group https-detection
*/
class Tests_HTTPS_Detection extends WP_UnitTestCase {
private $last_request_url;
public function setUp() {
parent::setUp();
remove_all_filters( 'option_home' );
remove_all_filters( 'option_siteurl' );
remove_all_filters( 'home_url' );
remove_all_filters( 'site_url' );
}
/**
* @ticket 47577
*/
public function test_wp_is_using_https() {
update_option( 'home', 'http://example.com/' );
update_option( 'siteurl', 'http://example.com/' );
$this->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
* @ticket 52484
*/
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->assertSame( array(), get_option( 'https_detection_errors' ) );
// If initial request fails and request without SSL verification succeeds,
// return 'ssl_verification_failed' error.
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->assertSame(
array( 'ssl_verification_failed' => array( __( 'SSL verification failed.' ) ) ),
get_option( 'https_detection_errors' )
);
// If both initial request and request without SSL verification fail,
// return 'https_request_failed' error.
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->assertSame(
array( 'https_request_failed' => array( __( 'HTTPS request failed.' ) ) ),
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->assertSame(
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->assertSame(
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->assertSame( 'https://example.com/', $this->last_request_url );
}
/**
* @ticket 47577
*/
public function test_pre_wp_update_https_detection_errors() {
// Override to enforce no errors being detected.
add_filter(
'pre_wp_update_https_detection_errors',
function() {
return new WP_Error();
}
);
wp_update_https_detection_errors();
$this->assertSame( array(), get_option( 'https_detection_errors' ) );
// Override to enforce an error being detected.
add_filter(
'pre_wp_update_https_detection_errors',
function() {
return new WP_Error(
'ssl_verification_failed',
'Bad SSL certificate.'
);
}
);
wp_update_https_detection_errors();
$this->assertSame(
array( 'ssl_verification_failed' => array( 'Bad SSL certificate.' ) ),
get_option( 'https_detection_errors' )
);
}
/**
* @ticket 47577
*/
public function test_wp_schedule_https_detection() {
wp_schedule_https_detection();
$this->assertSame( '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->assertSame( $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->assertSame( $expected, wp_cron_conditionally_prevent_sslverify( $request ) );
}
/**
* @ticket 47577
* @ticket 52542
*/
public function test_wp_is_local_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_local_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_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 ) );
}
/**
* @ticket 47577
*/
public function test_wp_is_local_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_local_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_local_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_local_html_output( $html ) );
// HTML does not include WLW manifest link.
$html = $this->get_sample_html_string();
$this->assertFalse( wp_is_local_html_output( $html ) );
}
/**
* @ticket 47577
*/
public function test_wp_is_local_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_local_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_local_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_local_html_output( $html ) );
// HTML does not include REST API link.
$html = $this->get_sample_html_string();
$this->assertFalse( wp_is_local_html_output( $html ) );
}
/**
* @ticket 47577
*/
public function test_wp_is_local_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_local_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' => '<!DOCTYPE html><html><head><title>404</title></head><body>Not Found</body></html>',
'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 '<!DOCTYPE html><html><head><title>Page Title</title>' . $head_tag . '</head><body>Page Content.</body></html>';
}
/**
* 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 );
};
}
}