From b74788219b07e1e3d4ebd7eb1e993d4cf6b916ce Mon Sep 17 00:00:00 2001 From: Krystian Szymukowicz Date: Tue, 3 Nov 2026 17:03:39 +0100 Subject: [PATCH] [!!!][BUGFIX] Apply allowed file extensions of resource to upload operations An operation without its own allowedFileExtensions used $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] instead of the list set in upload settings of the resource. Configured extensions are compared case-insensitively. --- Classes/Domain/Model/UploadSettings.php | 12 +- Documentation/HandlingFileUpload/Index.rst | 4 +- .../Miscellaneous/Changelog/Index.rst | 1 + .../Dispatcher/FileUploadDispatcherTest.php | 48 ++++++++ .../Unit/Domain/Model/UploadSettingsTest.php | 103 ++++++++++++++++++ 5 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Dispatcher/FileUploadDispatcherTest.php create mode 100644 Tests/Unit/Domain/Model/UploadSettingsTest.php diff --git a/Classes/Domain/Model/UploadSettings.php b/Classes/Domain/Model/UploadSettings.php index 508bb74b..22282723 100644 --- a/Classes/Domain/Model/UploadSettings.php +++ b/Classes/Domain/Model/UploadSettings.php @@ -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; diff --git a/Documentation/HandlingFileUpload/Index.rst b/Documentation/HandlingFileUpload/Index.rst index 7200c407..5cce666e 100644 --- a/Documentation/HandlingFileUpload/Index.rst +++ b/Documentation/HandlingFileUpload/Index.rst @@ -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``). @@ -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 ================ diff --git a/Documentation/Miscellaneous/Changelog/Index.rst b/Documentation/Miscellaneous/Changelog/Index.rst index 3d61a775..68a809d5 100644 --- a/Documentation/Miscellaneous/Changelog/Index.rst +++ b/Documentation/Miscellaneous/Changelog/Index.rst @@ -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. - [!!!] 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. - Files (classes extending Extbase's ``File``) nested in a response (e.g. members of a collection) are serialized like other objects, the same as in an item response, instead of the file reference output. diff --git a/Tests/Functional/Dispatcher/FileUploadDispatcherTest.php b/Tests/Functional/Dispatcher/FileUploadDispatcherTest.php new file mode 100644 index 00000000..0a61fbaa --- /dev/null +++ b/Tests/Functional/Dispatcher/FileUploadDispatcherTest.php @@ -0,0 +1,48 @@ + 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); + } + } + } +} diff --git a/Tests/Unit/Domain/Model/UploadSettingsTest.php b/Tests/Unit/Domain/Model/UploadSettingsTest.php new file mode 100644 index 00000000..8d1fc2c5 --- /dev/null +++ b/Tests/Unit/Domain/Model/UploadSettingsTest.php @@ -0,0 +1,103 @@ +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()); + } +}