Skip to content

Commit 3d768e0

Browse files
committed
MC-5054: User Can Bypass File Type Validation In PageBuilder's File Upload Spots
Move UploadTest to integration test
1 parent e6f35b7 commit 3d768e0

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

app/code/Magento/PageBuilder/Test/Unit/Controller/Adminhtml/ContentType/Image/UploadTest.php renamed to dev/tests/integration/testsuite/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image/UploadTest.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\PageBuilder\Test\Unit\Controller\Adminhtml\ContentType\Image;
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
710

811
use Magento\Framework\File\Mime;
912
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as Controller;
@@ -14,13 +17,12 @@
1417
class UploadTest extends \PHPUnit\Framework\TestCase
1518
{
1619
/**
17-
* Subject under test
1820
* @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
1921
*/
2022
private $controller;
2123

2224
/**
23-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
25+
* @var \Magento\Framework\ObjectManagerInterface
2426
*/
2527
private $objectManager;
2628

@@ -39,9 +41,12 @@ class UploadTest extends \PHPUnit\Framework\TestCase
3941
*/
4042
private $resultJsonFactory;
4143

44+
/**
45+
* @inheritdoc
46+
*/
4247
protected function setUp()
4348
{
44-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
49+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
4550

4651
$this->uploaderFactory = $this->createPartialMock(\Magento\Framework\File\UploaderFactory::class, ['create']);
4752

@@ -57,15 +62,26 @@ protected function setUp()
5762

5863
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
5964

60-
$this->controller = $this->objectManager->getObject(Controller::class, [
65+
$this->controller = $this->objectManager->create(Controller::class, [
6166
'resultJsonFactory' => $this->resultJsonFactory,
6267
'uploaderFactory' => $this->uploaderFactory
6368
]);
6469
}
6570

71+
/**
72+
* @inheritdoc
73+
*/
74+
protected function tearDown()
75+
{
76+
$_FILES = [];
77+
}
78+
79+
/**
80+
* Assert that file validation passes when uploaded file has correct extension and valid mime type
81+
*/
6682
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
6783
{
68-
$valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/a.png');
84+
$valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/a.png');
6985

7086
$_FILES = [
7187
'background_image' => [
@@ -77,9 +93,9 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
7793
]
7894
];
7995

80-
$uploader = $this->objectManager->getObject(\Magento\Framework\File\Uploader::class, [
96+
$uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [
8197
'fileId' => 'background_image',
82-
'fileMime' => $this->objectManager->getObject(Mime::class),
98+
'fileMime' => $this->objectManager->create(Mime::class),
8399
]);
84100

85101
$this->uploaderFactory
@@ -97,9 +113,12 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
97113
$this->controller->execute();
98114
}
99115

116+
/**
117+
* Assert that file validation fails when uploaded file has correct extension but invalid mime type
118+
*/
100119
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
101120
{
102-
$invalid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/not-a.png');
121+
$invalid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/not-a.png');
103122

104123
$_FILES = [
105124
'background_image' => [
@@ -111,9 +130,9 @@ public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMime
111130
]
112131
];
113132

114-
$uploader = $this->objectManager->getObject(\Magento\Framework\File\Uploader::class, [
133+
$uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [
115134
'fileId' => 'background_image',
116-
'fileMime' => $this->objectManager->getObject(Mime::class),
135+
'fileMime' => $this->objectManager->create(Mime::class),
117136
]);
118137

119138
$this->uploaderFactory

0 commit comments

Comments
 (0)