Skip to content

Commit c2d4e86

Browse files
authored
Merge pull request #4419 from magento-tsg/2.2.10-develop-pr101
[TSG] Fixes for 2.2.10 (pr101) (2.2.10-develop)
2 parents 5517446 + a5b92fe commit c2d4e86

File tree

17 files changed

+310
-55
lines changed

17 files changed

+310
-55
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Uploader.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\CatalogImportExport\Model\Import;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\ValidatorException;
10+
use Magento\Framework\Filesystem\Directory\ReadInterface;
911
use Magento\Framework\Filesystem\DriverPool;
1012
use Magento\Framework\App\ObjectManager;
1113

@@ -18,7 +20,6 @@
1820
*/
1921
class Uploader extends \Magento\MediaStorage\Model\File\Uploader
2022
{
21-
2223
/**
2324
* HTTP scheme
2425
* used to compare against the filename and select the proper DriverPool adapter
@@ -103,6 +104,11 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
103104
*/
104105
protected $_coreFileStorage;
105106

107+
/**
108+
* @var \Magento\Framework\Filesystem
109+
*/
110+
private $filesystem;
111+
106112
/**
107113
* Instance of random data generator.
108114
*
@@ -115,6 +121,11 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
115121
*/
116122
private $directoryResolver;
117123

124+
/**
125+
* @var \Magento\Framework\Filesystem\Directory\ReadFactory
126+
*/
127+
private $directoryReadFactory;
128+
118129
/**
119130
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
120131
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
@@ -125,8 +136,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
125136
* @param string|null $filePath
126137
* @param \Magento\Framework\App\Filesystem\DirectoryResolver|null $directoryResolver
127138
* @param \Magento\Framework\Math\Random|null $random
128-
* @throws \Magento\Framework\Exception\FileSystemException
129-
* @throws \Magento\Framework\Exception\LocalizedException
139+
* @param \Magento\Framework\Filesystem\Directory\ReadFactory|null $directoryReadFactory
140+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
130141
*/
131142
public function __construct(
132143
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
@@ -137,7 +148,8 @@ public function __construct(
137148
\Magento\Framework\Filesystem\File\ReadFactory $readFactory,
138149
$filePath = null,
139150
\Magento\Framework\App\Filesystem\DirectoryResolver $directoryResolver = null,
140-
\Magento\Framework\Math\Random $random = null
151+
\Magento\Framework\Math\Random $random = null,
152+
\Magento\Framework\Filesystem\Directory\ReadFactory $directoryReadFactory = null
141153
) {
142154
if ($filePath !== null) {
143155
$this->_setUploadFile($filePath);
@@ -148,10 +160,13 @@ public function __construct(
148160
$this->_validator = $validator;
149161
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
150162
$this->_readFactory = $readFactory;
163+
$this->filesystem = $filesystem;
151164
$this->directoryResolver = $directoryResolver
152165
?: ObjectManager::getInstance()->get(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
153166
$this->random = $random
154167
?: ObjectManager::getInstance()->get(\Magento\Framework\Math\Random::class);
168+
$this->directoryReadFactory = $directoryReadFactory
169+
?: ObjectManager::getInstance()->get(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
155170
$this->downloadDir = DirectoryList::getDefaultConfig()[DirectoryList::TMP][DirectoryList::PATH];
156171
}
157172

@@ -177,7 +192,7 @@ public function init()
177192
* @param string $fileName
178193
* @param bool $renameFileOff
179194
* @return array
180-
* @throws \Magento\Framework\Exception\LocalizedException
195+
* @throws LocalizedException
181196
*/
182197
public function move($fileName, $renameFileOff = false)
183198
{
@@ -242,7 +257,20 @@ private function downloadFileFromUrl($url, $driver)
242257
*/
243258
protected function _setUploadFile($filePath)
244259
{
245-
if (!$this->_directory->isReadable($filePath)) {
260+
try {
261+
$fullPath = $this->_directory->getAbsolutePath($filePath);
262+
if ($this->getTmpDir()) {
263+
$tmpDir = $this->getDirectoryReadByPath(
264+
$this->_directory->getAbsolutePath($this->getTmpDir())
265+
);
266+
} else {
267+
$tmpDir = $this->_directory;
268+
}
269+
$readable = $tmpDir->isReadable($fullPath);
270+
} catch (ValidatorException $exception) {
271+
$readable = false;
272+
}
273+
if (!$readable) {
246274
throw new \Magento\Framework\Exception\LocalizedException(
247275
__('File \'%1\' was not found or has read restriction.', $filePath)
248276
);
@@ -397,4 +425,17 @@ protected function chmod($file)
397425
{
398426
return;
399427
}
428+
429+
/**
430+
* Create an instance of directory with read permissions by path.
431+
*
432+
* @param string $path
433+
* @param string $driverCode
434+
*
435+
* @return ReadInterface
436+
*/
437+
private function getDirectoryReadByPath(string $path, string $driverCode = DriverPool::FILE): ReadInterface
438+
{
439+
return $this->directoryReadFactory->create($path, $driverCode);
440+
}
400441
}

app/code/Magento/Customer/Controller/Account/LoginPost.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Framework\App\Config\ScopeConfigInterface;
1919

2020
/**
21+
* Post login customer action.
22+
*
2123
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2224
*/
2325
class LoginPost extends \Magento\Customer\Controller\AbstractAccount
@@ -151,7 +153,6 @@ public function execute()
151153
try {
152154
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
153155
$this->session->setCustomerDataAsLoggedIn($customer);
154-
$this->session->regenerateId();
155156
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
156157
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
157158
$metadata->setPath('/');

app/code/Magento/Customer/Controller/Ajax/Login.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public function execute()
195195
$credentials['password']
196196
);
197197
$this->customerSession->setCustomerDataAsLoggedIn($customer);
198-
$this->customerSession->regenerateId();
199198
$redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
200199
if ($this->cookieManager->getCookie('mage-cache-sessid')) {
201200
$metadata = $this->cookieMetadataFactory->createCookieMetadata();

app/code/Magento/Customer/Model/Customer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\Reflection\DataObjectProcessor;
2020
use Magento\Store\Model\ScopeInterface;
2121
use Magento\Framework\App\ObjectManager;
22+
use Magento\Framework\Math\Random;
2223

2324
/**
2425
* Customer model
@@ -179,7 +180,7 @@ class Customer extends \Magento\Framework\Model\AbstractModel
179180
protected $_encryptor;
180181

181182
/**
182-
* @var \Magento\Framework\Math\Random
183+
* @var Random
183184
*/
184185
protected $mathRandom;
185186

@@ -240,6 +241,7 @@ class Customer extends \Magento\Framework\Model\AbstractModel
240241
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
241242
* @param array $data
242243
* @param AccountConfirmation|null $accountConfirmation
244+
* @param Random|null $mathRandom
243245
*
244246
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
245247
*/
@@ -264,7 +266,8 @@ public function __construct(
264266
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
265267
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
266268
array $data = [],
267-
AccountConfirmation $accountConfirmation = null
269+
AccountConfirmation $accountConfirmation = null,
270+
Random $mathRandom = null
268271
) {
269272
$this->metadataService = $metadataService;
270273
$this->_scopeConfig = $scopeConfig;
@@ -283,6 +286,7 @@ public function __construct(
283286
$this->indexerRegistry = $indexerRegistry;
284287
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
285288
->get(AccountConfirmation::class);
289+
$this->mathRandom = $mathRandom ?: ObjectManager::getInstance()->get(Random::class);
286290
parent::__construct(
287291
$context,
288292
$registry,
@@ -794,7 +798,7 @@ public function isConfirmationRequired()
794798
*/
795799
public function getRandomConfirmationKey()
796800
{
797-
return md5(uniqid());
801+
return $this->mathRandom->getRandomString(32);
798802
}
799803

800804
/**

app/code/Magento/Customer/Model/Plugin/CustomerNotification.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
use Magento\Framework\Exception\NoSuchEntityException;
1616
use Psr\Log\LoggerInterface;
1717

18+
/**
19+
* Plugin before \Magento\Framework\App\Action\AbstractAction::dispatch.
20+
*
21+
* Plugin to remove notifications from cache.
22+
*/
1823
class CustomerNotification
1924
{
2025
/**
@@ -66,6 +71,8 @@ public function __construct(
6671
}
6772

6873
/**
74+
* Removes notifications from cache.
75+
*
6976
* @param AbstractAction $subject
7077
* @param RequestInterface $request
7178
* @return void
@@ -82,10 +89,10 @@ public function beforeDispatch(AbstractAction $subject, RequestInterface $reques
8289
)
8390
) {
8491
try {
92+
$this->session->regenerateId();
8593
$customer = $this->customerRepository->getById($customerId);
8694
$this->session->setCustomerData($customer);
8795
$this->session->setCustomerGroupId($customer->getGroupId());
88-
$this->session->regenerateId();
8996
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId);
9097
} catch (NoSuchEntityException $e) {
9198
$this->logger->error($e);

app/code/Magento/Customer/Model/Session.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ public function setCustomerGroupId($id)
354354
}
355355

356356
/**
357-
* Get customer group id
358-
* If customer is not logged in system, 'not logged in' group id will be returned
357+
* Get customer group id.
358+
*
359+
* If customer is not logged in system, 'not logged in' group id will be returned.
359360
*
360361
* @return int
361362
*/
@@ -407,24 +408,29 @@ public function checkCustomerId($customerId)
407408
}
408409

409410
/**
411+
* Sets customer as logged in.
412+
*
410413
* @param Customer $customer
411414
* @return $this
412415
*/
413416
public function setCustomerAsLoggedIn($customer)
414417
{
418+
$this->regenerateId();
415419
$this->setCustomer($customer);
416420
$this->_eventManager->dispatch('customer_login', ['customer' => $customer]);
417421
$this->_eventManager->dispatch('customer_data_object_login', ['customer' => $this->getCustomerDataObject()]);
418-
$this->regenerateId();
419422
return $this;
420423
}
421424

422425
/**
426+
* Sets customer as logged in.
427+
*
423428
* @param CustomerData $customer
424429
* @return $this
425430
*/
426431
public function setCustomerDataAsLoggedIn($customer)
427432
{
433+
$this->regenerateId();
428434
$this->_httpContext->setValue(Context::CONTEXT_AUTH, true, false);
429435
$this->setCustomerData($customer);
430436

@@ -567,6 +573,8 @@ public function regenerateId()
567573
}
568574

569575
/**
576+
* Creates \Magento\Framework\UrlInterface object.
577+
*
570578
* @return \Magento\Framework\UrlInterface
571579
*/
572580
protected function _createUrl()

app/code/Magento/Customer/Test/Unit/Controller/Account/LoginPostTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Customer\Test\Unit\Controller\Account;
78

89
use Magento\Customer\Api\AccountManagementInterface;
@@ -291,9 +292,8 @@ public function testExecuteSuccessCustomRedirect()
291292
->method('setCustomerDataAsLoggedIn')
292293
->with($customerMock)
293294
->willReturnSelf();
294-
$this->session->expects($this->once())
295-
->method('regenerateId')
296-
->willReturnSelf();
295+
$this->session->expects($this->never())
296+
->method('regenerateId');
297297

298298
$this->accountRedirect->expects($this->never())
299299
->method('getRedirect')
@@ -356,9 +356,8 @@ public function testExecuteSuccess()
356356
->method('setCustomerDataAsLoggedIn')
357357
->with($customerMock)
358358
->willReturnSelf();
359-
$this->session->expects($this->once())
360-
->method('regenerateId')
361-
->willReturnSelf();
359+
$this->session->expects($this->never())
360+
->method('regenerateId');
362361

363362
$this->accountRedirect->expects($this->once())
364363
->method('getRedirect')

app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use Magento\Customer\Model\Customer;
1515
use Magento\Customer\Model\AccountConfirmation;
16+
use Magento\Framework\Math\Random;
1617

1718
/**
1819
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
* @SuppressWarnings(PHPMD.TooManyFields)
1921
*/
2022
class CustomerTest extends \PHPUnit\Framework\TestCase
2123
{
@@ -68,6 +70,14 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
6870
*/
6971
private $accountConfirmation;
7072

73+
/**
74+
* @var Random|\PHPUnit_Framework_MockObject_MockObject
75+
*/
76+
private $mathRandom;
77+
78+
/**
79+
* @inheritdoc
80+
*/
7181
protected function setUp()
7282
{
7383
$this->_website = $this->createMock(\Magento\Store\Model\Website::class);
@@ -100,6 +110,7 @@ protected function setUp()
100110
$this->_encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
101111
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
102112
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
113+
$this->mathRandom = $this->createMock(Random::class);
103114
$this->_model = $helper->getObject(
104115
\Magento\Customer\Model\Customer::class,
105116
[
@@ -112,7 +123,8 @@ protected function setUp()
112123
'registry' => $this->registryMock,
113124
'resource' => $this->resourceMock,
114125
'dataObjectProcessor' => $this->dataObjectProcessor,
115-
'accountConfirmation' => $this->accountConfirmation
126+
'accountConfirmation' => $this->accountConfirmation,
127+
'mathRandom' => $this->mathRandom,
116128
]
117129
);
118130
}
@@ -310,4 +322,20 @@ public function testUpdateData()
310322

311323
$this->assertEquals($this->_model->getData(), $expectedResult);
312324
}
325+
326+
/**
327+
* Check getRandomConfirmationKey use cryptographically secure function
328+
*
329+
* @return void
330+
*/
331+
public function testGetRandomConfirmationKey()
332+
{
333+
$this->mathRandom
334+
->expects($this->once())
335+
->method('getRandomString')
336+
->with(32)
337+
->willReturn('random_string');
338+
339+
$this->_model->getRandomConfirmationKey();
340+
}
313341
}

0 commit comments

Comments
 (0)