Skip to content

Commit be4dcea

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-95637' into 2.1.16-develop-pr60
2 parents 9116313 + e7f4524 commit be4dcea

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Uploader.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
*/
1818
class Uploader extends \Magento\MediaStorage\Model\File\Uploader
1919
{
20+
/**
21+
* HTTP scheme
22+
* used to compare against the filename and select the proper DriverPool adapter
23+
* @var string
24+
*/
25+
private $httpScheme = 'http://';
26+
2027
/**
2128
* Temp directory.
2229
*
@@ -99,7 +106,7 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
99106
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
100107
* @param \Magento\Framework\Filesystem $filesystem
101108
* @param \Magento\Framework\Filesystem\File\ReadFactory $readFactory
102-
* @param null $filePath
109+
* @param string|null $filePath
103110
* @param \Magento\Framework\App\Filesystem\DirectoryResolver|null $directoryResolver
104111
* @throws \Magento\Framework\Exception\LocalizedException
105112
*/
@@ -148,23 +155,38 @@ public function init()
148155
* @param string $fileName
149156
* @param bool $renameFileOff
150157
* @return array
158+
* @throws \Magento\Framework\Exception\LocalizedException
151159
*/
152160
public function move($fileName, $renameFileOff = false)
153161
{
154162
if ($renameFileOff) {
155163
$this->setAllowRenameFiles(false);
156164
}
165+
166+
if ($this->getTmpDir()) {
167+
$filePath = $this->getTmpDir() . '/';
168+
} else {
169+
$filePath = '';
170+
}
171+
157172
if (preg_match('/\bhttps?:\/\//i', $fileName, $matches)) {
158173
$url = str_replace($matches[0], '', $fileName);
159-
$read = $this->_readFactory->create($url, DriverPool::HTTP);
174+
$driver = $matches[0] === $this->httpScheme ? DriverPool::HTTP : DriverPool::HTTPS;
175+
$read = $this->_readFactory->create($url, $driver);
176+
177+
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
178+
if ($fileExtension && !$this->checkAllowedExtension($fileExtension)) {
179+
throw new \Magento\Framework\Exception\LocalizedException(__('Disallowed file type.'));
180+
}
181+
160182
$fileName = preg_replace('/[^a-z0-9\._-]+/i', '', $fileName);
161183
$this->_directory->writeFile(
162-
$this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName),
184+
$this->_directory->getRelativePath($filePath . $fileName),
163185
$read->readAll()
164186
);
165187
}
166188

167-
$filePath = $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName);
189+
$filePath = $this->_directory->getRelativePath($filePath . $fileName);
168190
$this->_setUploadFile($filePath);
169191
$destDir = $this->_directory->getAbsolutePath($this->getDestDir());
170192
$result = $this->save($destDir);
@@ -332,7 +354,7 @@ protected function _moveFile($tmpPath, $destPath)
332354
}
333355

334356
/**
335-
* {@inheritdoc}
357+
* @inheritdoc
336358
*/
337359
protected function chmod($file)
338360
{

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/UploaderTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ protected function setUp()
105105
null,
106106
$this->directoryResolver,
107107
])
108-
->setMethods(['_setUploadFile', 'save', 'getTmpDir'])
108+
->setMethods(['_setUploadFile', 'save', 'getTmpDir', 'checkAllowedExtension'])
109109
->getMock();
110110
}
111111

112112
/**
113113
* @dataProvider moveFileUrlDataProvider
114114
*/
115-
public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName)
115+
public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName, $checkAllowedExtension)
116116
{
117117
$destDir = 'var/dest/dir';
118-
$expectedRelativeFilePath = $this->uploader->getTmpDir() . '/' . $expectedFileName;
118+
$expectedRelativeFilePath = $expectedFileName;
119119
$this->directoryMock->expects($this->once())->method('isWritable')->with($destDir)->willReturn(true);
120120
$this->directoryMock->expects($this->any())->method('getRelativePath')->with($expectedRelativeFilePath);
121121
$this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($destDir)
@@ -140,6 +140,9 @@ public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName)
140140
$this->uploader->expects($this->once())->method('_setUploadFile')->will($this->returnSelf());
141141
$this->uploader->expects($this->once())->method('save')->with($destDir . '/' . $expectedFileName)
142142
->willReturn(['name' => $expectedFileName, 'path' => 'absPath']);
143+
$this->uploader->expects($this->exactly($checkAllowedExtension))
144+
->method('checkAllowedExtension')
145+
->willReturn(true);
143146

144147
$this->uploader->setDestDir($destDir);
145148
$result = $this->uploader->move($fileUrl);
@@ -151,7 +154,7 @@ public function testMoveFileName()
151154
{
152155
$destDir = 'var/dest/dir';
153156
$fileName = 'test_uploader_file';
154-
$expectedRelativeFilePath = $this->uploader->getTmpDir() . '/' . $fileName;
157+
$expectedRelativeFilePath = $fileName;
155158
$this->directoryMock->expects($this->once())->method('isWritable')->with($destDir)->willReturn(true);
156159
$this->directoryMock->expects($this->any())->method('getRelativePath')->with($expectedRelativeFilePath);
157160
$this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($destDir)
@@ -176,11 +179,13 @@ public function moveFileUrlDataProvider()
176179
'$fileUrl' => 'http://test_uploader_file',
177180
'$expectedHost' => 'test_uploader_file',
178181
'$expectedFileName' => 'httptest_uploader_file',
182+
'$checkAllowedExtension' => 0,
179183
],
180184
[
181185
'$fileUrl' => 'https://!:^&`;file',
182186
'$expectedHost' => '!:^&`;file',
183187
'$expectedFileName' => 'httpsfile',
188+
'$checkAllowedExtension' => 0,
184189
],
185190
];
186191
}

0 commit comments

Comments
 (0)