[BUGFIX] Ignore case in file extension filter 24/22724/5
authorAlexander Stehlik <alexander.stehlik@googlemail.com>
Sat, 3 Aug 2013 18:14:10 +0000 (20:14 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 7 Aug 2013 18:07:22 +0000 (20:07 +0200)
This patch makes the FileExtensionFilter case insensitive.

If the filter is configured to allow files with the ".EXT"
extension it will now also allow files with an ".ext"
extension.

Additionally the order of a data provider method in the
FileExtensionFilterTest has been corrected.

Resolves: #50568
Releases: 6.2, 6.1, 6.0
Change-Id: I5761ab7f06116ef847caeb2ae786e580730c0e6b
Reviewed-on: https://review.typo3.org/22724
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/core/Classes/Resource/Filter/FileExtensionFilter.php
typo3/sysext/core/Tests/Unit/Resource/Utility/FileExtensionFilterTest.php

index f91a6d0..306e89c 100644 (file)
@@ -120,7 +120,7 @@ class FileExtensionFilter {
         */
        protected function isAllowed(\TYPO3\CMS\Core\Resource\FileInterface $file) {
                $result = TRUE;
-               $fileExt = $file->getExtension();
+               $fileExt = strtolower($file->getExtension());
                // Check allowed file extensions
                if ($this->allowedFileExtensions !== NULL && count($this->allowedFileExtensions) > 0 && !in_array($fileExt, $this->allowedFileExtensions)) {
                        $result = FALSE;
@@ -138,7 +138,7 @@ class FileExtensionFilter {
         * @param mixed $allowedFileExtensions  Comma-separated list or array, of allowed file extensions
         */
        public function setAllowedFileExtensions($allowedFileExtensions) {
-               $this->allowedFileExtensions = $this->convertToArray($allowedFileExtensions);
+               $this->allowedFileExtensions = $this->convertToLowercaseArray($allowedFileExtensions);
        }
 
        /**
@@ -147,22 +147,29 @@ class FileExtensionFilter {
         * @param mixed $disallowedFileExtensions  Comma-separated list or array, of allowed file extensions
         */
        public function setDisallowedFileExtensions($disallowedFileExtensions) {
-               $this->disallowedFileExtensions = $this->convertToArray($disallowedFileExtensions);
+               $this->disallowedFileExtensions = $this->convertToLowercaseArray($disallowedFileExtensions);
        }
 
        /**
         * Converts mixed (string or array) input arguments into an array, NULL if empty.
         *
+        * All array values will be converted to lower case.
+        *
         * @param mixed $inputArgument Comma-separated list or array.
         * @return array
         */
-       protected function convertToArray($inputArgument) {
+       protected function convertToLowercaseArray($inputArgument) {
                $returnValue = NULL;
                if (is_array($inputArgument)) {
                        $returnValue = $inputArgument;
                } elseif (strlen($inputArgument) > 0) {
                        $returnValue = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $inputArgument);
                }
+
+               if (is_array($returnValue)) {
+                       $returnValue = array_map('strtolower', $returnValue);
+               }
+
                return $returnValue;
        }
 
index 70f6b76..8eaa030 100644 (file)
@@ -82,6 +82,17 @@ class FileExtensionFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * @return array
+        */
+       public function invalidInlineChildrenFilterParametersDataProvider() {
+               return array(
+                       array(NULL, NULL, NULL),
+                       array('', '', array(0, '', NULL, FALSE)),
+                       array(NULL, NULL, array(0, '', NULL, FALSE))
+               );
+       }
+
+       /**
         * @param array|string $allowed
         * @param array|string $disallowed
         * @param array|string $values
@@ -102,14 +113,46 @@ class FileExtensionFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @return array
         */
-       public function invalidInlineChildrenFilterParametersDataProvider() {
+       public function extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider() {
                return array(
-                       array(NULL, NULL, NULL),
-                       array('', '', array(0, '', NULL, FALSE)),
-                       array(NULL, NULL, array(0, '', NULL, FALSE))
+                       'Allowed extensions' => array(
+                               'ext1', 'EXT1', '', TRUE
+                       ),
+                       'Allowed extensions, lower and upper case mix' => array(
+                               'ext1', 'ext2, ExT1, Ext3', '', TRUE
+                       ),
+                       'Disallowed extensions' => array(
+                               'ext1', '', 'EXT1', FALSE
+                       ),
+                       'Disallowed extensions, lower and upper case mix' => array(
+                               'ext1', '', 'ext2, ExT1, Ext3', FALSE
+                       ),
+                       'Combine allowed / disallowed extensions' => array(
+                               'ext1', 'EXT1', 'EXT1', FALSE
+                       ),
                );
        }
 
+       /**
+        * @param string $fileExtension
+        * @param array|string $allowedExtensions
+        * @param array|string $disallowedExtensions
+        * @param boolean $isAllowed
+        * @test
+        * @dataProvider extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider
+        */
+       public function extensionFilterIgnoresCaseInAllowedExtensionCheck($fileExtension, $allowedExtensions, $disallowedExtensions, $isAllowed) {
+
+               /** @var \TYPO3\CMS\Core\Resource\File $file */
+               $file = new \TYPO3\CMS\Core\Resource\File(array('name' => 'file.' . $fileExtension));
+
+               /** @var \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter $filter */
+               $filter = $this->getAccessibleMock('\TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter', array('dummy'));
+               $filter->setAllowedFileExtensions($allowedExtensions);
+               $filter->setDisallowedFileExtensions($disallowedExtensions);
+               $result = $filter->_call('isAllowed', $file);
+               $this->assertEquals($isAllowed, $result);
+       }
 }
 
-?>
\ No newline at end of file
+?>