66namespace Magento \Wishlist \Controller \Index ;
77
88use Magento \Customer \Model \Session as CustomerSession ;
9+ use Magento \Framework \App \Action \HttpPostActionInterface ;
10+ use Magento \Framework \App \ActionInterface ;
911use Magento \Framework \Data \Form \FormKey ;
12+ use Magento \Framework \Data \Form \FormKey \Validator ;
1013use Magento \Framework \Exception \NotFoundException ;
1114use Magento \Framework \App \Config \ScopeConfigInterface ;
1215use Magento \Framework \App \RequestInterface ;
1316use Magento \Framework \App \Response \RedirectInterface ;
17+ use Magento \Framework \Message \ManagerInterface ;
1418use Magento \Store \Model \ScopeInterface ;
19+ use Magento \Wishlist \Model \AuthenticationStateInterface ;
1520use Magento \Wishlist \Model \DataSerializer ;
1621
1722/**
1823 * Wishlist plugin before dispatch
24+ *
25+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1926 */
2027class Plugin
2128{
2229 /**
23- * @var \Magento\Customer\Model\Session
30+ * @var CustomerSession
2431 */
2532 protected $ customerSession ;
2633
2734 /**
28- * @var \Magento\Wishlist\Model\ AuthenticationStateInterface
35+ * @var AuthenticationStateInterface
2936 */
3037 protected $ authenticationState ;
3138
3239 /**
33- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
40+ * @var ScopeConfigInterface
3441 */
3542 protected $ config ;
3643
3744 /**
38- * @var \Magento\Framework\App\Response\ RedirectInterface
45+ * @var RedirectInterface
3946 */
4047 protected $ redirector ;
4148
4249 /**
43- * @var \Magento\Framework\Message\ ManagerInterface
50+ * @var ManagerInterface
4451 */
4552 private $ messageManager ;
4653
@@ -54,23 +61,30 @@ class Plugin
5461 */
5562 private $ formKey ;
5663
64+ /**
65+ * @var Validator
66+ */
67+ private $ formKeyValidator ;
68+
5769 /**
5870 * @param CustomerSession $customerSession
59- * @param \Magento\Wishlist\Model\ AuthenticationStateInterface $authenticationState
71+ * @param AuthenticationStateInterface $authenticationState
6072 * @param ScopeConfigInterface $config
6173 * @param RedirectInterface $redirector
62- * @param \Magento\Framework\Message\ ManagerInterface $messageManager
74+ * @param ManagerInterface $messageManager
6375 * @param DataSerializer $dataSerializer
6476 * @param FormKey $formKey
77+ * @param Validator $formKeyValidator
6578 */
6679 public function __construct (
6780 CustomerSession $ customerSession ,
68- \ Magento \ Wishlist \ Model \ AuthenticationStateInterface $ authenticationState ,
81+ AuthenticationStateInterface $ authenticationState ,
6982 ScopeConfigInterface $ config ,
7083 RedirectInterface $ redirector ,
71- \ Magento \ Framework \ Message \ ManagerInterface $ messageManager ,
84+ ManagerInterface $ messageManager ,
7285 DataSerializer $ dataSerializer ,
73- FormKey $ formKey
86+ FormKey $ formKey ,
87+ Validator $ formKeyValidator
7488 ) {
7589 $ this ->customerSession = $ customerSession ;
7690 $ this ->authenticationState = $ authenticationState ;
@@ -79,18 +93,19 @@ public function __construct(
7993 $ this ->messageManager = $ messageManager ;
8094 $ this ->dataSerializer = $ dataSerializer ;
8195 $ this ->formKey = $ formKey ;
96+ $ this ->formKeyValidator = $ formKeyValidator ;
8297 }
8398
8499 /**
85100 * Perform customer authentication and wishlist feature state checks
86101 *
87- * @param \Magento\Framework\App\ ActionInterface $subject
102+ * @param ActionInterface $subject
88103 * @param RequestInterface $request
89104 * @return void
90- * @throws \Magento\Framework\Exception\ NotFoundException
105+ * @throws NotFoundException
91106 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
92107 */
93- public function beforeDispatch (\ Magento \ Framework \ App \ ActionInterface $ subject , RequestInterface $ request )
108+ public function beforeDispatch (ActionInterface $ subject , RequestInterface $ request )
94109 {
95110 if ($ this ->authenticationState ->isEnabled () && !$ this ->customerSession ->authenticate ()) {
96111 $ subject ->getActionFlag ()->set ('' , 'no-dispatch ' , true );
@@ -99,25 +114,32 @@ public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject,
99114 }
100115 $ data = $ request ->getParams ();
101116 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+ }
107124
108125 if ($ request ->getActionName () === 'add ' ) {
109126 $ this ->messageManager ->addErrorMessage (__ ('You must login or register to add items to your wishlist. ' ));
110127 }
111128 } elseif ($ this ->customerSession ->authenticate ()) {
112129 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 );
114135 $ this ->customerSession ->unsBeforeWishlistRequest ();
115136 } elseif ($ request ->getParam ('token ' )) {
116137 // check if the token is valid and retrieve the data
117138 $ data = $ this ->dataSerializer ->unserialize ($ request ->getParam ('token ' ));
118139 // Bypass CSRF validation if the token is valid
119140 if ($ data ) {
120141 $ data ['form_key ' ] = $ this ->formKey ->getFormKey ();
142+ $ request ->clearParams ();
121143 $ request ->setParams ($ data );
122144 }
123145 }
0 commit comments