Chris@18: fileStorage = $this->container->get('entity_type.manager')
Chris@18: ->getStorage('file');
Chris@18:
Chris@18: // Add a file field.
Chris@18: $this->fieldStorage = FieldStorageConfig::create([
Chris@18: 'entity_type' => 'entity_test',
Chris@18: 'field_name' => 'field_rest_file_test',
Chris@18: 'type' => 'file',
Chris@18: 'settings' => [
Chris@18: 'uri_scheme' => 'public',
Chris@18: ],
Chris@18: ])
Chris@18: ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
Chris@18: $this->fieldStorage->save();
Chris@18:
Chris@18: $this->field = FieldConfig::create([
Chris@18: 'entity_type' => 'entity_test',
Chris@18: 'field_name' => 'field_rest_file_test',
Chris@18: 'bundle' => 'entity_test',
Chris@18: 'settings' => [
Chris@18: 'file_directory' => 'foobar',
Chris@18: 'file_extensions' => 'txt',
Chris@18: 'max_filesize' => '',
Chris@18: ],
Chris@18: ])
Chris@18: ->setLabel('Test file field')
Chris@18: ->setTranslatable(FALSE);
Chris@18: $this->field->save();
Chris@18:
Chris@18: // Reload entity so that it has the new field.
Chris@18: $this->entity = $this->entityStorage->loadUnchanged($this->entity->id());
Chris@18:
Chris@18: $this->rebuildAll();
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testGetIndividual() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testPostIndividual() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testPatchIndividual() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testDeleteIndividual() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testCollection() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: *
Chris@18: * @requires module irrelevant_for_this_test
Chris@18: */
Chris@18: public function testRelationships() {}
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: */
Chris@18: protected function createEntity() {
Chris@18: // Create an entity that a file can be attached to.
Chris@18: $entity_test = EntityTest::create([
Chris@18: 'name' => 'Llama',
Chris@18: 'type' => 'entity_test',
Chris@18: ]);
Chris@18: $entity_test->setOwnerId($this->account->id());
Chris@18: $entity_test->save();
Chris@18:
Chris@18: return $entity_test;
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload POST route; needs second request to "use" file.
Chris@18: */
Chris@18: public function testPostFileUpload() {
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // DX: 405 when read-only mode is enabled.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(405, sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromUri('base:/admin/config/services/jsonapi')->setAbsolute()->toString(TRUE)->getGeneratedUrl()), $uri, $response);
Chris@18: $this->assertSame(['GET'], $response->getHeader('Allow'));
Chris@18:
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: // DX: 403 when unauthorized.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST'), $uri, $response);
Chris@18:
Chris@18: $this->setUpAuthorization('POST');
Chris@18:
Chris@18: // 404 when the field name is invalid.
Chris@18: $invalid_uri = Url::fromUri('base:' . static::$postUri . '_invalid');
Chris@18: $response = $this->fileRequest($invalid_uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist.', $invalid_uri, $response);
Chris@18:
Chris@18: // This request will have the default 'application/octet-stream' content
Chris@18: // type header.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument();
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
Chris@18:
Chris@18: // Test the file again but using 'filename' in the Content-Disposition
Chris@18: // header with no 'file' prefix.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument(2, 'example_0.txt');
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
Chris@18: $this->assertTrue($this->fileStorage->loadUnchanged(1)->isTemporary());
Chris@18:
Chris@18: // Verify that we can create an entity that references the uploaded file.
Chris@18: $entity_test_post_url = Url::fromRoute('jsonapi.entity_test--entity_test.collection.post');
Chris@18: $request_options = [];
Chris@18: $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
Chris@18: $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
Chris@18:
Chris@18: $request_options[RequestOptions::BODY] = Json::encode($this->getPostDocument());
Chris@18: $response = $this->request('POST', $entity_test_post_url, $request_options);
Chris@18: $this->assertResourceResponse(201, FALSE, $response);
Chris@18: $this->assertTrue($this->fileStorage->loadUnchanged(1)->isPermanent());
Chris@18: $this->assertSame([
Chris@18: [
Chris@18: 'target_id' => '1',
Chris@18: 'display' => NULL,
Chris@18: 'description' => "The most fascinating file ever!",
Chris@18: ],
Chris@18: ], EntityTest::load(2)->get('field_rest_file_test')->getValue());
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the 'file upload and "use" file in single request" POST route.
Chris@18: */
Chris@18: public function testPostFileUploadAndUseInSingleRequest() {
Chris@18: // Update the test entity so it already has a file. This allows verifying
Chris@18: // that this route appends files, and does not replace them.
Chris@18: mkdir('public://foobar');
Chris@18: file_put_contents('public://foobar/existing.txt', $this->testFileData);
Chris@18: $existing_file = File::create([
Chris@18: 'uri' => 'public://foobar/existing.txt',
Chris@18: ]);
Chris@18: $existing_file->setOwnerId($this->account->id());
Chris@18: $existing_file->setPermanent();
Chris@18: $existing_file->save();
Chris@18: $this->entity
Chris@18: ->set('field_rest_file_test', ['target_id' => $existing_file->id()])
Chris@18: ->save();
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . '/jsonapi/entity_test/entity_test/' . $this->entity->uuid() . '/field_rest_file_test');
Chris@18:
Chris@18: // DX: 405 when read-only mode is enabled.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(405, sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromUri('base:/admin/config/services/jsonapi')->setAbsolute()->toString(TRUE)->getGeneratedUrl()), $uri, $response);
Chris@18: $this->assertSame(['GET'], $response->getHeader('Allow'));
Chris@18:
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: // DX: 403 when unauthorized.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('PATCH'), $uri, $response);
Chris@18:
Chris@18: $this->setUpAuthorization('PATCH');
Chris@18:
Chris@18: // 404 when the field name is invalid.
Chris@18: $invalid_uri = Url::fromUri($uri->getUri() . '_invalid');
Chris@18: $response = $this->fileRequest($invalid_uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist.', $invalid_uri, $response);
Chris@18:
Chris@18: // This request fails despite the upload succeeding, because we're not
Chris@18: // allowed to view the entity we're uploading to.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('GET'), $uri, $response, FALSE, ['4xx-response', 'http_response'], ['url.site', 'user.permissions']);
Chris@18:
Chris@18: $this->setUpAuthorization('GET');
Chris@18:
Chris@18: // Reuploading the same file will result in the file being uploaded twice
Chris@18: // and referenced twice.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertSame(200, $response->getStatusCode());
Chris@18: $expected = [
Chris@18: 'jsonapi' => [
Chris@18: 'meta' => [
Chris@18: 'links' => [
Chris@18: 'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
Chris@18: ],
Chris@18: ],
Chris@18: 'version' => '1.0',
Chris@18: ],
Chris@18: 'links' => [
Chris@18: 'self' => ['href' => Url::fromUri('base:/jsonapi/entity_test/entity_test/' . $this->entity->uuid() . '/field_rest_file_test')->setAbsolute(TRUE)->toString()],
Chris@18: ],
Chris@18: 'data' => [
Chris@18: 0 => $this->getExpectedDocument(1, 'existing.txt', TRUE, TRUE)['data'],
Chris@18: 1 => $this->getExpectedDocument(2, 'example.txt', TRUE, TRUE)['data'],
Chris@18: 2 => $this->getExpectedDocument(3, 'example_0.txt', FALSE, TRUE)['data'],
Chris@18: ],
Chris@18: ];
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // The response document received for the POST request is identical to the
Chris@18: // response document received by GETting the same URL.
Chris@18: $request_options = [];
Chris@18: $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
Chris@18: $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
Chris@18: $response = $this->request('GET', $uri, $request_options);
Chris@18: $this->assertSame(200, $response->getStatusCode());
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Returns the JSON:API POST document referencing the uploaded file.
Chris@18: *
Chris@18: * @return array
Chris@18: * A JSON:API request document.
Chris@18: *
Chris@18: * @see ::testPostFileUpload()
Chris@18: * @see \Drupal\Tests\jsonapi\Functional\EntityTestTest::getPostDocument()
Chris@18: */
Chris@18: protected function getPostDocument() {
Chris@18: return [
Chris@18: 'data' => [
Chris@18: 'type' => 'entity_test--entity_test',
Chris@18: 'attributes' => [
Chris@18: 'name' => 'Dramallama',
Chris@18: ],
Chris@18: 'relationships' => [
Chris@18: 'field_rest_file_test' => [
Chris@18: 'data' => [
Chris@18: 'id' => File::load(1)->uuid(),
Chris@18: 'meta' => [
Chris@18: 'description' => 'The most fascinating file ever!',
Chris@18: ],
Chris@18: 'type' => 'file--file',
Chris@18: ],
Chris@18: ],
Chris@18: ],
Chris@18: ],
Chris@18: ];
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload POST route with invalid headers.
Chris@18: */
Chris@18: public function testPostFileUploadInvalidHeaders() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // The wrong content type header should return a 415 code.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Type' => 'application/vnd.api+json']);
Chris@18: $this->assertSame(415, $response->getStatusCode());
Chris@18:
Chris@18: // An empty Content-Disposition header should return a 400.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => FALSE]);
Chris@18: $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided.', $uri, $response);
Chris@18:
Chris@18: // An empty filename with a context in the Content-Disposition header should
Chris@18: // return a 400.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename=""']);
Chris@18: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $uri, $response);
Chris@18:
Chris@18: // An empty filename without a context in the Content-Disposition header
Chris@18: // should return a 400.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename=""']);
Chris@18: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $uri, $response);
Chris@18:
Chris@18: // An invalid key-value pair in the Content-Disposition header should return
Chris@18: // a 400.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'not_a_filename="example.txt"']);
Chris@18: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided.', $uri, $response);
Chris@18:
Chris@18: // Using filename* extended format is not currently supported.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename*="UTF-8 \' \' example.txt"']);
Chris@18: $this->assertResourceErrorResponse(400, 'The extended "filename*" format is currently not supported in the "Content-Disposition" header.', $uri, $response);
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload POST route with a duplicate file name.
Chris@18: *
Chris@18: * A new file should be created with a suffixed name.
Chris@18: */
Chris@18: public function testPostFileUploadDuplicateFile() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // This request will have the default 'application/octet-stream' content
Chris@18: // type header.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18:
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18:
Chris@18: // Make the same request again. The file should be saved as a new file
Chris@18: // entity that has the same file name but a suffixed file URI.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18:
Chris@18: // Loading expected normalized data for file 2, the duplicate file.
Chris@18: $expected = $this->getExpectedDocument(2, 'example_0.txt');
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload route with any path prefixes being stripped.
Chris@18: *
Chris@18: * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives
Chris@18: */
Chris@18: public function testFileUploadStrippedFilePath() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="directory/example.txt"']);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument();
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data. It should have been written to the configured
Chris@18: // directory, not /foobar/directory/example.txt.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="../../example_2.txt"']);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument(2, 'example_2.txt', TRUE);
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data. It should have been written to the configured
Chris@18: // directory, not /foobar/directory/example.txt.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_2.txt'));
Chris@18: $this->assertFalse(file_exists('../../example_2.txt'));
Chris@18:
Chris@18: // Check a path from the root. Extensions have to be empty to allow a file
Chris@18: // with no extension to pass validation.
Chris@18: $this->field->setSetting('file_extensions', '')
Chris@18: ->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="/etc/passwd"']);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument(3, 'passwd', TRUE);
Chris@18: // This mime will be guessed as there is no extension.
Chris@18: $expected['data']['attributes']['filemime'] = 'application/octet-stream';
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data. It should have been written to the configured
Chris@18: // directory, not /foobar/directory/example.txt.
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/passwd'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload route with a unicode file name.
Chris@18: */
Chris@18: public function testFileUploadUnicodeFilename() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // It is important that the filename starts with a unicode character. See
Chris@18: // https://bugs.php.net/bug.php?id=77239.
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="Èxample-✓.txt"']);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument(1, 'Èxample-✓.txt', TRUE);
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/Èxample-✓.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload route with a zero byte file.
Chris@18: */
Chris@18: public function testFileUploadZeroByteFile() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // Test with a zero byte file.
Chris@18: $response = $this->fileRequest($uri, NULL);
Chris@18: $this->assertSame(201, $response->getStatusCode());
Chris@18: $expected = $this->getExpectedDocument();
Chris@18: // Modify the default expected data to account for the 0 byte file.
Chris@18: $expected['data']['attributes']['filesize'] = 0;
Chris@18: $this->assertResponseData($expected, $response);
Chris@18:
Chris@18: // Check the actual file data.
Chris@18: $this->assertSame('', file_get_contents('public://foobar/example.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload route with an invalid file type.
Chris@18: */
Chris@18: public function testFileUploadInvalidFileType() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // Test with a JSON file.
Chris@18: $response = $this->fileRequest($uri, '{"test":123}', ['Content-Disposition' => 'filename="example.json"']);
Chris@18: $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nOnly files with the following extensions are allowed: txt."), $uri, $response);
Chris@18:
Chris@18: // Make sure that no file was saved.
Chris@18: $this->assertEmpty(File::load(1));
Chris@18: $this->assertFalse(file_exists('public://foobar/example.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload route with a file size larger than allowed.
Chris@18: */
Chris@18: public function testFileUploadLargerFileSize() {
Chris@18: // Set a limit of 50 bytes.
Chris@18: $this->field->setSetting('max_filesize', 50)
Chris@18: ->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: // Generate a string larger than the 50 byte limit set.
Chris@18: $response = $this->fileRequest($uri, $this->randomString(100));
Chris@18: $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nThe file is 100 bytes exceeding the maximum file size of 50 bytes."), $uri, $response);
Chris@18:
Chris@18: // Make sure that no file was saved.
Chris@18: $this->assertEmpty(File::load(1));
Chris@18: $this->assertFalse(file_exists('public://foobar/example.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload POST route with malicious extensions.
Chris@18: */
Chris@18: public function testFileUploadMaliciousExtension() {
Chris@18: // Allow all file uploads but system.file::allow_insecure_uploads is set to
Chris@18: // FALSE.
Chris@18: $this->field->setSetting('file_extensions', '')->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: $php_string = '';
Chris@18:
Chris@18: // Test using a masked exploit file.
Chris@18: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example.php"']);
Chris@18: // The filename is not munged because .txt is added and it is a known
Chris@18: // extension to apache.
Chris@18: $expected = $this->getExpectedDocument(1, 'example.php.txt', TRUE);
Chris@18: // Override the expected filesize.
Chris@18: $expected['data']['attributes']['filesize'] = strlen($php_string);
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertTrue(file_exists('public://foobar/example.php.txt'));
Chris@18:
Chris@18: // Add php as an allowed format. Allow insecure uploads still being FALSE
Chris@18: // should still not allow this. So it should still have a .txt extension
Chris@18: // appended even though it is not in the list of allowed extensions.
Chris@18: $this->field->setSetting('file_extensions', 'php')
Chris@18: ->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_2.php"']);
Chris@18: $expected = $this->getExpectedDocument(2, 'example_2.php.txt', TRUE);
Chris@18: // Override the expected filesize.
Chris@18: $expected['data']['attributes']['filesize'] = strlen($php_string);
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertTrue(file_exists('public://foobar/example_2.php.txt'));
Chris@18: $this->assertFalse(file_exists('public://foobar/example_2.php'));
Chris@18:
Chris@18: // Allow .doc file uploads and ensure even a mis-configured apache will not
Chris@18: // fallback to php because the filename will be munged.
Chris@18: $this->field->setSetting('file_extensions', 'doc')->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: // Test using a masked exploit file.
Chris@18: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_3.php.doc"']);
Chris@18: // The filename is munged.
Chris@18: $expected = $this->getExpectedDocument(3, 'example_3.php_.doc', TRUE);
Chris@18: // Override the expected filesize.
Chris@18: $expected['data']['attributes']['filesize'] = strlen($php_string);
Chris@18: // The file mime should be 'application/msword'.
Chris@18: $expected['data']['attributes']['filemime'] = 'application/msword';
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertTrue(file_exists('public://foobar/example_3.php_.doc'));
Chris@18: $this->assertFalse(file_exists('public://foobar/example_3.php.doc'));
Chris@18:
Chris@18: // Now allow insecure uploads.
Chris@18: \Drupal::configFactory()
Chris@18: ->getEditable('system.file')
Chris@18: ->set('allow_insecure_uploads', TRUE)
Chris@18: ->save();
Chris@18: // Allow all file uploads. This is very insecure.
Chris@18: $this->field->setSetting('file_extensions', '')->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_4.php"']);
Chris@18: $expected = $this->getExpectedDocument(4, 'example_4.php', TRUE);
Chris@18: // Override the expected filesize.
Chris@18: $expected['data']['attributes']['filesize'] = strlen($php_string);
Chris@18: // The file mime should also now be PHP.
Chris@18: $expected['data']['attributes']['filemime'] = 'application/x-httpd-php';
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertTrue(file_exists('public://foobar/example_4.php'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Tests using the file upload POST route no extension configured.
Chris@18: */
Chris@18: public function testFileUploadNoExtensionSetting() {
Chris@18: $this->setUpAuthorization('POST');
Chris@18: $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
Chris@18:
Chris@18: $uri = Url::fromUri('base:' . static::$postUri);
Chris@18:
Chris@18: $this->field->setSetting('file_extensions', '')
Chris@18: ->save();
Chris@18: $this->rebuildAll();
Chris@18:
Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']);
Chris@18: $expected = $this->getExpectedDocument(1, 'example.txt', TRUE);
Chris@18:
Chris@18: $this->assertResponseData($expected, $response);
Chris@18: $this->assertTrue(file_exists('public://foobar/example.txt'));
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: */
Chris@18: protected function getExpectedUnauthorizedAccessMessage($method) {
Chris@18: switch ($method) {
Chris@18: case 'GET':
Chris@18: return "The current user is not allowed to view this relationship. The 'view test entity' permission is required.";
Chris@18:
Chris@18: case 'POST':
Chris@18: return "The current user is not permitted to upload a file for this field. 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'.";
Chris@18:
Chris@18: case 'PATCH':
Chris@18: return "The current user is not permitted to upload a file for this field. The 'administer entity_test content' permission is required.";
Chris@18: }
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Returns the expected JSON:API document for the expected file entity.
Chris@18: *
Chris@18: * @param int $fid
Chris@18: * The file ID to load and create a JSON:API document for.
Chris@18: * @param string $expected_filename
Chris@18: * The expected filename for the stored file.
Chris@18: * @param bool $expected_as_filename
Chris@18: * Whether the expected filename should be the filename property too.
Chris@18: * @param bool $expected_status
Chris@18: * The expected file status. Defaults to FALSE.
Chris@18: *
Chris@18: * @return array
Chris@18: * A JSON:API response document.
Chris@18: */
Chris@18: protected function getExpectedDocument($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE, $expected_status = FALSE) {
Chris@18: $author = User::load($this->account->id());
Chris@18: $file = File::load($fid);
Chris@18: $self_url = Url::fromUri('base:/jsonapi/file/file/' . $file->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
Chris@18:
Chris@18: return [
Chris@18: 'jsonapi' => [
Chris@18: 'meta' => [
Chris@18: 'links' => [
Chris@18: 'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
Chris@18: ],
Chris@18: ],
Chris@18: 'version' => '1.0',
Chris@18: ],
Chris@18: 'links' => [
Chris@18: 'self' => ['href' => $self_url],
Chris@18: ],
Chris@18: 'data' => [
Chris@18: 'id' => $file->uuid(),
Chris@18: 'type' => 'file--file',
Chris@18: 'links' => [
Chris@18: 'self' => ['href' => $self_url],
Chris@18: ],
Chris@18: 'attributes' => [
Chris@18: 'created' => (new \DateTime())->setTimestamp($file->getCreatedTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
Chris@18: 'changed' => (new \DateTime())->setTimestamp($file->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
Chris@18: 'filemime' => 'text/plain',
Chris@18: 'filename' => $expected_as_filename ? $expected_filename : 'example.txt',
Chris@18: 'filesize' => strlen($this->testFileData),
Chris@18: 'langcode' => 'en',
Chris@18: 'status' => $expected_status,
Chris@18: 'uri' => [
Chris@18: 'value' => 'public://foobar/' . $expected_filename,
Chris@18: 'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename),
Chris@18: ],
Chris@18: 'drupal_internal__fid' => (int) $file->id(),
Chris@18: ],
Chris@18: 'relationships' => [
Chris@18: 'uid' => [
Chris@18: 'data' => [
Chris@18: 'id' => $author->uuid(),
Chris@18: 'type' => 'user--user',
Chris@18: ],
Chris@18: 'links' => [
Chris@18: 'related' => ['href' => $self_url . '/uid'],
Chris@18: 'self' => ['href' => $self_url . '/relationships/uid'],
Chris@18: ],
Chris@18: ],
Chris@18: ],
Chris@18: ],
Chris@18: ];
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Performs a file upload request. Wraps the Guzzle HTTP client.
Chris@18: *
Chris@18: * @param \Drupal\Core\Url $url
Chris@18: * URL to request.
Chris@18: * @param string $file_contents
Chris@18: * The file contents to send as the request body.
Chris@18: * @param array $headers
Chris@18: * Additional headers to send with the request. Defaults will be added for
Chris@18: * Content-Type and Content-Disposition. In order to remove the defaults set
Chris@18: * the header value to FALSE.
Chris@18: *
Chris@18: * @return \Psr\Http\Message\ResponseInterface
Chris@18: * The received response.
Chris@18: *
Chris@18: * @see \GuzzleHttp\ClientInterface::request()
Chris@18: */
Chris@18: protected function fileRequest(Url $url, $file_contents, array $headers = []) {
Chris@18: $request_options = [];
Chris@18: $headers = $headers + [
Chris@18: // Set the required (and only accepted) content type for the request.
Chris@18: 'Content-Type' => 'application/octet-stream',
Chris@18: // Set the required Content-Disposition header for the file name.
Chris@18: 'Content-Disposition' => 'file; filename="example.txt"',
Chris@18: // Set the required JSON:API Accept header.
Chris@18: 'Accept' => 'application/vnd.api+json',
Chris@18: ];
Chris@18: $request_options[RequestOptions::HEADERS] = array_filter($headers, function ($value) {
Chris@18: return $value !== FALSE;
Chris@18: });
Chris@18: $request_options[RequestOptions::BODY] = $file_contents;
Chris@18: $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
Chris@18:
Chris@18: return $this->request('POST', $url, $request_options);
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: */
Chris@18: protected function setUpAuthorization($method) {
Chris@18: switch ($method) {
Chris@18: case 'GET':
Chris@18: $this->grantPermissionsToTestedRole(['view test entity']);
Chris@18: break;
Chris@18:
Chris@18: case 'POST':
Chris@18: $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities', 'access content']);
Chris@18: break;
Chris@18:
Chris@18: case 'PATCH':
Chris@18: $this->grantPermissionsToTestedRole(['administer entity_test content', 'access content']);
Chris@18: break;
Chris@18: }
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * Asserts expected normalized data matches response data.
Chris@18: *
Chris@18: * @param array $expected
Chris@18: * The expected data.
Chris@18: * @param \Psr\Http\Message\ResponseInterface $response
Chris@18: * The file upload response.
Chris@18: */
Chris@18: protected function assertResponseData(array $expected, ResponseInterface $response) {
Chris@18: static::recursiveKSort($expected);
Chris@18: $actual = Json::decode((string) $response->getBody());
Chris@18: static::recursiveKSort($actual);
Chris@18:
Chris@18: $this->assertSame($expected, $actual);
Chris@18: }
Chris@18:
Chris@18: /**
Chris@18: * {@inheritdoc}
Chris@18: */
Chris@18: protected function getExpectedUnauthorizedAccessCacheability() {
Chris@18: // There is cacheability metadata to check as file uploads only allows POST
Chris@18: // requests, which will not return cacheable responses.
Chris@18: }
Chris@18:
Chris@18: }