forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpCrossOriginIsolation.php
More file actions
213 lines (174 loc) · 6.03 KB
/
wpCrossOriginIsolation.php
File metadata and controls
213 lines (174 loc) · 6.03 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
<?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;
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;
}
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;
}
// Clean up any output buffers started during tests.
while ( ob_get_level() > 1 ) {
ob_end_clean();
}
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.' );
}
/**
* @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.'
);
}
/**
* 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_output_buffer_adds_crossorigin_attributes() {
$_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';
// Start an outer buffer to capture the callback-processed output.
ob_start();
wp_start_cross_origin_isolation_output_buffer();
echo '<img src="https://external.example.com/image.jpg" />';
// Flush the inner buffer to trigger the callback, sending processed output to the outer buffer.
ob_end_flush();
$output = ob_get_clean();
$this->assertStringContainsString( 'crossorigin="anonymous"', $output );
}
}