Skip to content

Commit a1215e1

Browse files
Merge branch '2.3.0-qwerty' of github.com:magento/magento2ce into MAGETWO-95391
2 parents f722280 + 4671f49 commit a1215e1

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
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+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CodeMessDetector\Rule\Design;
10+
11+
use PHPMD\AbstractNode;
12+
use PHPMD\AbstractRule;
13+
use PHPMD\Node\ClassNode;
14+
use PHPMD\Node\MethodNode;
15+
use PDepend\Source\AST\ASTMethod;
16+
use PHPMD\Rule\MethodAware;
17+
18+
/**
19+
* Detect direct request usages.
20+
*/
21+
class RequestAwareBlockMethod extends AbstractRule implements MethodAware
22+
{
23+
/**
24+
* @inheritDoc
25+
*
26+
* @param ASTMethod|MethodNode $method
27+
*/
28+
public function apply(AbstractNode $method)
29+
{
30+
$definedIn = $method->getParentType();
31+
try {
32+
$isBlock = ($definedIn instanceof ClassNode)
33+
&& is_subclass_of(
34+
$definedIn->getFullQualifiedName(),
35+
\Magento\Framework\View\Element\AbstractBlock::class
36+
);
37+
} catch (\Throwable $exception) {
38+
//Failed to load classes.
39+
return;
40+
}
41+
42+
if ($isBlock) {
43+
$nodes = $method->findChildrenOfType('PropertyPostfix') + $method->findChildrenOfType('MethodPostfix');
44+
foreach ($nodes as $node) {
45+
$name = mb_strtolower($node->getFirstChildOfType('Identifier')->getImage());
46+
if ($name === '_request' || $name === 'getrequest') {
47+
$this->addViolation($method, [$method->getFullQualifiedName()]);
48+
break;
49+
}
50+
}
51+
}
52+
}
53+
}

dev/tests/static/framework/Magento/CodeMessDetector/resources/rulesets/design.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ class PostOrder implements ActionInterface
5454
...
5555
return $response;
5656
}
57+
}
58+
]]>
59+
</example>
60+
</rule>
61+
<rule name="RequestAwareBlockMethod"
62+
class="Magento\CodeMessDetector\Rule\Design\RequestAwareBlockMethod"
63+
message="{0} uses request object directly. Add user input validation and suppress this warning.">
64+
<description>
65+
<![CDATA[
66+
Blocks must not depend on being used with certain controllers.
67+
If you use request object in a block directly you must validate all user input inside the block.
68+
]]>
69+
</description>
70+
<priority>2</priority>
71+
<properties />
72+
<example>
73+
<![CDATA[
74+
class MyOrder extends AbstractBlock
75+
{
76+
77+
.......
78+
79+
public function getOrder()
80+
{
81+
$orderId = $this->getRequest()->getParam('order_id');
82+
//Validate customer having such order.
83+
if (!$this->hasOrder($this->getCustomerId(), $orderId)) {
84+
...deny access...
85+
}
86+
.....
87+
}
5788
}
5889
]]>
5990
</example>

dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
<!-- Magento Specific Rules -->
4949
<rule ref="Magento/CodeMessDetector/resources/rulesets/design.xml/FinalImplementation" />
5050
<rule ref="Magento/CodeMessDetector/resources/rulesets/design.xml/AllPurposeAction" />
51+
<rule ref="Magento/CodeMessDetector/resources/rulesets/design.xml/RequestAwareBlockMethod" />
5152

5253
</ruleset>

0 commit comments

Comments
 (0)