Debian Codegraph @

tests/phpunit/includes/upload/UploadFromUrlTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Api\ApiUsageException;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Page\File\FileDeleteForm;
6 use MediaWiki\Tests\Api\ApiTestCase;
7 use MediaWiki\Title\Title;
8 use MediaWiki\User\User;
9 use Wikimedia\TestingAccessWrapper;
10
11 /**
12 * @group large
13 * @group Upload
14 * @group Database
15 *
16 * @covers \UploadFromUrl
17 */
18 class UploadFromUrlTest extends ApiTestCase {
19 use MockHttpTrait;
20
21 /** @var User */
22 private $user;
23
24 protected function setUp(): void {
25 parent::setUp();
26 $this->user = $this->getTestSysop()->getUser();
27
28 $this->overrideConfigValues( [
29 MainConfigNames::EnableUploads => true,
30 MainConfigNames::AllowCopyUploads => true,
31 ] );
32 $this->setGroupPermissions( 'sysop', 'upload_by_url', true );
33 $this->setGroupPermissions( 'user', 'upload_by_url', false );
34
35 if ( $this->getServiceContainer()->getRepoGroup()->getLocalRepo()
36 ->newFile( 'UploadFromUrlTest.png' )->exists()
37 ) {
38 $this->deleteFile( 'UploadFromUrlTest.png' );
39 }
40
41 $this->installMockHttp();
42 }
43
44 /**
45 * Ensure that the job queue is empty before continuing
46 */
47 public function testClearQueue() {
48 $jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
49 $job = $jobQueueGroup->pop();
50 while ( $job ) {
51 $job = $jobQueueGroup->pop();
52 }
53 $this->assertFalse( $job );
54 }
55
56 public function testIsAllowedHostEmpty() {
57 $this->overrideConfigValues( [
58 MainConfigNames::CopyUploadsDomains => [],
59 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
60 ] );
61
62 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.bar' ) );
63 }
64
65 public function testIsAllowedHostDirectMatch() {
66 $this->overrideConfigValues( [
67 MainConfigNames::CopyUploadsDomains => [
68 'foo.baz',
69 'bar.example.baz',
70 ],
71 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
72 ] );
73
74 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
75
76 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
77 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://.foo.baz' ) );
78
79 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.baz' ) );
80 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://bar.example.baz' ) );
81 }
82
83 public function testIsAllowedHostLastWildcard() {
84 $this->overrideConfigValues( [
85 MainConfigNames::CopyUploadsDomains => [
86 '*.baz',
87 ],
88 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
89 ] );
90
91 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://baz' ) );
92 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example' ) );
93 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example.baz' ) );
94 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo/bar.baz' ) );
95
96 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
97 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://subdomain.foo.baz' ) );
98 }
99
100 public function testIsAllowedHostWildcardInMiddle() {
101 $this->overrideConfigValues( [
102 MainConfigNames::CopyUploadsDomains => [
103 'foo.*.baz',
104 ],
105 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
106 ] );
107
108 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.baz' ) );
109 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.bar.bar.baz' ) );
110 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.bar.baz.baz' ) );
111 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.com/.baz' ) );
112
113 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.example.baz' ) );
114 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://foo.bar.baz' ) );
115 }
116
117 public function testOnWikiDomainConfigEnabled() {
118 $this->overrideConfigValues( [
119 MainConfigNames::CopyUploadsDomains => [ 'example.com' ],
120 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => true,
121 ] );
122
123 $messageContent = "example.org # this is a comment\n# this too is commented foo.example.com\nexample.net";
124 $mock = $this->createMock( MessageCache::class );
125 $mock->method( 'get' )->willReturn( $messageContent );
126 $this->setService( 'MessageCache', $mock );
127
128 $this->assertEquals(
129 [ 'example.com', 'example.org', 'example.net' ],
130 TestingAccessWrapper::newFromClass( UploadFromUrl::class )->getAllowedHosts()
131 );
132
133 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
134 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.org' ) );
135 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://foo.example.com' ) );
136 }
137
138 public function testOnWikiDomainConfigDisabled() {
139 $this->overrideConfigValues( [
140 MainConfigNames::CopyUploadsDomains => [ 'example.com' ],
141 MainConfigNames::CopyUploadAllowOnWikiDomainConfig => false,
142 ] );
143
144 $mock = $this->createMock( MessageCache::class );
145 $mock->expects( $this->never() )->method( 'get' );
146 $this->setService( 'MessageCache', $mock );
147
148 $this->assertEquals(
149 [ 'example.com' ],
150 TestingAccessWrapper::newFromClass( UploadFromUrl::class )->getAllowedHosts()
151 );
152
153 $this->assertTrue( UploadFromUrl::isAllowedHost( 'https://example.com' ) );
154 $this->assertFalse( UploadFromUrl::isAllowedHost( 'https://example.org' ) );
155 }
156
157 /**
158 * @depends testClearQueue
159 */
160 public function testSetupUrlDownload( $data ) {
161 $this->overrideConfigValues( [
162 MainConfigNames::CopyUploadsDomains => [ 'www.example.com' ],
163 ] );
164 $token = $this->user->getEditToken();
165 $exception = false;
166
167 try {
168 $this->doApiRequest( [
169 'action' => 'upload',
170 ] );
171 } catch ( ApiUsageException $e ) {
172 $exception = true;
173 $this->assertApiErrorCode( 'missingparam', $e );
174 }
175 $this->assertTrue( $exception, "Got exception" );
176
177 $exception = false;
178 try {
179 $this->doApiRequest( [
180 'action' => 'upload',
181 'token' => $token,
182 ], $data );
183 } catch ( ApiUsageException $e ) {
184 $exception = true;
185 $this->assertApiErrorCode( 'missingparam', $e );
186 }
187 $this->assertTrue( $exception, "Got exception" );
188
189 $exception = false;
190 try {
191 $this->doApiRequest( [
192 'action' => 'upload',
193 'url' => 'http://www.example.com/test.png',
194 'token' => $token,
195 ], $data );
196 } catch ( ApiUsageException $e ) {
197 $exception = true;
198 $this->assertApiErrorCode( 'nofilename', $e );
199 }
200 $this->assertTrue( $exception, "Got exception" );
201
202 $this->getServiceContainer()->getUserGroupManager()->removeUserFromGroup( $this->user, 'sysop' );
203 $exception = false;
204 try {
205 $this->doApiRequest( [
206 'action' => 'upload',
207 'url' => 'http://www.example.com/test.png',
208 'filename' => 'UploadFromUrlTest.png',
209 'token' => $token,
210 ], $data );
211 } catch ( ApiUsageException $e ) {
212 $this->assertApiErrorCode( 'permissiondenied', $e );
213 $exception = true;
214 }
215 $this->assertTrue( $exception, "Got exception" );
216 }
217
218 private function assertUploadOk( UploadBase $upload ) {
219 $verificationResult = $upload->verifyUpload();
220
221 if ( $verificationResult['status'] !== UploadBase::OK ) {
222 $this->fail(
223 'Upload verification returned ' . $upload->getVerificationErrorCode(
224 $verificationResult['status']
225 )
226 );
227 }
228 }
229
230 /**
231 * @depends testClearQueue
232 */
233 public function testSyncDownload( $data ) {
234 $this->overrideConfigValues( [
235 MainConfigNames::CopyUploadsDomains => [ 'upload.wikimedia.org' ],
236 ] );
237 $file = __DIR__ . '/../../data/upload/png-plain.png';
238 $this->installMockHttp( file_get_contents( $file ) );
239
240 $this->getServiceContainer()->getUserGroupManager()->addUserToGroup( $this->user, 'users' );
241 $data = $this->doApiRequestWithToken( [
242 'action' => 'upload',
243 'filename' => 'UploadFromUrlTest.png',
244 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
245 'ignorewarnings' => true,
246 ], $data );
247
248 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
249 $this->deleteFile( 'UploadFromUrlTest.png' );
250
251 return $data;
252 }
253
254 protected function deleteFile( $name ) {
255 $t = Title::newFromText( $name, NS_FILE );
256 $this->assertTrue( $t->exists(), "File '$name' exists" );
257
258 if ( $t->exists() ) {
259 $file = $this->getServiceContainer()->getRepoGroup()
260 ->findFile( $name, [ 'ignoreRedirect' => true ] );
261 $empty = "";
262 FileDeleteForm::doDelete( $t, $file, $empty, "none", true, $this->user );
263 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $t );
264 $this->deletePage( $page );
265 }
266 $t = Title::newFromText( $name, NS_FILE );
267
268 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
269 }
270
271 public function testUploadFromUrl() {
272 $this->overrideConfigValues( [
273 MainConfigNames::CopyUploadsDomains => [ 'www.example.com' ],
274 ] );
275 $file = __DIR__ . '/../../data/upload/png-plain.png';
276 $this->installMockHttp( file_get_contents( $file ) );
277
278 $upload = new UploadFromUrl();
279 $upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
280 $status = $upload->fetchFile();
281
282 $this->assertStatusOK( $status );
283 $this->assertUploadOk( $upload );
284 }
285
286 public function testUploadFromUrlWithRedirect() {
287 $this->overrideConfigValues( [
288 MainConfigNames::CopyUploadsDomains => [ '*.example.com' ],
289 ] );
290 $file = __DIR__ . '/../../data/upload/png-plain.png';
291 $this->installMockHttp( [
292 // First response is a redirect
293 $this->makeFakeHttpRequest(
294 'Blaba',
295 302,
296 [ 'Location' => 'http://static.example.com/files/test.png' ]
297 ),
298 // Second response is a file
299 $this->makeFakeHttpRequest(
300 file_get_contents( $file )
301 ),
302 ] );
303
304 $upload = new UploadFromUrl();
305 $upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
306 $status = $upload->fetchFile();
307
308 $this->assertStatusOK( $status );
309 $this->assertUploadOk( $upload );
310 }
311
312 public function testUploadFromUrlRedirectToDisallowedHostFails() {
313 $this->overrideConfigValues( [
314 MainConfigNames::CopyUploadsDomains => [ 'www.example.com' ],
315 ] );
316
317 $this->installMockHttp( [
318 // Redirect from an allowed host to a disallowed host.
319 $this->makeFakeHttpRequest(
320 'redirecting',
321 302,
322 [ 'Location' => 'http://evil.example.org/test.png' ]
323 ),
324 ] );
325
326 $upload = new UploadFromUrl();
327 $upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
328 $status = $upload->fetchFile();
329
330 $this->assertStatusError( 'upload-copy-upload-invalid-domain', $status );
331 }
332
333 public function testUploadFromUrlTooManyRedirectsFails() {
334 $this->overrideConfigValues( [
335 MainConfigNames::CopyUploadsDomains => [ 'www.example.com' ],
336 ] );
337
338 // Default maxRedirects is 5. Queue 6 consecutive same-host redirects so the
339 // allowed-host check passes each time and $attemptsLeft drains to 0.
340 $this->installMockHttp( [
341 $this->makeFakeHttpRequest(
342 'redirect 1', 302, [ 'Location' => 'http://www.example.com/redirect-1.png' ]
343 ),
344 $this->makeFakeHttpRequest(
345 'redirect 2', 302, [ 'Location' => 'http://www.example.com/redirect-2.png' ]
346 ),
347 $this->makeFakeHttpRequest(
348 'redirect 3', 302, [ 'Location' => 'http://www.example.com/redirect-3.png' ]
349 ),
350 $this->makeFakeHttpRequest(
351 'redirect 4', 302, [ 'Location' => 'http://www.example.com/redirect-4.png' ]
352 ),
353 $this->makeFakeHttpRequest(
354 'redirect 5', 302, [ 'Location' => 'http://www.example.com/redirect-5.png' ]
355 ),
356 $this->makeFakeHttpRequest(
357 'redirect 6', 302, [ 'Location' => 'http://www.example.com/redirect-6.png' ]
358 ),
359 ] );
360
361 $upload = new UploadFromUrl();
362 $upload->initialize( 'Test.png', 'http://www.example.com/test.png' );
363 $status = $upload->fetchFile();
364
365 $this->assertStatusError( 'upload-too-many-redirects', $status );
366 }
367
368 public function testUploadFromUrlCacheKey() {
369 // Test we get back a properly formatted sha1 key out
370 $key = UploadFromUrl::getCacheKey( [ 'filename' => 'test.png', 'url' => 'https://example.com/example.png' ] );
371 $this->assertNotEmpty( $key );
372 $this->assertMatchesRegularExpression( "/^[0-9a-f]{40}$/", $key );
373 }
374
375 public function testUploadFromUrlCacheKeyMissingParam() {
376 $this->assertSame( "", UploadFromUrl::getCacheKey( [] ) );
377 }
378
379 }