Skip to content

Commit 58a0fa1

Browse files
authored
Merge pull request #7989 from magento-cia/cia-2.4.6-develop-bugfixes-11182022
Cia 2.4.6 develop bugfixes 11182022
2 parents 26d62b2 + 5562493 commit 58a0fa1

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

app/code/Magento/PageCache/Controller/Block.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
* PageCache controller
43
*
54
* Copyright © Magento, Inc. All rights reserved.
65
* See COPYING.txt for license details.
@@ -9,6 +8,8 @@
98

109
use Magento\Framework\Serialize\Serializer\Base64Json;
1110
use Magento\Framework\Serialize\Serializer\Json;
11+
use Magento\Framework\Validator\RegexFactory;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
1314

1415
abstract class Block extends \Magento\Framework\App\Action\Action
@@ -40,28 +41,42 @@ abstract class Block extends \Magento\Framework\App\Action\Action
4041
*/
4142
private $layoutCacheKeyName = 'mage_pagecache';
4243

44+
/**
45+
* @var RegexFactory
46+
*/
47+
private RegexFactory $regexValidatorFactory;
48+
49+
/**
50+
* Validation pattern for handles array
51+
*/
52+
private const VALIDATION_RULE_PATTERN = '/^[a-z0-9]+[a-z0-9_]*$/i';
53+
4354
/**
4455
* @param \Magento\Framework\App\Action\Context $context
4556
* @param \Magento\Framework\Translate\InlineInterface $translateInline
4657
* @param Json $jsonSerializer
4758
* @param Base64Json $base64jsonSerializer
4859
* @param LayoutCacheKeyInterface $layoutCacheKey
60+
* @param RegexFactory|null $regexValidatorFactory
4961
*/
5062
public function __construct(
5163
\Magento\Framework\App\Action\Context $context,
5264
\Magento\Framework\Translate\InlineInterface $translateInline,
5365
Json $jsonSerializer = null,
5466
Base64Json $base64jsonSerializer = null,
55-
LayoutCacheKeyInterface $layoutCacheKey = null
67+
LayoutCacheKeyInterface $layoutCacheKey = null,
68+
?RegexFactory $regexValidatorFactory = null
5669
) {
5770
parent::__construct($context);
5871
$this->translateInline = $translateInline;
5972
$this->jsonSerializer = $jsonSerializer
60-
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
73+
?: ObjectManager::getInstance()->get(Json::class);
6174
$this->base64jsonSerializer = $base64jsonSerializer
62-
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Base64Json::class);
75+
?: ObjectManager::getInstance()->get(Base64Json::class);
6376
$this->layoutCacheKey = $layoutCacheKey
64-
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
77+
?: ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
78+
$this->regexValidatorFactory = $regexValidatorFactory
79+
?: ObjectManager::getInstance()->get(RegexFactory::class);
6580
}
6681

6782
/**
@@ -79,6 +94,9 @@ protected function _getBlocks()
7994
}
8095
$blocks = $this->jsonSerializer->unserialize($blocks);
8196
$handles = $this->base64jsonSerializer->unserialize($handles);
97+
if (!$this->validateHandleParam($handles)) {
98+
return [];
99+
}
82100

83101
$layout = $this->_view->getLayout();
84102
$this->layoutCacheKey->addCacheKeys($this->layoutCacheKeyName);
@@ -95,4 +113,22 @@ protected function _getBlocks()
95113

96114
return $data;
97115
}
116+
117+
/**
118+
* Validates handles parameter
119+
*
120+
* @param array $handles
121+
* @return bool
122+
*/
123+
private function validateHandleParam($handles): bool
124+
{
125+
$validator = $this->regexValidatorFactory->create(['pattern' => self::VALIDATION_RULE_PATTERN]);
126+
foreach ($handles as $handle) {
127+
if ($handle && !$validator->isValid($handle)) {
128+
return false;
129+
}
130+
}
131+
132+
return true;
133+
}
98134
}

