Skip to content

Commit ebaa5ea

Browse files
committed
MAGETWO-89609: [Backport for 2.2.x] Improve import files validation
1 parent bf71446 commit ebaa5ea

File tree

2 files changed

+81
-0
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import
  • dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import

2 files changed

+81
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ protected function _validateFile()
241241

242242
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
243243
if (!$this->checkAllowedExtension($fileExtension)) {
244+
$this->_directory->delete($filePath);
244245
throw new \Exception('Disallowed file type.');
245246
}
246247
//run validate callbacks
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogImportExport\Model\Import;
10+
11+
use Magento\Framework\App\Bootstrap;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
14+
/**
15+
* Tests for the \Magento\CatalogImportExport\Model\Import\Uploader class.
16+
*/
17+
class UploaderTest extends \Magento\TestFramework\Indexer\TestCase
18+
{
19+
/**
20+
* @var \Magento\Framework\ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
26+
*/
27+
private $directory;
28+
29+
/**
30+
* @var \Magento\CatalogImportExport\Model\Import\Uploader
31+
*/
32+
private $uploader;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp()
38+
{
39+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
40+
$this->uploader = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Uploader::class);
41+
42+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
43+
44+
$appParams = \Magento\TestFramework\Helper\Bootstrap::getInstance()
45+
->getBootstrap()
46+
->getApplication()
47+
->getInitParams()[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
48+
$mediaPath = $appParams[DirectoryList::MEDIA][DirectoryList::PATH];
49+
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
50+
$tmpDir = $this->directory->getRelativePath($mediaPath . '/import');
51+
$this->uploader->setTmpDir($tmpDir);
52+
53+
parent::setUp();
54+
}
55+
56+
/**
57+
* @magentoAppIsolation enabled
58+
*/
59+
public function testMoveWithValidFile() : void
60+
{
61+
$fileName = 'magento_additional_image_one.jpg';
62+
$filePath = $this->directory->getAbsolutePath($this->uploader->getTmpDir() . '/' . $fileName);
63+
copy(__DIR__ . '/_files/' . $fileName, $filePath);
64+
$this->uploader->move($fileName);
65+
$this->assertTrue($this->directory->isExist($this->uploader->getTmpDir() . '/' . $fileName));
66+
}
67+
68+
/**
69+
* @magentoAppIsolation enabled
70+
* @expectedException \Exception
71+
*/
72+
public function testMoveWithInvalidFile() :void
73+
{
74+
$fileName = 'media_import_image.php';
75+
$filePath = $this->directory->getAbsolutePath($this->uploader->getTmpDir() . '/' . $fileName);
76+
copy(__DIR__ . '/_files/' . $fileName, $filePath);
77+
$this->uploader->move($fileName);
78+
$this->assertFalse($this->directory->isExist($this->uploader->getTmpDir() . '/' . $fileName));
79+
}
80+
}

0 commit comments

Comments
 (0)