Skip to content

Commit fb61230

Browse files
Merge pull request #7326 from magento-cia/AC-479
Bugfixes
2 parents e80efd8 + afc3728 commit fb61230

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

app/code/Magento/Store/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<shtml>shtml</shtml>
134134
<phpt>phpt</phpt>
135135
<pht>pht</pht>
136+
<phar>phar</phar>
136137
<svg>svg</svg>
137138
<xml>xml</xml>
138139
<xhtml>xhtml</xhtml>

lib/internal/Magento/Framework/File/Test/Unit/UploaderTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,55 @@
99

1010
use Magento\Framework\File\Uploader;
1111
use PHPUnit\Framework\TestCase;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\Filesystem;
15+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
16+
use Magento\Framework\Filesystem\DriverPool;
1217

1318
/**
1419
* Unit Test class for \Magento\Framework\File\Uploader
1520
*/
1621
class UploaderTest extends TestCase
1722
{
23+
/**
24+
* @var Uploader
25+
*/
26+
private $uploader;
27+
28+
/**
29+
* Allowed extensions array
30+
*
31+
* @var array
32+
*/
33+
private $_allowedMimeTypes = [
34+
'php' => 'text/plain',
35+
'txt' => 'text/plain'
36+
];
37+
38+
protected function setUp(): void
39+
{
40+
$class = new \ReflectionObject($this);
41+
$fileName = $class->getFilename();
42+
$fileType = 'php';
43+
$this->setupFiles(1230123, $fileName, $fileType);
44+
45+
$driverPool = $this->createMock(DriverPool::class);
46+
$directoryList = $this->createMock(DirectoryList::class);
47+
$filesystem = $this->createMock(Filesystem::class);
48+
$targetDirectory = $this->createMock(TargetDirectory::class);
49+
50+
$this->uploader = new Uploader(
51+
"fileId",
52+
null,
53+
$directoryList,
54+
$driverPool,
55+
$targetDirectory,
56+
$filesystem
57+
);
58+
$this->uploader->setAllowedExtensions(array_keys($this->_allowedMimeTypes));
59+
}
60+
1861
/**
1962
* @param string $fileName
2063
* @param string|bool $expectedCorrectedFileName
@@ -67,4 +110,76 @@ public function getCorrectFileNameProvider()
67110
]
68111
];
69112
}
113+
114+
/**
115+
* @param string $extension
116+
* @param bool $isValid
117+
*
118+
* @dataProvider checkAllowedExtensionProvider
119+
*/
120+
public function testCheckAllowedExtension(bool $isValid, string $extension)
121+
{
122+
$this->assertEquals(
123+
$isValid,
124+
$this->uploader->checkAllowedExtension($extension)
125+
);
126+
}
127+
128+
/**
129+
* @return array
130+
*/
131+
public function checkAllowedExtensionProvider(): array
132+
{
133+
return [
134+
[
135+
true,
136+
'txt'
137+
],
138+
[
139+
false,
140+
'png'
141+
],
142+
[
143+
false,
144+
'$#@$#@$3'
145+
],
146+
[
147+
false,
148+
'4324324324txt'
149+
],
150+
[
151+
false,
152+
'$#$#$jpeg..$#2$#@$#@$'
153+
],
154+
[
155+
false,
156+
'../../txt'
157+
],
158+
[
159+
true,
160+
'php'
161+
]
162+
];
163+
}
164+
165+
/**
166+
* Setup global variable $_FILES.
167+
*
168+
* @param int $fileSize
169+
* @param string $fileName
170+
* @param string $fileType
171+
* @return void
172+
*/
173+
private function setupFiles($fileSize, $fileName, $fileType)
174+
{
175+
$_FILES = [
176+
'fileId' => [
177+
'name' => $fileName,
178+
'type' => $fileType,
179+
'tmp_name' => $fileName,
180+
'error' => 0,
181+
'size' => $fileSize,
182+
]
183+
];
184+
}
70185
}

lib/internal/Magento/Framework/File/Uploader.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
* @SuppressWarnings(PHPMD.TooManyFields)
2626
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2728
*
2829
* @api
2930
* @since 100.0.2
@@ -148,28 +149,28 @@ class Uploader
148149
/**#@+
149150
* File upload type (multiple or single)
150151
*/
151-
const SINGLE_STYLE = 0;
152+
public const SINGLE_STYLE = 0;
152153

153-
const MULTIPLE_STYLE = 1;
154+
public const MULTIPLE_STYLE = 1;
154155

155156
/**#@-*/
156157

157158
/**
158159
* Temp file name empty code
159160
*/
160-
const TMP_NAME_EMPTY = 666;
161+
public const TMP_NAME_EMPTY = 666;
161162

162163
/**
163164
* Maximum Image Width resolution in pixels. For image resizing on client side
164165
* @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
165166
*/
166-
const MAX_IMAGE_WIDTH = 1920;
167+
public const MAX_IMAGE_WIDTH = 1920;
167168

168169
/**
169170
* Maximum Image Height resolution in pixels. For image resizing on client side
170171
* @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
171172
*/
172-
const MAX_IMAGE_HEIGHT = 1200;
173+
public const MAX_IMAGE_HEIGHT = 1200;
173174

174175
/**
175176
* Resulting of uploaded file
@@ -640,6 +641,11 @@ public function setAllowedExtensions($extensions = [])
640641
*/
641642
public function checkAllowedExtension($extension)
642643
{
644+
//File extensions should only be allowed to contain alphanumeric characters
645+
if (preg_match('/[^a-z0-9]/i', $extension)) {
646+
return false;
647+
}
648+
643649
if (!is_array($this->_allowedExtensions) || empty($this->_allowedExtensions)) {
644650
return true;
645651
}

0 commit comments

Comments
 (0)