app/code/Magento/PageCache/Test/Unit/Controller/Block/EsiTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Framework\View\Element\AbstractBlock;
1919
use Magento\Framework\View\Layout;
2020
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
21+
use Magento\Framework\Validator\Regex;
22+
use Magento\Framework\Validator\RegexFactory;
2123
use Magento\PageCache\Controller\Block;
2224
use Magento\PageCache\Controller\Block\Esi;
2325
use Magento\PageCache\Test\Unit\Block\Controller\StubBlock;
@@ -64,6 +66,11 @@ class EsiTest extends TestCase
6466
*/
6567
protected $translateInline;
6668

69+
/**
70+
* Validation pattern for handles array
71+
*/
72+
private const VALIDATION_RULE_PATTERN = '/^[a-z0-9]+[a-z0-9_]*$/i';
73+
6774
/**
6875
* Set up before test
6976
*/
@@ -98,6 +105,16 @@ protected function setUp(): void
98105

99106
$this->translateInline = $this->getMockForAbstractClass(InlineInterface::class);
100107

108+
$regexFactoryMock = $this->getMockBuilder(RegexFactory::class)
109+
->disableOriginalConstructor()
110+
->setMethods(['create'])
111+
->getMock();
112+
113+
$regexObject = new Regex(self::VALIDATION_RULE_PATTERN);
114+
115+
$regexFactoryMock->expects($this->any())->method('create')
116+
->willReturn($regexObject);
117+
101118
$helperObjectManager = new ObjectManager($this);
102119
$this->action = $helperObjectManager->getObject(
103120
Esi::class,
@@ -106,7 +123,8 @@ protected function setUp(): void
106123
'translateInline' => $this->translateInline,
107124
'jsonSerializer' => new Json(),
108125
'base64jsonSerializer' => new Base64Json(),
109-
'layoutCacheKey' => $this->layoutCacheKeyMock
126+
'layoutCacheKey' => $this->layoutCacheKeyMock,
127+
'regexValidatorFactory' => $regexFactoryMock
110128
]
111129
);
112130
}

app/code/Magento/PageCache/Test/Unit/Controller/Block/RenderTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Framework\View\Layout;
1919
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
2020
use Magento\Framework\View\Layout\ProcessorInterface;
21+
use Magento\Framework\Validator\Regex;
22+
use Magento\Framework\Validator\RegexFactory;
2123
use Magento\PageCache\Controller\Block;
2224
use Magento\PageCache\Controller\Block\Render;
2325
use Magento\PageCache\Test\Unit\Block\Controller\StubBlock;
@@ -69,6 +71,11 @@ class RenderTest extends TestCase
6971
*/
7072
protected $layoutCacheKeyMock;
7173

74+
/**
75+
* Validation pattern for handles array
76+
*/
77+
private const VALIDATION_RULE_PATTERN = '/^[a-z0-9]+[a-z0-9_]*$/i';
78+
7279
/**
7380
* @inheritDoc
7481
*/
@@ -111,6 +118,16 @@ protected function setUp(): void
111118

112119
$this->translateInline = $this->getMockForAbstractClass(InlineInterface::class);
113120

121+
$regexFactoryMock = $this->getMockBuilder(RegexFactory::class)
122+
->disableOriginalConstructor()
123+
->setMethods(['create'])
124+
->getMock();
125+
126+
$regexObject = new Regex(self::VALIDATION_RULE_PATTERN);
127+
128+
$regexFactoryMock->expects($this->any())->method('create')
129+
->willReturn($regexObject);
130+
114131
$helperObjectManager = new ObjectManager($this);
115132
$this->action = $helperObjectManager->getObject(
116133
Render::class,
@@ -119,7 +136,8 @@ protected function setUp(): void
119136
'translateInline' => $this->translateInline,
120137
'jsonSerializer' => new Json(),
121138
'base64jsonSerializer' => new Base64Json(),
122-
'layoutCacheKey' => $this->layoutCacheKeyMock
139+
'layoutCacheKey' => $this->layoutCacheKeyMock,
140+
'regexValidatorFactory' => $regexFactoryMock
123141
]
124142
);
125143
}

0 commit comments

Comments
 (0)