-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathwpCrossOriginIsolation.php
More file actions
480 lines (397 loc) · 14.6 KB
/
Copy pathwpCrossOriginIsolation.php
File metadata and controls
480 lines (397 loc) · 14.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* Tests for cross-origin isolation functions.
*
* @group media
* @covers ::wp_set_up_cross_origin_isolation
* @covers ::wp_start_cross_origin_isolation_output_buffer
* @covers ::wp_is_client_side_media_processing_enabled
*/
class Tests_Media_wpCrossOriginIsolation extends WP_UnitTestCase {
/**
* Original HTTP_USER_AGENT value.
*/
private ?string $original_user_agent;
/**
* Original HTTP_HOST value.
*/
private ?string $original_http_host;
/**
* Original HTTPS value.
*/
private ?string $original_https;
/**
* Original $_GET['action'] value.
*/
private ?string $original_get_action;
/**
* Original $_GET['p'] value.
*/
private ?string $original_get_p;
public function set_up() {
parent::set_up();
$this->original_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null;
$this->original_http_host = $_SERVER['HTTP_HOST'] ?? null;
$this->original_https = $_SERVER['HTTPS'] ?? null;
$this->original_get_action = $_GET['action'] ?? null;
$this->original_get_p = $_GET['p'] ?? null;
}
public function tear_down() {
if ( null === $this->original_user_agent ) {
unset( $_SERVER['HTTP_USER_AGENT'] );
} else {
$_SERVER['HTTP_USER_AGENT'] = $this->original_user_agent;
}
if ( null === $this->original_http_host ) {
unset( $_SERVER['HTTP_HOST'] );
} else {
$_SERVER['HTTP_HOST'] = $this->original_http_host;
}
if ( null === $this->original_https ) {
unset( $_SERVER['HTTPS'] );
} else {
$_SERVER['HTTPS'] = $this->original_https;
}
if ( null === $this->original_get_action ) {
unset( $_GET['action'] );
} else {
$_GET['action'] = $this->original_get_action;
}
if ( null === $this->original_get_p ) {
unset( $_GET['p'] );
} else {
$_GET['p'] = $this->original_get_p;
}
// Clean up any output buffers started during tests.
while ( ob_get_level() > 1 ) {
ob_end_clean();
}
$GLOBALS['current_screen'] = null;
remove_all_filters( 'wp_client_side_media_processing_enabled' );
parent::tear_down();
}
/**
* @ticket 64766
*/
public function test_returns_early_when_client_side_processing_disabled() {
add_filter( 'wp_client_side_media_processing_enabled', '__return_false' );
// Should not error or start an output buffer.
$level_before = ob_get_level();
wp_set_up_cross_origin_isolation();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after );
}
/**
* @ticket 64766
*/
public function test_returns_early_when_no_screen() {
// No screen is set, so it should return early.
$level_before = ob_get_level();
wp_set_up_cross_origin_isolation();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after );
}
/**
* This test must run in a separate process because the output buffer
* callback sends HTTP headers via header(), which would fail in the
* main PHPUnit process where output has already started.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_starts_output_buffer_for_chrome_137() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
$level_before = ob_get_level();
wp_start_cross_origin_isolation_output_buffer();
$level_after = ob_get_level();
$this->assertSame( $level_before + 1, $level_after, 'Output buffer should be started for Chrome 137.' );
ob_end_clean();
}
/**
* @ticket 64766
*/
public function test_does_not_start_output_buffer_for_chrome_136() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
$level_before = ob_get_level();
wp_start_cross_origin_isolation_output_buffer();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after, 'Output buffer should not be started for Chrome < 137.' );
}
/**
* @ticket 64766
*/
public function test_does_not_start_output_buffer_for_firefox() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; rv:128.0) Gecko/20100101 Firefox/128.0';
$level_before = ob_get_level();
wp_start_cross_origin_isolation_output_buffer();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after, 'Output buffer should not be started for Firefox.' );
}
/**
* @ticket 64766
*/
public function test_does_not_start_output_buffer_for_safari() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15';
$level_before = ob_get_level();
wp_start_cross_origin_isolation_output_buffer();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after, 'Output buffer should not be started for Safari.' );
}
/**
* The site editor home route on a classic theme skips DIP, because the
* editor renders the front end in a same-origin iframe and must reach its
* `contentDocument` to neutralize interactive elements. DIP would block
* that access.
*
* @ticket 65399
*
* @dataProvider data_classic_theme_site_editor_home_routes
*
* @param array $get The $_GET state representing the home route.
*/
public function test_skips_cross_origin_isolation_for_classic_theme_site_editor_home( array $get ) {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
$_SERVER['HTTP_HOST'] = 'localhost';
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
switch_theme( 'twentytwentyone' );
set_current_screen( 'site-editor' );
unset( $_GET['p'] );
foreach ( $get as $key => $value ) {
$_GET[ $key ] = $value;
}
$level_before = ob_get_level();
wp_set_up_cross_origin_isolation();
$level_after = ob_get_level();
$this->assertSame( $level_before, $level_after, 'DIP should be skipped on the classic-theme site editor home route.' );
}
/**
* Data provider for the classic-theme site editor home route.
*
* @return array[]
*/
public function data_classic_theme_site_editor_home_routes() {
return array(
'no p query var' => array( array() ),
'p query var is /' => array( array( 'p' => '/' ) ),
);
}
/**
* The site editor on a classic theme still sets up cross-origin isolation
* for routes other than the home route.
*
* @ticket 65399
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_sets_up_cross_origin_isolation_for_classic_theme_site_editor_non_home_route() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
$_SERVER['HTTP_HOST'] = 'localhost';
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
switch_theme( 'twentytwentyone' );
set_current_screen( 'site-editor' );
$_GET['p'] = '/page/about';
$level_before = ob_get_level();
wp_set_up_cross_origin_isolation();
$level_after = ob_get_level();
$this->assertSame( $level_before + 1, $level_after, 'DIP should be set up on a non-home site editor route.' );
ob_end_clean();
}
/**
* The site editor on a block theme always sets up cross-origin isolation,
* including on the home route, because block themes do not render the
* classic site preview iframe.
*
* @ticket 65399
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_sets_up_cross_origin_isolation_for_block_theme_site_editor_home() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
$_SERVER['HTTP_HOST'] = 'localhost';
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
switch_theme( 'twentytwentyfour' );
set_current_screen( 'site-editor' );
unset( $_GET['p'] );
$level_before = ob_get_level();
wp_set_up_cross_origin_isolation();
$level_after = ob_get_level();
$this->assertSame( $level_before + 1, $level_after, 'DIP should be set up on the block-theme site editor home route.' );
ob_end_clean();
}
/**
* @ticket 64803
*/
public function test_client_side_processing_disabled_on_non_secure_origin() {
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['HTTPS'] = '';
$this->assertFalse(
wp_is_client_side_media_processing_enabled(),
'Client-side media processing should be disabled on non-secure, non-localhost origins.'
);
}
/**
* @ticket 64803
*/
public function test_client_side_processing_enabled_on_localhost() {
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['HTTPS'] = '';
$this->assertTrue(
wp_is_client_side_media_processing_enabled(),
'Client-side media processing should be enabled on localhost.'
);
}
/**
* Verifies that cross-origin elements get crossorigin="anonymous" added.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @dataProvider data_elements_that_should_get_crossorigin
*
* @param string $html HTML input to process.
*/
public function test_output_buffer_adds_crossorigin( $html ) {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo $html;
ob_end_flush();
$output = ob_get_clean();
$this->assertStringContainsString( 'crossorigin="anonymous"', $output );
}
/**
* Data provider for elements that should receive crossorigin="anonymous".
*
* @return array[]
*/
public function data_elements_that_should_get_crossorigin() {
return array(
'cross-origin script' => array(
'<script src="https://external.example.com/script.js"></script>',
),
'cross-origin audio' => array(
'<audio src="https://external.example.com/audio.mp3"></audio>',
),
'cross-origin video' => array(
'<video src="https://external.example.com/video.mp4"></video>',
),
'cross-origin link stylesheet' => array(
'<link rel="stylesheet" href="https://external.example.com/style.css" />',
),
'cross-origin source inside video' => array(
'<video><source src="https://external.example.com/video.mp4" type="video/mp4" /></video>',
),
);
}
/**
* Verifies that certain elements do not get crossorigin="anonymous" added.
*
* Images are excluded because under Document-Isolation-Policy:
* isolate-and-credentialless, the browser handles cross-origin images
* in credentialless mode without needing explicit CORS headers.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @dataProvider data_elements_that_should_not_get_crossorigin
*
* @param string $html HTML input to process.
*/
public function test_output_buffer_does_not_add_crossorigin( $html ) {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo $html;
ob_end_flush();
$output = ob_get_clean();
$this->assertStringNotContainsString( 'crossorigin="anonymous"', $output );
}
/**
* Data provider for elements that should not receive crossorigin="anonymous".
*
* @return array[]
*/
public function data_elements_that_should_not_get_crossorigin() {
return array(
'cross-origin img' => array(
'<img src="https://external.example.com/image.jpg" />',
),
'cross-origin img with srcset' => array(
'<img src="https://external.example.com/image.jpg" srcset="https://external.example.com/image-2x.jpg 2x" />',
),
'link with cross-origin imagesrcset only' => array(
'<link rel="preload" as="image" imagesrcset="https://external.example.com/image.jpg 1x" href="/local-fallback.jpg" />',
),
'relative URL script' => array(
'<script src="/wp-includes/js/wp-embed.min.js"></script>',
),
);
}
/**
* Same-origin URLs should not get crossorigin="anonymous".
*
* Uses site_url() at runtime since the test domain varies by CI config.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_output_buffer_does_not_add_crossorigin_to_same_origin() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo '<script src="' . site_url( '/wp-includes/js/wp-embed.min.js' ) . '"></script>';
ob_end_flush();
$output = ob_get_clean();
$this->assertStringNotContainsString( 'crossorigin="anonymous"', $output );
}
/**
* Elements that already have a crossorigin attribute should not be modified.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_output_buffer_does_not_override_existing_crossorigin() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo '<script src="https://external.example.com/script.js" crossorigin="use-credentials"></script>';
ob_end_flush();
$output = ob_get_clean();
$this->assertStringContainsString( 'crossorigin="use-credentials"', $output, 'Existing crossorigin attribute should not be overridden.' );
$this->assertStringNotContainsString( 'crossorigin="anonymous"', $output );
}
/**
* Multiple tags in the same output should each be handled correctly.
*
* @ticket 64766
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_output_buffer_handles_mixed_tags() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36';
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo '<img src="https://external.example.com/image.jpg" />';
echo '<script src="https://external.example.com/script.js"></script>';
echo '<audio src="https://external.example.com/audio.mp3"></audio>';
ob_end_flush();
$output = ob_get_clean();
// IMG should NOT have crossorigin.
$this->assertStringContainsString( '<img src="https://external.example.com/image.jpg" />', $output, 'IMG should not be modified.' );
// Script and audio should have crossorigin.
$this->assertSame( 2, substr_count( $output, 'crossorigin="anonymous"' ), 'Script and audio should both get crossorigin, but not img.' );
}
}