Skip to content

Commit bd53306

Browse files
security-package: Added try/catch to Magento\ReCaptchaUi\Model\RequestHandler.php
1 parent 854bf80 commit bd53306

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

ReCaptchaCustomer/Observer/AjaxLoginObserver.php

+22-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
use Magento\Framework\App\ActionFlag;
1212
use Magento\Framework\Event\Observer;
1313
use Magento\Framework\Event\ObserverInterface;
14+
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Serialize\SerializerInterface;
16-
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
1717
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
18+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
1819
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
1920
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
21+
use Psr\Log\LoggerInterface;
2022

2123
/**
2224
* AjaxLoginObserver
@@ -53,28 +55,36 @@ class AjaxLoginObserver implements ObserverInterface
5355
*/
5456
private $isCaptchaEnabled;
5557

58+
/**
59+
* @var LoggerInterface
60+
*/
61+
private $logger;
62+
5663
/**
5764
* @param CaptchaResponseResolverInterface $captchaResponseResolver
5865
* @param ValidationConfigResolverInterface $validationConfigResolver
5966
* @param ValidatorInterface $captchaValidator
6067
* @param ActionFlag $actionFlag
6168
* @param SerializerInterface $serializer
6269
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
70+
* @param LoggerInterface $logger
6371
*/
6472
public function __construct(
6573
CaptchaResponseResolverInterface $captchaResponseResolver,
6674
ValidationConfigResolverInterface $validationConfigResolver,
6775
ValidatorInterface $captchaValidator,
6876
ActionFlag $actionFlag,
6977
SerializerInterface $serializer,
70-
IsCaptchaEnabledInterface $isCaptchaEnabled
78+
IsCaptchaEnabledInterface $isCaptchaEnabled,
79+
LoggerInterface $logger
7180
) {
7281
$this->captchaResponseResolver = $captchaResponseResolver;
7382
$this->validationConfigResolver = $validationConfigResolver;
7483
$this->captchaValidator = $captchaValidator;
7584
$this->actionFlag = $actionFlag;
7685
$this->serializer = $serializer;
7786
$this->isCaptchaEnabled = $isCaptchaEnabled;
87+
$this->logger = $logger;
7888
}
7989

8090
/**
@@ -91,11 +101,18 @@ public function execute(Observer $observer): void
91101
$request = $controller->getRequest();
92102
$response = $controller->getResponse();
93103

94-
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request);
95104
$validationConfig = $this->validationConfigResolver->get($key);
105+
try {
106+
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request);
107+
} catch (InputException $e) {
108+
$reCaptchaResponse = null;
109+
$this->logger->error($e);
110+
}
96111

97-
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
98-
if (false === $validationResult->isValid()) {
112+
if (null !== $reCaptchaResponse) {
113+
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
114+
}
115+
if (null === $reCaptchaResponse || false === $validationResult->isValid()) {
99116
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
100117

101118
$jsonPayload = $this->serializer->serialize([

ReCaptchaUi/Model/RequestHandler.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Magento\Framework\App\ActionFlag;
1212
use Magento\Framework\App\RequestInterface;
1313
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
14+
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
1516
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
17+
use Psr\Log\LoggerInterface;
1618

1719
/**
1820
* @inheritdoc
@@ -44,25 +46,33 @@ class RequestHandler implements RequestHandlerInterface
4446
*/
4547
private $actionFlag;
4648

49+
/**
50+
* @var LoggerInterface
51+
*/
52+
private $logger;
53+
4754
/**
4855
* @param CaptchaResponseResolverInterface $captchaResponseResolver
4956
* @param ValidationConfigResolverInterface $validationConfigResolver
5057
* @param ValidatorInterface $captchaValidator
5158
* @param MessageManagerInterface $messageManager
5259
* @param ActionFlag $actionFlag
60+
* @param LoggerInterface $logger
5361
*/
5462
public function __construct(
5563
CaptchaResponseResolverInterface $captchaResponseResolver,
5664
ValidationConfigResolverInterface $validationConfigResolver,
5765
ValidatorInterface $captchaValidator,
5866
MessageManagerInterface $messageManager,
59-
ActionFlag $actionFlag
67+
ActionFlag $actionFlag,
68+
LoggerInterface $logger
6069
) {
6170
$this->captchaResponseResolver = $captchaResponseResolver;
6271
$this->validationConfigResolver = $validationConfigResolver;
6372
$this->captchaValidator = $captchaValidator;
6473
$this->messageManager = $messageManager;
6574
$this->actionFlag = $actionFlag;
75+
$this->logger = $logger;
6676
}
6777

