Skip to content

Commit 0766aea

Browse files
author
Michail Slabko
committed
Merge remote-tracking branch 'origin/MAGETWO-42123' into goinc-bugsfixing
2 parents 67b6b58 + e04b97b commit 0766aea

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFile.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ class ValidatorFile extends Validator
2121
*
2222
* @var string
2323
*/
24-
protected $path = '/custom_options';
24+
protected $path = 'custom_options';
2525

2626
/**
2727
* Relative path for quote folder
2828
*
2929
* @var string
3030
*/
31-
protected $quotePath = '/custom_options/quote';
31+
protected $quotePath = 'custom_options/quote';
3232

3333
/**
3434
* Relative path for order folder
3535
*
3636
* @var string
3737
*/
38-
protected $orderPath = '/custom_options/order';
38+
protected $orderPath = 'custom_options/order';
3939

4040
/**
4141
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
@@ -175,12 +175,12 @@ public function validate($processingParams, $option)
175175
$_height = $imageSize[1];
176176
}
177177
}
178-
$uri = $this->filesystem->getUri(DirectoryList::MEDIA);
178+
179179
$userValue = [
180180
'type' => $fileInfo['type'],
181181
'title' => $fileInfo['name'],
182-
'quote_path' => $uri . $this->quotePath . $filePath,
183-
'order_path' => $uri . $this->orderPath . $filePath,
182+
'quote_path' => $this->quotePath . $filePath,
183+
'order_path' => $this->orderPath . $filePath,
184184
'fullpath' => $fileFullPath,
185185
'size' => $fileInfo['size'],
186186
'width' => $_width,

app/code/Magento/Sales/Model/Download.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Sales\Model;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
class Download
1112
{
@@ -29,19 +30,27 @@ class Download
2930
*/
3031
protected $_fileFactory;
3132

