Skip to content

Commit 92d8bca

Browse files
MC-15385: Path check for images
1 parent c47cd8f commit 92d8bca

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public function createDirectory($name, $path)
424424
/**
425425
* Recursively delete directory from storage
426426
*
427-
* @param string $path Target dir
427+
* @param string $path Absolute path to target directory
428428
* @return void
429429
* @throws \Magento\Framework\Exception\LocalizedException
430430
*/
@@ -493,7 +493,7 @@ public function deleteFile($target)
493493
/**
494494
* Upload and resize new file
495495
*
496-
* @param string $targetPath Target directory
496+
* @param string $targetPath Absolute path to target directory
497497
* @param string $type Type of storage, e.g. image, media etc.
498498
* @return array File info Array
499499
* @throws \Magento\Framework\Exception\LocalizedException
@@ -807,8 +807,8 @@ private function getExtensionsList($type = null): array
807807
/**
808808
* Check if path is not in excluded dirs.
809809
*
810-
* @param string $path
811-
* @param array $conditions
810+
* @param string $path Absolute path
811+
* @param array $conditions Exclude conditions
812812
* @return bool
813813
*/
814814
private function isPathAllowed($path, array $conditions): bool

dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolderTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\Response\HttpFactory as ResponseFactory;
1011

1112
/**
1213
* Test for \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFolder class.
@@ -38,6 +39,11 @@ class DeleteFolderTest extends \PHPUnit\Framework\TestCase
3839
*/
3940
private $filesystem;
4041

42+
/**
43+
* @var HttpFactory
44+
*/
45+
private $responseFactory;
46+
4147
/**
4248
* @inheritdoc
4349
*/
@@ -49,6 +55,7 @@ protected function setUp()
4955
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
5056
$this->imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
5157
$this->fullDirectoryPath = $this->imagesHelper->getStorageRoot();
58+
$this->responseFactory = $objectManager->get(ResponseFactory::class);
5259
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFolder::class);
5360
}
5461

@@ -83,6 +90,7 @@ public function testExecute()
8390
* can be removed.
8491
*
8592
* @magentoDataFixture Magento/Cms/_files/linked_media.php
93+
* @magentoAppIsolation enabled
8694
*/
8795
public function testExecuteWithLinkedMedia()
8896
{
@@ -106,6 +114,7 @@ public function testExecuteWithLinkedMedia()
106114
* under media directory.
107115
*
108116
* @return void
117+
* @magentoAppIsolation enabled
109118
*/
110119
public function testExecuteWithWrongDirectoryName()
111120
{
@@ -116,6 +125,31 @@ public function testExecuteWithWrongDirectoryName()
116125
$this->assertFileExists($this->fullDirectoryPath . $directoryName);
117126
}
118127

128+
/**
129+
* Execute method to check that there is no ability to remove folder which is in excluded directories list.
130+
*
131+
* @return void
132+
* @magentoAppIsolation enabled
133+
*/
134+
public function testExecuteWithExcludedDirectoryName()
135+
{
136+
$directoryName = 'downloadable';
137+
$expectedResponseMessage = 'We cannot delete directory /downloadable.';
138+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
139+
$mediaDirectory->create($directoryName);
140+
$this->assertFileExists($this->fullDirectoryPath . $directoryName);
141+
142+
$this->model->getRequest()->setParams(['node' => $this->imagesHelper->idEncode($directoryName)]);
143+
$this->model->getRequest()->setMethod('POST');
144+
$jsonResponse = $this->model->execute();
145+
$jsonResponse->renderResult($response = $this->responseFactory->create());
146+
$data = json_decode($response->getBody(), true);
147+
148+
$this->assertTrue($data['error']);
149+
$this->assertEquals($expectedResponseMessage, $data['message']);
150+
$this->assertFileExists($this->fullDirectoryPath . $directoryName);
151+
}
152+
119153
/**
120154
* @inheritdoc
121155
*/
@@ -128,5 +162,8 @@ public static function tearDownAfterClass()
128162
if ($directory->isExist('wysiwyg')) {
129163
$directory->delete('wysiwyg');
130164
}
165+
if ($directory->isExist('downloadable')) {
166+
$directory->delete('downloadable');
167+
}
131168
}
132169
}

dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/UploadTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class UploadTest extends \PHPUnit\Framework\TestCase
3333
*/
3434
private $fullDirectoryPath;
3535

36+
/**
37+
* @var string
38+
*/
39+
private $fullExcludedDirectoryPath;
40+
3641
/**
3742
* @var string
3843
*/
@@ -60,11 +65,13 @@ protected function setUp()
6065
{
6166
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
6267
$directoryName = 'directory1';
68+
$excludedDirName = 'downloadable';
6369
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
6470
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
6571
$imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
6672
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
6773
$this->fullDirectoryPath = $imagesHelper->getStorageRoot() . DIRECTORY_SEPARATOR . $directoryName;
74+
$this->fullExcludedDirectoryPath = $imagesHelper->getStorageRoot() . DIRECTORY_SEPARATOR . $excludedDirName;
6875
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
6976
$this->responseFactory = $this->objectManager->get(ResponseFactory::class);
7077
$this->model = $this->objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\Upload::class);
@@ -115,6 +122,34 @@ public function testExecute()
115122
$this->assertEquals($keys, $dataKeys);
116123
}
117124

125+
/**
126+
* Execute method with excluded directory path and file name to check that file can't be uploaded.
127+
*
128+
* @return void
129+
* @magentoAppIsolation enabled
130+
*/
131+
public function testExecuteWithExcludedDirectory()
132+
{
133+
$expectedError = 'We can\'t upload the file to current folder right now. Please try another folder.';
134+
$this->model->getRequest()->setParams(['type' => 'image/png']);
135+
$this->model->getRequest()->setMethod('POST');
136+
$this->model->getStorage()->getSession()->setCurrentPath($this->fullExcludedDirectoryPath);
137+
/** @var JsonResponse $jsonResponse */
138+
$jsonResponse = $this->model->execute();
139+
/** @var Response $response */
140+
$jsonResponse->renderResult($response = $this->responseFactory->create());
141+
$data = json_decode($response->getBody(), true);
142+
143+
$this->assertEquals($expectedError, $data['error']);
144+
$this->assertFalse(
145+
$this->mediaDirectory->isExist(
146+
$this->mediaDirectory->getRelativePath(
147+
$this->fullExcludedDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName
148+
)
149+
)
150+
);
151+
}
152+
118153
/**
119154
* Execute method with correct directory path and file name to check that file can be uploaded to the directory
120155
* located under linked folder.

0 commit comments

Comments
 (0)