6
6
namespace Magento \Wishlist \Controller \Index ;
7
7
8
8
use Magento \Customer \Model \Session as CustomerSession ;
9
+ use Magento \Framework \App \Action \HttpPostActionInterface ;
10
+ use Magento \Framework \App \ActionInterface ;
9
11
use Magento \Framework \Data \Form \FormKey ;
12
+ use Magento \Framework \Data \Form \FormKey \Validator ;
10
13
use Magento \Framework \Exception \NotFoundException ;
11
14
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
15
use Magento \Framework \App \RequestInterface ;
13
16
use Magento \Framework \App \Response \RedirectInterface ;
17
+ use Magento \Framework \Message \ManagerInterface ;
14
18
use Magento \Store \Model \ScopeInterface ;
19
+ use Magento \Wishlist \Model \AuthenticationStateInterface ;
15
20
use Magento \Wishlist \Model \DataSerializer ;
16
21
17
22
/**
18
23
* Wishlist plugin before dispatch
24
+ *
25
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
26
*/
20
27
class Plugin
21
28
{
22
29
/**
23
- * @var \Magento\Customer\Model\Session
30
+ * @var CustomerSession
24
31
*/
25
32
protected $ customerSession ;
26
33
27
34
/**
28
- * @var \Magento\Wishlist\Model\ AuthenticationStateInterface
35
+ * @var AuthenticationStateInterface
29
36
*/
30
37
protected $ authenticationState ;
31
38
32
39
/**
33
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
40
+ * @var ScopeConfigInterface
34
41
*/
35
42
protected $ config ;
36
43
37
44
/**
38
- * @var \Magento\Framework\App\Response\ RedirectInterface
45
+ * @var RedirectInterface
39
46
*/
40
47
protected $ redirector ;
41
48
42
49
/**
43
- * @var \Magento\Framework\Message\ ManagerInterface
50
+ * @var ManagerInterface
44
51
*/
45
52
private $ messageManager ;
46
53
@@ -54,23 +61,30 @@ class Plugin
54
61
*/
55
62
private $ formKey ;
56
63
64
+ /**
65
+ * @var Validator
66
+ */
67
+ private $ formKeyValidator ;
68
+
57
69
/**
58
70
* @param CustomerSession $customerSession
59
- * @param \Magento\Wishlist\Model\ AuthenticationStateInterface $authenticationState
71
+ * @param AuthenticationStateInterface $authenticationState
60
72
* @param ScopeConfigInterface $config
61
73
* @param RedirectInterface $redirector
62
- * @param \Magento\Framework\Message\ ManagerInterface $messageManager
74
+ * @param ManagerInterface $messageManager
63
75
* @param DataSerializer $dataSerializer
64
76
* @param FormKey $formKey
77
+ * @param Validator $formKeyValidator
65
78
*/
66
79
public function __construct (
67
80
CustomerSession $ customerSession ,
68
- \ Magento \ Wishlist \ Model \ AuthenticationStateInterface $ authenticationState ,
81
+ AuthenticationStateInterface $ authenticationState ,
69
82
ScopeConfigInterface $ config ,
70
83
RedirectInterface $ redirector ,
71
- \ Magento \ Framework \ Message \ ManagerInterface $ messageManager ,
84
+ ManagerInterface $ messageManager ,
72
85
DataSerializer $ dataSerializer ,
73
- FormKey $ formKey
86
+ FormKey $ formKey ,
87
+ Validator $ formKeyValidator
74
88
) {
75
89
$ this ->customerSession = $ customerSession ;
76
90
$ this ->authenticationState = $ authenticationState ;
@@ -79,18 +93,19 @@ public function __construct(
79
93
$ this ->messageManager = $ messageManager ;
80
94
$ this ->dataSerializer = $ dataSerializer ;
81
95
$ this ->formKey = $ formKey ;
96
+ $ this ->formKeyValidator = $ formKeyValidator ;
82
97
}
83
98
84
99
/**
85
100
* Perform customer authentication and wishlist feature state checks
86
101
*
87
- * @param \Magento\Framework\App\ ActionInterface $subject
102
+ * @param ActionInterface $subject
88
103
* @param RequestInterface $request
89
104
* @return void
90
- * @throws \Magento\Framework\Exception\ NotFoundException
105
+ * @throws NotFoundException
91
106
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
92
107
*/
93
- public function beforeDispatch (\ Magento \ Framework \ App \ ActionInterface $ subject , RequestInterface $ request )
108
+ public function beforeDispatch (ActionInterface $ subject , RequestInterface $ request )
94
109
{
95
110
if ($ this ->authenticationState ->isEnabled () && !$ this ->customerSession ->authenticate ()) {
96
111
$ subject ->getActionFlag ()->set ('' , 'no-dispatch ' , true );
@@ -99,25 +114,32 @@ public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject,
99
114
}
100
115
$ data = $ request ->getParams ();
101
116
unset($ data ['login ' ]);
102
- $ this ->customerSession ->setBeforeWishlistRequest ($ data );
103
- $ this ->customerSession ->setBeforeRequestParams ($ this ->customerSession ->getBeforeWishlistRequest ());
104
- $ this ->customerSession ->setBeforeModuleName ('wishlist ' );
105
- $ this ->customerSession ->setBeforeControllerName ('index ' );
106
- $ this ->customerSession ->setBeforeAction ('add ' );
117
+ if (!($ subject instanceof HttpPostActionInterface) || $ this ->formKeyValidator ->validate ($ request )) {
118
+ $ this ->customerSession ->setBeforeWishlistRequest ($ data );
119
+ $ this ->customerSession ->setBeforeRequestParams ($ this ->customerSession ->getBeforeWishlistRequest ());
120
+ $ this ->customerSession ->setBeforeModuleName ('wishlist ' );
121
+ $ this ->customerSession ->setBeforeControllerName ('index ' );
122
+ $ this ->customerSession ->setBeforeAction ($ request ->getActionName ());
123
+ }
107
124
108
125
if ($ request ->getActionName () === 'add ' ) {
109
126
$ this ->messageManager ->addErrorMessage (__ ('You must login or register to add items to your wishlist. ' ));
110
127
}
111
128
} elseif ($ this ->customerSession ->authenticate ()) {
112
129
if ($ this ->customerSession ->getBeforeWishlistRequest ()) {
113
- $ request ->setParams ($ this ->customerSession ->getBeforeWishlistRequest ());
130
+ $ data = $ this ->customerSession ->getBeforeWishlistRequest ();
131
+ // Bypass CSRF validation as the data comes from a request that was validated
132
+ $ data ['form_key ' ] = $ this ->formKey ->getFormKey ();
133
+ $ request ->clearParams ();
134
+ $ request ->setParams ($ data );
114
135
$ this ->customerSession ->unsBeforeWishlistRequest ();
115
136
} elseif ($ request ->getParam ('token ' )) {
116
137
// check if the token is valid and retrieve the data
117
138
$ data = $ this ->dataSerializer ->unserialize ($ request ->getParam ('token ' ));
118
139
// Bypass CSRF validation if the token is valid
119
140
if ($ data ) {
120
141
$ data ['form_key ' ] = $ this ->formKey ->getFormKey ();
142
+ $ request ->clearParams ();
121
143
$ request ->setParams ($ data );
122
144
}
123
145
}
0 commit comments