View source
<?php
namespace Drupal\Tests\rest\Functional;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\rest\RestResourceConfigInterface;
use Drupal\user\Entity\User;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
abstract class FileUploadResourceTestBase extends ResourceTestBase {
protected static $modules = [
'rest_test',
'entity_test',
'file',
];
protected static $resourceConfigId = 'file.upload';
protected static $postUri = 'file/upload/entity_test/entity_test/field_rest_file_test';
protected $testFileData = 'Hares sit on chairs, and mules sit on stools.';
protected $fieldStorage;
protected $field;
protected $entity;
protected $file;
protected $user;
protected $fileStorage;
protected function setUp() : void {
parent::setUp();
$this->fileStorage = $this->container
->get('entity_type.manager')
->getStorage('file');
$this->fieldStorage = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_rest_file_test',
'type' => 'file',
'settings' => [
'uri_scheme' => 'public',
],
])
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_rest_file_test',
'bundle' => 'entity_test',
'settings' => [
'file_directory' => 'foobar',
'file_extensions' => 'txt',
'max_filesize' => '',
],
])
->setLabel('Test file field')
->setTranslatable(FALSE);
$this->field
->save();
$this->entity = EntityTest::create([
'name' => 'Llama',
'type' => 'entity_test',
]);
$this->entity
->setOwnerId(isset($this->account) ? $this->account
->id() : 0);
$this->entity
->save();
$this->resourceConfigStorage
->create([
'id' => 'entity.entity_test',
'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
'configuration' => [
'methods' => [
'POST',
],
'formats' => [
static::$format,
],
'authentication' => [
static::$auth,
],
],
'status' => TRUE,
])
->save();
$this->resourceConfigStorage
->create([
'id' => 'entity.file',
'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
'configuration' => [
'methods' => [
'GET',
],
'formats' => [
static::$format,
],
'authentication' => isset(static::$auth) ? [
static::$auth,
] : [],
],
'status' => TRUE,
])
->save();
$this
->refreshTestStateAfterRestConfigChange();
}
public function testPostFileUpload() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertResourceErrorResponse(403, $this
->getExpectedUnauthorizedAccessMessage('POST'), $response);
$this
->setUpAuthorization('POST');
$invalid_uri = Url::fromUri('base:file/upload/entity_test/entity_test/field_rest_file_test_invalid');
$response = $this
->fileRequest($invalid_uri, $this->testFileData);
$this
->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist', $response);
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity();
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'filename="example.txt"',
]);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity(2, 'example_0.txt');
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
$this
->assertTrue($this->fileStorage
->loadUnchanged(1)
->isTemporary());
$entity_test_post_url = Url::fromRoute('rest.entity.entity_test.POST')
->setOption('query', [
'_format' => static::$format,
]);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions('POST'));
$request_options[RequestOptions::BODY] = $this->serializer
->encode($this
->getNormalizedPostEntity(), static::$format);
$response = $this
->request('POST', $entity_test_post_url, $request_options);
$this
->assertResourceResponse(201, FALSE, $response);
$this
->assertTrue($this->fileStorage
->loadUnchanged(1)
->isPermanent());
$this
->assertSame([
[
'target_id' => '1',
'display' => NULL,
'description' => "The most fascinating file ever!",
],
], EntityTest::load(2)
->get('field_rest_file_test')
->getValue());
}
protected function getNormalizedPostEntity() {
return [
'type' => [
[
'value' => 'entity_test',
],
],
'name' => [
[
'value' => 'Dramallama',
],
],
'field_rest_file_test' => [
[
'target_id' => 1,
'description' => 'The most fascinating file ever!',
],
],
];
}
public function testPostFileUploadInvalidHeaders() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Type' => static::$mimeType,
]);
$this
->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => FALSE,
]);
$this
->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided', $response);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'file; filename=""',
]);
$this
->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'filename=""',
]);
$this
->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'not_a_filename="example.txt"',
]);
$this
->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'filename*="UTF-8 \' \' example.txt"',
]);
$this
->assertResourceErrorResponse(400, 'The extended "filename*" format is currently not supported in the "Content-Disposition" header', $response);
}
public function testPostFileUploadDuplicateFile() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertSame(201, $response
->getStatusCode());
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity(2, 'example_0.txt');
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt'));
}
public function testPostFileUploadDuplicateFileRaceCondition() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertSame(201, $response
->getStatusCode());
unlink(\Drupal::service('file_system')
->realpath('public://foobar/example.txt'));
$response = $this
->fileRequest($uri, $this->testFileData);
$this
->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: validation failed.\nuri: The file public://foobar/example.txt already exists. Enter a unique file URI.\n"), $response);
}
public function testFileUploadStrippedFilePath() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'file; filename="directory/example.txt"',
]);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity();
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'file; filename="../../example_2.txt"',
]);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity(2, 'example_2.txt', TRUE);
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/example_2.txt'));
$this
->assertFileDoesNotExist('../../example_2.txt');
$this->field
->setSetting('file_extensions', '')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'file; filename="/etc/passwd"',
]);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity(3, 'passwd', TRUE);
$expected['filemime'][0]['value'] = 'application/octet-stream';
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/passwd'));
}
public function testFileUploadUnicodeFilename() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'file; filename="Èxample-✓.txt"',
]);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity(1, 'Èxample-✓.txt', TRUE);
$this
->assertResponseData($expected, $response);
$this
->assertSame($this->testFileData, file_get_contents('public://foobar/Èxample-✓.txt'));
}
public function testFileUploadZeroByteFile() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, NULL);
$this
->assertSame(201, $response
->getStatusCode());
$expected = $this
->getExpectedNormalizedEntity();
$expected['filesize'][0]['value'] = 0;
$this
->assertResponseData($expected, $response);
$this
->assertSame('', file_get_contents('public://foobar/example.txt'));
}
public function testFileUploadInvalidFileType() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, '{"test":123}', [
'Content-Disposition' => 'filename="example.json"',
]);
$this
->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nOnly files with the following extensions are allowed: <em class=\"placeholder\">txt</em>."), $response);
$this
->assertEmpty(File::load(1));
$this
->assertFileDoesNotExist('public://foobar/example.txt');
}
public function testFileUploadLargerFileSize() {
$this->field
->setSetting('max_filesize', 50)
->save();
$this
->refreshTestStateAfterRestConfigChange();
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$response = $this
->fileRequest($uri, $this
->randomString(100));
$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);
$this
->assertEmpty(File::load(1));
$this
->assertFileDoesNotExist('public://foobar/example.txt');
}
public function testFileUploadMaliciousExtension() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this->field
->setSetting('file_extensions', '')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$php_string = '<?php print "Drupal"; ?>';
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example.php"',
]);
$expected = $this
->getExpectedNormalizedEntity(1, 'example.php_.txt', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example.php_.txt');
$this->field
->setSetting('file_extensions', 'php txt')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_2.php"',
]);
$expected = $this
->getExpectedNormalizedEntity(2, 'example_2.php_.txt', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_2.php_.txt');
$this
->assertFileDoesNotExist('public://foobar/example_2.php');
$this->field
->setSetting('file_extensions', 'doc')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_3.php.doc"',
]);
$expected = $this
->getExpectedNormalizedEntity(3, 'example_3.php_.doc', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$expected['filemime'][0]['value'] = 'application/msword';
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_3.php_.doc');
$this
->assertFileDoesNotExist('public://foobar/example_3.php.doc');
$this->field
->setSetting('file_extensions', 'doc php')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_4.php.doc"',
]);
$expected = $this
->getExpectedNormalizedEntity(4, 'example_4.php_.doc', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$expected['filemime'][0]['value'] = 'application/msword';
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_4.php_.doc');
$this
->assertFileDoesNotExist('public://foobar/example_4.php.doc');
$this->field
->setSetting('file_extensions', '')
->save();
$this
->rebuildAll();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_5.php.png"',
]);
$expected = $this
->getExpectedNormalizedEntity(5, 'example_5.php_.png', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$expected['filemime'][0]['value'] = 'image/png';
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_5.php_.png');
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_6.cgi.png.txt"',
]);
$expected = $this
->getExpectedNormalizedEntity(6, 'example_6.cgi_.png_.txt', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$expected['filemime'][0]['value'] = 'text/plain';
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_6.cgi_.png_.txt');
$this->field
->setSetting('file_extensions', 'php')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_7.php"',
]);
$this
->assertResourceErrorResponse(422, "Unprocessable Entity: file validation failed.\nFor security reasons, your upload has been rejected.", $response);
$this
->assertFileDoesNotExist('public://foobar/example_7.php');
$this
->assertFileDoesNotExist('public://foobar/example_7.php.txt');
\Drupal::configFactory()
->getEditable('system.file')
->set('allow_insecure_uploads', TRUE)
->save();
$this->field
->setSetting('file_extensions', '')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $php_string, [
'Content-Disposition' => 'filename="example_7.php"',
]);
$expected = $this
->getExpectedNormalizedEntity(7, 'example_7.php', TRUE);
$expected['filesize'][0]['value'] = strlen($php_string);
$expected['filemime'][0]['value'] = 'application/x-httpd-php';
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example_7.php');
}
public function testFileUploadNoExtensionSetting() {
$this
->initAuthentication();
$this
->provisionResource([
static::$format,
], static::$auth ? [
static::$auth,
] : [], [
'POST',
]);
$this
->setUpAuthorization('POST');
$uri = Url::fromUri('base:' . static::$postUri);
$this->field
->setSetting('file_extensions', '')
->save();
$this
->refreshTestStateAfterRestConfigChange();
$response = $this
->fileRequest($uri, $this->testFileData, [
'Content-Disposition' => 'filename="example.txt"',
]);
$expected = $this
->getExpectedNormalizedEntity(1, 'example.txt', TRUE);
$this
->assertResponseData($expected, $response);
$this
->assertFileExists('public://foobar/example.txt');
}
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
}
protected function getExpectedUnauthorizedAccessMessage($method) {
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'.";
}
protected function getExpectedNormalizedEntity($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE) {
$author = User::load(static::$auth ? $this->account
->id() : 0);
$file = File::load($fid);
$expected_normalization = [
'fid' => [
[
'value' => (int) $file
->id(),
],
],
'uuid' => [
[
'value' => $file
->uuid(),
],
],
'langcode' => [
[
'value' => 'en',
],
],
'uid' => [
[
'target_id' => (int) $author
->id(),
'target_type' => 'user',
'target_uuid' => $author
->uuid(),
'url' => base_path() . 'user/' . $author
->id(),
],
],
'filename' => [
[
'value' => $expected_as_filename ? $expected_filename : 'example.txt',
],
],
'uri' => [
[
'value' => 'public://foobar/' . $expected_filename,
'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename),
],
],
'filemime' => [
[
'value' => 'text/plain',
],
],
'filesize' => [
[
'value' => strlen($this->testFileData),
],
],
'status' => [
[
'value' => FALSE,
],
],
'created' => [
[
'value' => (new \DateTime())
->setTimestamp($file
->getCreatedTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
'changed' => [
[
'value' => (new \DateTime())
->setTimestamp($file
->getChangedTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
];
return $expected_normalization;
}
protected function fileRequest(Url $url, $file_contents, array $headers = []) {
$url
->setOption('query', [
'_format' => static::$format,
]);
$request_options = [];
$headers = $headers + [
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'file; filename="example.txt"',
];
$request_options[RequestOptions::HEADERS] = array_filter($headers, function ($value) {
return $value !== FALSE;
});
$request_options[RequestOptions::BODY] = $file_contents;
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions('POST'));
return $this
->request('POST', $url, $request_options);
}
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this
->grantPermissionsToTestedRole([
'view test entity',
]);
break;
case 'POST':
$this
->grantPermissionsToTestedRole([
'create entity_test entity_test_with_bundle entities',
'access content',
]);
break;
}
}
protected function assertResponseData(array $expected, ResponseInterface $response) {
static::recursiveKSort($expected);
$actual = $this->serializer
->decode((string) $response
->getBody(), static::$format);
static::recursiveKSort($actual);
$this
->assertSame($expected, $actual);
}
protected function getExpectedUnauthorizedAccessCacheability() {
return new CacheableMetadata();
}
}