Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 6a68bd8

Browse files
committed
Query complexity limiter introduced
1 parent 54d065a commit 6a68bd8

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@
9797
</argument>
9898
</arguments>
9999
</type>
100+
<type name="Magento\Framework\GraphQl\Query\QueryComplexityLimiter">
101+
<arguments>
102+
<argument name="queryDepth" xsi:type="number">50</argument>
103+
<argument name="queryComplexity" xsi:type="number">150</argument>
104+
</arguments>
105+
</type>
100106
</config>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Query;
9+
10+
use GraphQL\Validator\DocumentValidator;
11+
use GraphQL\Validator\Rules\DisableIntrospection;
12+
use GraphQL\Validator\Rules\QueryDepth;
13+
use GraphQL\Validator\Rules\QueryComplexity;
14+
15+
/**
16+
* Sets limits for query complexity. A single GraphQL query can potentially
17+
* generate thousands of database operations so, the very complex queries
18+
* should be filtered and rejected.
19+
*/
20+
class QueryComplexityLimiter
21+
{
22+
/**
23+
* @var int
24+
*/
25+
private $queryDepth;
26+
27+
/**
28+
* @var int
29+
*/
30+
private $queryComplexity;
31+
32+
/**
33+
* @param int $queryDepth
34+
* @param int $queryComplexity
35+
*/
36+
public function __construct(
37+
int $queryDepth = 50,
38+
int $queryComplexity = 150
39+
) {
40+
$this->queryDepth = $queryDepth;
41+
$this->queryComplexity = $queryComplexity;
42+
}
43+
44+
public function execute(bool $disableIntrospection = false): void
45+
{
46+
DocumentValidator::addRule(new QueryDepth($this->queryDepth));
47+
DocumentValidator::addRule(new QueryComplexity($this->queryComplexity));
48+
49+
if ($disableIntrospection) {
50+
DocumentValidator::addRule(new DisableIntrospection());
51+
}
52+
}
53+
}

lib/internal/Magento/Framework/GraphQl/Query/QueryProcessor.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace Magento\Framework\GraphQl\Query;
99

10-
use GraphQL\Validator\DocumentValidator;
11-
use GraphQL\Validator\Rules\DisableIntrospection;
12-
use GraphQL\Validator\Rules\QueryDepth;
1310
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
1411
use Magento\Framework\GraphQl\Schema;
1512
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -24,12 +21,21 @@ class QueryProcessor
2421
*/
2522
private $exceptionFormatter;
2623

24+
/**
25+
* @var QueryComplexityLimiter
26+
*/
27+
protected $queryComplexityLimiter;
28+
2729
/**
2830
* @param ExceptionFormatter $exceptionFormatter
31+
* @param QueryComplexityLimiter $queryComplexityChecker
2932
*/
30-
public function __construct(ExceptionFormatter $exceptionFormatter)
31-
{
33+
public function __construct(
34+
ExceptionFormatter $exceptionFormatter,
35+
QueryComplexityLimiter $queryComplexityChecker
36+
) {
3237
$this->exceptionFormatter = $exceptionFormatter;
38+
$this->queryComplexityLimiter = $queryComplexityChecker;
3339
}
3440

3541
/**
@@ -49,10 +55,9 @@ public function process(
4955
array $variableValues = null,
5056
string $operationName = null
5157
) : array {
52-
if (!$this->exceptionFormatter->shouldShowDetail()) {
53-
DocumentValidator::addRule(new QueryDepth(10));
54-
DocumentValidator::addRule(new DisableIntrospection());
55-
}
58+
$disableIntrospection = $this->exceptionFormatter->shouldShowDetail();
59+
$this->queryComplexityLimiter->execute($disableIntrospection);
60+
5661
$rootValue = null;
5762
return \GraphQL\GraphQL::executeQuery(
5863
$schema,

0 commit comments

Comments
 (0)