Skip to content

Commit 707280d

Browse files
committed
MAGETWO-95550: Adding restriction for controller
1 parent b8daea7 commit 707280d

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function execute()
8282
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
8383
$dir = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
8484
$filePath = $path . '/' . \Magento\Framework\File\Uploader::getCorrectFileName($file);
85-
if ($dir->isFile($dir->getRelativePath($filePath)) && !preg_match('#.htaccess#', $file)) {
85+
if ($dir->isFile($dir->getRelativePath($filePath)) && !preg_match('/^\\.htaccess$/', $file)) {
8686
$this->getStorage()->deleteFile($filePath);
8787
}
8888
}

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ protected function setUp()
5454
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
5555
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
5656
copy($fixtureDir . '/' . $this->fileName, $filePath);
57-
$path = '/.htaccess';
58-
if (!$this->mediaDirectory->isFile($path)) {
59-
$this->mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all");
60-
}
6157
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::class);
6258
}
6359

@@ -95,21 +91,54 @@ public static function tearDownAfterClass()
9591
}
9692

9793
/**
98-
* Check that htaccess file couldn't be removed via
94+
* Creates file and tried to delete it via
9995
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
10096
*
97+
* @param string $fileName
10198
* @return void
10299
*/
103-
public function testDeleteHtaccess()
100+
private function createFileAndExecuteDelete($fileName)
104101
{
102+
$path = '/' . $fileName;
103+
if (!$this->mediaDirectory->isFile($path)) {
104+
$this->mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all");
105+
}
105106
$this->model->getRequest()->setMethod('POST')
106-
->setPostValue('files', [$this->imagesHelper->idEncode('.htaccess')]);
107-
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
107+
->setPostValue('files', [$this->imagesHelper->idEncode($fileName)]);
108+
$this->model->getStorage()->getSession()->setCurrentPath($this->mediaDirectory->getAbsolutePath());
108109
$this->model->execute();
110+
}
109111

112+
/**
113+
* Check that htaccess file couldn't be removed via
114+
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
115+
*
116+
* @return void
117+
*/
118+
public function testCouldNotDeleteHtaccess()
119+
{
120+
$fileName = '.htaccess';
121+
$this->createFileAndExecuteDelete($fileName);
110122
$this->assertTrue(
111123
$this->mediaDirectory->isExist(
112-
$this->mediaDirectory->getRelativePath('/' . '.htaccess')
124+
$this->mediaDirectory->getRelativePath('/' . $fileName)
125+
)
126+
);
127+
}
128+
129+
/**
130+
* Check that random file could be removed via
131+
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
132+
*
133+
* @return void
134+
*/
135+
public function testDeleteAnyFile()
136+
{
137+
$fileName = 'thtaccess';
138+
$this->createFileAndExecuteDelete($fileName);
139+
$this->assertFalse(
140+
$this->mediaDirectory->isExist(
141+
$this->mediaDirectory->getRelativePath('/' . $fileName)
113142
)
114143
);
115144
}

0 commit comments

Comments
 (0)