Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Classes/Domain/Model/UploadSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ public static function create(
array $attributes = [],
?AbstractOperationResourceSettings $uploadSettings = null
): AbstractOperationResourceSettings {
$uploadSettings = parent::create($attributes, $uploadSettings);
$base = $uploadSettings;
$uploadSettings = parent::create($attributes, $base);
$uploadSettings->folder = $attributes['folder'] ?? $uploadSettings->folder;
$uploadSettings->allowedFileExtensions = $attributes['allowedFileExtensions'] ??
GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']);
// Lowercase, as the extension of the uploaded file is compared in lowercase
$uploadSettings->allowedFileExtensions = array_map(
static fn(mixed $extension): string => strtolower((string)$extension),
$attributes['allowedFileExtensions']
?? $base?->allowedFileExtensions
?? GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'])
);
$uploadSettings->conflictMode = $attributes['conflictMode'] ?? $uploadSettings->conflictMode;
$uploadSettings->filenameHashAlgorithm = $attributes['filenameHashAlgorithm'] ?? $uploadSettings->filenameHashAlgorithm;
$uploadSettings->contentHashAlgorithm = $attributes['contentHashAlgorithm'] ?? $uploadSettings->contentHashAlgorithm;
Expand Down
4 changes: 3 additions & 1 deletion Documentation/HandlingFileUpload/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ There is plenty configuration options which allows you to customize upload endpo

- ``folder`` - destination folder (default: ``1:/user_upload/`` which means files will be uploaded into ``user_upload`` directory of file storage ID ``1``).

- ``allowedFileExtensions`` - Array of allowed file extensions (default: ``$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']``). Files with PHP extensions are always rejected.
- ``allowedFileExtensions`` - Array of allowed file extensions, case-insensitive (default: ``$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']``). An empty array allows all extensions. Files with PHP extensions are always rejected.

- ``conflictMode`` - What to do if file with the same name already exists: ``rename`` (default - new file name gets a suffix, e.g. ``image_01.jpg``), ``replace`` or ``cancel`` (values of ``\TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior``).

Expand Down Expand Up @@ -73,6 +73,8 @@ There is plenty configuration options which allows you to customize upload endpo
{
}

Settings of the resource apply to all its operations. An operation can override single settings in its own ``attributes={"upload"={...}}``.

Configuring TCA
================

Expand Down
1 change: 1 addition & 0 deletions Documentation/Miscellaneous/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog

5.0.0
=====
- [!!!] Apply ``allowedFileExtensions`` of the resource ``upload`` settings to its operations. Before, an operation without its own ``allowedFileExtensions`` used ``$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']``. Review the list when you rely on the previous behaviour. Configured extensions are compared case-insensitively.
- OpenAPI specification documents ``403 Forbidden`` (instead of ``404 Not Found``) for operations with ``security`` or ``security_post_denormalize``, the same status as returned by the API.
- [!!!] A new file reference created by ``uidLocal`` requires ``fileToken`` returned for the file by the API, otherwise ``400 Bad Request`` is returned. The same applies to a new file of a property of type ``File`` or a class extending it (``{"uid": 101, "fileToken": "..."}``). The token is returned in the response of upload endpoints and of operations with ``"fileTokens"=true`` in ``normalizationContext`` (files offered for selection, ``file.fileToken`` for file references). Pass ``fileToken`` when saving a reference. Custom upload code has to grant tokens with ``FileTokenService::grant()``. See :ref:`handling_file_upload_file_token`.
- Invalid values of file and file reference properties (e.g. a text or a boolean instead of an identifier or an object) return ``400 Bad Request`` instead of HTTP 500 or clearing the property.
Expand Down
48 changes: 48 additions & 0 deletions Tests/Functional/Dispatcher/FileUploadDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace SourceBroker\T3api\Tests\Functional\Dispatcher;

use PHPUnit\Framework\Attributes\Test;
use SourceBroker\T3api\Dispatcher\Bootstrap;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\Routing\RequestContext;
use TYPO3\CMS\Core\Http\Response;

/**
* Upload settings of the resource apply to its upload operation, see `T3apiTests\FunctionalTest\Domain\Model\File`
* (resource allows `png` only).
*/
class FileUploadDispatcherTest extends AbstractDispatcherTestCase
{
#[Test]
public function fileExtensionNotAllowedByResourceIsRejected(): void
{
// `gif` is allowed by default (`$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']`), but not by the resource
$path = tempnam(sys_get_temp_dir(), 't3api');
file_put_contents($path, 'GIF89a');
$request = SymfonyRequest::create(
'https://example.com/_api/files',
'POST',
[],
[],
['originalResource' => new UploadedFile($path, 'photo.gif', 'image/gif', null, true)]
);

$response = new Response();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1577000112816);

try {
$this->get(Bootstrap::class)
->processOperationByRequest((new RequestContext())->fromRequest($request), $request, $response);
} finally {
if (is_file($path)) {
unlink($path);
}
}
}
}
103 changes: 103 additions & 0 deletions Tests/Unit/Domain/Model/UploadSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace SourceBroker\T3api\Tests\Unit\Domain\Model;

use PHPUnit\Framework\Attributes\Test;
use SourceBroker\T3api\Domain\Model\UploadSettings;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class UploadSettingsTest extends UnitTestCase
{
private mixed $imageFileExtensionsBackup;

protected function setUp(): void
{
parent::setUp();
$this->imageFileExtensionsBackup = $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] ?? null;
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] = 'gif,JPG,png,svg';
}

protected function tearDown(): void
{
if ($this->imageFileExtensionsBackup === null) {
unset($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']);
} else {
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] = $this->imageFileExtensionsBackup;
}
parent::tearDown();
}

#[Test]
public function createWithoutAttributesOrBaseAllowsImageFileExtensions(): void
{
self::assertSame(['gif', 'jpg', 'png', 'svg'], UploadSettings::create()->getAllowedFileExtensions());
}

#[Test]
public function createAppliesGivenAllowedFileExtensions(): void
{
$settings = UploadSettings::create(['allowedFileExtensions' => ['pdf']]);

self::assertSame(['pdf'], $settings->getAllowedFileExtensions());
}

/**
* Mirrors an operation without its own `upload` attributes - the extensions set on the
* resource apply to the operation.
*/
#[Test]
public function createInheritsAllowedFileExtensionsFromBase(): void
{
$base = UploadSettings::create(['allowedFileExtensions' => ['pdf']]);

$settings = UploadSettings::create([], $base);

self::assertSame(['pdf'], $settings->getAllowedFileExtensions());
}

/**
* Mirrors an operation changing another upload setting only - the extensions set on the
* resource still apply to the operation.
*/
#[Test]
public function createInheritsAllowedFileExtensionsFromBaseWhenOtherAttributesAreGiven(): void
{
$base = UploadSettings::create(['allowedFileExtensions' => ['pdf']]);

$settings = UploadSettings::create(['folder' => '1:/documents/'], $base);

self::assertSame(['pdf'], $settings->getAllowedFileExtensions());
self::assertSame('1:/documents/', $settings->getFolder());
}

#[Test]
public function createInheritsEmptyAllowedFileExtensionsFromBase(): void
{
$base = UploadSettings::create(['allowedFileExtensions' => []]);

$settings = UploadSettings::create([], $base);

self::assertSame([], $settings->getAllowedFileExtensions());
}

#[Test]
public function createLowercasesAllowedFileExtensions(): void
{
$base = UploadSettings::create(['allowedFileExtensions' => ['JPG', 'Png']]);

self::assertSame(['jpg', 'png'], $base->getAllowedFileExtensions());
self::assertSame(['pdf'], UploadSettings::create(['allowedFileExtensions' => ['PDF']], $base)->getAllowedFileExtensions());
}

#[Test]
public function createOverridesAllowedFileExtensionsOfBaseWhenGiven(): void
{
$base = UploadSettings::create(['allowedFileExtensions' => ['pdf']]);

$settings = UploadSettings::create(['allowedFileExtensions' => ['jpg']], $base);

self::assertSame(['jpg'], $settings->getAllowedFileExtensions());
}
}
Loading