123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Backend\App\Request;
- use Magento\Backend\App\AbstractAction;
- use Magento\Framework\App\ActionInterface;
- use Magento\Framework\App\CsrfAwareActionInterface;
- use Magento\Framework\App\Request\InvalidRequestException;
- use Magento\Framework\App\Request\ValidatorInterface;
- use Magento\Framework\App\RequestInterface;
- use Magento\Backend\Model\Auth;
- use Magento\Framework\App\Request\Http as HttpRequest;
- use Magento\Framework\Controller\Result\RawFactory;
- use Magento\Framework\Controller\Result\Raw as RawResult;
- use Magento\Framework\Controller\Result\RedirectFactory;
- use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
- use Magento\Backend\Model\UrlInterface as BackendUrl;
- use Magento\Framework\Phrase;
- /**
- * Do backend validations.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class BackendValidator implements ValidatorInterface
- {
- /**
- * @var Auth
- */
- private $auth;
- /**
- * @var FormKeyValidator
- */
- private $formKeyValidator;
- /**
- * @var BackendUrl
- */
- private $backendUrl;
- /**
- * @var RedirectFactory
- */
- private $redirectFactory;
- /**
- * @var RawFactory
- */
- private $rawResultFactory;
- /**
- * @param Auth $auth
- * @param FormKeyValidator $formKeyValidator
- * @param BackendUrl $backendUrl
- * @param RedirectFactory $redirectFactory
- * @param RawFactory $rawResultFactory
- */
- public function __construct(
- Auth $auth,
- FormKeyValidator $formKeyValidator,
- BackendUrl $backendUrl,
- RedirectFactory $redirectFactory,
- RawFactory $rawResultFactory
- ) {
- $this->auth = $auth;
- $this->formKeyValidator = $formKeyValidator;
- $this->backendUrl = $backendUrl;
- $this->redirectFactory = $redirectFactory;
- $this->rawResultFactory = $rawResultFactory;
- }
- /**
- * Validate request
- *
- * @param RequestInterface $request
- * @param ActionInterface $action
- *
- * @return bool
- */
- private function validateRequest(
- RequestInterface $request,
- ActionInterface $action
- ): bool {
- /** @var bool|null $valid */
- $valid = null;
- if ($action instanceof CsrfAwareActionInterface) {
- $valid = $action->validateForCsrf($request);
- }
- if ($valid === null) {
- $validFormKey = true;
- $validSecretKey = true;
- if ($request instanceof HttpRequest && $request->isPost()) {
- $validFormKey = $this->formKeyValidator->validate($request);
- } elseif ($this->auth->isLoggedIn()
- && $this->backendUrl->useSecretKey()
- ) {
- $secretKeyValue = (string)$request->getParam(
- BackendUrl::SECRET_KEY_PARAM_NAME,
- null
- );
- $secretKey = $this->backendUrl->getSecretKey();
- $validSecretKey = ($secretKeyValue === $secretKey);
- }
- $valid = $validFormKey && $validSecretKey;
- }
- return $valid;
- }
- /**
- * Create exception
- *
- * @param RequestInterface $request
- * @param ActionInterface $action
- *
- * @return InvalidRequestException
- */
- private function createException(
- RequestInterface $request,
- ActionInterface $action
- ): InvalidRequestException {
- /** @var InvalidRequestException|null $exception */
- $exception = null;
- if ($action instanceof CsrfAwareActionInterface) {
- $exception = $action->createCsrfValidationException($request);
- }
- if ($exception === null) {
- if ($request instanceof HttpRequest && $request->isAjax()) {
- //Sending empty response for AJAX request since we don't know
- //the expected response format and it's pointless to redirect.
- /** @var RawResult $response */
- $response = $this->rawResultFactory->create();
- $response->setHttpResponseCode(401);
- $response->setContents('');
- $exception = new InvalidRequestException($response);
- } else {
- //For regular requests.
- $response = $this->redirectFactory->create()
- ->setUrl($this->backendUrl->getStartupPageUrl());
- $exception = new InvalidRequestException(
- $response,
- [
- new Phrase(
- 'Invalid security or form key. Please refresh the page.'
- )
- ]
- );
- }
- }
- return $exception;
- }
- /**
- * @inheritDoc
- */
- public function validate(
- RequestInterface $request,
- ActionInterface $action
- ): void {
- if ($action instanceof AbstractAction) {
- //Abstract Action has built-in validation.
- if (!$action->_processUrlKeys()) {
- throw new InvalidRequestException($action->getResponse());
- }
- } else {
- //Fallback validation.
- if (!$this->validateRequest($request, $action)) {
- throw $this->createException($request, $action);
- }
- }
- }
- }
|