Debian Codegraph @

tests/phpunit/unit/includes/Rest/CorsUtilsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Rest\CorsUtils;
8 use MediaWiki\Rest\Handler;
9 use MediaWiki\Rest\RequestInterface;
10 use MediaWiki\Rest\Response;
11 use MediaWiki\Rest\ResponseFactory;
12 use MediaWiki\Rest\ResponseInterface;
13 use MediaWiki\User\UserIdentityValue;
14 use MediaWikiUnitTestCase;
15
16 /**
17 * @covers \MediaWiki\Rest\CorsUtils
18 */
19 class CorsUtilsTest extends MediaWikiUnitTestCase {
20
21 private function createServiceOptions( array $options = [] ) {
22 $defaults = [
23 MainConfigNames::AllowedCorsHeaders => [],
24 MainConfigNames::AllowCrossOrigin => false,
25 MainConfigNames::RestAllowCrossOriginCookieAuth => false,
26 MainConfigNames::CanonicalServer => 'https://example.com',
27 MainConfigNames::CrossSiteAJAXdomains => [],
28 MainConfigNames::CrossSiteAJAXdomainExceptions => [],
29 ];
30
31 return new ServiceOptions( CorsUtils::CONSTRUCTOR_OPTIONS, array_merge( $defaults, $options ) );
32 }
33
34 /**
35 * @dataProvider provideAuthorizeAllowOrigin
36 */
37 public function testAuthorizeAllowOrigin( bool $isRegistered, bool $needsWriteAccess, string $origin ) {
38 $cors = new CorsUtils(
39 $this->createServiceOptions( [
40 MainConfigNames::CrossSiteAJAXdomains => [
41 'www.mediawiki.org',
42 ],
43 ] ),
44 $this->createNoOpMock( ResponseFactory::class ),
45 new UserIdentityValue( (int)$isRegistered, __CLASS__ )
46 );
47
48 $request = $this->createMock( RequestInterface::class );
49 $request->method( 'hasHeader' )
50 ->willReturnMap( [
51 [ 'Origin', (bool)$origin ]
52 ] );
53 $request->method( 'getHeader' )
54 ->willReturnMap( [
55 [ 'Origin', [ $origin ] ]
56 ] );
57
58 $handler = $this->createMock( Handler::class );
59 $handler->method( 'needsWriteAccess' )
60 ->willReturn( $needsWriteAccess );
61
62 $result = $cors->authorize(
63 $request,
64 $handler
65 );
66
67 $this->assertNull( $result );
68 }
69
70 public static function provideAuthorizeAllowOrigin() {
71 $origin = 'https://example.com';
72
73 return [
74 'User is registered' => [
75 true,
76 true,
77 $origin,
78 ],
79 'Handler does not need write access' => [
80 false,
81 false,
82 $origin,
83 ],
84 'Missing origin' => [
85 false,
86 true,
87 '',
88 ],
89 'Same origin' => [
90 false,
91 true,
92 $origin,
93 ],
94 'Trusted origin' => [
95 false,
96 true,
97 'https://www.mediawiki.org',
98 ],
99 ];
100 }
101
102 public function testAuthorizeDisallowOrigin() {
103 $cors = new CorsUtils(
104 $this->createServiceOptions(),
105 $this->createMock( ResponseFactory::class ),
106 new UserIdentityValue( 0, __CLASS__ )
107 );
108
109 $request = $this->createMock( RequestInterface::class );
110 $request->method( 'hasHeader' )
111 ->willReturnMap( [
112 [ 'Origin', true ]
113 ] );
114 $request->expects( $this->once() )
115 ->method( 'getHeader' )
116 ->willReturnMap( [
117 [ 'Origin', [ 'https://www.mediawiki.org' ] ]
118 ] );
119
120 $handler = $this->createMock( Handler::class );
121 $handler->method( 'needsWriteAccess' )
122 ->willReturn( true );
123
124 $result = $cors->authorize(
125 $request,
126 $handler
127 );
128
129 $this->assertSame( 'rest-cross-origin-anon-write', $result );
130 }
131
132 public function testModifyResponseNoChange() {
133 $cors = new CorsUtils(
134 $this->createServiceOptions(),
135 $this->createMock( ResponseFactory::class ),
136 new UserIdentityValue( 0, __CLASS__ )
137 );
138
139 $response = $this->createNoOpMock( ResponseInterface::class );
140
141 $result = $cors->modifyResponse(
142 $this->createMock( RequestInterface::class ),
143 $response
144 );
145
146 $this->assertSame( $response, $result );
147 }
148
149 public function testModifyResponseAllowOrigin() {
150 $cors = new CorsUtils(
151 $this->createServiceOptions( [
152 MainConfigNames::AllowCrossOrigin => true,
153 ] ),
154 $this->createNoOpMock( ResponseFactory::class ),
155 new UserIdentityValue( 0, __CLASS__ )
156 );
157
158 $response = new Response();
159
160 $result = $cors->modifyResponse(
161 $this->createMock( RequestInterface::class ),
162 $response
163 );
164
165 $this->assertSame( $response, $result );
166 $this->assertFalse( $result->hasHeader( 'Vary' ) );
167 $this->assertTrue( $result->hasHeader( 'Access-Control-Allow-Origin' ) );
168 $this->assertSame( [ '*' ], $result->getHeader( 'Access-Control-Allow-Origin' ) );
169 }
170
171 /**
172 * @dataProvider provideModifyResponseAllowTrustedOriginCookieAuth
173 * @param string $requestMethod
174 * @param bool $isRegistered
175 */
176 public function testModifyResponseAllowTrustedOriginCookieAuth( string $requestMethod, bool $isRegistered ) {
177 $cors = new CorsUtils(
178 $this->createServiceOptions( [
179 MainConfigNames::AllowCrossOrigin => true,
180 MainConfigNames::RestAllowCrossOriginCookieAuth => true,
181 ] ),
182 $this->createNoOpMock( ResponseFactory::class ),
183 new UserIdentityValue( (int)$isRegistered, __CLASS__ )
184 );
185
186 $request = $this->createMock( RequestInterface::class );
187 $request->method( 'hasHeader' )
188 ->willReturnMap( [
189 [ 'Origin', true ]
190 ] );
191 $request->method( 'getHeader' )
192 ->willReturnMap( [
193 [ 'Origin', [ 'https://example.com' ] ],
194 ] );
195 $request->method( 'getMethod' )
196 ->willReturn( $requestMethod );
197
198 $response = new Response();
199
200 $result = $cors->modifyResponse( $request, $response );
201
202 $this->assertSame( $response, $result );
203 $this->assertTrue( $result->hasHeader( 'Vary' ) );
204 $this->assertSame( [ 'Origin' ], $result->getHeader( 'Vary' ) );
205 $this->assertTrue( $result->hasHeader( 'Access-Control-Allow-Credentials' ) );
206 $this->assertSame( [ 'true' ], $result->getHeader( 'Access-Control-Allow-Credentials' ) );
207 $this->assertTrue( $result->hasHeader( 'Access-Control-Allow-Origin' ) );
208 $this->assertSame( [ 'https://example.com' ], $result->getHeader( 'Access-Control-Allow-Origin' ) );
209 }
210
211 public static function provideModifyResponseAllowTrustedOriginCookieAuth() {
212 return [
213 'OPTIONS request' => [
214 'OPTIONS',
215 false
216 ],
217 'Registered user on main request' => [
218 'POST',
219 true,
220 ],
221 ];
222 }
223
224 /**
225 * @dataProvider provideModifyResponseDisallowUntrustedOriginCookieAuth
226 */
227 public function testModifyResponseDisallowUntrustedOriginCookieAuth(
228 string $origin,
229 string $requestMethod,
230 bool $isRegistered
231 ) {
232 $cors = new CorsUtils(
233 $this->createServiceOptions( [
234 MainConfigNames::AllowCrossOrigin => true,
235 MainConfigNames::RestAllowCrossOriginCookieAuth => true,
236 ] ),
237 $this->createNoOpMock( ResponseFactory::class ),
238 new UserIdentityValue( (int)$isRegistered, __CLASS__ )
239 );
240
241 $request = $this->createMock( RequestInterface::class );
242 $request->method( 'hasHeader' )
243 ->willReturnMap( [
244 [ 'Origin', (bool)$origin ]
245 ] );
246 $request->method( 'getHeader' )
247 ->willReturnMap( [
248 [ 'Origin', [ $origin ] ],
249 ] );
250 $request->method( 'getMethod' )
251 ->willReturn( $requestMethod );
252
253 $response = new Response();
254
255 $result = $cors->modifyResponse( $request, $response );
256
257 $this->assertSame( $response, $result );
258 $this->assertTrue( $result->hasHeader( 'Vary' ) );
259 $this->assertSame( [ 'Origin' ], $result->getHeader( 'Vary' ) );
260 $this->assertFalse( $result->hasHeader( 'Access-Control-Allow-Credentials' ) );
261 $this->assertTrue( $result->hasHeader( 'Access-Control-Allow-Origin' ) );
262 $this->assertSame( [ '*' ], $result->getHeader( 'Access-Control-Allow-Origin' ) );
263 }
264
265 public static function provideModifyResponseDisallowUntrustedOriginCookieAuth() {
266 return [
267 'Missing Origin' => [
268 '',
269 'GET',
270 true,
271 ],
272 'Untrusted Origin' => [
273 'www.mediawiki.org',
274 'GET',
275 true
276 ],
277 'Trusted Origin, anon user' => [
278 'example.com',
279 'POST',
280 false
281 ],
282 ];
283 }
284
285 public function testCreatePreflightResponse() {
286 $responseFactory = $this->createMock( ResponseFactory::class );
287 $responseFactory->method( 'createNoContent' )
288 ->willReturn( new Response() );
289
290 $cors = new CorsUtils(
291 $this->createServiceOptions(),
292 $responseFactory,
293 new UserIdentityValue( 0, __CLASS__ )
294 );
295
296 $methods = [ 'POST' ];
297 $response = $cors->createPreflightResponse( $methods );
298
299 $this->assertInstanceOf( ResponseInterface::class, $response );
300 $this->assertTrue( $response->hasHeader( 'Access-Control-Allow-Headers' ) );
301 $this->assertTrue( $response->hasHeader( 'Access-Control-Allow-Methods' ) );
302 $this->assertSame( $methods, $response->getHeader( 'Access-Control-Allow-Methods' ) );
303 }
304
305 public function testCreatePreflightResponse_allow_headers() {
306 $responseFactory = $this->createMock( ResponseFactory::class );
307 $responseFactory->method( 'createNoContent' )
308 ->willReturn( new Response() );
309
310 $cors = new CorsUtils(
311 $this->createServiceOptions( [
312 MainConfigNames::AllowedCorsHeaders => [ 'Authorization', 'BlaHeader', ],
313 ] ),
314 $responseFactory,
315 new UserIdentityValue( 0, __CLASS__ )
316 );
317
318 $methods = [ 'POST' ];
319 $response = $cors->createPreflightResponse( $methods );
320 $this->assertTrue( $response->hasHeader( 'Access-Control-Allow-Headers' ) );
321 $header = $response->getHeader( 'Access-Control-Allow-Headers' );
322 $this->assertContains( 'Authorization', $header );
323 $this->assertContains( 'Content-Type', $header );
324 $this->assertSameSize( $header, array_unique( $header ) );
325 }
326 }