Skip to content

Commit 3b5dc1c

Browse files
committed
security: Properly implement CsrfAwareActionInterface
1 parent 8241a42 commit 3b5dc1c

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

Controller/Index/Html.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
use Loki\Components\Util\Controller\ComponentUpdateFactory;
1111
use Magento\Framework\App\Action\HttpGetActionInterface;
1212
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\App\CsrfAwareActionInterface;
14+
use Magento\Framework\App\Request\InvalidRequestException;
15+
use Magento\Framework\App\RequestInterface;
1316
use Magento\Framework\App\ResponseInterface;
1417
use Magento\Framework\App\State as AppState;
1518
use Magento\Framework\Controller\Result\Json as JsonResult;
1619
use Magento\Framework\Controller\Result\JsonFactory as JsonResultFactory;
1720
use Magento\Framework\Controller\ResultInterface;
21+
use Magento\Framework\Data\Form\FormKey\Validator;
1822
use Magento\Framework\View\Element\AbstractBlock;
1923
use Magento\Framework\View\LayoutInterface;
2024
use Loki\Components\Config\Config;
@@ -29,7 +33,7 @@
2933
use Psr\Log\LoggerInterface;
3034
use 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

Comments
 (0)