3 namespace Drupal\Tests\rest\Functional;
5 use Drupal\Component\Render\PlainTextOutput;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\file\Entity\File;
13 use Drupal\rest\RestResourceConfigInterface;
14 use Drupal\user\Entity\User;
15 use GuzzleHttp\RequestOptions;
16 use Psr\Http\Message\ResponseInterface;
19 * Tests binary data file upload route.
21 abstract class FileUploadResourceTestBase extends ResourceTestBase {
23 use BcTimestampNormalizerUnixTestTrait;
28 public static $modules = ['rest_test', 'entity_test', 'file'];
33 protected static $resourceConfigId = 'file.upload';
40 protected static $postUri = 'file/upload/entity_test/entity_test/field_rest_file_test';
47 protected $testFileData = 'Hares sit on chairs, and mules sit on stools.';
50 * The test field storage config.
52 * @var \Drupal\field\Entity\FieldStorageConfig
54 protected $fieldStorage;
59 * @var \Drupal\field\Entity\FieldConfig
66 * @var \Drupal\Core\Entity\EntityInterface
71 * Created file entity.
73 * @var \Drupal\file\Entity\File
78 * An authenticated user.
80 * @var \Drupal\user\UserInterface
85 * The entity storage for the 'file' entity type.
87 * @var \Drupal\Core\Entity\EntityStorageInterface
89 protected $fileStorage;
94 public function setUp() {
97 $this->fileStorage = $this->container->get('entity_type.manager')
101 $this->fieldStorage = FieldStorageConfig::create([
102 'entity_type' => 'entity_test',
103 'field_name' => 'field_rest_file_test',
106 'uri_scheme' => 'public',
109 ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
110 $this->fieldStorage->save();
112 $this->field = FieldConfig::create([
113 'entity_type' => 'entity_test',
114 'field_name' => 'field_rest_file_test',
115 'bundle' => 'entity_test',
117 'file_directory' => 'foobar',
118 'file_extensions' => 'txt',
119 'max_filesize' => '',
122 ->setLabel('Test file field')
123 ->setTranslatable(FALSE);
124 $this->field->save();
126 // Create an entity that a file can be attached to.
127 $this->entity = EntityTest::create([
129 'type' => 'entity_test',
131 $this->entity->setOwnerId(isset($this->account) ? $this->account->id() : 0);
132 $this->entity->save();
134 // Provision entity_test resource.
135 $this->resourceConfigStorage->create([
136 'id' => 'entity.entity_test',
137 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
139 'methods' => ['POST'],
140 'formats' => [static::$format],
141 'authentication' => [static::$auth],
146 $this->refreshTestStateAfterRestConfigChange();
150 * Tests using the file upload POST route.
152 public function testPostFileUpload() {
153 $this->initAuthentication();
155 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
157 $uri = Url::fromUri('base:' . static::$postUri);
159 // DX: 403 when unauthorized.
160 $response = $this->fileRequest($uri, $this->testFileData);
161 $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST'), $response);
163 $this->setUpAuthorization('POST');
165 // 404 when the field name is invalid.
166 $invalid_uri = Url::fromUri('base:file/upload/entity_test/entity_test/field_rest_file_test_invalid');
167 $response = $this->fileRequest($invalid_uri, $this->testFileData);
168 $this->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist', $response);
170 // This request will have the default 'application/octet-stream' content
172 $response = $this->fileRequest($uri, $this->testFileData);
173 $this->assertSame(201, $response->getStatusCode());
174 $expected = $this->getExpectedNormalizedEntity();
175 $this->assertResponseData($expected, $response);
177 // Check the actual file data.
178 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
180 // Test the file again but using 'filename' in the Content-Disposition
181 // header with no 'file' prefix.
182 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']);
183 $this->assertSame(201, $response->getStatusCode());
184 $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt');
185 $this->assertResponseData($expected, $response);
187 // Check the actual file data.
188 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
189 $this->assertTrue($this->fileStorage->loadUnchanged(1)->isTemporary());
191 // Verify that we can create an entity that references the uploaded file.
192 $entity_test_post_url = Url::fromRoute('rest.entity.entity_test.POST')
193 ->setOption('query', ['_format' => static::$format]);
194 $request_options = [];
195 $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
196 $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST'));
198 $request_options[RequestOptions::BODY] = $this->serializer->encode($this->getNormalizedPostEntity(), static::$format);
199 $response = $this->request('POST', $entity_test_post_url, $request_options);
200 $this->assertResourceResponse(201, FALSE, $response);
201 $this->assertTrue($this->fileStorage->loadUnchanged(1)->isPermanent());
206 'description' => "The most fascinating file ever!",
208 ], EntityTest::load(2)->get('field_rest_file_test')->getValue());
212 * Returns the normalized POST entity referencing the uploaded file.
216 * @see ::testPostFileUpload()
217 * @see \Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase::getNormalizedPostEntity()
219 protected function getNormalizedPostEntity() {
223 'value' => 'entity_test',
228 'value' => 'Dramallama',
231 'field_rest_file_test' => [
234 'description' => 'The most fascinating file ever!',
241 * Tests using the file upload POST route with invalid headers.
243 public function testPostFileUploadInvalidHeaders() {
244 $this->initAuthentication();
246 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
248 $this->setUpAuthorization('POST');
250 $uri = Url::fromUri('base:' . static::$postUri);
252 // The wrong content type header should return a 415 code.
253 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Type' => static::$mimeType]);
254 $this->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response);
256 // An empty Content-Disposition header should return a 400.
257 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => '']);
258 $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided', $response);
260 // An empty filename with a context in the Content-Disposition header should
262 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename=""']);
263 $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
265 // An empty filename without a context in the Content-Disposition header
266 // should return a 400.
267 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename=""']);
268 $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
270 // An invalid key-value pair in the Content-Disposition header should return
272 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'not_a_filename="example.txt"']);
273 $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
275 // Using filename* extended format is not currently supported.
276 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename*="UTF-8 \' \' example.txt"']);
277 $this->assertResourceErrorResponse(400, 'The extended "filename*" format is currently not supported in the "Content-Disposition" header', $response);
281 * Tests using the file upload POST route with a duplicate file name.
283 * A new file should be created with a suffixed name.
285 public function testPostFileUploadDuplicateFile() {
286 $this->initAuthentication();
288 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
290 $this->setUpAuthorization('POST');
292 $uri = Url::fromUri('base:' . static::$postUri);
294 // This request will have the default 'application/octet-stream' content
296 $response = $this->fileRequest($uri, $this->testFileData);
298 $this->assertSame(201, $response->getStatusCode());
300 // Make the same request again. The file should be saved as a new file
301 // entity that has the same file name but a suffixed file URI.
302 $response = $this->fileRequest($uri, $this->testFileData);
303 $this->assertSame(201, $response->getStatusCode());
305 // Loading expected normalized data for file 2, the duplicate file.
306 $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt');
307 $this->assertResponseData($expected, $response);
309 // Check the actual file data.
310 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
314 * Tests using the file upload route with any path prefixes being stripped.
316 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives
318 public function testFileUploadStrippedFilePath() {
319 $this->initAuthentication();
321 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
323 $this->setUpAuthorization('POST');
325 $uri = Url::fromUri('base:' . static::$postUri);
327 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="directory/example.txt"']);
328 $this->assertSame(201, $response->getStatusCode());
329 $expected = $this->getExpectedNormalizedEntity();
330 $this->assertResponseData($expected, $response);
332 // Check the actual file data. It should have been written to the configured
333 // directory, not /foobar/directory/example.txt.
334 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
336 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="../../example_2.txt"']);
337 $this->assertSame(201, $response->getStatusCode());
338 $expected = $this->getExpectedNormalizedEntity(2, 'example_2.txt', TRUE);
339 $this->assertResponseData($expected, $response);
341 // Check the actual file data. It should have been written to the configured
342 // directory, not /foobar/directory/example.txt.
343 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_2.txt'));
344 $this->assertFalse(file_exists('../../example_2.txt'));
346 // Check a path from the root. Extensions have to be empty to allow a file
347 // with no extension to pass validation.
348 $this->field->setSetting('file_extensions', '')
350 $this->refreshTestStateAfterRestConfigChange();
352 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="/etc/passwd"']);
353 $this->assertSame(201, $response->getStatusCode());
354 $expected = $this->getExpectedNormalizedEntity(3, 'passwd', TRUE);
355 // This mime will be guessed as there is no extension.
356 $expected['filemime'][0]['value'] = 'application/octet-stream';
357 $this->assertResponseData($expected, $response);
359 // Check the actual file data. It should have been written to the configured
360 // directory, not /foobar/directory/example.txt.
361 $this->assertSame($this->testFileData, file_get_contents('public://foobar/passwd'));
365 * Tests using the file upload route with a unicode file name.
367 public function testFileUploadUnicodeFilename() {
368 $this->initAuthentication();
370 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
372 $this->setUpAuthorization('POST');
374 $uri = Url::fromUri('base:' . static::$postUri);
376 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="example-✓.txt"']);
377 $this->assertSame(201, $response->getStatusCode());
378 $expected = $this->getExpectedNormalizedEntity(1, 'example-✓.txt', TRUE);
379 $this->assertResponseData($expected, $response);
380 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example-✓.txt'));
384 * Tests using the file upload route with a zero byte file.
386 public function testFileUploadZeroByteFile() {
387 $this->initAuthentication();
389 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
391 $this->setUpAuthorization('POST');
393 $uri = Url::fromUri('base:' . static::$postUri);
395 // Test with a zero byte file.
396 $response = $this->fileRequest($uri, NULL);
397 $this->assertSame(201, $response->getStatusCode());
398 $expected = $this->getExpectedNormalizedEntity();
399 // Modify the default expected data to account for the 0 byte file.
400 $expected['filesize'][0]['value'] = 0;
401 $this->assertResponseData($expected, $response);
403 // Check the actual file data.
404 $this->assertSame('', file_get_contents('public://foobar/example.txt'));
408 * Tests using the file upload route with an invalid file type.
410 public function testFileUploadInvalidFileType() {
411 $this->initAuthentication();
413 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
415 $this->setUpAuthorization('POST');
417 $uri = Url::fromUri('base:' . static::$postUri);
419 // Test with a JSON file.
420 $response = $this->fileRequest($uri, '{"test":123}', ['Content-Disposition' => 'filename="example.json"']);
421 $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nOnly files with the following extensions are allowed: <em class=\"placeholder\">txt</em>."), $response);
423 // Make sure that no file was saved.
424 $this->assertEmpty(File::load(1));
425 $this->assertFalse(file_exists('public://foobar/example.txt'));
429 * Tests using the file upload route with a file size larger than allowed.
431 public function testFileUploadLargerFileSize() {
432 // Set a limit of 50 bytes.
433 $this->field->setSetting('max_filesize', 50)
435 $this->refreshTestStateAfterRestConfigChange();
437 $this->initAuthentication();
439 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
441 $this->setUpAuthorization('POST');
443 $uri = Url::fromUri('base:' . static::$postUri);
445 // Generate a string larger than the 50 byte limit set.
446 $response = $this->fileRequest($uri, $this->randomString(100));
447 $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nThe file is <em class=\"placeholder\">100 bytes</em> exceeding the maximum file size of <em class=\"placeholder\">50 bytes</em>."), $response);
449 // Make sure that no file was saved.
450 $this->assertEmpty(File::load(1));
451 $this->assertFalse(file_exists('public://foobar/example.txt'));
455 * Tests using the file upload POST route with malicious extensions.
457 public function testFileUploadMaliciousExtension() {
458 $this->initAuthentication();
460 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
461 // Allow all file uploads but system.file::allow_insecure_uploads is set to
463 $this->field->setSetting('file_extensions', '')->save();
464 $this->refreshTestStateAfterRestConfigChange();
466 $this->setUpAuthorization('POST');
468 $uri = Url::fromUri('base:' . static::$postUri);
470 $php_string = '<?php print "Drupal"; ?>';
472 // Test using a masked exploit file.
473 $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example.php"']);
474 // The filename is not munged because .txt is added and it is a known
475 // extension to apache.
476 $expected = $this->getExpectedNormalizedEntity(1, 'example.php.txt', TRUE);
477 // Override the expected filesize.
478 $expected['filesize'][0]['value'] = strlen($php_string);
479 $this->assertResponseData($expected, $response);
480 $this->assertTrue(file_exists('public://foobar/example.php.txt'));
482 // Add php as an allowed format. Allow insecure uploads still being FALSE
483 // should still not allow this. So it should still have a .txt extension
484 // appended even though it is not in the list of allowed extensions.
485 $this->field->setSetting('file_extensions', 'php')
487 $this->refreshTestStateAfterRestConfigChange();
489 $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_2.php"']);
490 $expected = $this->getExpectedNormalizedEntity(2, 'example_2.php.txt', TRUE);
491 // Override the expected filesize.
492 $expected['filesize'][0]['value'] = strlen($php_string);
493 $this->assertResponseData($expected, $response);
494 $this->assertTrue(file_exists('public://foobar/example_2.php.txt'));
495 $this->assertFalse(file_exists('public://foobar/example_2.php'));
497 // Allow .doc file uploads and ensure even a mis-configured apache will not
498 // fallback to php because the filename will be munged.
499 $this->field->setSetting('file_extensions', 'doc')->save();
500 $this->refreshTestStateAfterRestConfigChange();
502 // Test using a masked exploit file.
503 $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_3.php.doc"']);
504 // The filename is munged.
505 $expected = $this->getExpectedNormalizedEntity(3, 'example_3.php_.doc', TRUE);
506 // Override the expected filesize.
507 $expected['filesize'][0]['value'] = strlen($php_string);
508 // The file mime should be 'application/msword'.
509 $expected['filemime'][0]['value'] = 'application/msword';
510 $this->assertResponseData($expected, $response);
511 $this->assertTrue(file_exists('public://foobar/example_3.php_.doc'));
512 $this->assertFalse(file_exists('public://foobar/example_3.php.doc'));
514 // Now allow insecure uploads.
515 \Drupal::configFactory()
516 ->getEditable('system.file')
517 ->set('allow_insecure_uploads', TRUE)
519 // Allow all file uploads. This is very insecure.
520 $this->field->setSetting('file_extensions', '')->save();
521 $this->refreshTestStateAfterRestConfigChange();
523 $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_4.php"']);
524 $expected = $this->getExpectedNormalizedEntity(4, 'example_4.php', TRUE);
525 // Override the expected filesize.
526 $expected['filesize'][0]['value'] = strlen($php_string);
527 // The file mime should also now be PHP.
528 $expected['filemime'][0]['value'] = 'application/x-httpd-php';
529 $this->assertResponseData($expected, $response);
530 $this->assertTrue(file_exists('public://foobar/example_4.php'));
534 * Tests using the file upload POST route no extension configured.
536 public function testFileUploadNoExtensionSetting() {
537 $this->initAuthentication();
539 $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']);
541 $this->setUpAuthorization('POST');
543 $uri = Url::fromUri('base:' . static::$postUri);
545 $this->field->setSetting('file_extensions', '')
547 $this->refreshTestStateAfterRestConfigChange();
549 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']);
550 $expected = $this->getExpectedNormalizedEntity(1, 'example.txt', TRUE);
552 $this->assertResponseData($expected, $response);
553 $this->assertTrue(file_exists('public://foobar/example.txt'));
559 protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
560 // The file upload resource only accepts binary data, so there are no
561 // normalization edge cases to test, as there are no normalized entity
562 // representations incoming.
568 protected function getExpectedUnauthorizedAccessMessage($method) {
569 return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test entity_test_with_bundle entities'.";
573 * Gets the expected file entity.
576 * The file ID to load and create normalized data for.
577 * @param string $expected_filename
578 * The expected filename for the stored file.
579 * @param bool $expected_as_filename
580 * Whether the expected filename should be the filename property too.
583 * The expected normalized data array.
585 protected function getExpectedNormalizedEntity($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE) {
586 $author = User::load(static::$auth ? $this->account->id() : 0);
587 $file = File::load($fid);
589 $expected_normalization = [
592 'value' => (int) $file->id(),
597 'value' => $file->uuid(),
607 'target_id' => (int) $author->id(),
608 'target_type' => 'user',
609 'target_uuid' => $author->uuid(),
610 'url' => base_path() . 'user/' . $author->id(),
615 'value' => $expected_as_filename ? $expected_filename : 'example.txt',
620 'value' => 'public://foobar/' . $expected_filename,
621 'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename),
626 'value' => 'text/plain',
631 'value' => strlen($this->testFileData),
640 $this->formatExpectedTimestampItemValues($file->getCreatedTime()),
643 $this->formatExpectedTimestampItemValues($file->getChangedTime()),
647 return $expected_normalization;
651 * Performs a file upload request. Wraps the Guzzle HTTP client.
653 * @see \GuzzleHttp\ClientInterface::request()
655 * @param \Drupal\Core\Url $url
657 * @param string $file_contents
658 * The file contents to send as the request body.
659 * @param array $headers
660 * Additional headers to send with the request. Defaults will be added for
661 * Content-Type and Content-Disposition.
663 * @return \Psr\Http\Message\ResponseInterface
665 protected function fileRequest(Url $url, $file_contents, array $headers = []) {
666 // Set the format for the response.
667 $url->setOption('query', ['_format' => static::$format]);
669 $request_options = [];
670 $request_options[RequestOptions::HEADERS] = $headers + [
671 // Set the required (and only accepted) content type for the request.
672 'Content-Type' => 'application/octet-stream',
673 // Set the required Content-Disposition header for the file name.
674 'Content-Disposition' => 'file; filename="example.txt"',
676 $request_options[RequestOptions::BODY] = $file_contents;
677 $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST'));
679 return $this->request('POST', $url, $request_options);
685 protected function setUpAuthorization($method) {
688 $this->grantPermissionsToTestedRole(['view test entity']);
691 $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities', 'access content']);
697 * Asserts expected normalized data matches response data.
699 * @param array $expected
701 * @param \Psr\Http\Message\ResponseInterface $response
702 * The file upload response.
704 protected function assertResponseData(array $expected, ResponseInterface $response) {
705 static::recursiveKSort($expected);
706 $actual = $this->serializer->decode((string) $response->getBody(), static::$format);
707 static::recursiveKSort($actual);
709 $this->assertSame($expected, $actual);
715 protected function getExpectedUnauthorizedAccessCacheability() {
716 // There is cacheability metadata to check as file uploads only allows POST
717 // requests, which will not return cacheable responses.