File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed
framework/Magento/CodeMessDetector
testsuite/Magento/Test/Php/_files/phpmd Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -54,6 +54,37 @@ class PostOrder implements ActionInterface
54
54
...
55
55
return $response;
56
56
}
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
+ }
57
88
}
58
89
]]>
59
90
</example >
Original file line number Diff line number Diff line change 48
48
<!-- Magento Specific Rules -->
49
49
<rule ref =" Magento/CodeMessDetector/resources/rulesets/design.xml/FinalImplementation" />
50
50
<rule ref =" Magento/CodeMessDetector/resources/rulesets/design.xml/AllPurposeAction" />
51
+ <rule ref =" Magento/CodeMessDetector/resources/rulesets/design.xml/RequestAwareBlockMethod" />
51
52
52
53
</ruleset >
You can’t perform that action at this time.
0 commit comments