Skip to content

Commit b1cedbb

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent 9febeb1 commit b1cedbb

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

app/code/Magento/GraphQl/Model/Backpressure/BackpressureContextFactory.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212
use Magento\Framework\App\Backpressure\IdentityProviderInterface;
13+
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\App\RequestInterface;
1415
use Magento\Framework\GraphQl\Config\Element\Field;
15-
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
1616

1717
/**
18-
* Creates context for fields.
18+
* Creates context for fields
1919
*/
2020
class BackpressureContextFactory
2121
{
@@ -50,7 +50,7 @@ public function __construct(
5050
}
5151

5252
/**
53-
* Creates context if possible.
53+
* Creates context if possible
5454
*
5555
* @param Field $field
5656
* @return ContextInterface|null
@@ -62,12 +62,15 @@ public function create(Field $field): ?ContextInterface
6262
return null;
6363
}
6464

65-
return new GraphQlContext(
66-
$this->request,
67-
$this->identityProvider->fetchIdentity(),
68-
$this->identityProvider->fetchIdentityType(),
69-
$typeId,
70-
$field->getResolver()
65+
return ObjectManager::getInstance()->create(
66+
GraphQlContext::class,
67+
[
68+
$this->request,
69+
$this->identityProvider->fetchIdentity(),
70+
$this->identityProvider->fetchIdentityType(),
71+
$typeId,
72+
$field->getResolver()
73+
]
7174
);
7275
}
7376
}

app/code/Magento/GraphQl/Model/Backpressure/BackpressureFieldValidator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Framework\GraphQl\Query\Resolver\Argument\ValidatorInterface;
1616

1717
/**
18-
* Enforces backpressure for queries/mutations.
18+
* Enforces backpressure for queries/mutations
1919
*/
2020
class BackpressureFieldValidator implements ValidatorInterface
2121
{
@@ -47,14 +47,16 @@ public function __construct(
4747
public function validate(Field $field, $args): void
4848
{
4949
$context = $this->backpressureContextFactory->create($field);
50-
if ($context) {
51-
try {
52-
$this->backpressureEnforcer->enforce($context);
53-
} catch (BackpressureExceededException $exception) {
54-
throw new GraphQlInputException(
55-
__('Something went wrong while processing the request. Try again later')
56-
);
57-
}
50+
if (!$context) {
51+
return;
52+
}
53+
54+
try {
55+
$this->backpressureEnforcer->enforce($context);
56+
} catch (BackpressureExceededException $exception) {
57+
throw new GraphQlInputException(
58+
__('Something went wrong while processing the request. Try again later')
59+
);
5860
}
5961
}
6062
}

app/code/Magento/GraphQl/Model/Backpressure/CompositeRequestTypeExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212

1313
/**
14-
* Extracts using other extractors.
14+
* Extracts using other extractors
1515
*/
1616
class CompositeRequestTypeExtractor implements RequestTypeExtractorInterface
1717
{

app/code/Magento/GraphQl/Model/Backpressure/GraphQlContext.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212
use Magento\Framework\App\RequestInterface;
1313

14+
/**
15+
* GraphQl request context
16+
*/
1417
class GraphQlContext implements ContextInterface
1518
{
1619
/**
@@ -92,7 +95,7 @@ public function getTypeId(): string
9295
}
9396

9497
/**
95-
* Field's resolver class name.
98+
* Field's resolver class name
9699
*
97100
* @return string
98101
*/

app/code/Magento/GraphQl/Model/Backpressure/RequestTypeExtractorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212

1313
/**
14-
* Extracts request type for fields.
14+
* Extracts request type for fields
1515
*/
1616
interface RequestTypeExtractorInterface
1717
{
1818
/**
19-
* Extracts type ID if possible.
19+
* Extracts type ID if possible
2020
*
2121
* @param Field $field
2222
* @return string|null

app/code/Magento/QuoteGraphQl/Model/BackpressureRequestTypeExtractor.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use ReflectionException;
1616

1717
/**
18-
* Identifies which quote fields need backpressure management.
18+
* Identifies which quote fields need backpressure management
1919
*/
2020
class BackpressureRequestTypeExtractor implements RequestTypeExtractorInterface
2121
{
@@ -38,13 +38,10 @@ public function __construct(OrderLimitConfigManager $config)
3838
public function extract(Field $field): ?string
3939
{
4040
$fieldResolver = $this->resolver($field->getResolver());
41-
4241
$placeOrderName = $this->resolver(PlaceOrder::class);
43-
4442
$setPaymentAndPlaceOrder = $this->resolver(SetPaymentAndPlaceOrder::class);
4543

46-
if (($field->getResolver() === $setPaymentAndPlaceOrder ||
47-
$placeOrderName === $fieldResolver)
44+
if (($field->getResolver() === $setPaymentAndPlaceOrder || $placeOrderName === $fieldResolver)
4845
&& $this->config->isEnforcementEnabled()
4946
) {
5047
return OrderLimitConfigManager::REQUEST_TYPE_ID;

0 commit comments

Comments
 (0)