Skip to content

Commit 0bb5a64

Browse files
committed
ACP2E-3447: Using the wrong Store ID in GraphQL header causes fatal memory error
1 parent 6acfd6a commit 0bb5a64

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

app/code/Magento/Store/App/Request/StorePathInfoValidator.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\Store\App\Request;
@@ -43,6 +44,11 @@ class StorePathInfoValidator
4344
*/
4445
private $storeCodeValidator;
4546

47+
/**
48+
* @var array
49+
*/
50+
private array $validatedStoreCodes = [];
51+
4652
/**
4753
* @param ScopeConfigInterface $config
4854
* @param StoreRepositoryInterface $storeRepository
@@ -79,17 +85,25 @@ public function getValidStoreCode(Http $request, string $pathInfo = '') : ?strin
7985
$pathInfo = $this->pathInfo->getPathInfo($request->getRequestUri(), $request->getBaseUrl());
8086
}
8187
$storeCode = $this->getStoreCode($pathInfo);
88+
8289
if (empty($storeCode) || $storeCode === Store::ADMIN_CODE || !$this->storeCodeValidator->isValid($storeCode)) {
8390
return null;
8491
}
8592

93+
if (array_key_exists($storeCode, $this->validatedStoreCodes)) {
94+
return $this->validatedStoreCodes[$storeCode];
95+
}
96+
8697
try {
8798
$this->storeRepository->getActiveStoreByCode($storeCode);
8899

100+
$this->validatedStoreCodes[$storeCode] = $storeCode;
89101
return $storeCode;
90102
} catch (NoSuchEntityException $e) {
103+
$this->validatedStoreCodes[$storeCode] = null;
91104
return null;
92105
} catch (StoreIsInactiveException $e) {
106+
$this->validatedStoreCodes[$storeCode] = null;
93107
return null;
94108
}
95109
}

app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\Store\Test\Unit\App\Request;
@@ -183,6 +184,36 @@ public function testGetValidStoreCode(string $pathInfo, bool $isStoreCodeValid,
183184
$this->assertEquals($expectedResult, $result);
184185
}
185186

187+
/**
188+
* @dataProvider getValidStoreCodeDataProvider
189+
* @param string $pathInfo
190+
* @param bool $isStoreCodeValid
191+
* @param string|null $expectedResult
192+
*/
193+
public function testGetValidStoreCodeResultIsCached(
194+
string $pathInfo,
195+
bool $isStoreCodeValid,
196+
?string $expectedResult
197+
): void {
198+
$this->configMock->method('getValue')
199+
->with(Store::XML_PATH_STORE_IN_URL)
200+
->willReturn(true);
201+
$this->pathInfoMock->method('getPathInfo')
202+
->willReturn('/store2/path2/');
203+
$this->storeCodeValidatorMock->method('isValid')
204+
->willReturn($isStoreCodeValid);
205+
$store = $this->createMock(Store::class);
206+
$this->storeRepositoryMock
207+
->expects($expectedResult ? $this->once() : $this->never())
208+
->method('getActiveStoreByCode')
209+
->willReturn($store);
210+
211+
$result1 = $this->storePathInfoValidator->getValidStoreCode($this->requestMock, $pathInfo);
212+
$result2 = $this->storePathInfoValidator->getValidStoreCode($this->requestMock, $pathInfo);
213+
$this->assertEquals($expectedResult, $result1);
214+
$this->assertEquals($result1, $result2);
215+
}
216+
186217
public static function getValidStoreCodeDataProvider(): array
187218
{
188219
return [

0 commit comments

Comments
 (0)