Skip to content

Commit f6ac4d2

Browse files
cia-2.4.7-beta1-develop-bugfixes-01132023
Merge branch 'AC-7025' into cia-2.4.7-beta1-develop-bugfixes-01132023
2 parents 0e75f36 + 185a0ea commit f6ac4d2

File tree

5 files changed

+183
-24
lines changed

5 files changed

+183
-24
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Catalog\Controller\Adminhtml\Product;
810

9-
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
10-
use Magento\Backend\App\Action;
1111
use Magento\Catalog\Controller\Adminhtml\Product;
12+
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
1213
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\RegexValidator;
1315

1416
class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product implements HttpGetActionInterface
1517
{
1618
/**
1719
* @var Initialization\StockDataFilter
1820
* @deprecated 101.0.0
21+
* @see Initialization\StockDataFilter
1922
*/
2023
protected $stockFilter;
2124

@@ -30,23 +33,32 @@ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product implements
3033
protected $resultForwardFactory;
3134

3235
/**
33-
* @param Action\Context $context
36+
* @var RegexValidator
37+
*/
38+
private RegexValidator $regexValidator;
39+
40+
/**
41+
* @param Context $context
3442
* @param Builder $productBuilder
3543
* @param Initialization\StockDataFilter $stockFilter
3644
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
3745
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
46+
* @param RegexValidator|null $regexValidator
3847
*/
3948
public function __construct(
4049
\Magento\Backend\App\Action\Context $context,
4150
Product\Builder $productBuilder,
4251
Initialization\StockDataFilter $stockFilter,
4352
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
44-
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
53+
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
54+
RegexValidator $regexValidator = null
4555
) {
4656
$this->stockFilter = $stockFilter;
4757
parent::__construct($context, $productBuilder);
4858
$this->resultPageFactory = $resultPageFactory;
4959
$this->resultForwardFactory = $resultForwardFactory;
60+
$this->regexValidator = $regexValidator
61+
?: ObjectManager::getInstance()->get(RegexValidator::class);
5062
}
5163

5264
/**
@@ -56,6 +68,11 @@ public function __construct(
5668
*/
5769
public function execute()
5870
{
71+
$typeId = $this->getRequest()->getParam('type');
72+
if (!$this->regexValidator->validateParamRegex($typeId)) {
73+
return $this->resultForwardFactory->create()->forward('noroute');
74+
}
75+
5976
if (!$this->getRequest()->getParam('set')) {
6077
return $this->resultForwardFactory->create()->forward('noroute');
6178
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php

100644100755
Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Magento\Catalog\Controller\Adminhtml\Product\NewAction;
1717
use Magento\Catalog\Model\Product;
1818
use Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest;
19+
use Magento\Framework\RegexValidator;
20+
use Magento\Framework\Validator\Regex;
21+
use Magento\Framework\Validator\RegexFactory;
1922
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2023
use Magento\Framework\View\Result\PageFactory;
2124
use PHPUnit\Framework\MockObject\MockObject;
@@ -42,6 +45,26 @@ class NewActionTest extends ProductTest
4245
*/
4346
protected $initializationHelper;
4447

48+
/**
49+
* @var RegexValidator|MockObject
50+
*/
51+
private $regexValidator;
52+
53+
/**
54+
* @var RegexFactory
55+
*/
56+
private $regexValidatorFactoryMock;
57+
58+
/**
59+
* @var Regex|MockObject
60+
*/
61+
private $regexValidatorMock;
62+
63+
/**
64+
* @var ForwardFactory&MockObject|MockObject
65+
*/
66+
private $resultForwardFactory;
67+
4568
protected function setUp(): void
4669
{
4770
$this->productBuilder = $this->createPartialMock(
@@ -63,37 +86,78 @@ protected function setUp(): void
6386
->disableOriginalConstructor()
6487
->setMethods(['create'])
6588
->getMock();
66-
$resultPageFactory->expects($this->atLeastOnce())
67-
->method('create')
68-
->willReturn($this->resultPage);
6989

7090
$this->resultForward = $this->getMockBuilder(Forward::class)
7191
->disableOriginalConstructor()
7292
->getMock();
73-
$resultForwardFactory = $this->getMockBuilder(ForwardFactory::class)
93+
$this->resultForwardFactory = $this->getMockBuilder(ForwardFactory::class)
94+
->disableOriginalConstructor()
95+
->onlyMethods(['create'])
96+
->getMock();
97+
98+
$this->regexValidatorFactoryMock = $this->getMockBuilder(RegexFactory::class)
7499
->disableOriginalConstructor()
75100
->setMethods(['create'])
76101
->getMock();
77-
$resultForwardFactory->expects($this->any())
78-
->method('create')
79-
->willReturn($this->resultForward);
102+
$this->regexValidatorMock = $this->createMock(Regex::class);
103+
$this->regexValidatorFactoryMock->method('create')
104+
->willReturn($this->regexValidatorMock);
80105

106+
$this->regexValidator = new regexValidator($this->regexValidatorFactoryMock);
81107
$this->action = (new ObjectManager($this))->getObject(
82108
NewAction::class,
83109
[
84110
'context' => $this->initContext(),
85111
'productBuilder' => $this->productBuilder,
86112
'resultPageFactory' => $resultPageFactory,
87-
'resultForwardFactory' => $resultForwardFactory,
113+
'resultForwardFactory' => $this->resultForwardFactory,
114+
'regexValidator' => $this->regexValidator,
88115
]
89116
);
90117
}
91118

92-
public function testExecute()
119+
/**
120+
* Test execute method input validation.
121+
*
122+
* @param string $value
123+
* @param bool $exceptionThrown
124+
* @dataProvider validationCases
125+
*/
126+
public function testExecute(string $value, bool $exceptionThrown): void
127+
{
128+
if ($exceptionThrown) {
129+
$this->action->getRequest()->expects($this->any())
130+
->method('getParam')
131+
->willReturn($value);
132+
$this->resultForwardFactory->expects($this->any())
133+
->method('create')
134+
->willReturn($this->resultForward);
135+
$this->resultForward->expects($this->once())
136+
->method('forward')
137+
->with('noroute')
138+
->willReturn(true);
139+
$this->assertTrue($this->action->execute());
140+
} else {
141+
$this->action->getRequest()->expects($this->any())->method('getParam')->willReturn($value);
142+
$this->regexValidatorMock->expects($this->any())
143+
->method('isValid')
144+
->with($value)
145+
->willReturn(true);
146+
147+
$this->assertEquals(true, $this->regexValidator->validateParamRegex($value));
148+
}
149+
}
150+
151+
/**
152+
* Validation cases.
153+
*
154+
* @return array
155+
*/
156+
public function validationCases(): array
93157
{
94-
$this->action->getRequest()->expects($this->any())->method('getParam')->willReturn(true);
95-
$this->action->getRequest()->expects($this->any())->method('getFullActionName')
96-
->willReturn('catalog_product_new');
97-
$this->action->execute();
158+
return [
159+
'execute-with-exception' => ['simple\' and true()]|*[self%3a%3ahandle%20or%20self%3a%3alayout',true],
160+
'execute-without-exception' => ['catalog_product_new',false]
161+
];
98162
}
99163
}

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,5 @@ Details,Details
819819
"Failed to retrieve product links for ""%1""","Failed to retrieve product links for ""%1"""
820820
"The linked product SKU is invalid. Verify the data and try again.","The linked product SKU is invalid. Verify the data and try again."
821821
"The linked products data is invalid. Verify the data and try again.","The linked products data is invalid. Verify the data and try again."
822+
"The url has invalid characters. Please correct and try again.","The url has invalid characters. Please correct and try again."
822823

app/code/Magento/Sales/Controller/Adminhtml/Order/Create/LoadBlock.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Controller\Adminhtml\Order\Create;
79

8-
use Magento\Framework\App\Action\HttpGetActionInterface;
9-
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1010
use Magento\Backend\App\Action;
11+
use Magento\Backend\App\Action\Context;
1112
use Magento\Backend\Model\View\Result\ForwardFactory;
12-
use Magento\Framework\View\Result\PageFactory;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1315
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Controller\Result\RawFactory;
17+
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\RegexValidator;
19+
use Magento\Framework\View\Result\PageFactory;
1520
use Magento\Sales\Controller\Adminhtml\Order\Create as CreateAction;
1621
use Magento\Store\Model\StoreManagerInterface;
1722

23+
/**
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
*/
1826
class LoadBlock extends CreateAction implements HttpPostActionInterface, HttpGetActionInterface
1927
{
2028
/**
@@ -28,13 +36,19 @@ class LoadBlock extends CreateAction implements HttpPostActionInterface, HttpGet
2836
private $storeManager;
2937

3038
/**
31-
* @param Action\Context $context
32-
* @param \Magento\Catalog\Helper\Product $productHelper
33-
* @param \Magento\Framework\Escaper $escaper
39+
* @var RegexValidator
40+
*/
41+
private RegexValidator $regexValidator;
42+
43+
/**
44+
* @param Context $context
45+
* @param Product $productHelper
46+
* @param Escaper $escaper
3447
* @param PageFactory $resultPageFactory
3548
* @param ForwardFactory $resultForwardFactory
3649
* @param RawFactory $resultRawFactory
3750
* @param StoreManagerInterface|null $storeManager
51+
* @param RegexValidator|null $regexValidator
3852
*/
3953
public function __construct(
4054
Action\Context $context,
@@ -43,7 +57,8 @@ public function __construct(
4357
PageFactory $resultPageFactory,
4458
ForwardFactory $resultForwardFactory,
4559
RawFactory $resultRawFactory,
46-
StoreManagerInterface $storeManager = null
60+
StoreManagerInterface $storeManager = null,
61+
RegexValidator $regexValidator = null
4762
) {
4863
$this->resultRawFactory = $resultRawFactory;
4964
parent::__construct(
@@ -55,6 +70,8 @@ public function __construct(
5570
);
5671
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
5772
->get(StoreManagerInterface::class);
73+
$this->regexValidator = $regexValidator
74+
?: ObjectManager::getInstance()->get(RegexValidator::class);
5875
}
5976

6077
/**
@@ -64,6 +81,7 @@ public function __construct(
6481
*
6582
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6683
* @SuppressWarnings(PHPMD.NPathComplexity)
84+
* @throws LocalizedException
6785
*/
6886
public function execute()
6987
{
@@ -84,6 +102,12 @@ public function execute()
84102
$asJson = $request->getParam('json');
85103
$block = $request->getParam('block');
86104

105+
if ($block && !$this->regexValidator->validateParamRegex($block)) {
106+
throw new LocalizedException(
107+
__('The url has invalid characters. Please correct and try again.')
108+
);
109+
}
110+
87111
/** @var \Magento\Framework\View\Result\Page $resultPage */
88112
$resultPage = $this->resultPageFactory->create();
89113
if ($asJson) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework;
9+
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Validator\RegexFactory;
12+
13+
class RegexValidator extends RegexFactory
14+
{
15+
16+
/**
17+
* @var RegexFactory
18+
*/
19+
private RegexFactory $regexValidatorFactory;
20+
21+
/**
22+
* Validation pattern for handles array
23+
*/
24+
private const VALIDATION_RULE_PATTERN = '/^[a-z0-9,.]+[a-z0-9_,.]*$/i';
25+
26+
/**
27+
* @param RegexFactory|null $regexValidatorFactory
28+
*/
29+
public function __construct(
30+
?RegexFactory $regexValidatorFactory = null
31+
) {
32+
$this->regexValidatorFactory = $regexValidatorFactory
33+
?: ObjectManager::getInstance()->get(RegexFactory::class);
34+
}
35+
36+
/**
37+
* Validates parameter regex
38+
*
39+
* @param string $params
40+
* @param string $pattern
41+
* @return bool
42+
*/
43+
public function validateParamRegex(string $params, string $pattern = self::VALIDATION_RULE_PATTERN): bool
44+
{
45+
$validator = $this->regexValidatorFactory->create(['pattern' => $pattern]);
46+
47+
if ($params && !$validator->isValid($params)) {
48+
return false;
49+
}
50+
51+
return true;
52+
}
53+
}

0 commit comments

Comments
 (0)