Skip to content

Commit e22c998

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent f4ba3b0 commit e22c998

File tree

3 files changed

+72
-39
lines changed

3 files changed

+72
-39
lines changed

app/code/Magento/Webapi/Controller/Rest/RequestValidator.php

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Webapi\Controller\Rest;
89

@@ -15,9 +16,10 @@
1516
use Magento\Store\Model\StoreManagerInterface;
1617
use Magento\Framework\Webapi\Backpressure\BackpressureContextFactory;
1718
use Magento\Framework\Webapi\Exception as WebapiException;
19+
use Magento\Webapi\Controller\Rest\Router\Route;
1820

1921
/**
20-
* This class is responsible for validating the request
22+
* Validates a request
2123
*/
2224
class RequestValidator
2325
{
@@ -52,8 +54,6 @@ class RequestValidator
5254
private BackpressureEnforcerInterface $backpressureEnforcer;
5355

5456
/**
55-
* Initialize dependencies
56-
*
5757
* @param RestRequest $request
5858
* @param Router $router
5959
* @param StoreManagerInterface $storeManager
@@ -80,20 +80,62 @@ public function __construct(
8080
}
8181

8282
/**
83-
* Validate request
83+
* Validates the request
8484
*
8585
* @throws AuthorizationException
8686
* @throws WebapiException
8787
* @return void
8888
*/
8989
public function validate()
9090
{
91-
$this->checkPermissions();
9291
$route = $this->router->match($this->request);
92+
$this->checkPermissions($route);
93+
$this->onlyHttps($route);
94+
$this->checkBackpressure($route);
95+
}
96+
97+
/**
98+
* Perform authentication and authorization
99+
*
100+
* @param Route $route
101+
* @return void
102+
* @throws AuthorizationException
103+
*/
104+
private function checkPermissions(Route $route)
105+
{
106+
if ($this->authorization->isAllowed($route->getAclResources())) {
107+
return;
108+
}
109+
110+
throw new AuthorizationException(
111+
__(
112+
"The consumer isn't authorized to access %resources.",
113+
['resources' => implode(', ', $route->getAclResources())]
114+
)
115+
);
116+
}
117+
118+
/**
119+
* Checks if operation allowed only in HTTPS
120+
*
121+
* @param Route $route
122+
* @throws WebapiException
123+
*/
124+
private function onlyHttps(Route $route)
125+
{
93126
if ($route->isSecure() && !$this->request->isSecure()) {
94127
throw new WebapiException(__('Operation allowed only in HTTPS'));
95128
}
129+
}
96130

131+
/**
132+
* Checks backpressure
133+
*
134+
* @param Route $route
135+
* @throws WebapiException
136+
*/
137+
private function checkBackpressure(Route $route)
138+
{
97139
$context = $this->backpressureContextFactory->create(
98140
$route->getServiceClass(),
99141
$route->getServiceMethod(),
@@ -107,21 +149,4 @@ public function validate()
107149
}
108150
}
109151
}
110-
111-
/**
112-
* Perform authentication and authorization.
113-
*
114-
* @throws \Magento\Framework\Exception\AuthorizationException
115-
* @return void
116-
*/
117-
private function checkPermissions()
118-
{
119-
$route = $this->router->match($this->request);
120-
if (!$this->authorization->isAllowed($route->getAclResources())) {
121-
$params = ['resources' => implode(', ', $route->getAclResources())];
122-
throw new AuthorizationException(
123-
__("The consumer isn't authorized to access %resources.", $params)
124-
);
125-
}
126-
}
127152
}

app/code/Magento/Webapi/Controller/Soap/Request/Handler.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
use Magento\Webapi\Model\ServiceMetadata;
3131

3232
/**
33-
* Handler of requests to SOAP server.
33+
* Handler of requests to SOAP server
3434
*
35-
* The main responsibility is to instantiate proper action controller (service) and execute requested method on it.
35+
* The main responsibility is to instantiate proper action controller (service) and execute requested method on it
3636
*
3737
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3838
*/
@@ -101,8 +101,6 @@ class Handler
101101
private $inputArraySizeLimitValue;
102102

103103
/**
104-
* Initialize dependencies.
105-
*
106104
* @param WebapiRequest $request
107105
* @param ObjectManagerInterface $objectManager
108106
* @param SoapConfig $apiConfig
@@ -149,7 +147,7 @@ public function __construct(
149147
}
150148

151149
/**
152-
* Handler for all SOAP operations.
150+
* Handler for all SOAP operations
153151
*
154152
* @param string $operation
155153
* @param array $arguments
@@ -173,18 +171,7 @@ public function __call($operation, $arguments)
173171
}
174172

175173
//Backpressure enforcement
176-
$context = $this->backpressureContextFactory->create(
177-
$serviceMethodInfo['class'],
178-
$serviceMethodInfo['method'],
179-
$operation
180-
);
181-
if ($context) {
182-
try {
183-
$this->backpressureEnforcer->enforce($context);
184-
} catch (BackpressureExceededException $exception) {
185-
throw new WebapiException(__('Something went wrong, please try again later'));
186-
}
187-
}
174+
$this->backpressureEnforcement($serviceMethodInfo['class'], $serviceMethodInfo['method'], $operation);
188175

189176
if (!$this->authorization->isAllowed($serviceMethodInfo[ServiceMetadata::KEY_ACL_RESOURCES])) {
190177
throw new AuthorizationException(
@@ -296,4 +283,24 @@ protected function _prepareResponseData($data, $serviceClassName, $serviceMethod
296283
}
297284
return [self::RESULT_NODE_NAME => $result];
298285
}
286+
287+
/**
288+
* Backpressure enforcement
289+
*
290+
* @param string $class
291+
* @param string $method
292+
* @param string $operation
293+
* @throws WebapiException
294+
*/
295+
private function backpressureEnforcement(string $class, string $method, string $operation)
296+
{
297+
$context = $this->backpressureContextFactory->create($class, $method, $operation);
298+
if ($context) {
299+
try {
300+
$this->backpressureEnforcer->enforce($context);
301+
} catch (BackpressureExceededException $exception) {
302+
throw new WebapiException(__('Something went wrong, please try again later'));
303+
}
304+
}
305+
}
299306
}

app/code/Magento/Webapi/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Message,Message
2222
"If empty, UTF-8 will be used.","If empty, UTF-8 will be used."
2323
"Web Services Configuration","Web Services Configuration"
2424
"REST and SOAP configurations, generated WSDL file","REST and SOAP configurations, generated WSDL file"
25+
"Something went wrong, please try again later","Something went wrong, please try again later"

0 commit comments

Comments
 (0)