1010use Loki \Components \Util \Controller \ComponentUpdateFactory ;
1111use Magento \Framework \App \Action \HttpGetActionInterface ;
1212use Magento \Framework \App \Action \HttpPostActionInterface ;
13+ use Magento \Framework \App \CsrfAwareActionInterface ;
14+ use Magento \Framework \App \Request \InvalidRequestException ;
15+ use Magento \Framework \App \RequestInterface ;
1316use Magento \Framework \App \ResponseInterface ;
1417use Magento \Framework \App \State as AppState ;
1518use Magento \Framework \Controller \Result \Json as JsonResult ;
1619use Magento \Framework \Controller \Result \JsonFactory as JsonResultFactory ;
1720use Magento \Framework \Controller \ResultInterface ;
21+ use Magento \Framework \Data \Form \FormKey \Validator ;
1822use Magento \Framework \View \Element \AbstractBlock ;
1923use Magento \Framework \View \LayoutInterface ;
2024use Loki \Components \Config \Config ;
2933use Psr \Log \LoggerInterface ;
3034use Magento \Framework \Message \ManagerInterface as MessageManager ;
3135
32- class Html implements HttpPostActionInterface, HttpGetActionInterface
36+ class Html implements HttpPostActionInterface, HttpGetActionInterface, CsrfAwareActionInterface
3337{
3438 public function __construct (
3539 private readonly LayoutLoader $ layoutLoader ,
@@ -44,6 +48,7 @@ public function __construct(
4448 private readonly LoggerInterface $ logger ,
4549 private readonly ComponentRegistry $ componentRegistry ,
4650 private readonly ComponentUpdateFactory $ componentUpdateFactory ,
51+ private Validator $ formKeyValidator
4752 ) {
4853 }
4954
@@ -106,6 +111,14 @@ private function getComponentUpdates(array $updates, LayoutInterface $layout): a
106111 {
107112 $ componentUpdates = [];
108113 foreach ($ updates as $ updateIndex => $ update ) {
114+ if (!isset ($ update ['blockName ' ])) {
115+ continue ;
116+ }
117+
118+ if (!isset ($ update ['update ' ])) {
119+ continue ;
120+ }
121+
109122 $ block = $ this ->getBlock ($ layout , $ update ['blockName ' ]);
110123 $ componentUpdate = $ this ->componentUpdateFactory ->create ([
111124 'block ' => $ block ,
@@ -182,7 +195,6 @@ private function getJsonRedirect(RedirectException $redirectException): JsonResu
182195
183196 private function allowRendering (array $ data ): bool
184197 {
185- // @todo: Double-check that this is still working
186198 foreach ($ data ['updates ' ] as $ update ) {
187199 if (!isset ($ update ['render ' ])) {
188200 continue ;
@@ -219,4 +231,14 @@ private function addExceptionMessage(Exception $exception): void
219231
220232 $ this ->messageManager ->addErrorMessage (__ ($ exception ->getMessage ()));
221233 }
234+
235+ public function createCsrfValidationException (RequestInterface $ request
236+ ): ?InvalidRequestException {
237+ return null ;
238+ }
239+
240+ public function validateForCsrf (RequestInterface $ request ): ?bool
241+ {
242+ return $ this ->formKeyValidator ->validate ($ request );
243+ }
222244}
0 commit comments