6878
/**
@@ -74,20 +84,20 @@ public function execute(
7484
HttpResponseInterface $response,
7585
string $redirectOnFailureUrl
7686
): void {
77-
87+
$validationConfig = $this->validationConfigResolver->get($key);
7888
try {
7989
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request);
80-
$validationConfig = $this->validationConfigResolver->get($key);
90+
} catch (InputException $e) {
91+
$reCaptchaResponse = null;
92+
$this->logger->error($e);
93+
}
8194

95+
if (null !== $reCaptchaResponse) {
8296
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
83-
if (false === $validationResult->isValid()) {
84-
$this->messageManager->addErrorMessage($validationConfig->getValidationFailureMessage());
85-
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
97+
}
8698

87-
$response->setRedirect($redirectOnFailureUrl);
88-
}
89-
} catch (\Exception $e) {
90-
$this->messageManager->addErrorMessage($e->getMessage());
99+
if (null === $reCaptchaResponse || false === $validationResult->isValid()) {
100+
$this->messageManager->addErrorMessage($validationConfig->getValidationFailureMessage());
91101
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
92102

93103
$response->setRedirect($redirectOnFailureUrl);

ReCaptchaUser/Observer/LoginObserver.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
use Magento\Framework\App\RequestInterface;
1111
use Magento\Framework\Event\Observer;
1212
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\Exception\InputException;
1314
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\Exception\Plugin\AuthenticationException;
15-
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
1616
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
17+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
1718
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
1819
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
20+
use Psr\Log\LoggerInterface;
1921

2022
/**
2123
* LoginObserver
@@ -52,12 +54,18 @@ class LoginObserver implements ObserverInterface
5254
*/
5355
private $loginActionName;
5456

57+
/**
58+
* @var LoggerInterface
59+
*/
60+
private $logger;
61+
5562
/**
5663
* @param CaptchaResponseResolverInterface $captchaResponseResolver
5764
* @param ValidationConfigResolverInterface $validationConfigResolver
5865
* @param ValidatorInterface $captchaValidator
5966
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
6067
* @param RequestInterface $request
68+
* @param LoggerInterface $logger
6169
* @param string $loginActionName
6270
*/
6371
public function __construct(
@@ -66,6 +74,7 @@ public function __construct(
6674
ValidatorInterface $captchaValidator,
6775
IsCaptchaEnabledInterface $isCaptchaEnabled,
6876
RequestInterface $request,
77+
LoggerInterface $logger,
6978
string $loginActionName
7079
) {
7180
$this->captchaResponseResolver = $captchaResponseResolver;
@@ -74,6 +83,7 @@ public function __construct(
7483
$this->isCaptchaEnabled = $isCaptchaEnabled;
7584
$this->request = $request;
7685
$this->loginActionName = $loginActionName;
86+
$this->logger = $logger;
7787
}
7888

7989
/**
@@ -88,11 +98,18 @@ public function execute(Observer $observer): void
8898
if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)
8999
&& $this->request->getFullActionName() === $this->loginActionName
90100
) {
91-
$reCaptchaResponse = $this->captchaResponseResolver->resolve($this->request);
92101
$validationConfig = $this->validationConfigResolver->get($key);
102+
try {
103+
$reCaptchaResponse = $this->captchaResponseResolver->resolve($this->request);
104+
} catch (InputException $e) {
105+
$reCaptchaResponse = null;
106+
$this->logger->error($e);
107+
}
93108

94-
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
95-
if (false === $validationResult->isValid()) {
109+
if (null !== $reCaptchaResponse) {
110+
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
111+
}
112+
if (null === $reCaptchaResponse || false === $validationResult->isValid()) {
96113
throw new AuthenticationException(__($validationConfig->getValidationFailureMessage()));
97114
}
98115
}

0 commit comments

Comments
 (0)