Skip to content

Commit 653b9fe

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-72013' into 2.3-develop-pr1
2 parents c3ec50c + f5532ed commit 653b9fe

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Viewfile.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,15 @@ public function __construct(
132132
*/
133133
public function execute()
134134
{
135-
$file = null;
136-
$plain = false;
137-
if ($this->getRequest()->getParam('file')) {
138-
// download file
139-
$file = $this->urlDecoder->decode(
140-
$this->getRequest()->getParam('file')
141-
);
142-
} elseif ($this->getRequest()->getParam('image')) {
143-
// show plain image
144-
$file = $this->urlDecoder->decode(
145-
$this->getRequest()->getParam('image')
146-
);
147-
$plain = true;
148-
} else {
149-
throw new NotFoundException(__('Page not found.'));
150-
}
135+
list($file, $plain) = $this->getFileParams();
151136

152137
/** @var \Magento\Framework\Filesystem $filesystem */
153138
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
154139
$directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
155140
$fileName = CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER . '/' . ltrim($file, '/');
156141
$path = $directory->getAbsolutePath($fileName);
157-
if (!$directory->isFile($fileName)
158-
&& !$this->_objectManager->get(\Magento\MediaStorage\Helper\File\Storage::class)->processStorageFile($path)
142+
if (mb_strpos($path, '..') !== false || (!$directory->isFile($fileName)
143+
&& !$this->_objectManager->get(\Magento\MediaStorage\Helper\File\Storage::class)->processStorageFile($path))
159144
) {
160145
throw new NotFoundException(__('Page not found.'));
161146
}
@@ -198,4 +183,32 @@ public function execute()
198183
);
199184
}
200185
}
186+
187+
/**
188+
* Get parameters from request.
189+
*
190+
* @return array
191+
* @throws NotFoundException
192+
*/
193+
private function getFileParams()
194+
{
195+
$file = null;
196+
$plain = false;
197+
if ($this->getRequest()->getParam('file')) {
198+
// download file
199+
$file = $this->urlDecoder->decode(
200+
$this->getRequest()->getParam('file')
201+
);
202+
} elseif ($this->getRequest()->getParam('image')) {
203+
// show plain image
204+
$file = $this->urlDecoder->decode(
205+
$this->getRequest()->getParam('image')
206+
);
207+
$plain = true;
208+
} else {
209+
throw new NotFoundException(__('Page not found.'));
210+
}
211+
212+
return [$file, $plain];
213+
}
201214
}

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/ViewfileTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,53 @@ public function testExecuteGetParamImage()
206206
);
207207
$this->assertSame($this->resultRawMock, $controller->execute());
208208
}
209+
210+
/**
211+
* @expectedException \Magento\Framework\Exception\NotFoundException
212+
* @expectedExceptionMessage Page not found.
213+
*/
214+
public function testExecuteInvalidFile()
215+
{
216+
$file = '../../../app/etc/env.php';
217+
$decodedFile = base64_encode($file);
218+
$fileName = 'customer/' . $file;
219+
$path = 'path';
220+
221+
$this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
222+
223+
$this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
224+
225+
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
226+
->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
227+
->willReturn($this->directoryMock);
228+
229+
$this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(false);
230+
231+
$this->objectManagerMock->expects($this->any())->method('get')
232+
->willReturnMap(
233+
[
234+
[\Magento\Framework\Filesystem::class, $this->fileSystemMock],
235+
[\Magento\MediaStorage\Helper\File\Storage::class, $this->storage],
236+
]
237+
);
238+
239+
$this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
240+
$fileFactoryMock = $this->createMock(
241+
\Magento\Framework\App\Response\Http\FileFactory::class,
242+
[],
243+
[],
244+
'',
245+
false
246+
);
247+
248+
$controller = $this->objectManager->getObject(
249+
\Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
250+
[
251+
'context' => $this->contextMock,
252+
'urlDecoder' => $this->urlDecoderMock,
253+
'fileFactory' => $fileFactoryMock,
254+
]
255+
);
256+
$controller->execute();
257+
}
209258
}

0 commit comments

Comments
 (0)