33+
/**
34+
* @var string
35+
*/
36+
protected $rootDirBasePath;
37+
3238
/**
3339
* @param \Magento\Framework\Filesystem $filesystem
3440
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase
3541
* @param \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory
3642
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
43+
* @param string $rootDirBasePath
3744
*/
3845
public function __construct(
3946
\Magento\Framework\Filesystem $filesystem,
4047
\Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase,
4148
\Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory,
42-
\Magento\Framework\App\Response\Http\FileFactory $fileFactory
49+
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
50+
$rootDirBasePath = DirectoryList::MEDIA
4351
) {
44-
$this->_rootDir = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
52+
$this->rootDirBasePath = $rootDirBasePath;
53+
$this->_rootDir = $filesystem->getDirectoryWrite($this->rootDirBasePath);
4554
$this->_fileStorageDatabase = $fileStorageDatabase;
4655
$this->_storageDatabaseFactory = $storageDatabaseFactory;
4756
$this->_fileFactory = $fileFactory;
@@ -57,18 +66,19 @@ public function __construct(
5766
public function downloadFile($info)
5867
{
5968
$relativePath = $info['order_path'];
60-
if ($this->_isCanProcessed($relativePath)) {
69+
if (!$this->_isCanProcessed($relativePath)) {
6170
//try get file from quote
6271
$relativePath = $info['quote_path'];
63-
if ($this->_isCanProcessed($relativePath)) {
64-
throw new \Exception();
72+
if (!$this->_isCanProcessed($relativePath)) {
73+
throw new LocalizedException(
74+
__('Path "%1" is not part of allowed directory "%2"', $relativePath, $this->rootDirBasePath)
75+
);
6576
}
6677
}
67-
6878
$this->_fileFactory->create(
6979
$info['title'],
7080
['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'],
71-
DirectoryList::ROOT
81+
$this->rootDirBasePath
7282
);
7383
}
7484

@@ -79,32 +89,28 @@ public function downloadFile($info)
7989
protected function _isCanProcessed($relativePath)
8090
{
8191
$filePath = $this->_rootDir->getAbsolutePath($relativePath);
82-
return (!$this->_rootDir->isFile(
83-
$relativePath
84-
) || !$this->_rootDir->isReadable(
85-
$relativePath
86-
)) && !$this->_processDatabaseFile(
87-
$filePath
88-
);
92+
return (strpos($this->_rootDir->getDriver()->getRealPath($filePath), $relativePath) !== false
93+
&& $this->_rootDir->isFile($relativePath) && $this->_rootDir->isReadable($relativePath))
94+
|| $this->_processDatabaseFile($filePath, $relativePath);
8995
}
9096

9197
/**
9298
* Check file in database storage if needed and place it on file system
9399
*
94100
* @param string $filePath
101+
* @param string $relativePath
95102
* @return bool
96103
*/
97-
protected function _processDatabaseFile($filePath)
104+
protected function _processDatabaseFile($filePath, $relativePath)
98105
{
99106
if (!$this->_fileStorageDatabase->checkDbUsage()) {
100107
return false;
101108
}
102-
$relativePath = $this->_fileStorageDatabase->getMediaRelativePath($filePath);
103109
$file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath);
104110
if (!$file->getId()) {
105111
return false;
106112
}
107-
$stream = $this->_rootDir->openFile($filePath, 'w+');
113+
$stream = $this->_rootDir->openFile($relativePath, 'w+');
108114
$stream->lock();
109115
$stream->write($filePath, $file->getContent());
110116
$stream->unlock();

app/code/Magento/Sales/Test/Unit/Model/DownloadTest.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class DownloadTest extends \PHPUnit_Framework_TestCase
3939
*/
4040
protected $writeDirectoryMock;
4141

42+
/**
43+
* @var \PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
protected $driverMock;
46+
4247
protected function setUp()
4348
{
4449
$this->writeDirectoryMock = $this->getMockBuilder('Magento\Framework\Filesystem\Directory\Write')
@@ -49,9 +54,10 @@ protected function setUp()
4954
->getMock();
5055
$this->filesystemMock->expects($this->any())
5156
->method('getDirectoryWrite')
52-
->with(DirectoryList::ROOT)
57+
->with(DirectoryList::MEDIA)
5358
->will($this->returnValue($this->writeDirectoryMock));
5459

60+
$this->driverMock = $this->getMockForAbstractClass('Magento\Framework\Filesystem\DriverInterface');
5561
$this->storageMock = $this->getMockBuilder('Magento\MediaStorage\Helper\File\Storage\Database')
5662
->disableOriginalConstructor()
5763
->getMock();
@@ -83,17 +89,23 @@ public function testInstanceOf()
8389
}
8490

8591
/**
86-
* @expectedException \Exception
92+
* @param $realPatchCheck
93+
* @param $isFile
94+
* @param $isReadable
95+
* @expectedException \Magento\Framework\Exception\LocalizedException
96+
* @dataProvider dataProviderForTestDownloadFileException
8797
*/
88-
public function testDownloadFileException()
98+
public function testDownloadFileException($realPatchCheck, $isFile, $isReadable)
8999
{
90100
$info = ['order_path' => 'test/path', 'quote_path' => 'test/path2', 'title' => 'test title'];
91-
$isFile = true;
92-
$isReadable = false;
93101

94102
$this->writeDirectoryMock->expects($this->any())
95103
->method('getAbsolutePath')
96104
->will($this->returnArgument(0));
105+
$this->writeDirectoryMock->expects($this->any())
106+
->method('getDriver')
107+
->willReturn($this->driverMock);
108+
$this->driverMock->expects($this->any())->method('getRealPath')->willReturn($realPatchCheck);
97109
$this->writeDirectoryMock->expects($this->any())
98110
->method('isFile')
99111
->will($this->returnValue($isFile));
@@ -104,12 +116,25 @@ public function testDownloadFileException()
104116
$this->storageFactoryMock->expects($this->any())
105117
->method('checkDbUsage')
106118
->will($this->returnValue(false));
119+
$this->httpFileFactoryMock->expects($this->never())->method('create');
107120

108121
$this->model->downloadFile($info);
109122
}
110123

111124
/**
112-
* @expectedException \Exception
125+
* @return array
126+
*/
127+
public function dataProviderForTestDownloadFileException()
128+
{
129+
return [
130+
[1, true, false],
131+
[1, false, true],
132+
[false, true, true],
133+
];
134+
}
135+
136+
/**
137+
* @expectedException \Magento\Framework\Exception\LocalizedException
113138
*/
114139
public function testDownloadFileNoStorage()
115140
{
@@ -120,6 +145,11 @@ public function testDownloadFileNoStorage()
120145
$this->writeDirectoryMock->expects($this->any())
121146
->method('getAbsolutePath')
122147
->will($this->returnArgument(0));
148+
$this->writeDirectoryMock->expects($this->any())
149+
->method('getDriver')
150+
->willReturn($this->driverMock);
151+
$this->driverMock->expects($this->any())->method('getRealPath')->willReturn(true);
152+
123153
$this->writeDirectoryMock->expects($this->any())
124154
->method('isFile')
125155
->will($this->returnValue($isFile));
@@ -130,9 +160,6 @@ public function testDownloadFileNoStorage()
130160
$this->storageMock->expects($this->any())
131161
->method('checkDbUsage')
132162
->will($this->returnValue(true));
133-
$this->storageMock->expects($this->any())
134-
->method('getMediaRelativePath')
135-
->will($this->returnArgument(0));
136163

137164
$storageDatabaseMock = $this->getMockBuilder('Magento\MediaStorage\Model\File\Storage\Database')
138165
->disableOriginalConstructor()
@@ -153,6 +180,7 @@ public function testDownloadFileNoStorage()
153180
$this->storageFactoryMock->expects($this->any())
154181
->method('create')
155182
->will($this->returnValue($storageDatabaseMock));
183+
$this->httpFileFactoryMock->expects($this->never())->method('create');
156184

157185
$this->model->downloadFile($info);
158186
}
@@ -178,6 +206,11 @@ public function testDownloadFile()
178206
$this->writeDirectoryMock->expects($this->any())
179207
->method('getAbsolutePath')
180208
->will($this->returnArgument(0));
209+
$this->writeDirectoryMock->expects($this->any())
210+
->method('getDriver')
211+
->willReturn($this->driverMock);
212+
$this->driverMock->expects($this->any())->method('getRealPath')->willReturn(true);
213+
181214
$this->writeDirectoryMock->expects($this->any())
182215
->method('isFile')
183216
->will($this->returnValue($isFile));
@@ -195,9 +228,6 @@ public function testDownloadFile()
195228
$this->storageMock->expects($this->any())
196229
->method('checkDbUsage')
197230
->will($this->returnValue(true));
198-
$this->storageMock->expects($this->any())
199-
->method('getMediaRelativePath')
200-
->will($this->returnArgument(0));
201231

202232
$storageDatabaseMock = $this->getMockBuilder('Magento\MediaStorage\Model\File\Storage\Database')
203233
->disableOriginalConstructor()
@@ -220,7 +250,7 @@ public function testDownloadFile()
220250
->with(
221251
$info['title'],
222252
['value' => $info['order_path'], 'type' => 'filename'],
223-
DirectoryList::ROOT,
253+
DirectoryList::MEDIA,
224254
'application/octet-stream',
225255
null
226256
);

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ protected function expectedValidate()
241241
return [
242242
'type' => 'image/jpeg',
243243
'title' => 'test.jpg',
244-
'quote_path' => 'pub/media/custom_options/quote/t/e/e1d601731b4b1a84163cd0e9370a4fcb.jpg',
245-
'order_path' => 'pub/media/custom_options/order/t/e/e1d601731b4b1a84163cd0e9370a4fcb.jpg',
244+
'quote_path' => 'custom_options/quote/t/e/e1d601731b4b1a84163cd0e9370a4fcb.jpg',
245+
'order_path' => 'custom_options/order/t/e/e1d601731b4b1a84163cd0e9370a4fcb.jpg',
246246
'size' => '3300',
247247
'width' => 136,
248248
'height' => 131,

0 commit comments

Comments
 (0)